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

PYVERS=$(shell pyversions -v -r debian/control)
PYDEF=$(shell pyversions -dv)
#RUBYDEF=$(shell ruby --version|cut -d' ' -f2|cut -d. -f1,2)
RUBYDEF=1.8
PERLDEF=$(shell perl -V:version|cut -d\' -f2)
PERL_VENDOR_ARCH=$(shell perl -V:vendorarch|cut -d\' -f2)
PERL_ARCH_NAME=$(shell perl -V:archname|cut -d\' -f2)
PERL_PATH="$(PERL_VENDOR_ARCH)/$(PERL_ARCH_NAME)"
SWIGVER=$(shell swig -version |grep Version|cut -d' ' -f3|sed -e 's/\.//g')

GDALVER=$(shell cat VERSION|sed -e 's/\./ /g')
GDAL_MAJOR=$(word 1,$(GDALVER))
GDAL_MINOR=$(word 2,$(GDALVER))
GDAL_PATCH=$(word 3,$(GDALVER))

versions:
	@echo Gdal version: $(GDAL_MAJOR).$(GDAL_MINOR).$(GDAL_PATCH)
	@echo Swig version: $(SWIGVER)
	@echo Python default: $(PYDEF)
	@echo Python versions: $(PYVERS)
	@echo Ruby default: $(RUBYDEF)
	@echo Perl default: $(PERLDEF)

#
# The following targets are useful to autogen independent plugins sources
#
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/libgdal-grass-*.tar.gz $(CURDIR)/..
	rm -f $(CURDIR)/GDALmake.opt

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

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



configure: configure-stamp
configure-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-libtiff=internal \
			--with-geotiff=internal \
			--with-jasper \
			--with-netcdf \
			--with-xerces \
			--with-geos \
			--with-sqlite3 \
			--with-curl \
			--with-pg \
			--with-ogdi \
			--with-mysql \
			--with-perl \
			--with-ruby \
			--with-python \
			--with-odbc \
			--with-dods-root=/usr \
			--with-static-proj4=yes \
			--with-spatialite=/usr \
			--with-cfitsio=no \
			--with-ecw=no \
			--with-mrsid=no \
			--with-poppler=yes \
			--with-openjpeg=yes \
			--with-freexl=yes \
			--with-libkml=yes \
			--with-armadillo=yes \
			--with-liblzma=yes \
			--with-epsilon=/usr; \
		mv GDALmake.opt GDALmake.opt-$$V; \
	done
	
	touch $@

build-arch: build
build-indep: build

build: configure-stamp build-stamp
build-stamp: 
	dh_testdir
	
	cp `ls GDALmake.opt-*|tail -1` GDALmake.opt
	$(MAKE) lib-target apps-target 
	rm -rf $(CURDIR)/swig/perl/*.c  $(CURDIR)/swig/perl/*.cpp
	$(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.
	# Apply conditionally an on-fly patch for pre 1.3.38 swig versions.
	for V in $(PYVERS); do \
		cp GDALmake.opt-$$V GDALmake.opt; \
		$(MAKE) -C $(CURDIR)/swig/python clean; \
		$(MAKE) -C $(CURDIR)/swig/python generate build; \
		$(MAKE) -C $(CURDIR)/swig/python install DESTDIR=$(CURDIR)/debian/python-tmp; \
	done

	mkdir $(CURDIR)/html || true
	$(MAKE) docs
	$(MAKE) man
	# a temporary workaround for doxygen problem #564243 
	cd man/man1 && \
			for i in `find . -name "*__*"`; do basename=`basename $$i`; mv $$basename `echo $$basename|sed -e 's/__/_/'`; done
	touch $@

clean: 
	dh_testdir
	dh_testroot
	
	[ ! -f GDALmake.opt ] || $(MAKE) distclean
	rm -rf $(CURDIR)/swig/ruby/.libs $(CURDIR)/swig/perl/*.bs
	rm -rf $(CURDIR)/.libs
	rm -f GDALmake.opt-*
	rm -rf $(CURDIR)/man/man1
	rm -rf $(CURDIR)/debian/tmp $(CURDIR)/debian/python-tmp
	rm -rf $(CURDIR)/html $(CURDIR)/doc/br/html $(CURDIR)/doc/ru/html
	rm -rf $(CURDIR)/frmts/o/.libs
	rm -f  $(CURDIR)/islibdappost310.*
	# Removes per arch symbols for compatible archs
	rm -f $(CURDIR)/debian/libgdal*.symbols.kfreebsd-i386 $(CURDIR)/debian/libgdal*.symbols.kfreebsd-amd64
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	
	cp `ls GDALmake.opt-*|tail -1` GDALmake.opt
	$(MAKE) install BINDINGS="perl ruby" DESTDIR=$(CURDIR)/debian/tmp \
			INST_DATA="\$$(prefix)/share/gdal/$(GDAL_MAJOR).$(GDAL_MINOR)" 
	$(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/gdal/$(GDAL_MAJOR).$(GDAL_MINOR)/LICENSE.TXT
	# removing empty packlist MakeMaker files 
	-find $(CURDIR)/debian/tmp$(PERL_PATH) -type f -name .packlist -exec rm -f {} \;
	# removing embedded rpath in perl shlibs
	-find $(CURDIR)/debian/tmp/$(PERL_PATH) -type f -name "*.so" -exec chrpath --delete {} \;
	# removing embedded rpath in main lib
	-find $(CURDIR)/debian/tmp/usr/lib -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$(PERL_PATH)/auto/* $(CURDIR)/debian/tmp/usr/lib/perl5/auto/.
	mv $(CURDIR)/debian/tmp$(PERL_PATH)/Geo $(CURDIR)/debian/tmp/usr/lib/perl5/.
	mv $(CURDIR)/debian/tmp/usr/local/lib/site_ruby/$(RUBYDEF)/* $(CURDIR)/debian/tmp/usr/lib/ruby/$(RUBYDEF)/.
	# copy sample programs for python-gdal
	install -o root -g root -d $(CURDIR)/debian/tmp/usr/share/doc/python-gdal/examples
	install -o root -g root -m 755 $(CURDIR)/swig/python/samples/*.py $(CURDIR)/debian/tmp/usr/share/doc/python-gdal/examples/.
	# create lintian overrides file
	echo "libgdal1: embedded-library usr/lib/libgdal.so.$$(basename debian/tmp/usr/lib/libgdal.so.*.*.*|cut -d. -f3,4,5): tiff" \
		>$(CURDIR)/debian/libgdal1.lintian-overrides

binary-common: 
	dh_testdir
	dh_testroot
	dh_install --autodest --list-missing
	dh_installdocs
	dh_installexamples
	dh_installchangelogs NEWS
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_lintian
	dh_python2 -X.info
	dh_numpy
	dh_perl
	# Adds per arch symbols for compatible archs
	ln -f $(CURDIR)/debian/libgdal*.symbols.i386 $(CURDIR)/debian/libgdal1.symbols.kfreebsd-i386
	ln -f $(CURDIR)/debian/libgdal*.symbols.amd64 $(CURDIR)/debian/libgdal1.symbols.kfreebsd-amd64
	# Forces failure ignoring to collect new symbols on all archs
	dh_makeshlibs -- -c0
	# Forces failure when symbols change
	#dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -L libgdal1 -l debian/libgdal1/usr/lib
	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 gdal-grass-dist versions build-arch build-indep
