#!/usr/bin/make -f
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# The build system doesn't use any flags from the environment! Pass
# CFLAGS/CPPFLAGS via OPTIMIZER, LDFLAGS by patching the build system.

# temporary fix for bug #1067362
CFLAGS:=$(filter-out -Werror=implicit-function-declaration,$(CFLAGS))

%:
	dh $@

override_dh_auto_configure:
	cp -p /usr/share/misc/config.* .
	yes | /bin/bash ./configure \
		--with-HTML=yes \
		--with-REGEX=no \
		--with-LIBREGEX='' \
		--with-REGEXINC='' \
		--with-UTMP=auto \
		--with-DIR_BIN="/usr/bin" \
		--with-DIR_LIBDATA="/etc/hylafax" \
		--with-DIR_LIB="/usr/lib/hylafax" \
		--with-DIR_LIBEXEC="/usr/sbin" \
		--with-DIR_MAN="/usr/share/man" \
		--with-DIR_SBIN="/usr/sbin" \
		--with-DIR_LOCKS="/var/lock" \
		--with-PATH_AFM="/usr/share/fonts/type1/gsfonts" \
		--with-FONTMAP="/usr/share/ghostscript/9.19/lib" \
		--with-MANSCHEME="bsd-source-gzip-strip" \
		--with-DIR_HTML="/usr/share/doc/hylafax" \
		--with-DIR_CGI="/usr/lib/cgi-bin" \
		--with-HTMLPATH="/doc/hylafax" \
		--with-SYSGID="root" \
		--with-SYSUID="root" \
		--with-PATH_GETTY="/etc/hylafax/getty-link" \
		--with-PATH_VGETTY="/etc/hylafax/vgetty-link" \
		--with-PATH_EGETTY="/etc/hylafax/egetty-link" \
		--with-CONFIG_MAXGID="65534" \
		--with-CMP="/usr/bin/cmp" \
		--with-COL="/usr/bin/col" \
		--with-MKFIFO="/usr/bin/mkfifo" \
		--with-SORT="/usr/bin/sort" \
		--with-OPTIMIZER="$(CFLAGS) $(CPPFLAGS)" \
		--with-MANDIR="/usr/share/man" \
		--with-PATH_DPSRIP="" \
		--with-PATH_IMPRIP="/usr/bin/psrip" \
		--with-AWK="/usr/bin/awk"
#		--with-INSTALL=/usr/bin/install \
 
override_dh_clean:
	[ ! -f Makefile ] || make distclean
	[ ! -d man/cman.apps ] \
	    || rmdir man/cman.apps man/cman.files man/sman.apps man/sman.files
	rm -f port/Makefile port/install.sh port/mkdepend
	rm -f config.guess config.sub
	rm -f dummy xgnu.c dummy.c xmakeinc t dummy.o dummy.C confMakefile config.log a.out
	dh_clean
 
PWD		:= $(shell pwd)
p_client	= hylafax-client
p_client_dbg	= hylafax-client-dbg
p_server	= hylafax-server
p_server_dbg	= hylafax-server-dbg

d_client	= debian/$(p_client)
d_client_dbg	= debian/$(p_client_dbg)
d_server	= debian/$(p_server)
d_server_dbg	= debian/$(p_server_dbg)

d_dbg		= usr/lib/debug/

override_dh_auto_install: install-client install-server

