#!/usr/bin/make -f
# -*- makefile -*-

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

export SHELL=/bin/bash
export DH_COMPAT=4

CFLAGS = -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

configure: configure-stamp
configure-stamp: patch-stamp
	cp -p /usr/share/misc/config.* .
	yes | /bin/bash ./configure \
		--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-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-PATH_SENDMAIL="/usr/sbin/sendmail" \
		--with-PATH_GSRIP="/usr/bin/gs" \
		--with-MANSCHEME="bsd-source-gzip-strip" \
		--with-INSTALL='${PORT}/../debian/install ${PORT}' \
		--with-LIBTIFF="-ltiff" \
		--with-TIFFINC="/usr/include" \
		--with-TIFFBIN="/usr/bin" \
		--with-LIBZ="-lz" \
		--with-ZLIBINC="-I/usr/include" \
		--with-CONFIG_MAXGID="65534" \
		--with-AWK="/usr/bin/awk" \
		--with-CMP="/usr/bin/cmp" \
		--with-COL="/usr/bin/col" \
		--with-MKFIFO="/usr/bin/mkfifo" \
		--with-SORT="/usr/bin/sort" \
		--with-OPTIMIZER="$(CFLAGS)" \
		--with-FUSER="/bin/fuser" \
		--with-MANDIR="/usr/share/man" \
		--with-IMPRIP="/usr/bin/psrip" \
		--with-BASE64ENCODE="/usr/bin/mimencode" \
		--with-MIMENCODE="/usr/bin/mimencode" \
		--with-UUENCODE="/usr/bin/uuencode" \
		--with-QPENCODE="/usr/bin/mimencode -q"

	touch configure-stamp

build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: configure-stamp
#	$(MAKE) all COPTIONS="$(CFLAGS)"
	$(MAKE) all
	touch build-arch-stamp

build-indep: build-indep-stamp
build-indep-stamp: configure-stamp
	touch build-indep-stamp

