#!/usr/bin/make -f

srcpkg = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Source:' | cut -d ' ' -f 2,2)
debver = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Version:' | cut -d ' ' -f 2,2 )
upstreamver = $(shell echo $(debver) | cut -d '-' -f 1,1 )

# this figures out the last merge point from 'master' into the Debian branch and
# then described this commit relative to the last release tag (V...)
# If this should make any sense the local upstream branch must track upstream's
# master or whatever other source branch.
gitver = $(shell [ -x /usr/bin/git ] && git describe --tags --match 'V[0-9]_[0-9]_[0-9]' $$(git merge-base -a HEAD upstream) | sed -e 's/^V//' -e 's/_/./g' -e 's/-/+git/')~dfsg.1

# one ring to rule them all ...
%:
	dh $@


override_dh_auto_configure:
	dh_auto_configure -- \
		-DCONDOR_PACKAGE_BUILD:BOOL=ON \
		-DPROPER:BOOL=ON \
		-DCLIPPED:BOOL=ON \
		-DUW_BUILD:BOOL=OFF \
		-D_DEBUG:BOOL=TRUE \
		-DBUILDID:STRING="Debian-$(debver)"\
		-DFORCE_PRE_RELEASE:STRING="Debian-$(debver)"\
		-DCMAKE_INSTALL_RPATH:STRING="/usr/lib/condor" \
		-DHAVE_EXT_GSOAP:BOOL=ON \
		-DHAVE_EXT_CURL:BOOL=ON \
		-DHAVE_EXT_OPENSSL:BOOL=ON \
		-DHAVE_EXT_BOOST:BOOL=ON \
		-DHAVE_EXT_GLOBUS:BOOL=ON \
		-DHAVE_EXT_KRB5:BOOL=ON \
		-DHAVE_EXT_LIBVIRT:BOOL=ON \
		-DHAVE_EXT_LIBXML2:BOOL=ON \
		-DHAVE_EXT_OPENSSL:BOOL=ON \
		-DHAVE_EXT_PCRE:BOOL=ON \
		-DWITH_LIBCGROUP:BOOL=OFF
		# upstream says: tests aren't reliablei enough yet
		#-DBUILD_TESTS:BOOL=OFF \
		# upstream says: contrib is to unstable for initial packaging
		#-DWANT_CONTRIB:BOOL=OFF \
		#-DHAVE_EXT_POSTGRESQL:BOOL=OFF \
		#-DEXTERNAL_INCLUDES:STRING="/usr/include/postgresql"


override_dh_auto_build: doc/build
	dh_auto_build -- VERBOSE=1
	# post-fixing things that have to be done due to changes introduced by
	# repackaging
	# 1. rebuild java stuff
	#cd src/condor_starter.V6.1 && javac *.java
	#cp src/condor_chirp/*.java src/condor_chirp/chirp/java/client/
	#$(MAKE) -C src/condor_chirp/chirp/java/client/
	#cp src/condor_chirp/chirp/java/client/Chirp.jar src/condor_chirp/


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


