#!/usr/bin/make -f
# -*- makefile -*-
# debian/rules for fis-gtm

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

MARCH := $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
ARCH := $(shell dpkg-architecture -qDEB_HOST_GNU_CPU)
UVER := $(shell LC_ALL=C dpkg-parsechangelog | awk '/^Version:/{sub(/-[0-9]*$$/,"");print $$2;}')
UAPIVER := $(shell echo $(UVER) | sed -e 's,+.*,,')
UAPIDIR := V$(UAPIVER)_$(ARCH)
SRCPKG := $(shell dpkg-parsechangelog | sed -n 's/^Source: //p')
BINPKG := $(shell awk '/Package:.*[0-9]/{print $$2;}' debian/control)
GTM_INSTALL_DIR := lib/$(MARCH)/fis-gtm/$(UAPIDIR)
LOCAL_GTM_INSTALL_DIR := $(CURDIR)/debian/$(BINPKG)/usr/$(GTM_INSTALL_DIR)

%:
	dh $@ --parallel

# Set up locale support in the pbuilder environment
override_dh_auto_build:
	mkdir -p debian/tmp/locale/
	localedef -f UTF-8 -i en_US ./debian/tmp/locale/en_US.UTF-8/
	export LOCPATH=$(CURDIR)/debian/tmp/locale/ && \
	export LC_ALL=en_US.UTF-8 && \
	dh_auto_build

override_dh_auto_configure:
	dh_auto_configure -- -DCMAKE_INSTALL_PREFIX:PATH=/usr -DGTM_INSTALL_DIR=$(GTM_INSTALL_DIR)

override_dh_auto_install:
	#echo "umask is: "
	#umask
	dh_auto_install --destdir=debian/$(BINPKG)-stage1 $@
	cd debian/$(BINPKG)-stage1/usr/$(GTM_INSTALL_DIR) && \
		gtm_destdir=$(CURDIR)/debian/$(BINPKG) \
		gtmcompile="-embed_source" \
		./gtminstall --utf8 default --user root --group root --verbose \
			--distrib . \
			--installdir $(LOCAL_GTM_INSTALL_DIR)
	#@echo "I: Removing redundant license file. One is already available."
	find $(LOCAL_GTM_INSTALL_DIR) -name COPYING -delete
	# remove empty directories
	find $(LOCAL_GTM_INSTALL_DIR) -type d -empty -delete
	# fix permissions of directories
	find $(LOCAL_GTM_INSTALL_DIR) -type d -exec chmod u+x \{\} \;

override_dh_fixperms:
	#echo "umask is: "
	#umask
	dh_fixperms
	#echo "overriding dh_fixperms"
	#echo "I: Fixing up permissions for removed write rights -- we aren't done yet!"
	chmod +w -R $(LOCAL_GTM_INSTALL_DIR)
	chmod +w -R $(LOCAL_GTM_INSTALL_DIR)/utf8
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/utf8
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/plugin
	#echo "I: Fixing up permissions for setuid rights -- we aren't done yet!"
	chmod 4755 $(LOCAL_GTM_INSTALL_DIR)/gtmsecshr
	chmod 0500 $(LOCAL_GTM_INSTALL_DIR)/gtmsecshrdir
	chmod 4500 $(LOCAL_GTM_INSTALL_DIR)/gtmsecshrdir/gtmsecshr
	#echo "I: changing permissions to 644 for files that will be sourced"
	chmod 0644 $(LOCAL_GTM_INSTALL_DIR)/gtmprofile
	chmod 0644 $(LOCAL_GTM_INSTALL_DIR)/gtmprofile_preV54000
	chmod 0644 $(LOCAL_GTM_INSTALL_DIR)/gtcm_slist
	chmod 0644 $(LOCAL_GTM_INSTALL_DIR)/gtmcshrc
	#echo "I: Fixing executable permissions"
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/dse
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/ftok
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/geteuid
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/gtcm_gnp_server
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/gtcm_pkdisp
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/gtcm_play
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/gtcm_run
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/gtcm_server
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/gtcm_shmclean
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/gtm
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/gtmbase
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/gtmstart
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/gtmstop
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/lke
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/mumps
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/mupip
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/semstat2
	chmod 0755 $(LOCAL_GTM_INSTALL_DIR)/plugin/gtmcrypt/*.sh

	#echo "I: Final permissions after dh_fixperms"
	#ls -l $(LOCAL_GTM_INSTALL_DIR)

# yoh: although it might sound to remove write permissions for the
# deployed materials, as far as I see it, it is useless, since the
# user (root) would always be able to revert them to become writable.

# Because 'the' binary package name will change, let's just generate
# override files
# The override is needed according to upstream for GTM to generate executable 'call-in's
override_dh_lintian:
	echo "$(BINPKG) binary: shlib-with-executable-stack usr/lib/*/fis-gtm/*/libgtmshr.so" >  debian/$(BINPKG).lintian-overrides
	echo "$(BINPKG) binary: shared-lib-without-dependency-information usr/lib/*/fis-gtm/*/libgtmutil.so" >> debian/$(BINPKG).lintian-overrides
	echo "# The following permissions are really set intentionally in override_dh_fixpermissions" >> debian/$(BINPKG).lintian-overrides
	echo "$(BINPKG): setuid-binary usr/lib/*/fis-gtm/*/gtmsecshrdir/gtmsecshr 4500 root/root" >> debian/$(BINPKG).lintian-overrides
	echo "$(BINPKG): executable-is-not-world-readable usr/lib/*/fis-gtm/*/gtmsecshrdir/gtmsecshr 4500" >> debian/$(BINPKG).lintian-overrides
	echo "$(BINPKG): setuid-binary usr/lib/*/fis-gtm/*/gtmsecshr 4755 root/root" >> debian/$(BINPKG).lintian-overrides
	echo "$(BINPKG): non-standard-dir-perm usr/lib/*/fis-gtm/*/gtmsecshrdir/ 0500 != 0755" >> debian/$(BINPKG).lintian-overrides
	echo "$(BINPKG): package-contains-hardlink usr/lib/*/fis-gtm/*/gtmsecshr -> usr/lib/*/fis-gtm/*/utf8/gtmsecshr" >> debian/$(BINPKG).lintian-overrides
	echo "$(BINPKG): package-contains-hardlink usr/lib/*/fis-gtm/*/gtmsecshrdir/gtmsecshr -> usr/lib/*/fis-gtm/*/utf8/gtmsecshrdir/gtmsecshr" >> debian/$(BINPKG).lintian-overrides

	dh_lintian

override_dh_auto_clean:
	dh_auto_clean
	rm -rf debian/$(BINPKG)-stage1
	rm -f debian/fis-gtm.links debian/fis-gtm-$(UVER).lintian-overrides

# for some strange reason dh_strip does not seem to do a reliable job
# FIXME: there should be some more elegant solution like this
override_dh_strip:
	dh_strip
	if [ -d $(LOCAL_GTM_INSTALL_DIR) ] ; then \
	    cd $(LOCAL_GTM_INSTALL_DIR) ; \
	    strip dse gtcm_gnp_server gtcm_play gtcm_server gtcm_shmclean lke mupip ; \
	fi
	find $(LOCAL_GTM_INSTALL_DIR) -name maskpass -exec strip --remove-section=.comment \{\} \;

get-orig-source:
	mkdir -p ../tarballs
	uscan --verbose --force-download --destdir=../tarballs

