#! /usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS = -Wall -g
DEBUG_OPTS=
STRIP_OPTS=

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
        STRIP_OPTS += dh_strip --dbg-package=clamav-dbg -p$@
else
	DEBUG_OPTS += --enable-debug
endif
ifneq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
  with_check := disabled by DEB_BUILD_OPTIONS.
else
  ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
    with_check := disabled because cross-compiling.
  else
    with_check := yes
  endif
endif

config.status: configure
	dh_testdir
	egrep '^#[[:alpha:]]' etc/clamd.conf | sed -e 's/^#//' | awk '{print $$1}' | while read opt; do \
	  if ! grep -q "$${opt}" debian/clamav-base.postinst.in ; then \
	    echo "Unhandled option(clamd.conf): $${opt}"; \
	    exit 1; \
	  fi;\
	done;
	egrep '^#[[:alpha:]]' etc/freshclam.conf | sed -e 's/^#//' | awk '{print $$1}' | while read opt; do \
	  if ! grep -q "$${opt}" debian/clamav-freshclam.postinst.in ; then \
	    echo "Unhandled option (freshclam.conf): $${opt}"; \
	    exit 1; \
	  fi;\
	done;
	egrep '^#[[:alpha:]]' etc/clamav-milter.conf | sed -e 's/^#//' | awk '{print $$1}' | while read opt; do \
	  if ! grep -q "$${opt}" debian/clamav-milter.postinst.in ; then \
	    echo "Unhandled option (clamav-milter.conf): $${opt}"; \
	    exit 1; \
	  fi;\
	done;
	# Add here commands to configure the package.
	./configure --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --disable-clamav --with-dbdir=/var/lib/clamav/ --sysconfdir=/etc/clamav --enable-milter --disable-clamuko --with-gnu-ld --enable-dns-fix ${DEBUG_OPTS} --disable-unrar --libdir=\$${prefix}/lib --with-system-tommath  --with-ltdl-include=/usr/include --with-ltdl-lib=/usr/lib

build: build-stamp
build-stamp: build-arch build-indep
	touch build-stamp

build-indep: build-indep-stamp
build-indep-stamp:
	perl -pe 's~#COMMON-FUNCTIONS#~qx{cat debian/common_functions}~eg' < debian/clamav-base.config.in > debian/clamav-base.config
	perl -pe 's~#COMMON-FUNCTIONS#~qx{cat debian/common_functions}~eg' < debian/clamav-base.postinst.in > debian/clamav-base.postinst
	touch build-indep-stamp

build-arch: build-arch-stamp
build-arch-stamp:  config.status
	dh_testdir

ifeq ($(PO2DEBCONF),yes)
	po2debconf -e utf8 debian/clamav-base.templates.master > debian/clamav-base.templates
	po2debconf -e utf8 debian/clamav-freshclam.templates.master > debian/clamav-freshclam.templates
	po2debconf -e utf8 debian/clamav-milter.templates.master > debian/clamav-milter.templates
endif

	$(MAKE) CFLAGS="${CFLAGS}" pkgdatadir=/var/lib/clamav/ all
	perl -pe 's~#COMMON-FUNCTIONS#~qx{cat debian/common_functions}~eg' < debian/clamav-milter.init.in > debian/clamav-milter.init
	perl -pe 's~#COMMON-FUNCTIONS#~qx{cat debian/common_functions}~eg' < debian/clamav-milter.config.in > debian/clamav-milter.config
	perl -pe 's~#COMMON-FUNCTIONS#~qx{cat debian/common_functions}~eg' < debian/clamav-milter.postinst.in > debian/clamav-milter.postinst
	perl -pe 's~#COMMON-FUNCTIONS#~qx{cat debian/common_functions}~eg' < debian/clamav-daemon.postinst.in > debian/clamav-daemon.postinst
	perl -pe 's~#COMMON-FUNCTIONS#~qx{cat debian/common_functions}~eg' < debian/clamav-daemon.init.in > debian/clamav-daemon.init
	perl -pe 's~#COMMON-FUNCTIONS#~qx{cat debian/common_functions}~eg' < debian/clamav-freshclam.init.in > debian/clamav-freshclam.init
	perl -pe 's~#COMMON-FUNCTIONS#~qx{cat debian/common_functions}~eg' < debian/clamav-freshclam.config.in > debian/clamav-freshclam.config
	perl -pe 's~#COMMON-FUNCTIONS#~qx{cat debian/common_functions}~eg' < debian/clamav-freshclam.postinst.in > debian/clamav-freshclam.postinst
	touch build-arch-stamp

