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

%:
	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/gs-esp/8.15/lib:/usr/share/gs-esp/7.07/lib:/usr/share/gs/6.53" \
		--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)" \
		--with-MANDIR="/usr/share/man" \
		--with-PATH_IMPRIP="/usr/bin/psrip"
#		--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_server	= hylafax-server

d_client	= debian/$(p_client)
d_server	= debian/$(p_server)

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 \
	    $(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

	cp -p util/faxmsg util/faxconfig util/faxmodem util/faxstate util/faxwatch \
		util/faxinfo util/tiffcheck util/typetest util/dialtest util/faxadduser util/faxdeluser \
		faxd/faxq faxd/faxqclean faxd/faxgetty faxd/faxsend \
		faxd/pagesend faxd/tsitest faxd/tagtest faxd/cqtest \
		faxd/choptest \
		hfaxd/hfaxd \
		etc/ondelay \
		etc/lockname \
	    $(d_server)/usr/sbin/.
	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`; \
	  install -p $$i $(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)

	install --mode=a+rx -p debian/copy_configuration_from_spool $(d_server)/usr/lib/hylafax/bin/

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

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

override_dh_installinit:
	dh_installinit -p$(p_server) --name=hylafax --no-start

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
	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)

	# needed by sendfax ...
	mkdir -p $(d_client)/usr/sbin
	ln -sf ../bin/textfmt $(d_client)/usr/sbin/textfmt

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 4777 $(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
