#!/usr/bin/make -f

# Export DH_OPTIONS to save options for binary-common
export DH_OPTIONS

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0 -D_REENTRANT
else
	CFLAGS += -O2 -D_REENTRANT
endif

CONFIGURATION_OPTIONS = --with-enhanced-ligand-tools --disable-static --with-gemmi=/usr/lib/$(DEB_HOST_MULTIARCH) --with-libdw
ifneq ($(DEB_HOST_ARCH),hurd-i386)
	CONFIGURATION_OPTIONS += --with-sound
endif

# Link with libatomic on riscv64, mips64el and loong64
ifneq (,$(filter $(DEB_HOST_ARCH),riscv64 mips64el loong64))
	export DEB_LDFLAGS_MAINT_APPEND += -Wl,--no-as-needed -latomic -Wl,--as-needed
endif

override_dh_auto_configure:
	dh_auto_configure --buildsystem autoconf -- $(CONFIGURATION_OPTIONS)
	dh_auto_configure --buildsystem cmake

execute_after_dh_auto_build:
	dh_auto_build --buildsystem cmake

override_dh_auto_test:

override_dh_auto_install:
	dh_auto_install
	dh_auto_install --buildsystem cmake

	dh_auto_install --sourcedirectory data
	dh_auto_install --sourcedirectory rama-data
	dh_auto_install --sourcedirectory python
	dh_auto_install --sourcedirectory pyrogen

	# Drop the remaining *.la files
	find debian/tmp -name '*.la' | xargs --no-run-if-empty rm

	# Adjust the LD_LIBRARY_PATH setting
	sed -i 's/$${exec_prefix}\/libexec\/coot-1/LD_LIBRARY_PATH=$${prefix}\/lib\/'$(DEB_HOST_MULTIARCH)'\/coot $${exec_prefix}\/libexec\/coot-1/' \
	    debian/tmp/usr/bin/coot
	sed -i 's/LD_LIBRARY_PATH=$$COOT_PREFIX\/lib/LD_LIBRARY_PATH=$$COOT_PREFIX\/lib\/'$(DEB_HOST_MULTIARCH)'\/coot/g' \
	    debian/tmp/usr/bin/*
	sed -i 's/LD_LIBRARY_PATH=$$prfx\/lib/LD_LIBRARY_PATH=\/usr\/lib\/'$(DEB_HOST_MULTIARCH)'\/coot/g' \
	    debian/tmp/usr/bin/pyrogen

	# Wrapper for layla (#1072618)
	mkdir --parents debian/tmp/usr/libexec
	mv debian/tmp/usr/bin/layla debian/tmp/usr/libexec
	echo '#!/bin/sh' > debian/tmp/usr/bin/layla
	echo LD_LIBRARY_PATH=/usr/lib/$(DEB_HOST_MULTIARCH)/coot /usr/libexec/layla >> debian/tmp/usr/bin/layla
	chmod +x debian/tmp/usr/bin/layla

	# decode image and convert to smaller sizes
	# TODO: let us reuse pixmaps/coot-icon.png
	uudecode < debian/coot-button-64x64.png.uu
	for g in 22x22 24x24 32x32 48x48; do \
	  convert -geometry $$g coot-button-64x64.png coot-button-$$g.png;\
	done

	# install the newly generated png's in the hicolor theme
	for g in 22x22 24x24 32x32 48x48 64x64; do \
	   install -D coot-button-$$g.png debian/tmp/usr/share/icons/hicolor/$$g/apps/coot.png;\
	done

execute_after_dh_install-arch:
	rm debian/coot/usr/lib/$(DEB_HOST_MULTIARCH)/coot/libcootapi.so*

override_dh_install-indep:
	dh_install -i --exclude=scheme

override_dh_installdocs:
	dh_installdocs RELEASE-NOTES # debian/citation.bib -- not available (?)

override_dh_shlibdeps:
	dh_shlibdeps -l/usr/lib/$(DEB_HOST_MULTIARCH)/coot

execute_after_dh_auto_clean:
	dh_auto_clean --buildsystem cmake

debian/copyright.hints:
	mkdir debian/licensecheck
	rsync -a --exclude .git --exclude debian --exclude '*.jpg' --exclude '*.png' --exclude '*.ogg' . debian/licensecheck/
	sed -i 's/;;;;/ */g' debian/licensecheck/scheme/* # Fixup scheme comments which are not understood by licensecheck
	(cd debian/licensecheck && licensecheck -r . --deb-machine --lines 100) > $@
	rm -rf debian/licensecheck

%:
	dh $@