#!/usr/bin/make -f

#export DH_VERBOSE=1

# This helps facilitate reproducibility in building
export LC_ALL=C.UTF-8

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
NOOPT = -no-opt
endif
# Pick up `-j' from xxx-buildpackage:
ifneq (,$(DEB_BUILD_OPTIONS))
PARALLEL = -parallel $(shell echo $(DEB_BUILD_OPTIONS) | sed -e 's/.*parallel=\([0-9]\+\).*/\1/')
endif
ifneq (,$(DEB_PARALLEL_JOBS))
PARALLEL = -parallel $(DEB_PARALLEL_JOBS)
endif

PRECMD=$(CURDIR)/debian/linux32-wrapper

export DEB_BUILD_HARDENING=1
# for qmon, sigh
export DEB_BUILD_HARDENING_FORMAT=0

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

export SGE_INPUT_CFLAGS = $(CPPFLAGS) $(CFLAGS)
export SGE_INPUT_LDFLAGS = $(LDFLAGS)

export LD_RUN_PATH=/usr/lib/gridengine

%:
	dh $@ --parallel --with autoreconf,autotools-dev

override_dh_auto_build: build-stamp
build-stamp: $(QUILT_STAMPFN)
	cd source && \
	  ${PRECMD} sh scripts/bootstrap.sh && \
	  ${PRECMD} \
	    ./aimk -with-jemalloc -DDEFAULT_EDITOR='\"sensible-editor\"' -no-remote -debug \
	      -DSGE_LD_PATH='\"/usr/lib/gridengine/\"' -DDEFAULT_EDITOR='\"sensible-editor\"' $(NOOPT) $(PARALLEL) && \
	  ${PRECMD} ./aimk -man && \
	  ${PRECMD} ant drmaa.javadoc juti.javadoc jgdi.javadoc jjsv.javadoc
	touch build-stamp

override_dh_clean:
# If just copying Debian debian over SGE distribution tarball
	cd debian && rm -rf sge.* sge-* README
	dh_clean

override_dh_auto_clean:
	rm -f build-stamp
# aimk clean doesn't DTRT
	cd source && rm -rf CLASSES JAVADOCS MANSBUILD_* dist/qmon/Qmon
	find source -type d -name "LINUX*" -print0 | xargs --null rm -rf
	find source -type d -name "build" -print0 | xargs --null rm -rf
	find source -name "*_dependencies" -print0 | xargs --null rm -f
	for t in common exec master client qmon; do \
	  cp debian/gridengine.templates debian/gridengine-$$t.templates; done
	debconf-updatepo
	rm -f debian/qmon.desktop

override_dh_auto_install:
	install -d debian/tmp/usr
	install -d debian/tmp/usr/share
	install -d debian/tmp/lib/$(DEB_HOST_GNU_TYPE)/security
	cd source && /usr/bin/yes | \
	  ${PRECMD} scripts/distinst -basedir ${CURDIR}/debian -vdir tmp/usr -allall -noexit
	cd debian/tmp/usr && \
	  mv man share && \
	  rm -rf hadoop dtrace ckpt util/sgeSMF start_gui_installer && \
	  rm -f util/resources/loadsensors/interix-loadsensor.sh && \
	  rm -f doc/arc_depend* doc/javadocs/index.html && \
	  rm -rf util/gui-installer && \
	  mv util/resources/drmaa4ruby .
# qmake clashes with qt
	cd debian/tmp/usr/bin/lx-* && mv qmake qmake_sge
# Rename some of the spool utils so we can replace them with wrapper scripts
# which set LD_LIBRARY_PATH for the spool libraries
	cd debian/tmp/usr/utilbin/lx* && \
	  mv spooledit spooledit.bin && mv spooldefaults spooldefaults.bin && \
	  install -m 755 $(CURDIR)/debian/spool.wrapper spooledit && \
	  install -m 755 $(CURDIR)/debian/spool.wrapper spooldefaults && \
	  mv $(CURDIR)/debian/tmp/usr/examples/jobsbin/lx*/work .
# Make manpage names sane
	cd debian/tmp/usr/share/man && \
	for k in hostnameutils submit; do \
	  mv man1/$$k.1 man1/sge_$$k.1; done && \
	mv man1/qmake.1 man1/qmake_sge.1 && \
	for k in access_list accounting bootstrap calendar_conf checkpoint \
	    complex host_aliases host_conf hostgroup project qtask queue_conf \
	    reporting sched_conf share_tree user; do \
	  mv man5/$$k.5 man5/sge_$$k.5;	done
# Remove manpage symlinks in favour of mandb
	find debian/tmp/usr/share/man -type l | xargs rm -f
	mandb -u -c debian/tmp/usr/share/man
	chmod 755 debian/scripts/init_cluster debian/gridengine-wrapper
	cp debian/bootstrap debian/tmp/default-bootstrap
	cp debian/configuration debian/tmp/default-configuration
	cp source/clients/qmon/qmon.desktop debian
	mv debian/tmp/usr/lib/*/pam_sge* debian/tmp/lib/$(DEB_HOST_GNU_TYPE)/security

override_dh_installinit:
	dh_installinit -p gridengine-master
# Start the execd after the master
	dh_installinit -p gridengine-exec -- defaults 21

override_dh_fixperms:
	dh_fixperms
# not sure why this gets made executable, causing lintian whinges
	if [ -d debian/gridengine-common ]; then \
		chmod -x \
		 debian/gridengine-common/usr/share/gridengine/util/dl.sh \
		 debian/gridengine-common/usr/share/gridengine/util/resources/scripts/README*; \
	fi

override_dh_installchangelogs:
	dh_installchangelogs --keep NEWS
