#!/usr/bin/make -f

non_default_python3_versions = $(shell python3 -c "print (\"`py3versions -v -s`\".replace(\"`py3versions -v -d`\", '').strip())")

export DH_VERBOSE=1

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

PY3:=$(shell py3versions -d)
CF:=$(shell dpkg-buildflags --get CFLAGS) 

ARCH_NOGOLANG:= hurd-i386 kfreebsd-amd64 kfreebsd-i386 sh4 ia64 alpha hppa m68k powerpc ppc64 x32
GOLANG:= $(if $(filter $(DEB_TARGET_ARCH), $(ARCH_NOGOLANG)),,--with-golang-path=/usr)

override_dh_auto_configure:
	# disable warning about unused results (necessary because warning is error
	# is ON)
	CFLAGS="$(CF) -I /usr/include/$(PY3) -DNDEBUG -Wno-unused-result" bash ./configure --with-globus-path=/usr \
		--prefix=/usr \
		$(GOLANG) \
		--with-krb5-path=/usr --with-readline-path=/usr \
		--with-mpi-path=/usr --with-fuse-path=/usr --with-zlib-path=/usr \
		--with-python3-path /usr/bin/python3 \
		--with-python2-path no \
		--without-system-parrot \
		--build-date "$(shell LC_ALL=C date --utc --date=@$${SOURCE_DATE_EPOCH})" 

override_dh_auto_test:
	# disable the tests for now on upstream's request. they will be reenabled
	# once the test suite can properly deal with the buildd environment