clean:
	debconf-updatepo
	dh_testdir 
	dh_testroot 

ifeq (Makefile,$(wildcard Makefile))
	$(MAKE) distclean
endif
	rm -f target.h config.log
	rm -f build-arch-stamp build-indep-stamp install-indep-stamp install-arch-stamp build-stamp install-stamp
	rm -f debian/clamav-base.config
	rm -f debian/clamav-base.postinst
	rm -f debian/clamav-daemon.postinst
	rm -f debian/clamav-daemon.init
	rm -f debian/clamav-milter.init
	rm -f debian/clamav-milter.config
	rm -f debian/clamav-milter.postinst
	rm -f debian/clamav-freshclam.init
	rm -f debian/clamav-freshclam.config
	rm -f debian/clamav-freshclam.postinst
	dh_clean

check:
	dh_testdir
ifeq ($(with_check),yes)
ifneq (,$(filter alpha ia64 sparc, $(shell dpkg-architecture -qDEB_BUILD_ARCH)))
	@echo "Ignoring errors because of #521737, #521556, #521688"
	-$(MAKE) check
else
	$(MAKE) check
endif
else
	@echo "Testsuite not run: $(with_check)."
endif

install: install-indep install-arch
install-indep: install-indep-stamp
install-indep-stamp: build-indep-stamp
	dh_testdir
	dh_testroot
	dh_clean -k -i -d
	
	touch install-indep-stamp

clamav-base: install
	dh_testdir
	dh_testroot
	dh_installdirs -p$@
	dh_installdocs -p$@
	dh_install -X.svn -p$@
	chmod +x debian/$@/usr/share/bug/$@/script
	dh_installchangelogs -p$@ ChangeLog 
	dh_installdebconf -p$@
	dh_installman -p$@
	dh_installexamples -p$@
	dh_compress -p$@ -Xexamples/
	dh_md5sums -p$@
	dh_installdeb -p$@
	dh_gencontrol -p$@
	dh_fixperms -p$@
	dh_builddeb -p$@

clamav-docs: install-indep
	dh_testdir
	dh_testroot
	dh_installdirs -p$@
	dh_installdocs -p$@
	dh_install -X.svn -p$@
	dh_installchangelogs -p$@ ChangeLog 
	dh_compress -p$@ -X.pdf -X.html -X.css -X.png -X.uu
	rm -rf debian/clamav-docs/usr/share/doc/clamav-docs/html/.svn
	dh_md5sums -p$@
	dh_installdeb -p$@
	dh_gencontrol -p$@
	dh_fixperms -p$@
	dh_builddeb -p$@

clamav-testfiles: install-indep
	dh_testdir
	dh_testroot
	dh_installdirs -p$@
	dh_installdocs -p$@
	dh_install -X.svn -p$@
	dh_installchangelogs -p$@ ChangeLog 
	dh_compress -p$@
	dh_md5sums -p$@
	dh_installdeb -p$@
	dh_gencontrol -p$@
	dh_fixperms -p$@
	dh_builddeb -p$@

install-arch: install-arch-stamp check
install-arch-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k -a
	# Add here commands to install the package into debian/clamav.
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp/
	
	touch install-arch-stamp

clamav-dbg:
	dh_testdir
	dh_testroot
	dh_installdocs -p$@
	dh_installchangelogs -p$@ ChangeLog
	dh_compress -p$@
	dh_md5sums -p$@
	dh_shlibdeps -l debian/libclamav6/usr/lib -p$@
	dh_installdeb -p$@
	dh_gencontrol -p$@
	dh_builddeb -p$@

clamav: install
	dh_testdir
	dh_testroot
	dh_installdirs -p$@
	dh_installexamples -X.svn -p$@
	dh_install -X.svn -p$@
	chmod +x debian/$@/usr/share/bug/$@/script
	dh_installman -p$@
	dh_installchangelogs -p$@ ChangeLog 
	dh_installdocs -p$@ 
	dh_link -p$@
	${STRIP_OPTS}
	dh_compress -p$@
	dh_md5sums -p$@
	dh_installdeb -p$@
	dh_shlibdeps -l debian/libclamav6/usr/lib -p$@
	dh_gencontrol -p$@
	dh_fixperms -p$@
	dh_builddeb -p$@

