#!/usr/bin/make -f
# © 2025 Friedhelm Mehnert <friedhelm@friedhelms.net>

# This hack saves compile time by moving big parts of the source
# out of the way!
# Set this to false to compile everything.
fast_build := true

export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-format
export DEB_LDFLAGS_MAINT_APPEND = -g
export DEB_CFLAGS_MAINT_APPEND = -std=gnu89 -Wno-incompatible-pointer-types \
       -Wno-unused-result -Wno-deprecated-declarations

env_cflags := "$(shell dpkg-buildflags --get CFLAGS)"
env_cppflags := "$(shell dpkg-buildflags --get CPPFLAGS)"
env_ldflags := "$(shell dpkg-buildflags --get LDFLAGS)"

%:
	dh $@

# Configure is handled by "upstream make". So we do nothing.
# We also don't care if parts of the source are missing.
override_dh_auto_configure override_dh_missing:


# There is no "make clean". A hidden ".clean" script is used instead.
override_dh_auto_clean:
	./.clean
	if test -d ./debian/backup/cal; then mv ./debian/backup/* .; fi

# We don't need the profiled versions of the libs. We save A LOT of
# compile time by not building them. We simply remove their make files.
override_dh_auto_build:
	find . -name '*_p.mk' -delete
	mkdir -p ./debian/backup
ifeq ($(fast_build), true)
	mv ./bsh ./debian/backup
	mv ./btcflash ./debian/backup
	mv ./cal ./debian/backup
	mv ./calc ./debian/backup
	mv ./calltree ./debian/backup
	mv ./change ./debian/backup
	mv ./compare ./debian/backup
	mv ./copy ./debian/backup
	mv ./count ./debian/backup
	mv ./cut ./debian/backup
	mv ./hdump ./debian/backup
	mv ./label ./debian/backup
	mv ./match ./debian/backup
	mv ./mdigest ./debian/backup
	mv ./mt ./debian/backup
	mv ./pbosh ./debian/backup
	mv ./obosh ./debian/backup
	mv ./p ./debian/backup
	mv ./patch ./debian/backup
	mv ./paste ./debian/backup
	mv ./printf ./debian/backup
	mv ./pxupgrade ./debian/backup
	mv ./rmt ./debian/backup
	mv ./sccs ./debian/backup
	mv ./sdd ./debian/backup
	mv ./scgcheck ./debian/backup
	mv ./scgskeleton ./debian/backup
	mv ./sgrow ./debian/backup
	mv ./sh ./debian/backup
	mv ./sfind ./debian/backup
	mv ./sformat ./debian/backup
	mv ./sunpro ./debian/backup
	mv ./star_sym ./debian/backup
	mv ./strar ./debian/backup
	mv ./tartest ./debian/backup
	mv ./translit ./debian/backup
	mv ./tests ./debian/backup
	mv ./termcap ./debian/backup
	mv ./udiff ./debian/backup
	mv ./ved ./debian/backup
	mv ./vms ./debian/backup
	mv ./libboshcmd ./debian/backup
	mv ./libdbgmalloc ./debian/backup
	mv ./libshedit ./debian/backup
	mv ./libvms ./debian/backup
endif
	make GMAKE_NOWARN=true CPPOPTX=$(env_cppflags) COPTX=$(env_cflags) LDOPTX=$(env_ldflags) ARCH=$(DEB_HOST_ARCH) -f SMakefile

override_dh_auto_install:
	make install GMAKE_NOWARN=true DESTDIR=$(CURDIR)/debian/tmp ARCH=$(DEB_HOST_ARCH) -f SMakefile
ifeq ($(fast_build), true)
	mv ./debian/backup/* .
endif

override_dh_installman:
	mv ./debian/tmp/usr/share/man/man1/rscsi.1 ./debian/tmp/usr/share/man/man8/rscsi.8
	mv ./debian/tmp/usr/share/man/man8/mkisofs.8 ./debian/tmp/usr/share/man/man1/mkisofs.1
	mv ./debian/tmp/usr/share/man/man8/mkhybrid.8 ./debian/tmp/usr/share/man/man1/mkhybrid.1
	dh_installman

override_dh_strip:
	dh_strip --no-automatic-dbgsym