doc/build:
	mkdir -p $@
	$(MAKE) -C doc release-html MANDIR=$(CURDIR)/$@/html
		TARGETDIR=$(CURDIR)/$@/html
	# remove archives
	-rm $@/html/*.Z $@/html/*.gz
	$(MAKE) -C doc/makeman makeman
	for html in $@/html/condor_*.html; do \
		doc/makeman/makeman -v -i $$html -s 1; \
	done
	mkdir -p $@/man
	# sanitize and copy
	for man in $@/html/condor_*.1; do \
		sed -r -e 's,$@/html/,,' -e "s/1 date$$/1 \"$$(date +'%B %Y')\"/" \
			   -e '/\.SH Name/{ n; n; s/\W/ \\- /; }' -e 's/ (\.[A-Z])/\1/' \
			   -e "s/($$(echo $$(basename $$man) | cut -d . -f1,1))/\1 /g" \
			   -e '/\\$$/{ n; s/\.br//; }' -e 's,/SPAN>, ,g' \
			   -e '/[ ]*\.P/{ n; s/^[ ]*\.P//; }' \
			   -e 's/^[ ]*\././' \
			   -e 's/^\s//' < $$man > $@/man/$$(basename $$man) ; \
	done
	rm $@/html/condor_*.1
	# last manpage for help2man compatibles
	help2man -N -n "minimalistic qsub emulation for Condor" debian/condor_qsub > $@/man/condor_qsub.1


override_dh_auto_install:
	dh_auto_install
	# things we do not need in Debian
	rm debian/tmp/usr/sbin/condor_install*
	# remove compat symlinks
	rm debian/tmp/usr/sbin/condor_vm_vmware.pl
	rm debian/tmp/usr/sbin/grid_monitor.sh


override_dh_install:
	dh_install
	# fix permissions
	chmod -x debian/condor/usr/lib/condor/*.pm
	chmod -x debian/condor/etc/default/condor
	# remove RPATH from public lib
	chrpath -d debian/libclassad*/usr/lib/libclassad.so.*.*
	# kill the default local config -- debconf will handle that
	rm debian/condor/etc/condor/condor_config.local
	# modify condor config file with default Debian config
	sed -i \
		-e 's,^[#]*\(REQUIRE_LOCAL_CONFIG_FILE\s*=\).*,\1 FALSE,'  \
		-e 's,^\(CONDOR_ADMIN\s*=\).*,\1 root@localhost,' \
		-e 's,^\(MAIL\s*=\).*,\1 /usr/bin/mail,' \
		-e 's,^\(COLLECTOR_NAME\s*=\).*,\1 Debian Condor Pool - $$(CONDOR_HOST),' \
		-e 's,^\(SPOOL\s*=\).*,\1 \$$(LOCAL_DIR)/spool/condor,' \
		-e '/^EXECUTE\s*=/a\LOCAL_UNIV_EXECUTE\t\t= \$$(LOCAL_DIR)/spool/condor/local_univ_execute' \
		-e 's/^\(DAEMON_LIST\s*=\).*/\1 MASTER, STARTD, SCHEDD, COLLECTOR, NEGOTIATOR/' \
		-e 's,^[#]*\(CONDOR_DEVELOPERS\s*=\).*,\1 NONE,'  \
		-e 's,^[#]*\(CONDOR_DEVELOPERS_COLLECTOR\s*=\).*,\1 NONE,'  \
		-e 's,^\(CRED_STORE_DIR\s*=\).*,\1 \$$(LOCAL_DIR)/lib/condor/cred_dir,' \
		-e 's,^[#]*\(SSH_TO_JOB_SSHD_CONFIG_TEMPLATE\s*=\).*,\1 /etc/condor/condor_ssh_to_job_sshd_config_template,'  \
		-e 's,^\(JAVA_BENCHMARK_TIME\s*=\).*,\1 0,' \
		debian/condor/etc/condor/condor_config


override_dh_auto_clean:
	dh_auto_clean
	# clean up our own mess
	-find $(CURDIR) -name '*.class' -delete -o -name '*.jar' -delete
	-rm src/condor_chirp/chirp/java/client/*.java
	# clean leftovers of upstream clean run
	-rm src/condor_tests/list*
	-rm src/condor_examples/condor.boot.debian \
		src/condor_examples/condor_config.patched
	-rm src/condor_tests/Condor.pm src/condor_tests/CondorPersonal.pm \
		src/condor_tests/CondorTest.pm src/condor_tests/CondorUtils.pm \
		src/condor_tests/batch_test.pl
	-rm src/condor_utils/param_info.c
	# docs
	$(MAKE) -C doc reallyclean
	-rm -f doc/condor-*-Manual.tar
	-rm -rf doc/just-man-pages
	-rm -rf doc/build
	-rm -f doc/symbol.tex doc/figuresizing.tex doc/fontsize.tex


override_dh_clean:
	dh_clean
	debconf-updatepo


# PDF come in dedicated doc package -- no compression
override_dh_compress:
	dh_compress -X.pdf


override_dh_installinit:
	dh_installinit -pcondor --onlyscripts --noscripts


override_dh_strip:
	dh_strip --dbg-package=condor-dbg


dfsg-source-tree:
	-quilt pop -a
	@echo "Testing for uncommited changes"
	@git diff --quiet HEAD
# remove windows binaries that come without sources
	rm -rf msconfig
# remove embedded tarballs with 3rd-party software that is
# currently not used in any way
	find . -name '*.tar.gz' -delete
	find . -name '*.tgz' -delete
# remove all java binaries
# and other stuff that will be deleted during clean runs anyway
	find $(CURDIR) -regextype posix-egrep -regex '.*(\.(jar|class|pdf)|TAGS)' -print0 \
		| xargs -0 -i git rm -f --ignore-unmatch -- {} || true
# commit any cleanup results
	@if ! git diff --quiet HEAD; then \
		git commit -e -a -m "Remove unwanted (e.g. non-DFSG-compliant) content"; \
	fi


# make orig tarball from repository content
get-orig-source: dfsg-source-tree
	# orig tarball, turn directory into something nicer
	git archive --format=tar --prefix=$(srcpkg)-$(gitver)/ HEAD | \
		gzip -9 > $(srcpkg)_$(gitver).orig.tar.gz
