#!/usr/bin/make -f

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

# This serves to change compilers easily
#  It should be set to 'cc' by default. Alternatives can be 'gcc-9', 'gcc-10'
#  or others, depending on the build environment in use.
# Does not work with imake.
#export CC=cc

# Make sure lintian does not complain about missing hardenings
# Does not work with imake.
#export DEB_BUILD_MAINT_OPTIONS=hardening=+all

%:
	dh $@

override_dh_auto_clean:
	# Generate Makefiles by imake and execute their clean targets.
	xmkmf
	cd Xvnc && ./configure
	dh_auto_clean
	dh_auto_clean --sourcedirectory=Xvnc --no-parallel
	# Remove directory not cleaned natively.
	-rmdir Xvnc/exports
	# Remove Makefiles generated by imake.
	[ '$(DH_VERBOSE)' != '1' ] || find -name 'Makefile' -ls
	find -name 'Makefile' | xargs rm
	# Restore Xvnc/Makefile contained in source.
	tar -xzf ../tightvnc_1.3.10.orig.tar.gz \
		--no-same-owner \
		--strip-components=1 \
		vnc_unixsrc/Xvnc/Makefile
	# Remove file generated by Xvnc/configure.
	-rm Xvnc/config/cf/platform.def
	# Remove files generated by override_dh_auto_build.
	-rm debian/faq.txt debian/start.txt

override_dh_auto_configure:
	# Generate Makefiles by imake.
	# This call relies on the imake configuration installed with xutils-dev.
	xmkmf
	# This call uses its own imake configuration located in Xvnc/config/cf.
	cd Xvnc && ./configure

override_dh_auto_build:
	# Build everything but Xvnc.
	dh_auto_build -- World
	# Build Xvnc.
	dh_auto_build --sourcedirectory=Xvnc --no-parallel -- World
	# Generate text files from html sources.
	lynx -dump debian/faq.html   | sed 's,file://.*/,,' > debian/faq.txt
	lynx -dump debian/start.html | sed 's,file://.*/,,' > debian/start.txt

override_dh_auto_install:
# tightvncpasswd
	install -m 755 vncpasswd/vncpasswd \
		$(CURDIR)/debian/tightvncserver/usr/bin/tightvncpasswd
	install -m 644 vncpasswd/vncpasswd.man \
		$(CURDIR)/debian/tightvncserver/usr/share/man/man1/tightvncpasswd.1
# tightvncserver
	install -m 755 Xvnc/programs/Xserver/Xvnc \
		$(CURDIR)/debian/tightvncserver/usr/bin/Xtightvnc
	install -m 755 vncserver \
		$(CURDIR)/debian/tightvncserver/usr/bin/tightvncserver
	install -m 644 tightvncserver.conf \
		$(CURDIR)/debian/tightvncserver/etc/tightvncserver.conf
	install -m 755 vncconnect/vncconnect \
		$(CURDIR)/debian/tightvncserver/usr/bin/tightvncconnect
	install -m 644 vncserver.man \
		$(CURDIR)/debian/tightvncserver/usr/share/man/man1/tightvncserver.1
	install -m 644 vncconnect/vncconnect.man \
		$(CURDIR)/debian/tightvncserver/usr/share/man/man1/tightvncconnect.1
	install -m 644 Xvnc/programs/Xserver/Xvnc.man \
		$(CURDIR)/debian/tightvncserver/usr/share/man/man1/Xtightvnc.1
# xtightvncviewer
	install -m 755 vncviewer/vncviewer \
		$(CURDIR)/debian/xtightvncviewer/usr/bin/xtightvncviewer
	install -m 644 vncviewer/Vncviewer \
		$(CURDIR)/debian/xtightvncviewer/etc/X11/app-defaults/Vncviewer
	install -m 644 vncviewer/vncviewer.man \
		$(CURDIR)/debian/xtightvncviewer/usr/share/man/man1/xtightvncviewer.1
