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

# Enable hardening build flags
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

# Disable PIE on Ubuntu where it's still problematic
VENDOR_DERIVES_FROM_UBUNTU ?= $(shell dpkg-vendor --derives-from Ubuntu && echo yes)
DISTRIBUTION_RELEASE       := $(shell lsb_release -cs)

ifeq ($(VENDOR_DERIVES_FROM_UBUNTU),yes)
  ifneq (,$(filter $(DISTRIBUTION_RELEASE),xenial bionic))
    export DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie
  endif
endif

include /usr/share/dpkg/pkg-info.mk

UPSTREAM_VERSION = $(shell echo $(DEB_VERSION_UPSTREAM) | sed -e 's/\+.*//')

PYVERS=$(shell py3versions -v -r)
PYDEF=$(shell py3versions -dv)
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))

ifeq (,$(DEB_HOST_MULTIARCH))
  DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
endif

ifneq ($(wildcard /usr/lib/$(DEB_HOST_MULTIARCH)/hdf5/serial/libhdf5.so),)
  WITH_HDF5:=--with-hdf5=/usr/lib/$(DEB_HOST_MULTIARCH)/hdf5/serial
else
  WITH_HDF5:=
endif

ifneq (,$(filter $(DEB_HOST_ARCH),amd64 i386))
  WITH_SSE=yes
  WITH_SSSE3=yes
  WITH_AVX=yes
else
  WITH_SSE=no
  WITH_SSSE3=no
  WITH_AVX=no
endif

NJOBS := -j1
ifneq (,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
NJOBS := -j$(subst parallel=,,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
endif

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

#
# The following targets are useful to autogen independent plugins sources
#
gdal-grass: override_dh_auto_configure gdal-grass-dist override_dh_auto_clean override_dh_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: override_dh_auto_configure gdal-ecw-dist override_dh_auto_clean override_dh_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: override_dh_auto_configure gdal-mrsid-dist override_dh_auto_clean override_dh_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

%:
	if [ "$@" != "gdal-grass" -a "$@" != "gdal-ecw" -a "$@" != "gdal-mrsid" ]; then \
		dh $@ --with python3 ; \
	fi

override_dh_clean:
	dh_clean config.log

override_dh_auto_clean:
	[ ! -f GDALmake.opt ] || $(MAKE) distclean
	rm -rf $(CURDIR)/swig/perl/*.bs $(CURDIR)/swig/perl/doc
	rm -rf $(CURDIR)/.libs
	rm -f GDALmake.opt-*
	rm -rf $(CURDIR)/debian/tmp $(CURDIR)/debian/python-tmp
	rm -rf $(CURDIR)/doc/br/html $(CURDIR)/doc/ru/html
	rm -rf $(CURDIR)/frmts/o/.libs
	rm -f  $(CURDIR)/islibdappost310.*

override_dh_auto_configure:
	for V in $(PYVERS); do \
		PYTHON=/usr/bin/python$$V ./configure --prefix=/usr \
			--mandir=\$$\{prefix\}/share/man \
			--includedir=\$$\{prefix\}/include/gdal \
			--with-hide-internal-symbols=yes \
			--with-rename-internal-libtiff-symbols=yes \
			--with-rename-internal-libgeotiff-symbols=yes \
			--with-threads \
			--with-sse=$(WITH_SSE) \
			--with-ssse3=$(WITH_SSSE3) \
			--with-avx=$(WITH_AVX) \
			--with-bash-completion=yes \
			--with-armadillo=yes \
			--with-cfitsio=yes \
			--with-charls \
			--with-curl \
			--with-libdeflate \
			--with-ecw=no \
			--with-epsilon=/usr \
			--with-freexl=yes \
			--with-geos \
			--with-geotiff=yes \
			--with-grass=no \
			$(WITH_HDF5) \
			--with-heif \
			--with-libjson-c=/usr \
			--with-libkml=yes \
			--with-liblzma=yes \
			--with-libtiff=yes \
			--with-mrsid=no \
			--with-mysql \
			--with-netcdf \
			--with-odbc \
			--with-ogdi \
			--with-openjpeg=yes \
			--with-pg \
			--with-poppler=yes \
			--with-proj=yes \
			--with-qhull=yes \
			--with-sosi=yes \
			--with-spatialite=/usr \
			--with-sqlite3 \
			--with-webp \
			--with-xerces \
			--with-zstd \
			--with-dods-root=/usr \
			--with-perl \
			--with-python; \
		mv GDALmake.opt GDALmake.opt-$$V; \
	done

override_dh_auto_build:
	cp `ls GDALmake.opt-*|tail -1` GDALmake.opt
	$(MAKE) $(NJOBS) lib-target apps-target
	rm -rf $(CURDIR)/swig/perl/*.c  $(CURDIR)/swig/perl/*.cpp
	$(MAKE) $(NJOBS) -C $(CURDIR)/swig/perl generate build doc

	# It needs pre-installing just after the building due to intermediate cleaning.
	for V in $(PYVERS); do \
		cp GDALmake.opt-$$V GDALmake.opt && \
		$(MAKE) $(NJOBS) -C $(CURDIR)/swig/python clean && \
		$(MAKE) -C $(CURDIR)/swig/python generate && \
		$(MAKE) $(NJOBS) -C $(CURDIR)/swig/python build && \
		$(MAKE) $(NJOBS) -C $(CURDIR)/swig/python install DESTDIR=$(CURDIR)/debian/python-tmp; \
	done

override_dh_auto_install:
	cp `ls GDALmake.opt-*|tail -1` GDALmake.opt
	$(MAKE) $(NJOBS) install BINDINGS="perl" DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) $(NJOBS) install-man DESTDIR=$(CURDIR)/debian/tmp \
		"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 useless autogenerated man pages
	rm -f $(CURDIR)/debian/tmp/usr/share/man/man1/_*_apps_.1

	# removing license file
	rm -f $(CURDIR)/debian/tmp/usr/share/gdal/LICENSE.TXT

	# removing la file
	rm -f $(CURDIR)/debian/tmp/usr/lib/libgdal.la

	# removing embedded rpath in main lib
	-find $(CURDIR)/debian/tmp/usr/lib -type f -name "*.so*" -exec chrpath --delete {} \;

	# copy sample programs for python3-gdal
	install -o root -g root -d $(CURDIR)/debian/tmp/usr/share/doc/python3-gdal/examples
	install -o root -g root -m 755 $(CURDIR)/swig/python/samples/*.py $(CURDIR)/debian/tmp/usr/share/doc/python3-gdal/examples/.

override_dh_install:
	dh_install --autodest --list-missing

override_dh_installchangelogs:
	dh_installchangelogs NEWS

override_dh_installdocs:
	dh_installdocs -A third_party/LercLib/NOTICE

	# Remove autogenerated Doxygen files
	rm -f $(CURDIR)/debian/*/usr/share/doc/libgdal-perl-doc/*/*.md5

override_dh_python3:
	dh_python3 -X.info -pgdal-bin -ppython3-gdal --shebang=/usr/bin/python3
	dh_numpy3 -pgdal-bin -ppython3-gdal

override_dh_makeshlibs:
	# Forces failure ignoring to collect new symbols on all archs
	dh_makeshlibs -- -v$(UPSTREAM_VERSION) -c0
	# Forces failure when symbols change
	#dh_makeshlibs -- -v$(UPSTREAM_VERSION)

override_dh_shlibdeps:
	dh_shlibdeps -L libgdal26 -l debian/libgdal26/usr/lib