install-server:
	cp -p debian/hosts.hfaxd hfaxd/hfaxd.conf util/cover.templ \
		etc/dialrules etc/dialrules.sf-ba etc/dialrules.europe \
		etc/lutRS18.pcf debian/hfaxd.systemd.conf \
	    $(d_server)/etc/hylafax/.
	cp -p debian/hylafax.config $(d_server)/etc/hylafax/config
	cp -pR etc/templates $(d_server)/etc/hylafax/
	rm -r $(d_server)/etc/hylafax/templates/html-sample1

	install --mode=a+rx -p debian/hylafax_wrapper $(d_server)/usr/sbin/hylafax_wrapper

	for i in faxd/faxq hfaxd/hfaxd faxd/faxgetty; \
	do \
	  cp -p $$i $(d_server)/usr/sbin/; \
	done
	for i in util/faxmsg util/faxconfig util/faxmodem util/faxstate util/faxwatch \
		util/faxinfo util/tiffcheck util/dialtest util/faxadduser util/faxdeluser \
		faxd/faxqclean faxd/faxsend \
		faxd/pagesend faxd/tsitest faxd/tagtest faxd/cqtest \
		faxd/choptest \
		etc/ondelay \
		etc/lockname; \
	do \
	  b=`basename $$i`; \
	  cp -p $$i $(d_server)/usr/lib/hylafax/bin/$$b; \
	  ln --symbolic --relative $(d_server)/usr/sbin/hylafax_wrapper $(d_server)/usr/sbin/$$b; \
	done
	for i in \
	  util/xferfaxstats.sh util/recvstats.sh util/faxcron.sh \
	  etc/faxaddmodem.sh etc/faxsetup.sh etc/probemodem.sh; \
	do \
	  b=`basename $$i .sh`; \
	  cp -p $$i $(d_server)/usr/lib/hylafax/bin/$$b; \
	  chmod a+rx $(d_server)/usr/lib/hylafax/bin/$$b; \
	  ln --symbolic --relative $(d_server)/usr/sbin/hylafax_wrapper $(d_server)/usr/sbin/$$b; \
	done
	for i in \
	  ps2fax.gs.sh pdf2fax.gs.sh pcl2fax.sh tiff2fax.sh tiff2pdf.sh \
	  archive.sh notify.sh notify-4.1.sh faxrcvd.sh pollrcvd.sh mkcover.sh wedged.sh \
	  common-functions.sh dictionary.sh qp-encode.awk b64-encode.awk; \
	do \
	  b=`basename $$i .sh`; \
	  install -p util/$$i $(d_server)/var/spool/hylafax/bin/$$b; \
	done
	install -p util/notify.awk $(d_server)/var/spool/hylafax/bin/
	chmod a-x $(d_server)/var/spool/hylafax/bin/dictionary
	chmod a-x $(d_server)/var/spool/hylafax/bin/common-functions

	cp -p config/* $(d_server)/var/spool/hylafax/config/
	-rm $(d_server)/var/spool/hylafax/config/Makefile
	-rm $(d_server)/var/spool/hylafax/config/Makefile.in

	ln -sf faxmsg $(d_server)/usr/sbin/faxanswer
	ln -sf faxmsg $(d_server)/usr/sbin/faxabort
	ln -sf faxmsg $(d_server)/usr/sbin/faxquit
	ln -sf faxmsg $(d_server)/usr/sbin/faxlock

	# override lintian warnings
	cp -p debian/lintian.override.hylafax-server \
		$(d_server)/usr/share/lintian/overrides/$(p_server)

	cp debian/hylafax-server.hylafax.default-maintainer $(d_server)/usr/share/hylafax/hylafax.default-maintainer


override_dh_installman:
	: # install server man pages
	for i in man/sman.files/*.4f; do \
	  b=`basename $$i .4f`; \
	  cp -p $$i $(d_server)/usr/share/man/man5/$$b.5; \
	done
	for i in man/sman.apps/*.1m; do \
	  b=`basename $$i .1m`; \
	  cp -p $$i $(d_server)/usr/share/man/man8/$$b.8; \
	done
	for i in debian/man/*.8; do \
	  b=`basename $$i .8`; \
	  cp -p $$i $(d_server)/usr/share/man/man8/$$b.8; \
	done

	: # install client man pages
	for i in man/cman.files/*.4f; do \
	  b=`basename $$i .4f`; \
	  cp -p $$i $(d_client)/usr/share/man/man5/$$b.5; \
	done
	for i in man/cman.apps/*.1; do \
	  b=`basename $$i .1`; \
	  [ $$b != sgi2fax ] && cp -p $$i $(d_client)/usr/share/man/man1/$$b.1; \
	done

	cp -p util/typetest $(d_client)/usr/bin/

override_dh_installexamples:
	dh_installexamples -p$(p_server) debian/examples/*

override_dh_installinit:
	dh_installinit --no-start --no-enable -p$(p_server) --name=hylafax
	dh_installsystemd --no-start --no-enable -p$(p_server) --name=faxgetty@
	dh_installsystemd --no-start --no-enable -p$(p_server) --name=hfaxd
	dh_installsystemd --no-start --no-enable -p$(p_server) --name=faxq
	dh_installsystemd --no-start --no-enable -p$(p_server) --name=hylafax

override_dh_installcron:
	dh_installcron -p$(p_server) --name=hylafax

override_dh_installdirs:
	dh_installdirs -p$(p_server) \
		etc/hylafax \
		usr/sbin \
		var/log/hylafax \
		usr/lib/hylafax/bin \
		var/spool/hylafax/archive var/spool/hylafax/client var/spool/hylafax/config \
		var/spool/hylafax/dev var/spool/hylafax/info var/spool/hylafax/log var/spool/hylafax/bin \
		var/spool/hylafax/etc var/spool/hylafax/recvq var/spool/hylafax/status var/spool/hylafax/sendq \
		var/spool/hylafax/doneq var/spool/hylafax/docq var/spool/hylafax/tmp var/spool/hylafax/pollq \
		usr/share/man/man5 usr/share/man/man8 \
		usr/share/lintian/overrides \
		usr/share/hylafax
	dh_installdirs -p$(p_client) \
		etc/hylafax \
		usr/bin \
		usr/lib \
		usr/share/man/man1 usr/share/man/man5 \
		usr/share/hylafax \
		usr/share/doc/hylafax-client/faxmail \
		usr/share/lintian/overrides

install-client:
	cp -p debian/hyla.conf faxcover/faxcover.ps faxmail/faxmail.ps \
		libhylafax/typerules \
	    $(d_client)/etc/hylafax/.
	touch $(d_client)/etc/hylafax/faxcover.ps
	cp -p libhylafax/pagesizes $(d_client)/usr/share/hylafax/.
	install -p util/textfmt faxalter/faxalter faxcover/faxcover \
		faxmail/faxmail faxrm/faxrm faxstat/faxstat \
		sendfax/sendfax sendpage/sendpage \
	$(d_client)/usr/bin/.

	install -p faxmail/mailfax.sh-postfix faxmail/mailfax.sh-qmail \
		faxmail/mailfax.sh-sendmail faxmail/mailfax.sh-smail \
		faxmail/README \
	$(d_client)/usr/share/doc/hylafax-client/faxmail/

	for i in faxcover/edit-faxcover.sh; \
	do \
	  b=`basename $$i .sh`; \
	  install -p $$i $(d_client)/usr/bin/$$b; \
	done

	# override lintian warnings
	cp -p debian/lintian.override.hylafax-client \
		$(d_client)/usr/share/lintian/overrides/$(p_client)

override_dh_installchangelogs:
	dh_installchangelogs -a doc/RELEASENOTES-6.0.txt

override_dh_fixperms:
	dh_fixperms
ifneq (,$(findstring hylafax-server, $(shell dh_listpackages)))
	chgrp uucp $(d_server)/etc/hylafax
	for i in client config dev info log recvq status archive docq doneq pollq sendq tmp .; \
	do chown uucp:uucp $(d_server)/var/spool/hylafax/$$i; done
	for i in archive docq doneq pollq sendq; \
	do chmod 755 $(d_server)/var/spool/hylafax/$$i; done
	chmod 775 $(d_server)/var/spool/hylafax
	chmod 1777 $(d_server)/var/spool/hylafax/tmp
	chmod 755  $(d_server)/var/spool/hylafax/dev
	chown uucp $(d_server)/etc/hylafax/hosts.hfaxd
	chmod 600  $(d_server)/etc/hylafax/hosts.hfaxd
endif

override_dh_makeshlibs:
	dh_makeshlibs -p$(p_client)

#override_dh_shlibdeps:
#	dh_shlibdeps -L libfaxutil -phylafax-server

override_dh_installdocs:
	dh_installdocs -a -A \
		debian/README.mgetty debian/README.Debian \
		TODO CONTRIBUTORS

override_dh_strip:
	dh_strip -p$(p_client) --dbg-package=$(p_client_dbg)
	dh_strip -p$(p_server) --dbg-package=$(p_server_dbg)

override_dh_installlogrotate:
	dh_installlogrotate -p$(p_server) --name xferfaxlog
