#!/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)
PERLDEF=$(shell perl -V:version|cut -d\' -f2)
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)
	@echo GDAL version: $(GDAL_MAJOR).$(GDAL_MINOR).$(GDAL_PATCH)

#
# 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
	dh_autoreconf sh -- ./autogen.sh
	
	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-hide-internal-symbols=yes \
			--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-epsilon=/usr; \
		mv GDALmake.opt GDALmake.opt-$$V; \
	done
	
	touch $@

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
	if [ ! -f $(CURDIR)/swig/ruby/gdal_wrap.cpp.orig ] ; then \
        mv -f $(CURDIR)/swig/ruby/gdal_wrap.cpp $(CURDIR)/swig/gdal-wrap.cpp.orig ;\
    fi
	$(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

	$(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)/swig/perl/*.c  $(CURDIR)/swig/perl/*.cpp
	if [ -f $(CURDIR)/swig/ruby/gdal_wrap.cpp.orig ] ; then \
        mv -f $(CURDIR)/swig/ruby/gdal_wrap.cpp.orig $(CURDIR)/swig/gdal-wrap.cpp ;\
    fi
	rm -f GDALmake.opt-*
	rm -rf $(CURDIR)/man/man1
	rm -rf $(CURDIR)/debian/tmp $(CURDIR)/debian/python-tmp

	dh_autoreconf_clean    
	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/1.7" 
	$(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/1.7/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 {} \;
	# 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/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)/.
	# 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-1.7.0: embedded-library usr/lib/libgdal1.7.0.so.$$(basename debian/tmp/usr/lib/libgdal1.7.0.so.*.*.*|cut -d. -f5,6,7): tiff" \
		>$(CURDIR)/debian/libgdal1-1.7.0.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_pycentral -X.info
	dh_perl
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -L libgdal1-1.7.0 -l debian/libgdal1-1.7.0/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
