#!/usr/bin/make -f
# -*- makefile -*-
#
# debian package by Alessandro Amici 2002-2003.
# Based on: GNU copyright 1997 to 1999 by Joey Hess.

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

# This is the debhelper compatibility version to use.
export DH_COMPAT=4

# dpatch targets
include /usr/share/dpatch/dpatch.make

# extract the non-prefix part of INST_PYMOD
inst_pymod=$(shell awk -F '/usr' '/INST_PYMOD/ {print $$2}' GDALmake.opt)

configure: configure-stamp
configure-stamp: patch-stamp
	dh_testdir

	./configure --prefix=/usr \
		--mandir=\$$\{prefix\}/share/man \
		--with-threads \
		--with-grass=no \
		--with-geotiff=internal \
		--with-jasper \
		--with-netcdf \
		--with-xerces \
		--with-geos \
		--with-mysql

	# Patch the generated libtool to avoid passing -rpath when linking,
	# and to explicitly link libraries against the libraries they
	# depend on.
	sed < libtool > libtool-2 \
	-e 's/^hardcode_libdir_flag_spec.*$$/hardcode_libdir_flag_spec=" -D__LIBTOOL_IS_A_FOOL__ "/' \
	-e '/^archive_cmds="/s/"$$/ \\$$deplibs"/' \
	-e 's/^hardcode_into_libs.*$$/hardcode_into_libs=no/'
	mv libtool-2 libtool
	chmod 755 libtool 

	touch configure-stamp

build: configure-stamp build-stamp
build-stamp: 
	dh_testdir

	$(MAKE)
	$(MAKE) docs

	touch build-stamp

clean: clean-unpatched unpatch
clean-unpatched:
	# reminiscence to old procedure of building man pages, will now created
	# by doxygen. This rm will be deleted in further releases of gdal
	sh -c "if (test -d man/man1) then rm -r man/man1; fi"

	dh_testdir
	dh_testroot

	-$(MAKE) distclean

	dh_clean build-stamp configure-stamp

install: 
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) install prefix=$(CURDIR)/debian/tmp/usr \
		"INST_PYMOD=\$$(prefix)$(inst_pymod)"
	$(MAKE) install-docs prefix=$(CURDIR)/debian/tmp/usr \
		"INST_DOCS=\$$(prefix)/share/doc/libgdal-doc"\
		"INST_MAN=\$$(prefix)/share/man"

	# To reach compatibility with window-version which puts gdal-header
	# files into /usr/include/gdal. I don't want to place headerfiles directly
	# into /usr/include/gdal to avoid incompatibility with older debian
	# package versions
	ln -fs . `pwd`/debian/tmp/usr/include/gdal

binary-common: 
	dh_testdir
	dh_testroot
	dh_movefiles
	dh_installdocs
	dh_installchangelogs ChangeLog
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -L libgdal1-1.3.1 -l debian/libgdal1-1.3.1/usr/lib
	dh_python
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-arch: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

binary-indep: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

binary: binary-arch binary-indep
.PHONY: build clean binary-arch binary-indep binary-common binary install patch unpatch clean-unpatched
