#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

%:
	dh $@

override_dh_auto_build:
	dh_auto_build
	docbook-to-man debian/checkinstall.sgml > debian/checkinstall.8
	docbook-to-man debian/installwatch.sgml > debian/installwatch.1

override_dh_auto_clean:
	$(MAKE) clean
	rm -f debian/checkinstall.8
	rm -f debian/installwatch.1

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
TESTDIR=$(CURDIR)/debian/testtmp
override_dh_auto_test:
		$(MAKE) -C installwatch test PREFIX=$(TESTDIR)
		rm -rf $(TESTDIR)
endif

TMPDIR=$(CURDIR)/debian/tmp
override_dh_auto_install:
	# We override all paths: we want the binary in bin instead of
	# sbin, the configuration file in etc, the locales in the
	# standard directory, and the installwatch library in a subdir
	# of usr/lib
	$(MAKE) install PREFIX=$(TMPDIR)/usr LCDIR=$(TMPDIR)/usr/share/locale \
		CONFDIR=$(TMPDIR)/etc BINDIR=$(TMPDIR)/usr/bin \
		LIBDIR=$(TMPDIR)/usr/lib/checkinstall
	# Delete the checkinstallrc-dist file from etc
	rm -f $(TMPDIR)/etc/checkinstallrc-dist

	# installwatch's documentation
	mkdir -p $(TMPDIR)/usr/share/doc/checkinstall
	gzip -9nc installwatch/CHANGELOG \
		> $(TMPDIR)/usr/share/doc/checkinstall/installwatch.changelog.gz
	gzip -9nc debian/changelog.installwatch.old \
		> $(TMPDIR)/usr/share/doc/checkinstall/installwatch.changelog.Debian.gz
	cp installwatch/BUGS \
		$(TMPDIR)/usr/share/doc/checkinstall/BUGS.installwatch
	cp installwatch/README \
		$(TMPDIR)/usr/share/doc/checkinstall/README.installwatch
	cp installwatch/TODO \
		$(TMPDIR)/usr/share/doc/checkinstall/TODO.installwatch

	# Enable installwatch to find the shared library
	sed -i -e 's|$(TMPDIR)||' $(TMPDIR)/usr/bin/installwatch
	sed -i -e 's|PREFIX/lib|PREFIX/lib/checkinstall|' \
		$(TMPDIR)/usr/bin/installwatch

	# Remove absolute buildpath
	sed -i -e 's|^INSTALLDIR=.*|INSTALLDIR=/usr|' $(TMPDIR)/usr/bin/checkinstall

	# Remove unwanted makepak binary
	rm -f $(TMPDIR)/usr/bin/makepak

override_dh_installman:
	dh_installman debian/installwatch.1 debian/checkinstall.8
