#!/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

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

config.status:
	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 

	# 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 

patch: patch-stamp
patch-stamp:
	dh_testdir
	#patch -p1 < debian/patches/01_gcc4.patch
	touch patch-stamp

build: patch build-stamp
build-stamp: config.status
	dh_testdir

	$(MAKE)
	$(MAKE) docs

	touch build-stamp
unpatch: patch unpatch-stamp
unpatch-stamp:
	dh_testdir
	# This is here for reference. It should be inside the following
	# conditional.
	# patch -p1 -R < debian/patches/01_gcc4.patch ;
	if [ -e patch-stamp ]; then \
		rm patch-stamp ; \
	fi
clean: unpatch
	# 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: build
	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-arch: build install
	dh_testdir
	dh_testroot
	dh_movefiles
	dh_installdocs
	dh_installchangelogs ChangeLog
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs -V'libgdal1c2a (>= 1.2.6)'
	dh_installdeb
	dh_shlibdeps -L libgdal1c2a -l.
	dh_python
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-arch
.PHONY: build clean binary-arch binary install
