#! /usr/bin/make -f

CFLAGS := -fPIC -ansi -pedantic

# Some special build options
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
   CFLAGS += -g -O0
   LDFLAGS += -g
else
   CFLAGS += -O2
endif

ifneq (,$(findstring verbose,$(DEB_BUILD_OPTIONS)))
   DH_VERBOSE=1
   export DH_VERBOSE
endif

build:		build-stamp
install:	install-stamp
binary:		binary-arch binary-indep

PATHCONFIG	:= --prefix=/usr/lib/gmt
DESTDIR		:= $(CURDIR)/debian/tmp
PATHVARS	:= prefix=$(DESTDIR)/usr/lib/gmt

build-stamp: 
	dh_testdir
	dh_prep
	
	# Use current autotools helpers
	-test -r /usr/share/misc/config.sub && \
		mv config.sub config.sub.original && \
		cp -f /usr/share/misc/config.sub config.sub
	-test -r /usr/share/misc/config.guess && \
		mv config.guess config.guess.original && \
		cp -f /usr/share/misc/config.guess config.guess
	
	$(CURDIR)/configure \
		--enable-shared $(PATHCONFIG) --disable-mex --enable-netcdf=/usr \
		--enable-octave \
	    --enable-mex-mdir=$(shell octave-config -p LOCALFCNFILEDIR)/gmt \
		--enable-mex-xdir=$(shell octave-config -p LOCALOCTFILEDIR)/gmt
	$(MAKE) CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" all suppl 
	touch $@

install-stamp: build-stamp
	dh_testdir
	dh_testroot
	
	# Very tricky: GMT does not conform to FHS. As it proved to be very
	# time consuming to bend this beast I'll create a fake GMT tree
	# which links to the Debian accepted locations and install into
	# that tree.
	mkdir -p  $(DESTDIR)/usr/lib/gmt $(DESTDIR)/usr/share/gmt \
		$(DESTDIR)/usr/share/man $(DESTDIR)/usr/include/gmt \
		$(DESTDIR)/usr/share/doc $(DESTDIR)/etc/gmt $(DESTDIR)/usr/bin
	ln -fs ../../share/man $(DESTDIR)/usr/lib/gmt/man
	ln -fs ../../include/gmt $(DESTDIR)/usr/lib/gmt/include
	ln -fs ../../share/gmt $(DESTDIR)/usr/lib/gmt/share
	
	$(MAKE) $(PATHVARS) install-all
	$(MAKE) -C src/mex DESTDIR=$(DESTDIR) install
	-find $(DESTDIR)/usr/lib/octave -name "*.mex" -exec rename 's/\.mex/.oct/' {} \;
	
	# Okay, we still have to move the documentation (the trick above does
	# not work because the Makefile would try to overwrite the link with
	# a directory). So move the docs and link them to where GMT will look
	# for them. 
	mv $(DESTDIR)/usr/lib/gmt/share/doc/gmt $(DESTDIR)/usr/share/doc/
	ln -fs ../../../share/doc/gmt $(DESTDIR)/usr/lib/gmt/share/doc/gmt
	
	# Configuration files are still at the wrong place, let's fix this
	mv $(DESTDIR)/usr/share/gmt/conf/*.conf $(DESTDIR)/etc/gmt/
	install -m644 debian/coastline.conf $(DESTDIR)/etc/gmt/
	for file in $(DESTDIR)/etc/gmt/*.conf; do \
		ln -fs /etc/gmt/`basename $$file` $(DESTDIR)/usr/share/gmt/`basename $$file`; \
	done
	
	# GMT wrapper does not help in /usr/lib/gmt/bin (not in path by default)
	# So: Move it to /usr/bin and patch it a bit locally to alter the PATH var.
	# Also use 'pager' instead of more as default pager in the script.
	sed -e 's/^exec /PATH=\$$PATH:\$${exec_prefix}\/bin exec /' \
	    -e 's/:-more/:-pager/' \
	    	$(DESTDIR)/usr/lib/gmt/bin/GMT >$(DESTDIR)/usr/bin/GMT
	rm -f $(DESTDIR)/usr/lib/gmt/bin/GMT
	
	# Move the manpages directory into the right place...
	for section in 1 3 5; do \
		mkdir -p $(DESTDIR)/usr/share/man/man$${section}; \
		for manpage in $(DESTDIR)/usr/lib/gmt/share/man/man$${section}/*.$${section}; do \
			sed -e "s/^\.TH \(.*\) $${section}/\.TH \1 $${section}gmt/" $${manpage}|gzip \
			       >$(DESTDIR)/usr/share/man/man$${section}/`basename $${manpage}`gmt.gz; \
			rm -f $${manpage}; \
		done; \
	done
	rm -rf $(DESTDIR)/usr/share/gmt/man
	
	touch $@

binary-arch: install-stamp
	dh_testdir -a
	dh_testroot -a
	dh_install --list-missing 
	dh_installchangelogs -a
	dh_installchangelogs -pgmt ChangeLog
	dh_installdocs -a
	dh_installexamples -a
	dh_installmenu -a
	dh_installman -a
	dh_strip -a
	dh_link -a
	dh_compress -a -X.pdf
	dh_fixperms -a
	dh_makeshlibs -a
	dh_shlibdeps -a -l$(DESTDIR)/usr/lib/gmt/lib
	dh_installdeb -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a
	

binary-indep: install-stamp
	dh_testdir -i
	dh_testroot -i
	dh_install --list-missing 
	dh_installchangelogs -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installmenu -i
	dh_installman -i
	dh_strip -i
	dh_link -i
	dh_compress -i -X.pdf
	# Uncompress examples stuff 
	find  $(CURDIR)/debian/gmt-examples/usr/share/doc/gmt-examples/examples -name "*.gz" -exec gunzip {} \;
	# Remove +x 
	find  $(CURDIR)/debian/gmt-examples/usr/share/doc/gmt-examples/examples -name "*.bat" -exec chmod -x {} \;
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

clean:
	dh_testdir
	dh_testroot
	rm -f install-stamp build-stamp
	# Reset upstream autotools helpers
	[ ! -f config.sub.original ] || mv -f config.sub.original config.sub 
	[ ! -f config.guess.original ] || mv -f config.guess.original config.guess
	[ ! -f Makefile ] || $(MAKE) distclean || true
	# Remove a few files still around after upstream cleaning
	rm -f src/mgg/libgmt_mgg.a \
		  share/conf/gmt.conf \
		  share/conf/gmtdefaults_SI \
		  share/conf/gmtdefaults_US src/isogmt
	dh_clean
