#!/usr/bin/make -f
# Made with the aid of dh_make, by Craig Small
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.
# Some lines taken from debmake, by Cristoph Lameter.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export DH_ALWAYS_EXCLUDE=CVS:.svn

PROJ_VERSION=$(shell dpkg-parsechangelog | sed -ne 's/^Version: \(.*\)-.*/\1/p')

JAVA_HOME = /usr/lib/jvm/default-java

CPPFLAGS=$(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS=$(shell dpkg-buildflags --get LDFLAGS)
CFLAGS=$(shell dpkg-buildflags --get CFLAGS)
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
CFLAGS += -g
endif
# `nostrip' handled by dh_strip...

CFLAGS += -I$(JAVA_HOME)/include/linux

AUTORECONF_BACKUP += INSTALL Makefile.in aclocal.m4
AUTORECONF_BACKUP += config.guess config.sub configure
AUTORECONF_BACKUP += depcomp install-sh ltmain.sh
AUTORECONF_BACKUP += jniwrap/Makefile.in
AUTORECONF_BACKUP += jniwrap/org/Makefile.in
AUTORECONF_BACKUP += jniwrap/org/proj4/Makefile.in
AUTORECONF_BACKUP += man/Makefile.in man/man1/Makefile.in man/man3/Makefile.in
AUTORECONF_BACKUP += missing mkinstalldirs
AUTORECONF_BACKUP += nad/Makefile.in src/Makefile.in
AUTORECONF_BACKUP += src/proj_config.h.in

datumgrids: datumgrids-stamp
datumgrids-stamp:
	unshar -c -d $(CURDIR)/nad $(CURDIR)/debian/datumgrids.shar
	unshar -c -d $(CURDIR)/nad $(CURDIR)/debian/datumgrids-ch.shar
	touch $@

config: datumgrids config.status
config.status: 
	for FILE in $(AUTORECONF_BACKUP); do \
		mv $${FILE} $${FILE}.pre-autoreconf ; \
	done
	dh_autoreconf
	./configure --prefix=/usr --infodir=/usr/share/info --mandir=/usr/share/man --with-jni=$(JAVA_HOME)/include

build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: config
	dh_testdir
	if [ ! -e "jniwrap/libs/jproj.jar" ]; then \
		dh_auto_build -Sant -Djniwrap -- do_javah jar_it do_make_help ; \
	fi
	$(MAKE) CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" CPPFLAGS="$(CPPFLAGS)"
	touch $@

build-indep: build-indep-stamp
build-indep-stamp: config
	touch $@

clean: 
	dh_testdir
	dh_testroot
	( cd $(CURDIR)/nad && cat $(CURDIR)/debian/datumgrids.list $(CURDIR)/debian/datumgrids-ch.list | xargs rm -f )
	rm -f datumgrids-stamp
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f $(CURDIR)/debian/*.log
	dh_auto_clean -Sant -Djniwrap
	dh_autoreconf_clean
	for FILE in $(AUTORECONF_BACKUP); do \
		if [ -e "$${FILE}.pre-autoreconf" ]; then \
			mv $${FILE}.pre-autoreconf $${FILE} ; \
		fi ; \
	done
	dh_clean nad/FL nad/MD nad/TN nad/WI nad/WO \
	         nad/alaska nad/conus nad/hawaii \
	         nad/null nad/prvi \
	         nad/stgeorge nad/stlrnc nad/stpaul \
	         src/org_proj4_PJ.h src/org_proj4_PJ_Type.h

install: install-arch install-indep

install-arch: install-arch-stamp
install-arch-stamp: build-arch
	dh_testdir
	dh_testroot
	dh_installdirs -a
	dh_prep -a
	
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install 
	cp $(CURDIR)/src/projects.h $(CURDIR)/debian/tmp/usr/include/.
	cp $(CURDIR)/src/org_proj4_PJ.h $(CURDIR)/debian/tmp/usr/include/.
	cp $(CURDIR)/src/org_proj4_PJ_Type.h $(CURDIR)/debian/tmp/usr/include/.
	
	# Fix manpages 
	for man in $(CURDIR)/debian/tmp/usr/share/man/man1/*.1 $(CURDIR)/debian/tmp/usr/share/man/man3/*.3; \
	do \
		sed -i -e 's/^\.nr LL 5.5i$$/\.\\"/' $$man; \
	done
	# Fixating man section in pj_init.3
	sed -i -e 's/\(.TH PJ_INIT\) 3U\(.*\)/\1 3\2/' $(CURDIR)/debian/tmp/usr/share/man/man3/pj_init.3
	# Empty dependency_libs in .la files
	sed -i "/dependency_libs/ s/'.*'/''/" `find . -name '*.la'`
	touch $@

install-indep: install-indep-stamp
install-indep-stamp: build-indep
	touch $@

install-data: install-data-stamp
install-data-stamp: build-arch
	dh_testdir
	dh_testroot
	dh_installdirs
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install-data
	touch $@

# Build architecture-independent files here.
binary-indep: install-indep
	# proj has no "Architecture: all" binary packages

# Build architecture-dependent files here.
binary-arch: install-arch
	dh_testdir -a
	dh_testroot -a
	dh_install -a -X.svn
	dh_installdocs -a AUTHORS README
	dh_installexamples -a
	dh_installman -a
	dh_link -a
	dh_installchangelogs -a ChangeLog upstream
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
# Forces failure ignoring to collect new symbols
	dh_makeshlibs -a -- -c0 -v$(PROJ_VERSION)
	dh_shlibdeps -a
	dh_installdeb -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

source diff:                                                                  
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

binary: binary-arch binary-indep

.PHONY: build clean binary-indep binary-arch binary datumgrids config install