clamav-freshclam: install
	dh_testdir
	dh_testroot
	dh_installdirs -p$@
	dh_installdocs -p$@ 
	dh_installchangelogs -p$@ ChangeLog 
	dh_installexamples -p$@
	dh_install -X.svn -p$@
	chmod +x debian/$@/usr/share/bug/$@/script
	dh_installlogcheck -p$@
	dh_installinit -p$@ --noscripts
	dh_installman -p$@
	dh_link -p$@
	dh_installlogrotate -p$@
	${STRIP_OPTS}
	dh_compress -p$@ -Xexamples/
	dh_installdebconf -p$@
	dh_md5sums -p$@
	dh_installdeb -p$@
	dh_shlibdeps -l debian/libclamav6/usr/lib -p$@
	dh_gencontrol -p$@
	dh_fixperms -p$@ -Xclamav-freshclam-ifupdown
	chmod +x debian/clamav-freshclam/etc/network/if-up.d/clamav-freshclam-ifupdown
	chmod +x debian/clamav-freshclam/etc/network/if-down.d/clamav-freshclam-ifupdown
	chmod +x debian/clamav-freshclam/etc/ppp/ip-up.d/clamav-freshclam-ifupdown
	chmod +x debian/clamav-freshclam/etc/ppp/ip-down.d/clamav-freshclam-ifupdown
	dh_builddeb -p$@

clamav-daemon: install
	dh_testdir
	dh_testroot
	dh_installdirs -p$@
	dh_install -X.svn -p$@
	chmod +x debian/$@/usr/share/bug/$@/script
	dh_installlogcheck -p$@
	dh_installinit -p$@
	dh_installman -p$@
	dh_installdocs -p$@ 
	dh_installchangelogs -p$@ ChangeLog 
	dh_link -p$@
	dh_installlogrotate -p$@
	${STRIP_OPTS}
	dh_compress -p$@
	dh_md5sums -p$@
	dh_installdeb -p$@
	dh_shlibdeps -l debian/libclamav6/usr/lib -p$@
	dh_gencontrol -p$@
	dh_fixperms -p$@
	dh_builddeb -p$@

clamav-milter: install
	dh_testdir
	dh_testroot
	dh_installdirs -p$@
	dh_installdocs -p$@
	dh_install -X.svn -p$@
	chmod +x debian/$@/usr/share/bug/$@/script
	dh_installinit -p$@
	dh_installman -p$@
	dh_installdocs -p$@ 
	dh_installchangelogs -p$@ ChangeLog 
	dh_installdebconf -p$@
	dh_installlogcheck -p$@
	dh_link -p$@
	dh_installlogrotate -p$@
	dh_installexamples -p$@
	dh_compress -p$@ -Xexamples/
	${STRIP_OPTS}
	dh_md5sums -p$@
	dh_installdeb -p$@
	dh_shlibdeps -l debian/libclamav6/usr/lib -p$@
	dh_gencontrol -p$@
	dh_fixperms -p$@
	dh_builddeb -p$@

libclamav6: install
	dh_testdir
	dh_testroot
	dh_installdirs -p$@
	dh_installdocs -p$@
	dh_install -X.svn -p$@
	dh_installchangelogs -p$@ ChangeLog 
	dh_lintian -p$@
	${STRIP_OPTS}
	dh_compress -p$@
	dh_makeshlibs -V -p$@
	dh_md5sums -p$@
	dh_installdeb -p$@
	dh_shlibdeps -l debian/libclamav6/usr/lib -p$@
	dh_gencontrol -p$@
	dh_fixperms -p$@
	dh_builddeb -p$@

libclamav-dev: install
	dh_testdir
	dh_testroot
	dh_installdirs -p$@
	dh_installman -p$@
	dh_installdocs -p$@ 
	dh_installchangelogs -p$@ ChangeLog 
	dh_install -X.svn -p$@
	dh_link -p$@
	${STRIP_OPTS}
	dh_compress -p$@
	dh_makeshlibs -V -p$@
	dh_md5sums -p$@
	dh_installdeb -p$@
	dh_shlibdeps -l debian/tmp/usr/lib -p$@
	dh_gencontrol -p$@
	dh_fixperms -p$@
	dh_builddeb -p$@

# Build architecture independant packages using the common target.
binary-indep: build install clamav-base clamav-testfiles clamav-docs

# Build architecture dependant packages using the common target.
binary-arch: build install libclamav6 clamav clamav-daemon clamav-freshclam clamav-milter libclamav-dev clamav-dbg

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install check
