#!/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*' $$(git merge-base -a HEAD upstream) | sed -e 's/^V//' -e 's/_/./g' -e 's/-/+git/')

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


override_dh_auto_configure:
	dh_auto_configure -- \
		-DBUILD_SHARED_LIBS:BOOL=ON \
		-DPROPER:BOOL=ON \
		-DCLIPPED:BOOL=ON \
		-DUW_BUILD:BOOL=OFF \
		-D_DEBUG:BOOL=TRUE \
		-DBUILDID:STRING="Debian [$(debver)]"\
		-DWITH_GSOAP:BOOL=OFF \
		-DCMAKE_SKIP_BUILD_RPATH:BOOL=ON \
		-DCMAKE_INSTALL_RPATH:STRING="/usr/lib/condor" \
		-DHAVE_EXT_CLASSADS:BOLL=ON -DCLASSADS_FOUND=classad_ns \
		-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 \
		-DHAVE_EXT_ZLIB:BOOL=ON
		# 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/


doc/build:
	mkdir -p $@
	$(MAKE) -C doc/doc release-pdf MANDIR=$(CURDIR)/$@/pdf \
		TARGETDIR=$(CURDIR)/$@/pdf
	cd doc/doc && latex2html -address condor-admin@cs.wisc.edu -split 4 \
		-link 2 -show_section_numbers -antialias -tmp /tmp -long_titles 3 \
		-toc_depth 2 -local_icons ref.tex
	cp -r doc/doc/ref $@/html
	$(MAKE) -C doc/doc/makeman makeman
	for html in $@/html/condor_*.html; do \
		doc/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/^\s//' < $$man > $@/man/$$(basename $$man) ; \
	done
	rm $@/html/condor_*.1


override_dh_auto_install:
	dh_auto_install
	# fix various bits -- upstream should do that
	chmod -x debian/tmp/usr/etc/sysconfig/condor
	chmod -x debian/tmp/usr/lib/*.pm
	# things we do not need in Debian
	rm debian/tmp/usr/sbin/condor_install* \
		debian/tmp/usr/sbin/condor_cleanup* \
		debian/tmp/usr/sbin/install_release \
		debian/tmp/usr/sbin/cleanup_release
	# place condor config file with default Debian config
	sed -e 's,^\(CONDOR_HOST\s*=\).*,\1 \$$(FULL_HOSTNAME),' \
		-e 's,^\(RELEASE_DIR\s*=\).*,\1 /usr,' \
		-e 's,^\(MAIL\s*=\).*,\1 /usr/bin/mail,' \
		-e 's,^\(LOCAL_DIR\s*=\).*,\1 /var,' \
		-e 's,^\(LOCAL_CONFIG_FILE\s*=\).*,\1 /etc/condor/condor_config.local,' \
		-e 's,^\(LOCAL_CONFIG_DIR\s*=\).*,\1 /etc/condor/config.d,' \
		-e 's,^[#]*\(REQUIRE_LOCAL_CONFIG_FILE\s*=\).*,\1 FALSE,'  \
		-e 's,^[#]*\(CONDOR_DEVELOPERS\s*=\).*,\1 NONE,'  \
		-e 's,^[#]*\(CONDOR_DEVELOPERS_COLLECTOR\s*=\).*,\1 NONE,'  \
		-e 's,^\(CONDOR_ADMIN\s*=\).*,\1 root@localhost,' \
		-e 's,^\(UID_DOMAIN\s*=\).*,\1 \$$(FULL_HOSTNAME),' \
		-e 's,^\(FILESYSTEM_DOMAIN\s*=\).*,\1 \$$(FULL_HOSTNAME),' \
		-e 's,^\(COLLECTOR_NAME\s*=\).*,\1 Debian Condor Pool,' \
		-e 's,^\(ALLOW_WRITE\s*=\).*,\1 \$$(FULL_HOSTNAME),' \
		-e 's/^\(DAEMON_LIST\s*=\).*/\1 MASTER, STARTD, SCHEDD, COLLECTOR, NEGOTIATOR/' \
		-e 's,^\(LOCK\s*=\).*,\1 \$$(LOCAL_DIR)/lock/condor,' \
		-e 's,^\(LOG\s*=\).*,\1 \$$(LOCAL_DIR)/log/condor,' \
		-e 's,^\(SPOOL\s*=\).*,\1 \$$(LOCAL_DIR)/spool/condor,' \
		-e 's,^\(EXECUTE\s*=\).*,\1 \$$(LOCAL_DIR)/lib/condor/execute,' \
		-e '/^EXECUTE\s*=/a\LOCAL_UNIV_EXECUTE\t\t= \$$(LOCAL_DIR)/spool/condor/local_univ_execute' \
		-e 's,^\(CRED_STORE_DIR\s*=\).*,\1 \$$(LOCAL_DIR)/lib/condor/cred_dir,' \
		-e 's,^\(LIB\s*=\).*,\1 \$$(RELEASE_DIR)/lib/condor,' \
		-e 's,^\(INCLUDE\s*=\).*,\1 \$$(RELEASE_DIR)/include/condor,' \
		-e 's,^\(LIBEXEC\s*=\).*,\1 \$$(RELEASE_DIR)/lib/condor/libexec,' \
		-e 's,^\(SHARE\s*=\).*,\1 \$$(RELEASE_DIR)/share/condor,' \
		-e '/^LOG\s*=/a\RUN\t\t= \$$(LOCAL_DIR)/run/condor' \
		-e 's,^\(PROCD_ADDRESS\s*=\).*,\1 \$$(RUN)/procd_pipe,' \
		< debian/tmp/usr/etc/examples/condor_config \
		> debian/condor/etc/condor/condor_config
	# and now Debian's init script
	sed -e 's,/etc/sysconfig/condor,/etc/default/condor,g' \
		< debian/tmp/usr/etc/init.d/condor > debian/condor/etc/init.d/condor


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/doc reallyclean
	-rm -rf doc/doc/just-man-pages
	-rm -rf doc/build
	-rm -f doc/doc/symbol.tex doc/doc/figuresizing.tex doc/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 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
	git archive --format=tar --prefix=$(srcpkg)-doc-$(gitver)/ \
				--remote=doc/ HEAD | \
		gzip -9 > $(srcpkg)_$(gitver).orig-doc.tar.gz
