#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

VERSION = $(shell echo $(DEB_VERSION_UPSTREAM) | sed 's|+ds||')

%:
	dh $@

execute_before_dh_clean:
	rm -f $(MANS)

override_dh_auto_configure:
	dh_auto_configure -- --enable-qsoptex --enable-shared

# prepend topcom- to all binaries
execute_after_dh_auto_install:
	cd debian/tmp/usr/bin && for f in *; do mv "$$f" "topcom-$$f"; done

execute_before_dh_installdocs:
	iconv -f ISO-8859-1 -t UTF-8 $(CURDIR)/README -o $(CURDIR)/README

execute_before_dh_installman: debian/topcom.7

debian/topcom.7: debian/topcom.txt
	txt2man -t TOPCOM -s 7 -r $(VERSION) $< > $@

# topcom.txt (the txt2man source) is generated from the "Commands" and
# "Command Line Options" sections of the upstream manual, so the manual
# stays the single source of truth.  Runs on the patched manual.
debian/topcom.txt: doc/TOPCOM-manual.tex debian/manual2txt.pl
	perl debian/manual2txt.pl $< > $@

MAN1DIR = usr/share/man/man1
TOPCOM7 = usr/share/man/man7/topcom.7
override_dh_link:
	$(eval TOPCOM_COMMANDS=$(notdir \
		$(wildcard debian/topcom/usr/bin/topcom-*)))
	dh_link $(patsubst %, $(TOPCOM7) $(MAN1DIR)/%.1, $(TOPCOM_COMMANDS))