clean: clean1 unpatch
clean1:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp configure-stamp
	[ ! -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,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
p_doc		= hylafax-doc

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

install: install-server install-client install-doc
install-doc:
	dh_testdir
	dh_testroot
	dh_clean -k -p$(p_doc)
	dh_installdirs -p$(p_doc) \
		usr/share/doc/$(p_doc)

install-server:
	dh_testdir
	dh_testroot
	dh_clean -k -p$(p_server)

	: # install server package
	dh_installdirs -p$(p_server) \
		etc/hylafax \
		usr/sbin \
		var/log/hylafax \
		usr/lib/hylafax/bin \
		var/spool/hylafax/{archive,client,config,dev,info,log,bin,etc} \
		var/spool/hylafax/{recvq,status,sendq,doneq,docq,tmp,pollq} \
		usr/share/man/{man5,man8} \
		usr/share/lintian/overrides

	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/

	cp -p util/{faxmsg,faxconfig,faxmodem,faxstate,faxwatch} \
		util/{faxinfo,tiffcheck,typetest,dialtest,faxadduser,faxdeluser} \
		faxd/{faxq,faxqclean,faxgetty,faxsend} \
		faxd/{pagesend,tsitest,tagtest,cqtest} \
		faxd/choptest \
		hfaxd/hfaxd \
		etc/ondelay \
	    $(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 \
	  util/{ps2fax.gs.sh,pdf2fax.gs.sh,pcl2fax.sh,tiff2fax.sh,tiff2pdf.sh} \
	  util/{archive.sh,notify.sh,notify-4.1.sh,faxrcvd.sh,pollrcvd.sh,mkcover.sh,wedged.sh} \
	  util/{common-functions.sh,dictionary.sh}; \
	do \
	  b=`basename $$i .sh`; \
	  install -p $$i $(d_server)/var/spool/hylafax/bin/$$b; \
	done
	install -p util/notify.awk $(d_server)/var/spool/hylafax/bin/.

	env LC_ALL=C LANG=C sh -c "cp -p config/[a-z]* $(d_server)/var/spool/hylafax/config/."

	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
#	ln -sf /etc/hylafax $(d_server)/var/spool/hylafax/etc
#	ln -sf ps2fax.gs $(d_server)/var/spool/hylafax/bin/ps2fax
#	ln -sf pdf2fax.gs $(d_server)/var/spool/hylafax/bin/pdf2fax

	: # 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

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

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

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

install-client:
	dh_testdir
	dh_testroot
	dh_clean -k -p$(p_client)

	: # install client package
	dh_installdirs -p$(p_client) \
		etc/hylafax \
		usr/bin \
		usr/lib \
		usr/share/man/{man1,man5} \
		usr/share/hylafax \
		usr/share/doc/hylafax-client/faxmail \
		usr/share/lintian/overrides

	cp -p debian/hyla.conf faxcover/faxcover.ps faxmail/faxmail.ps \
		util/typerules \
	    $(d_client)/etc/hylafax/.
	touch $(d_client)/etc/hylafax/faxcover.ps
	cp -p util/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

	: # 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 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
	dh_install

# Build architecture independant packages using the common target.
binary-indep: build-indep install-doc
	dh_testdir
	dh_testroot
	dh_installdocs -i -A \
		debian/README.mgetty debian/README.Debian \
		TODO CONTRIBUTORS
	dh_installchangelogs -i doc/RELEASENOTES-4.4.txt
	dh_installexamples -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-server install-client
	dh_testdir
	dh_testroot
	dh_installchangelogs -a doc/RELEASENOTES-4.4.txt
	dh_installdocs -a -A \
		debian/README.mgetty debian/README.Debian \
		TODO CONTRIBUTORS
	dh_installexamples
	dh_installdebconf -p$(p_server)
	dh_installinit -p$(p_server) --name=hylafax --no-start
	dh_installcron -p$(p_server) --name=hylafax
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	chgrp uucp $(d_server)/etc/hylafax
	chown uucp:uucp \
	    $(d_server)/var/spool/hylafax/{client,config,dev,info,log,recvq,status,archive,docq,doneq,pollq,sendq,tmp,.}
	chmod 755 \
	    $(d_server)/var/spool/hylafax/{archive,docq,doneq,pollq,sendq}
	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

	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -phylafax-client \
		debian/hylafax-client/usr/bin/faxalter \
		debian/hylafax-client/usr/bin/faxcover \
		debian/hylafax-client/usr/bin/faxmail \
		debian/hylafax-client/usr/bin/faxrm \
		debian/hylafax-client/usr/bin/faxstat \
		debian/hylafax-client/usr/bin/sendfax \
		debian/hylafax-client/usr/bin/sendpage \
		debian/hylafax-client/usr/bin/textfmt
	dh_shlibdeps -phylafax-server \
		debian/hylafax-server/usr/sbin/choptest \
		debian/hylafax-server/usr/sbin/cqtest \
		debian/hylafax-server/usr/sbin/dialtest \
		debian/hylafax-server/usr/sbin/faxadduser \
		debian/hylafax-server/usr/sbin/faxconfig \
		debian/hylafax-server/usr/sbin/faxdeluser \
		debian/hylafax-server/usr/sbin/faxgetty \
		debian/hylafax-server/usr/sbin/faxinfo \
		debian/hylafax-server/usr/sbin/faxmodem \
		debian/hylafax-server/usr/sbin/faxmsg \
		debian/hylafax-server/usr/sbin/faxq \
		debian/hylafax-server/usr/sbin/faxqclean \
		debian/hylafax-server/usr/sbin/faxsend \
		debian/hylafax-server/usr/sbin/faxstate \
		debian/hylafax-server/usr/sbin/faxwatch \
		debian/hylafax-server/usr/sbin/hfaxd \
		debian/hylafax-server/usr/sbin/ondelay \
		debian/hylafax-server/usr/sbin/pagesend \
		debian/hylafax-server/usr/sbin/tagtest \
		debian/hylafax-server/usr/sbin/tiffcheck \
		debian/hylafax-server/usr/sbin/tsitest \
		debian/hylafax-server/usr/sbin/typetest
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

include /usr/share/dpatch/dpatch.make

binary: binary-arch binary-indep
.PHONY: build clean clean1 binary-indep binary-arch binary install install-client install-server install-doc configure patch unpatch
