#!/usr/bin/make -f

# 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

VERSION=$(shell echo `head -2 $(CURDIR)/include/VERSION` | sed -e 's/ //')
ABI=$(shell echo `head -3 $(CURDIR)/include/VERSION` | sed -e 's/ //g' -e 's/RC/-/')
GRASS_ABI=grass$(ABI)
BASE_NAME=grass$(VERSION)
PKG_NAME=grass

# TODO: fix these
CFLAGS+=-Wno-error=format-security

CFLAGS+=-Wall

CFLAGS+=-DNDEBUG

TEMPLATES=$(wildcard debian/*.in)

# Use SOURCE_DATE_EPOCH as random seed for reproducible builds
ifdef SOURCE_DATE_EPOCH
	export GRASS_RANDOM_SEED=$(SOURCE_DATE_EPOCH)
endif

versions:
	@echo "Version: $(VERSION)"
	@echo "ABI: $(ABI)"

templates:
	for TEMPLATE in $(TEMPLATES) ; do \
	    sed -e 's/@VERSION@/$(VERSION)/g' -e 's/@ABI@/$(ABI)/g' $$TEMPLATE > `echo $$TEMPLATE | sed 's/.in$$//'` ; \
	done

%:
	dh $@ --with python3

override_dh_clean:
	dh_clean build-arch-stamp install-arch-stamp debian/grass-gui.image-file-in-usr-lib.list

	$(RM) lib/proj/nad2bin
	$(RM) tools/timer/main.o
	$(RM) raster/r.terraflow/IOStream/lib/src/libiostream.a
	$(RM) debian/$(BASE_NAME).1
	$(RM) error.log

	# clean doxygen documentation (programming man)
	$(RM) doxygen_sqlite3.db lib/doxygen_sqlite3.db
	$(RM) -r \
		doxygenhtml \
		html latex \
		rfc/html rfc/latex \
		lib/html lib/latex \
		gui/wxpython/html gui/wxpython/latex
	for LIBRARY in \
		db \
		g3d \
		gis \
		gmath \
		gpde \
		ogsf \
		proj \
		python \
		segment \
		vector \
		vector/dglib ; \
	do \
		$(RM) -r lib/$$LIBRARY/html lib/$$LIBRARY/latex ; \
	done

override_dh_auto_clean:
	[ ! -f $(CURDIR)/include/Make/Platform.make ] || $(MAKE) distclean

override_dh_auto_configure: templates
	dh_auto_configure -- \
                    --prefix=/usr/lib \
                    --enable-largefile \
                    --enable-socket \
                    --enable-shared \
                    --with-blas \
                    --with-bzlib \
                    --with-cairo \
                    --with-cxx \
                    --with-freetype \
                    --with-freetype-includes=/usr/include/freetype2 \
                    --with-gdal \
                    --with-geos \
                    --with-lapack \
                    --with-motif \
                    --with-mysql \
                    --with-mysql-includes="$(shell mysql_config --include | sed -e 's/-I//g')" \
                    --with-netcdf \
                    --with-nls \
                    --with-odbc \
                    --with-openmp \
                    --with-postgres \
                    --with-postgres-includes=$(shell pg_config --includedir) \
                    --with-proj-share=/usr/share/proj \
                    --with-python \
                    --with-readline \
                    --with-sqlite \
                    --with-wxwidgets=/usr/bin/wx-config \
                    --with-x \
                    --with-zstd

# This target generates arch-dep stuff, unfortunately most doc requires working bins to
# be properly generated...
override_dh_auto_build-arch: build-arch-stamp
build-arch-stamp:
	dh_auto_build
	chmod 755 debian/fixpaths.sh
	touch $@

override_dh_auto_build-indep: build-arch-stamp
	# generate the Programmers' manual (in HTML)
	$(MAKE) htmldocs-single

	# save ~7mb of disk space by compressing PNG images (but takes a long time)
	##for file in `find debian/tmp/programming-manual/ -iname \*.png` ; do \
	##	optipng -o5 $$file ; \
	##done

override_dh_auto_install-arch: install-arch-stamp
install-arch-stamp:
	# install grass core into debian/tmp
	dh_auto_install -- \
		INST_DIR=/usr/lib/$(BASE_NAME) \
		UNIX_BIN=/usr/bin

	# fix links
	debian/fixpaths.sh $(CURDIR)/debian/tmp debian/tmp

	# delete python compiled bytecode files (maybe dh_python2 takes care of this for us?)
	find debian/tmp/ -type f -name "*.pyc" -delete

	# change section from 1 to .1grass
	mv debian/tmp/usr/lib/$(BASE_NAME)/docs/man/man1/grass7.1 debian/$(BASE_NAME).1
	for m in debian/tmp/usr/lib/$(BASE_NAME)/docs/man/man1/*.1; do \
		n=`basename $$m .1`; mv $$m debian/tmp/usr/lib/$(BASE_NAME)/docs/man/man1/$$n.1grass; done
	for m in debian/tmp/usr/lib/$(BASE_NAME)/docs/man/man1/*.1grass; do \
		sed -i -e 's/^.TH \(.*\) 1/.TH \1 1grass/' "$$m"; done

	# escape minus signs which are command line options not hyphens
	for m in debian/tmp/usr/lib/$(BASE_NAME)/docs/man/man1/*.1grass; do \
		sed -i -e 's/\([ ([]\)-\([a-z]\)/\1\\-\2/g' \
			-e 's/\([ []\)--\([a-z]\)/\1\\-\\-\2/g' \
			-e 's/\[-\\fB/[\\-\\fB/' \
			-e 's/\[--\\fB/[\\-\\-\\fB/g' \
			-e 's/"\\fB-\([a-zA-Z0-9]\)/"\\fB\\-\1/' \
			-e 's/"\\fB--\([a-zA-Z0-9]\)/"\\fB\\-\\-\1/' \
			-e 's/\\fI-\([a-zA-Z0-9]\)/\\fI\-\1/g' \
			"$$m"; done

	# adjust from build-server build dir to end-user's install dir
	sed -i -e "s+^\(GRASS_HOME[ ]*=\) .*+\1 /usr/lib/$(BASE_NAME)+" \
		-e "s+^\(RUN_GISBASE[ ]*=\) .*+\1 /usr/lib/$(BASE_NAME)+" \
		debian/tmp/usr/lib/$(BASE_NAME)/include/Make/Platform.make

	# install pkg-config file
	mkdir -p debian/$(PKG_NAME)-dev/usr/share/pkgconfig
	install -m 644 grass.pc debian/$(PKG_NAME)-dev/usr/share/pkgconfig/$(BASE_NAME).pc

	# install icons and desktop file
	mkdir -p debian/tmp/usr/share/icons
	mv debian/tmp/usr/lib/$(BASE_NAME)/share/icons/hicolor debian/tmp/usr/share/icons

	mkdir -p debian/tmp/usr/share/applications
	mv debian/tmp/usr/lib/$(BASE_NAME)/share/applications/grass.desktop debian/tmp/usr/share/applications/$(BASE_NAME).desktop

	# install AppStream metadata
	mkdir -p debian/tmp/usr/share/metainfo
	mv debian/tmp/usr/lib/$(BASE_NAME)/share/metainfo/org.osgeo.grass.appdata.xml debian/tmp/usr/share/metainfo/org.osgeo.grass.appdata.xml

	# install grass wrapper scripts
	install -m 755 debian/x-grass7 debian/tmp/usr/bin/x-$(BASE_NAME)

	ln -fs /usr/lib/$(shell objdump -p /usr/lib/`gdal-config --libs | cut -d' ' -f2 | sed -e 's/-l/lib/'`.so | grep SONAME | cut -d' ' -f18) \
		debian/tmp/usr/lib/$(BASE_NAME)/lib/libgdal.so

	# Remove empty directory
	rmdir debian/tmp/usr/lib/$(BASE_NAME)/gui/wxpython/scripts/

	# Remove files not installed or installed by debhelper
	$(RM) -r debian/tmp/usr/lib/$(BASE_NAME)/AUTHORS \
	         debian/tmp/usr/lib/$(BASE_NAME)/CHANGES \
	         debian/tmp/usr/lib/$(BASE_NAME)/CITING \
	         debian/tmp/usr/lib/$(BASE_NAME)/COPYING \
	         debian/tmp/usr/lib/$(BASE_NAME)/GPL.TXT \
	         debian/tmp/usr/lib/$(BASE_NAME)/INSTALL \
	         debian/tmp/usr/lib/$(BASE_NAME)/REQUIREMENTS.html \
	         debian/tmp/usr/lib/$(BASE_NAME)/config.status \
	         debian/tmp/usr/lib/$(BASE_NAME)/contributors.csv \
	         debian/tmp/usr/lib/$(BASE_NAME)/contributors_extra.csv \
	         debian/tmp/usr/lib/$(BASE_NAME)/demolocation/PERMANENT/.tmp/* \
	         debian/tmp/usr/lib/$(BASE_NAME)/translation_status.json \
	         debian/tmp/usr/lib/$(BASE_NAME)/translators.csv

	# Move image files from /usr/lib/$(BASE_NAME) to /usr/share/$(BASE_NAME)
	mkdir -p debian/tmp/usr/share/$(BASE_NAME)/gui
	mv debian/tmp/usr/lib/$(BASE_NAME)/gui/icons/  debian/tmp/usr/share/$(BASE_NAME)/gui/
	mv debian/tmp/usr/lib/$(BASE_NAME)/gui/images/ debian/tmp/usr/share/$(BASE_NAME)/gui/

	find debian/tmp/usr/lib/$(BASE_NAME)/gui/wxpython/ -type f \( -name "*.jpg" -or -name "*.png" \) -print | sort > debian/grass-gui.image-file-in-usr-lib.list

	while read file; do \
		dir=`dirname "$$file" | sed 's/usr\/lib\//usr\/share\//'` ; \
		if [ ! -e "$$dir" ]; then \
			mkdir -p "$$dir" ; \
		fi ; \
		mv "$$file" "$$dir" ; \
	done < debian/grass-gui.image-file-in-usr-lib.list

	# Remove empty files
	find debian/tmp/ -type f -empty -name "class_graphical*" -print -delete

	touch $@

override_dh_auto_install-indep: install-arch-stamp
	install -m 644 -d debian/tmp
	install -m 644 -d debian/tmp/programming-manual/html
	cp -r lib/html/* debian/tmp/programming-manual/html/
	rm -f debian/tmp/programming-manual/html/*.md5

override_dh_install-arch:
	# Strip binaries
	strip --strip-unneeded --remove-section=.comment --remove-section=.note debian/tmp/usr/lib/$(BASE_NAME)/bin/r.in.png
	strip --strip-unneeded --remove-section=.comment --remove-section=.note debian/tmp/usr/lib/$(BASE_NAME)/bin/r.out.png

	dh_install --arch

	# delete duplicated grass-gui stuff from grass-core package
	for COMPONENT in gui wxpython; do \
		rm -rf debian/$(PKG_NAME)-core/usr/lib/$(BASE_NAME)/$$COMPONENT; \
	done

override_dh_install-indep:
	dh_install --indep

override_dh_installchangelogs:
	dh_installchangelogs CHANGES

override_dh_installman:
	dh_installman -p$(PKG_NAME)-core debian/$(BASE_NAME).1

override_dh_python3:
	dh_python3 -pgrass-core -pgrass-gui -pgrass-dev
	dh_python3 -pgrass-core /usr/lib/$(BASE_NAME)/etc/
	dh_python3 -pgrass-core /usr/lib/$(BASE_NAME)/scripts/
	dh_python3 -pgrass-gui /usr/lib/$(BASE_NAME)/gui/
	dh_python3 -pgrass-dev /usr/lib/$(BASE_NAME)/tools/
	dh_numpy3 -pgrass-core -pgrass-gui

override_dh_shlibdeps:
	dh_shlibdeps -l$(CURDIR)/debian/tmp/usr/lib/$(BASE_NAME)/lib

override_dh_compress:
	dh_compress -XAUTHORS

override_dh_fixperms:
	dh_fixperms -Xr.in.png -Xr.out.png

override_dh_gencontrol:
	dh_gencontrol -- -Vgrass:Provides="$(GRASS_ABI)"

.PHONY: templates versions
