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

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

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

PYVERS=$(shell pyversions -v -r debian/control)
PYDEF=$(shell pyversions -dv)
RUBYDEF=$(shell ruby --version|cut -d' ' -f2|cut -d. -f1,2)
PERLDEF=$(shell perl -V:version|cut -d\' -f2)

gdal-grass: configure gdal-grass-dist clean
gdal-grass-dist:
	ln -fs $(CURDIR)/GDALmake.opt-$(PYDEF) $(CURDIR)/GDALmake.opt
	[ -e $(CURDIR)/GDALmake.opt ] && $(MAKE) -C $(CURDIR)/frmts/grass dist
	mv $(CURDIR)/frmts/grass/gdal-grass-*.tar.gz $(CURDIR)/..
	rm -f $(CURDIR)/GDALmake.opt

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

	for V in $(PYVERS); do \
		PYTHON=/usr/bin/python$$V ./configure --prefix=/usr \
			--mandir=\$$\{prefix\}/share/man \
			--includedir=\$$\{prefix\}/include/gdal \
			--with-threads \
			--with-grass=no \
			--with-geotiff=internal \
			--with-xerces \
			--with-sqlite \
			--without-perl \
			--without-ruby \
			--with-cfitsio=no; \
		mv GDALmake.opt GDALmake.opt-$$V; \
	done

	# Overrides the generated libtool with official one, due to 
	# breakage in upstream version.
	#cp /usr/bin/libtool $(CURDIR)/libtool

	# 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

	cp `ls GDALmake.opt-*|tail -1` GDALmake.opt
	$(MAKE) lib-target apps-target 
	#$(MAKE) -C $(CURDIR)/swig/perl generate build
	#$(MAKE) -C $(CURDIR)/swig/ruby generate build

	# it needs pre-installing just after the building due to intermediate cleaning
	for V in $(PYVERS); do \
		cp GDALmake.opt-$$V GDALmake.opt; \
		$(MAKE) -C $(CURDIR)/swig/python clean; \
		$(MAKE) -C $(CURDIR)/swig/python build; \
		$(MAKE) -C $(CURDIR)/swig/python install DESTDIR=$(CURDIR)/debian/python-tmp; \
		echo "Patching ogr.py" ; \
		patch $(CURDIR)/debian/python-tmp/usr/lib/python$$V/site-packages/osgeo/ogr.py $(CURDIR)/debian/ogr.py.diff; \
	done
	#$(MAKE) docs
	$(MAKE) man

	touch build-stamp

clean: clean-unpatched unpatch
clean-unpatched:
	dh_testdir
	dh_testroot

	[ ! -f GDALmake.opt ] || $(MAKE) distclean
	rm -rf $(CURDIR)/swig/ruby/.libs $(CURDIR)/swig/perl/*.bs
	rm -rf $(CURDIR)/swig/perl/*.c  $(CURDIR)/swig/perl/*.cpp
	rm -f GDALmake.opt-*
	rm -rf $(CURDIR)/man/man1
	rm -rf $(CURDIR)/debian/tmp $(CURDIR)/debian/python-tmp

	dh_clean build-stamp configure-stamp

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	cp `ls GDALmake.opt-*|tail -1` GDALmake.opt
	$(MAKE) install BINDINGS="perl ruby" DESTDIR=$(CURDIR)/debian/tmp \
			INST_DATA="\$$(prefix)/share/gdal15" 
	#$(MAKE) install-docs DESTDIR=$(CURDIR)/debian/tmp \
	#	"INST_DOCS=\$$(prefix)/share/doc/libgdal-doc"\
	#	"INST_MAN=\$$(prefix)/share/man" 
	$(MAKE) install-man DESTDIR=$(CURDIR)/debian/tmp \
		"INST_DOCS=\$$(prefix)/share/doc/libgdal-doc"\
		"INST_MAN=\$$(prefix)/share/man" 

	# install python stuff previuosly built and pre-installed
	cp -a $(CURDIR)/debian/python-tmp/usr/lib/* $(CURDIR)/debian/tmp/usr/lib/.
	install -o root -g root -d $(CURDIR)/debian/tmp/usr/bin
	install -o root -g root -m 755 $(CURDIR)/swig/python/scripts/*.py $(CURDIR)/debian/tmp/usr/bin/.                                        

	# removing license file
	rm -f $(CURDIR)/debian/tmp/usr/share/gdal15/LICENSE.TXT
	# removing empty packlist MakeMaker files 
	#-find $(CURDIR)/debian/tmp/usr/local/lib/perl -type f -name .packlist -exec rm -f {} \;
	# removing embedded rpath in perl shlibs
	#-find $(CURDIR)/debian/tmp/usr/local/lib/perl -type f -name "*.so" -exec chrpath --delete {} \;

	# moves ruby/perl stuff the right dirs
	#install -o root -g root -d $(CURDIR)/debian/tmp/usr/lib/perl5/auto
	#install -o root -g root -d $(CURDIR)/debian/tmp/usr/lib/ruby/$(RUBYDEF)
	#mv $(CURDIR)/debian/tmp/usr/local/lib/perl/$(PERLDEF)/auto/* $(CURDIR)/debian/tmp/usr/lib/perl5/auto/.
	#mv $(CURDIR)/debian/tmp/usr/local/lib/perl/$(PERLDEF)/Geo $(CURDIR)/debian/tmp/usr/lib/perl5/.
	#mv $(CURDIR)/debian/tmp/usr/local/lib/site_ruby/$(RUBYDEF)/* $(CURDIR)/debian/tmp/usr/lib/ruby/$(RUBYDEF)/.

binary-common: 
	dh_testdir
	dh_testroot
	dh_movefiles
	#dh_installdocs
	#dh_installchangelogs NEWS
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_pycentral -X.info
	rm -f $(CURDIR)/debian/python-gdal/usr/share/pycentral/python-gdal/site-packages/GDAL-1.5.0.egg-info
	dh_perl
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -L libgdal1-1.5.0 -l debian/libgdal1-1.5.0/usr/lib
	dh_gencontrol
	dh_md5sums
	#maemo-optify
	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 gdal-grass-dist
