#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

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

# if this package is built in an i386 build environment on an amd64 host,
# a toolchain file is required so cmake uses the right CMAKE_SYSTEM_PROCESSOR
CMAKE_SYSTEM_PROCESSOR = $(shell  cmake --system-information | grep CMAKE_SYSTEM_PROCESSOR | cut -d'"' -f2)
DEB_TARGET_ARCH = $(shell dpkg --print-architecture)
export CMAKE_TOOLCHAIN := ""
ifeq ($(DEB_TARGET_ARCH), i386)
ifeq ($(CMAKE_SYSTEM_PROCESSOR), x86_64)
    export CMAKE_TOOLCHAIN := "-DCMAKE_TOOLCHAIN_FILE=$(CURDIR)/debian/i386-toolchain.cmake"
endif
endif

%:
	dh $@ --with python3


override_dh_auto_configure:
	dh_auto_configure -- \
		$(CMAKE_TOOLCHAIN) \
		-DPACKAGEID:STRING="$(debver)"\
		-DBUILD_TESTING:BOOL=ON \
		-DCMAKE_SKIP_RPATH:BOOL=ON \
		-DCONDOR_PACKAGE_BUILD:BOOL=ON \
		-DCMAKE_INSTALL_PREFIX:PATH=/


override_dh_auto_build: docs/build
	dh_auto_build --parallel
	# 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/ -l$(CURDIR)/debian/tmp/usr/lib/condor --dpkg-shlibdeps-params=--ignore-missing-info


docs/build:
	mkdir -p $@
	$(MAKE) -C docs man
	mkdir -p $@/man
	mv docs/_build/man/* $@/man


override_dh_auto_install:
	dh_auto_install
	rm -rf debian/tmp/etc/rc.d
	# Remove tarball files that we do not use
	rm -f debian/tmp/usr/bin/make-personal-from-tarball
	rm -f debian/tmp/usr/sbin/condor_configure
	rm -f debian/tmp/usr/sbin/condor_install
	# create blahp files
	mkdir --mode 0755 debian/tmp/etc/blahp
	for batch_system in condor kubernetes lsf nqs pbs sge slurm; do \
		mv debian/tmp/usr/libexec/blahp/$${batch_system}_local_submit_attributes.sh debian/tmp/etc/blahp/; \
		ln -s ../../../etc/blahp/$${batch_system}_local_submit_attributes.sh \
			debian/tmp/usr/libexec/blahp/$${batch_system}_local_submit_attributes.sh; \
	done
	# Remove extranenous files
	rm debian/tmp/usr/share/doc/condor/LICENSE
	rm debian/tmp/usr/share/doc/condor/README
	# This isn't ready for release yet.
	rm -fr debian/tmp/usr/lib/python3/dist-packages/classad3


override_dh_fixperms-arch:
	dh_fixperms
	chmod a-x debian/condor/usr/libexec/condor/interactive.sub
	chmod a-x debian/condor/usr/libexec/condor/ce-audit.so
	chmod a-x debian/condor/usr/libexec/condor/libclassad_python_user.*.so


override_dh_install-arch:
	dh_install
	# fix permissions
	# remove duplicate that is also installed as a config file
	rm -f debian/condor/usr/libexec/condor/linux-kernel-tuning
	# move etc/bash_completion.d/ to usr/share/bash-completion/completions
	mkdir -p debian/condor/usr/share/bash-completion/completions
	mv debian/condor/etc/bash_completion.d/* debian/condor/usr/share/bash-completion/completions
	rmdir debian/condor/etc/bash_completion.d


override_dh_auto_clean:
	dh_auto_clean
	# clean up our own mess
	find $(CURDIR) -name '*.class' -delete -o -name '*.jar' -delete
	find . -name '*.pyc' -delete
	# clean leftovers of upstream clean run
	rm -f src/condor_tests/list*
	rm -f src/condor_examples/condor.boot.debian \
		src/condor_examples/condor_config.patched
	rm -f 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 \
		src/condor_tests/condor_credmon_oauth_dummy
	rm -f src/condor_utils/param_info.c
	# docs
	$(MAKE) -C docs clean
	rm -rf docs/just-man-pages
	rm -rf docs/_build


override_dh_installdocs:
	dh_installdocs
	for html in `find debian -name "*.html"` ; do \
	    sed -i \
		-e 's#https://maxcdn.bootstrapcdn.com/bootstrap/[^/]*/css/bootstrap.min.css#file:///usr/share/javascript/bootstrap/css/bootstrap.min.css#' \
		-e 's#https://maxcdn.bootstrapcdn.com/bootstrap/.*/js/bootstrap.min.js#file:///usr/share/javascript/bootstrap/js/bootstrap.min.js#' \
		-e 's#[htps:]*//code.jquery.com/jquery-[^/]*\.min\.js#file:///usr/share/javascript/jquery/jquery.min.js#' \
		$${html} ; \
	done

# 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 --dbgsym-migration='condor-dbg (<< 9.3.1~dfsg-1~)'

override_dh_auto_test:

override_dh_gencontrol:
	dh_gencontrol -- \
		-Vlib:Depends="$(foreach package, libscitokens libssl, \
			$(shell dpkg-query -W -f '$${Depends}' $(package)-dev \
				| sed -E 's/.*($(package)[[:alnum:].-]+).*/\1,/' )) \
			$(shell dpkg-query -W -f '$${Depends}' voms-dev \
				| sed -E 's/.*(libvomsapi[[:alnum:].-]+).*/\1,/' )"