override_dh_auto_install:
	# copies needed for install to work
	cp work_queue/src/bindings/python3/work_queue_example.py work_queue/src/bindings/python3/work_queue.py
	cp chirp/src/bindings/python3/chirp_python_example.py chirp/src/bindings/python3/chirp.py
	dh_auto_install -- CCTOOLS_INSTALL_DIR=$(CURDIR)/debian/tmp 
	# make extensions for other supported python versions
	if [ -n "$(non_default_python3_versions)" ]; then \
		for pyver in $(non_default_python3_versions); do \
			CFLAGS="$(CF) -I /usr/include/python$${pyver} -DNDEBUG -Wno-unused-result" \
			bash ./configure --prefix --with-globus-path=/usr --with-krb5-path=/usr --with-readline-path=/usr \
				--with-mpi-path=/usr --with-fuse-path=/usr --with-zlib-path=/usr \
				--with-python3-path /usr/bin/python$${pyver} --with-python2-path no ; \
			make -B -C work_queue/src/bindings/python3 clean install CCTOOLS_INSTALL_DIR=$(CURDIR)/debian/tmp ; \
			make -B -C resource_monitor/src clean install CCTOOLS_INSTALL_DIR=$(CURDIR)/debian/tmp  ; \
			make -B -C chirp/src clean install CCTOOLS_INSTALL_DIR=$(CURDIR)/debian/tmp  ; \
		done ; \
	fi
	# for now remove the 'apps' scripts till upstream confirms that the language
	# extension can be removed
	rm -f debian/tmp/bin/*.py
	find debian -name .gitignore -delete
	# fix permissions
	find debian/tmp/lib -name '*.py' -exec chmod 644 {} \;
	# remove undesired pieces
	-rm debian/tmp/doc/COPYING
	# upstream says it should not go into the page
	-rm debian/tmp/bin/watchdog
	# HDFS helpers are useless without HDFS in Debian
	-rm debian/tmp/bin/*_hdfs
	# newer doxygen puts jquery copy
	#-rm debian/tmp/doc/api/html/jquery.js
	$(MAKE) doc
	for v in vine_example_gutenberg vine_example_blast vine_example_poncho vine_example_mosaic vine_example_watch ; do \
		rm  debian/tmp/doc/cctools/examples/taskvine/C/$$v ; done
	find debian/tmp/ -type f -size 0 -delete 


override_dh_auto_clean:
	[ -e Makefile.config ] && dh_auto_clean || true
	-rm -f Makefile.config configure.rerun
	-rm -rf api hdfs-setup.template python/python-workqueue/setup.cfg
	# remove testrun left-overs
	-rm dttools/src/microbench dttools/src/work_queue_example
	-find $(CURDIR) -name '*.test.log' -delete
	-find $(CURDIR) -name '*_test' -delete
	-find $(CURDIR) -name '*.[ao]' -delete
	-find $(CURDIR) -name '*.so' -delete
	find doc/man/md/ -name '*.md' -delete
	find doc/man/ -name '*.1' -delete
	find doc/man -name '*.html' -delete
	-rm -f work_queue/src/work_queue_example work_queue/src/work_queue_example_json work_queue/src/work_queue_status
	-rm -f work_queue/src/work_queue_test_watch resource_monitor/src/rmonitor_snapshot work_queue/src/work_queue_worker
	-rm -f work_queue/src/work_queue_server resource_monitor/src/resource_monitor resource_monitor/src/rmonitor_poll_example
	-rm -f batch_job/src/work_queue_factory resource_monitor/src/piggybacker makeflow/src/makeflow_mpi_submitter
	-rm -rf sand/test/filter_verification/random.cand.filter.tmp/
	-rm -rf sand/test/test_20.cand.filter.tmp/
	-rm -f sand/test/test_20.cand sand/test/test_20.cfa sand/test/*.log
	-rm -rf makeflow/test/input/ makeflow/test/worker.pid
	-rm -rf makeflow/test/dirs/testcase.subdir.01.makeflow.makeflowlog
	-rm -rf makeflow/test/dirs/testcase.subdir.01.makeflow.wqlog
	-rm -rf makeflow/test/src/
	-rm dttools/src/multirun sand/test/banded.log makeflow/src/makeflow makeflow/src/makeflow_viz batch_job/src/work_queue_pool
	-rm -f makeflow/src/makeflow_mpi_starter makeflow/src/makeflow_analyze grow/src/grow_fuse dttools/src/jx_repl
	-rm -f chirp/src/chirp dttools/src/catalog_update dttools/src/env_replace makeflow/src/makeflow_linker
	-rm -f makeflow/src/makeflow_status dttools/src/jx2env dttools/src/jx2json dttools/src/watchdog ftp_lite/src/ftp_lite_copy
	-rm -f chirp/src/chirp_benchmark deltadb/src/deltadb_upgrade_log dttools/src/disk_allocator deltadb/src/deltadb_query
	-rm -f chirp/src/chirp_distribute chirp/src/chirp_status dataswarm/worker/ds_worker deltadb/src/catalog_server dttools/src/catalog_query
	-rm -f dataswarm/manager/ds_manager chirp/src/chirp_stream_files chirp/src/confuga_adm chirp/src/chirp_fuse chirp/src/chirp_get
	-rm -f chirp/src/chirp_put chirp/src/chirp_server chirp/src/bindings/python3/Python.framework
	-rm -f work_queue/src/bindings/python3/Python.framework work_queue/src/sge_submit_workers
	for d in chirp/src/bindings/perl/CChirp.pm chirp/src/bindings/perl/chirp_wrap.c chirp/src/bindings/python3/CChirp.py \
 		chirp/src/bindings/python3/Chirp.py chirp/src/bindings/python3/chirp_wrap.c config.mk \
		doc/api/html/chirp_client_perl.html doc/api/html/chirp_stat_perl.html doc/api/html/work_queue_perl.html	\
 		doc/api/html/work_queue_task_perl.html resource_monitor/src/bindings/python3/resource_monitor.py \
		resource_monitor/src/bindings/python3/rmonitor_wrap.c resource_monitor/src/rmonitor_piggyback.h \
 		work_queue/src/bindings/perl/work_queue.pm work_queue/src/bindings/perl/work_queue_wrap.c \
		work_queue/src/bindings/python3/work_queue.py work_queue/src/bindings/python3/work_queue_wrap.c ; \
		do rm -f $$d ; done