#! /usr/bin/make -f

RELEASE		:= 4.5.6
PARTS		:= doc share src suppl 
ARCHIVES	:= $(PARTS:%=upstream/GMT$(RELEASE)_%.tar.bz2)

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

unpack:		stamps/unpack-stamp
patch:		stamps/patch-stamp
build:		stamps/build-stamp
install:	stamps/install-stamp
binary:		binary-arch binary-indep

download-upstream:	$(ARCHIVES)

create-orig-tar:	$(ARCHIVES)
	dh_testdir
	mkdir -p gmt-$(RELEASE)/upstream
	ln -f $(ARCHIVES) gmt-$(RELEASE)/upstream
	tar cf - gmt-$(RELEASE)|gzip -9 > gmt_$(RELEASE).orig.tar.gz
	mv gmt_$(RELEASE).orig.tar.gz ..
	rm -R gmt-$(RELEASE)

upstream/%:
	dh_testdir
	mkdir -p upstream
	wget -O upstream/$*.partial ftp://ibis.grdl.noaa.gov/pub/gmt/$*
	mv upstream/$*.partial upstream/$*

stamps/unpack-stamp: 
	dh_testdir
	for i in $(ARCHIVES); do \
		tar xjf $$i || exit 1; \
	done
	mkdir -p stamps
	touch $@

stamps/patch-stamp:	stamps/unpack-stamp
	dh_testdir
	ln -sf GMT$(RELEASE) GMT
	QUILT_PATCH_OPTS="-p1" QUILT_PATCHES=debian/patches quilt push -a --color=auto || [ $$? = 2 ]
	touch $@

unpatch:
	dh_testdir
	QUILT_PATCH_OPTS="-p1" QUILT_PATCHES=debian/patches quilt pop -a || true
	rm -rf stamps/patch-stamp GMT .pc

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

stamps/build-stamp: stamps/patch-stamp
	dh_testdir
	dh_prep
	cd GMT$(RELEASE) && ./configure \
		--enable-shared $(PATHCONFIG) --disable-mex --enable-netcdf=/usr
	$(MAKE) -C GMT$(RELEASE) CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" all suppl 
	touch $@

stamps/install-stamp: stamps/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) -C GMT$(RELEASE) $(PATHVARS) install-all
	
	# 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
	
	# Move stuff from the doc directory up one level and replace the dir
	# with a symlink to .
#	mv $(DESTDIR)/usr/share/doc/gmt/doc/* $(DESTDIR)/usr/share/doc/gmt/
#	rmdir $(DESTDIR)/usr/share/doc/gmt/doc
#	ln -fs . $(DESTDIR)/usr/share/doc/gmt/doc
	
	# 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: stamps/install-stamp
	dh_testdir -a
	dh_testroot -a
	dh_install --list-missing --sourcedir=$(DESTDIR)
	dh_installchangelogs -a
	dh_installchangelogs -pgmt GMT$(RELEASE)/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: stamps/install-stamp
	dh_testdir -i
	dh_testroot -i
	dh_install --list-missing --sourcedir=$(DESTDIR)
	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 -Rf stamps GMT$(RELEASE) GMT .pc
	dh_clean
