#!/usr/bin/make -f
# -*- makefile -*-
# 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
maindir         = $(shell pwd)
b               = $(shell pwd)/debian

# This has to be exported to make some magic below work.
export DH_OPTIONS

# 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
LDFLAGS = -lpthread

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

# Which packages should we build?

CONFIGURE       = CFLAGS='$(CFLAGS)' ./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr \
			--mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --localstatedir=/var \
			--sysconfdir=/etc/nufw/ --with-mysql-log --with-pgsql-log --with-system-auth --with-ldap \
			--with-utf8 --with-nfqueue --with-nfconntrack --without-fixedtimeout --enable-pam-nufw \
			--with-prelude-log --with-mysql-auth
			# --with-utf8 --with-nfqueue --with-nfconntrack --with-fixedtimeout #FIXME : THIS line should be used

config.status: configure
	dh_testdir
	# Add here commands to configure the package.
	CFLAGS="$(CFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr \
		--mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --localstatedir=/var \
		--sysconfdir=/etc/nufw/ --with-mysql-log --with-pgsql-log --with-system-auth --with-ldap \
		--with-nfqueue --with-nfconntrack --with-fixedtimeout --with-utf8 --enable-pam-nufw \
		--with-prelude-log --with-mysql-auth

build: build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	rm -f build-nufw build-nufw-stamp
	rm -f config-nufw-stamp install-stamp

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f tests/pki/Makefile
	-find scripts/nuauth_command/ -name '*.pyc' -exec rm {} \;
	[ ! -d scripts/nuauth_command/build ] || rm -rf scripts/nuauth_command/build
	[ ! -f scripts/nuauth_command/scripts/nuauth_command ] || rm -f scripts/nuauth_command/scripts/nuauth_command
	rm -f tests/inl_tests/libnobuffer.so
	rm -f config.log

	dh_clean

config-nufw: config-nufw-stamp
config-nufw-stamp:
	dh_testdir -a
	@echo "Doing $@"
	[ ! -f Makefile ] || make clean
	$(CONFIGURE)
	touch config-nufw-stamp

build-nufw:build-nufw-stamp
build-nufw-stamp: config-nufw-stamp
	dh_testdir -a
	@echo "Doing $@"
	$(MAKE)
	#$(MAKE) && \
	#(cd src/nufw && mv nufw nufw.plain);
	touch build-nufw-stamp

build-stamp: build-nufw
	@echo "--- Compiling"
	dh_testdir
	touch build-stamp

libnussl1:
	@echo "--- Building: $@"
	dh_installdirs           -p$@ -P$(b)/$@
	dh_testdir -p$@ -P$(b)/$@
	dh_testroot -p$@ -P$(b)/$@
	dh_installchangelogs -p$@ -P$(b)/$@ ChangeLog
	dh_installdocs -p$@ -P$(b)/$@
	dh_installexamples -p$@ -P$(b)/$@
	dh_install -p$@
#	rm debian/$@/usr/sbin/nufw
#	dh_installmenu
	dh_installdebconf -p$@ -P$(b)/$@
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installcron
#	dh_installinfo
	dh_link -p$@ -P$(b)/$@
	dh_strip -p$@ -P$(b)/$@
	dh_compress -p$@ -P$(b)/$@
	dh_fixperms -p$@ -P$(b)/$@
#	dh_perl
#	dh_python
	dh_makeshlibs -p$@ -P$(b)/$@ -n
	dh_installdeb -p$@ -P$(b)/$@
	dh_shlibdeps -p$@ -P$(b)/$@
	dh_gencontrol -p$@ -P$(b)/$@
	dh_md5sums -p$@ -P$(b)/$@
	dh_builddeb -p$@ -P$(b)/$@

libnussl-dev:
	@echo "--- Building: $@"
	dh_installdirs           -p$@ -P$(b)/$@
	dh_testdir -p$@ -P$(b)/$@
	dh_testroot -p$@ -P$(b)/$@
	dh_installchangelogs -p$@ -P$(b)/$@ ChangeLog
	dh_installdocs -p$@ -P$(b)/$@
	dh_installexamples -p$@ -P$(b)/$@
	dh_install -p$@
#	rm debian/$@/usr/sbin/nufw
#	dh_installmenu
	dh_installdebconf -p$@ -P$(b)/$@
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installcron
#	dh_installinfo
	dh_link -p$@ -P$(b)/$@
	dh_strip -p$@ -P$(b)/$@
	dh_compress -p$@ -P$(b)/$@
	dh_fixperms -p$@ -P$(b)/$@
#	dh_perl
#	dh_python
	dh_makeshlibs -p$@ -P$(b)/$@ -n
	dh_installdeb -p$@ -P$(b)/$@
	dh_shlibdeps -L libnuclient3 -l debian/libnuclient3/usr/lib -p$@ -P$(b)/$@
	dh_gencontrol -p$@ -P$(b)/$@
	dh_md5sums -p$@ -P$(b)/$@
	dh_builddeb -p$@ -P$(b)/$@

nufw:
	@echo "--- Building: $@"
	dh_installdirs           -p$@ -P$(b)/$@
	dh_testdir -p$@ -P$(b)/$@
	dh_testroot -p$@ -P$(b)/$@
	dh_installchangelogs -p$@ -P$(b)/$@ ChangeLog
	dh_installdocs -p$@ -P$(b)/$@
	dh_installexamples -p$@ -P$(b)/$@
#	dh_install -p$@ -P$(b)/$@
#	rm debian/$@/usr/sbin/nufw
	/usr/bin/install -m 755 src/nufw/nufw debian/nufw/usr/sbin/nufw
#	dh_installmenu
	dh_installdebconf -p$@ -P$(b)/$@
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
	dh_installinit -p$@ -P$(b)/$@ -u"start 40 2 3 4 5 . stop 89 0 1 6 ."
#	dh_installcron
#	dh_installinfo
	dh_installman -pnufw doc/nufw.8
	dh_link -p$@ -P$(b)/$@
	dh_strip -p$@ -P$(b)/$@
	dh_compress -p$@ -P$(b)/$@
	dh_fixperms -p$@ -P$(b)/$@
#	dh_perl
#	dh_python
	dh_makeshlibs -p$@ -P$(b)/$@ -n
	dh_installdeb -p$@ -P$(b)/$@
	dh_shlibdeps -p$@ -P$(b)/$@
	dh_gencontrol -p$@ -P$(b)/$@
	dh_md5sums -p$@ -P$(b)/$@
	dh_builddeb -p$@ -P$(b)/$@


libnuclient3:
	@echo "--- Building: $@"
	dh_installdirs           -p$@ -P$(b)/$@
	dh_testdir -p$@ -P$(b)/$@
	dh_testroot -p$@ -P$(b)/$@
	dh_installchangelogs -p$@ -P$(b)/$@ ChangeLog
	dh_installdocs -p$@ -P$(b)/$@
	dh_installexamples -p$@ -P$(b)/$@
	dh_install -p$@
#	rm debian/$@/usr/sbin/nufw
#	dh_installmenu
	dh_installdebconf -p$@ -P$(b)/$@
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installcron
#	dh_installinfo
	dh_link -p$@ -P$(b)/$@
	dh_strip -p$@ -P$(b)/$@
	dh_compress -p$@ -P$(b)/$@
	dh_fixperms -p$@ -P$(b)/$@
#	dh_perl
#	dh_python
	dh_makeshlibs -p$@ -P$(b)/$@ -n
	dh_installdeb -p$@ -P$(b)/$@
	dh_shlibdeps -p$@ -P$(b)/$@
	dh_gencontrol -p$@ -P$(b)/$@
	dh_md5sums -p$@ -P$(b)/$@
	dh_builddeb -p$@ -P$(b)/$@

libnuclient-dev:
	@echo "--- Building: $@"
	dh_installdirs           -p$@ -P$(b)/$@
	dh_testdir -p$@ -P$(b)/$@
	dh_testroot -p$@ -P$(b)/$@
	dh_installchangelogs -p$@ -P$(b)/$@ ChangeLog
	dh_installdocs -p$@ -P$(b)/$@
	dh_installexamples -p$@ -P$(b)/$@
	dh_install -p$@
#	rm debian/$@/usr/sbin/nufw
#	dh_installmenu
	dh_installdebconf -p$@ -P$(b)/$@
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installcron
#	dh_installinfo
	dh_link -p$@ -P$(b)/$@
	dh_strip -p$@ -P$(b)/$@
	dh_compress -p$@ -P$(b)/$@
	dh_fixperms -p$@ -P$(b)/$@
#	dh_perl
#	dh_python
	dh_makeshlibs -p$@ -P$(b)/$@ -n
	dh_installdeb -p$@ -P$(b)/$@
	dh_shlibdeps -L libnuclient3 -l debian/libnuclient3/usr/lib -p$@ -P$(b)/$@
	dh_gencontrol -p$@ -P$(b)/$@
	dh_md5sums -p$@ -P$(b)/$@
	dh_builddeb -p$@ -P$(b)/$@

libpam-nufw:
	@echo "--- Building: $@"
	dh_installdirs           -p$@ -P$(b)/$@
	dh_testdir -p$@ -P$(b)/$@
	dh_testroot -p$@ -P$(b)/$@
	dh_installchangelogs -p$@ -P$(b)/$@ ChangeLog
	dh_installdocs -p$@ -P$(b)/$@
	dh_installexamples -p$@ -P$(b)/$@
	dh_install -p$@
#	rm debian/$@/usr/sbin/nufw
	# remove local rpath (#495769)
	chrpath -d $(CURDIR)/debian/libpam-nufw/lib/security/pam_nufw.so
#	dh_installmenu
	dh_installdebconf -p$@ -P$(b)/$@
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime

#	dh_installcron
#	dh_installinfo
	dh_link -p$@ -P$(b)/$@
	dh_strip -p$@ -P$(b)/$@
	dh_compress -p$@ -P$(b)/$@
	dh_fixperms -p$@ -P$(b)/$@
#	dh_perl
#	dh_python
	dh_makeshlibs -p$@ -P$(b)/$@ -n
	dh_installdeb -p$@ -P$(b)/$@
	dh_shlibdeps -p$@ -P$(b)/$@
	dh_gencontrol -p$@ -P$(b)/$@
	dh_md5sums -p$@ -P$(b)/$@
	dh_builddeb -p$@ -P$(b)/$@

nuauth:
	@echo "--- Building: $@"
	dh_installdirs           -p$@ -P$(b)/$@
	dh_testdir -p$@ -P$(b)/$@
	dh_testroot -p$@ -P$(b)/$@
	dh_installchangelogs -p$@ -P$(b)/$@ ChangeLog
	dh_installdocs -p$@ -P$(b)/$@
	dh_installexamples -p$@ -P$(b)/$@
	mkdir -p $(CURDIR)/debian/$@/etc/nufw/
	dh_install -p$@
	sed -e "s^#\? *nuauth_tls_key=.*nuauth_tls_key=\"/etc/nufw/certs/nuauth-key.pem\"" \
	    -e "s^#\? *nuauth_tls_cert=.*nuauth_tls_cert=\"/etc/nufw/certs/nuauth-cert.pem\"" \
	    -e "s^ *plaintext_aclfile=.*plaintext_aclfile=\"/etc/nufw/acls.nufw\"" \
	    -e "s^ *plaintext_userfile=.*plaintext_userfile=\"/etc/nufw/users.nufw\"" \
	    -e "s^ *nuauth_user_check_module=.*nuauth_user_check_module=\"system\"" \
	    -e "s^ *nuauth_tls_request_cert=.*nuauth_tls_request_cert=1" \
	    -e "s^#\? *nuauth_tls_disable_nufw_fqdn_check=.*nuauth_tls_disable_nufw_fqdn_check=1" \
	     < $(CURDIR)/conf/nuauth.conf >$(CURDIR)/debian/$@/etc/nufw/nuauth.conf
	mv $(CURDIR)/debian/$@/etc/nufw/nuauth.conf $(CURDIR)/debian/$@/usr/share/nuauth/nuauth.conf
	cp $(CURDIR)/conf/acls.nufw $(CURDIR)/debian/$@/etc/nufw/
	cp $(CURDIR)/conf/users-plaintext.nufw $(CURDIR)/debian/$@/etc/nufw/
	cp $(CURDIR)/conf/periods.xml $(CURDIR)/debian/$@/etc/nufw/
	pwd
#	dh_install -p$@ -P$(b)/$@
#	dh_installmenu
	dh_installdebconf -p$@ -P$(b)/$@
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installcron
#	dh_installinfo
	dh_link -p$@ -P$(b)/$@
	dh_strip -p$@ -P$(b)/$@
	dh_installinit -p$@ -P$(b)/$@ -u"start 40 2 3 4 5 . stop 89 0 1 6 ."
	dh_installman -pnuauth doc/nuauth.8
	dh_compress -p$@ -P$(b)/$@
	dh_fixperms -p$@ -P$(b)/$@
#	dh_perl
#	dh_python
	dh_makeshlibs -p$@ -P$(b)/$@ -n
	dh_installdeb -p$@ -P$(b)/$@
	dh_shlibdeps -p$@ -P$(b)/$@
	dh_gencontrol -p$@ -P$(b)/$@
	dh_md5sums -p$@ -P$(b)/$@
	dh_builddeb -p$@ -P$(b)/$@

nuauth-extra:
	@echo "--- Building: $@"
	dh_installdirs           -p$@ -P$(b)/$@
	dh_testdir -p$@ -P$(b)/$@
	dh_testroot -p$@ -P$(b)/$@
	dh_installchangelogs -p$@ -P$(b)/$@ ChangeLog
	dh_installdocs -p$@ -P$(b)/$@
	dh_installexamples -p$@ -P$(b)/$@
	dh_install -p$@
#	rm debian/$@/usr/sbin/nufw
#	dh_installmenu
	dh_installdebconf -p$@ -P$(b)/$@
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installcron
#	dh_installinfo
	dh_link -p$@ -P$(b)/$@
	dh_strip -p$@ -P$(b)/$@
	dh_compress -p$@ -P$(b)/$@
	dh_fixperms -p$@ -P$(b)/$@
#	dh_perl
#	dh_python
	dh_makeshlibs -p$@ -P$(b)/$@ -n
	dh_installdeb -p$@ -P$(b)/$@
	dh_shlibdeps -p$@ -P$(b)/$@
	dh_gencontrol -p$@ -P$(b)/$@
	dh_md5sums -p$@ -P$(b)/$@
	dh_builddeb -p$@ -P$(b)/$@

nuauth-log-mysql:
	@echo "--- Building: $@"
	dh_installdirs           -p$@ -P$(b)/$@
	dh_testdir -p$@ -P$(b)/$@
	dh_testroot -p$@ -P$(b)/$@
	dh_installchangelogs -p$@ -P$(b)/$@ ChangeLog
	dh_installdocs -p$@ -P$(b)/$@
	dh_installexamples -p$@ -P$(b)/$@
	dh_install -p$@
#	rm debian/$@/usr/sbin/nufw
#	dh_installmenu
	dh_installdebconf -p$@ -P$(b)/$@
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installcron
#	dh_installinfo
	dh_link -p$@ -P$(b)/$@
	dh_strip -p$@ -P$(b)/$@
	dh_compress -p$@ -P$(b)/$@
	dh_fixperms -p$@ -P$(b)/$@
#	dh_perl
#	dh_python
	dh_makeshlibs -p$@ -P$(b)/$@ -n
	dh_installdeb -p$@ -P$(b)/$@
	dh_shlibdeps -p$@ -P$(b)/$@
	dh_gencontrol -p$@ -P$(b)/$@
	dh_md5sums -p$@ -P$(b)/$@
	dh_builddeb -p$@ -P$(b)/$@

nuauth-log-pgsql:
	@echo "--- Building: $@"
	dh_installdirs           -p$@ -P$(b)/$@
	dh_testdir -p$@ -P$(b)/$@
	dh_testroot -p$@ -P$(b)/$@
	dh_installchangelogs -p$@ -P$(b)/$@ ChangeLog
	dh_installdocs -p$@ -P$(b)/$@
	dh_installexamples -p$@ -P$(b)/$@
	dh_install -p$@
#	rm debian/$@/usr/sbin/nufw
#	dh_installmenu
	dh_installdebconf -p$@ -P$(b)/$@
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installcron
#	dh_installinfo
	dh_link -p$@ -P$(b)/$@
	dh_strip -p$@ -P$(b)/$@
	dh_compress -p$@ -P$(b)/$@
	dh_fixperms -p$@ -P$(b)/$@
#	dh_perl
#	dh_python
	dh_makeshlibs -p$@ -P$(b)/$@ -n
	dh_installdeb -p$@ -P$(b)/$@
	dh_shlibdeps -p$@ -P$(b)/$@
	dh_gencontrol -p$@ -P$(b)/$@
	dh_md5sums -p$@ -P$(b)/$@
	dh_builddeb -p$@ -P$(b)/$@

nuauth-utils:
	@echo "--- Building: $@"
	dh_installdirs           -p$@ -P$(b)/$@
	dh_testdir -p$@ -P$(b)/$@
	dh_testroot -p$@ -P$(b)/$@
	dh_installchangelogs -p$@ -P$(b)/$@ ChangeLog
	dh_installdocs -p$@ -P$(b)/$@
	dh_installexamples -p$@ -P$(b)/$@
	dh_install -p$@
#	rm debian/$@/usr/sbin/nufw
#	dh_installmenu
	dh_installdebconf -p$@ -P$(b)/$@
	cp $(CURDIR)/conf/nuaclgen.conf $(CURDIR)/debian/$@/etc/nufw/
	cp $(CURDIR)/conf/nutop.conf $(CURDIR)/debian/$@/etc/nufw/
	cp $(CURDIR)/scripts/nuaclgen $(CURDIR)/debian/$@/usr/bin
	cp $(CURDIR)/scripts/nutop $(CURDIR)/debian/$@/usr/bin
	cp $(CURDIR)/scripts/clean_conntrack.pl $(CURDIR)/debian/$@/usr/sbin
	#make -C $(CURDIR)/scripts/nuauth_command install DESTDIR=$(CURDIR)/debian/$@/
	cd $(CURDIR)/scripts/nuauth_command && ./setup.py install --no-compile \
		--install-lib=$(CURDIR)/debian/$@/usr/share/python-support/nuauth_command \
		--prefix=$(CURDIR)/debian/$@/usr
	#find $(CURDIR)/debian/$@/ -name \*.pyc -exec rm -f {} \;
	dh_python
	dh_pysupport
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installcron
#	dh_installinfo
	dh_link -p$@ -P$(b)/$@
	dh_strip -p$@ -P$(b)/$@
	dh_installman -pnuauth-utils doc/nuaclgen.8
	dh_installman -pnuauth-utils doc/nutop.8
	dh_compress -p$@ -P$(b)/$@
	dh_fixperms -p$@ -P$(b)/$@
#	dh_perl
#	dh_python
	dh_makeshlibs -p$@ -P$(b)/$@ -n
	dh_installdeb -p$@ -P$(b)/$@
	dh_shlibdeps -p$@ -P$(b)/$@
	dh_gencontrol -p$@ -P$(b)/$@
	dh_md5sums -p$@ -P$(b)/$@
	dh_builddeb -p$@ -P$(b)/$@

nutcpc:
	@echo "--- Building: $@"
	dh_installdirs           -p$@ -P$(b)/$@
	dh_testdir -p$@ -P$(b)/$@
	dh_testroot -p$@ -P$(b)/$@
	dh_installchangelogs -p$@ -P$(b)/$@ ChangeLog
	dh_installdocs -p$@ -P$(b)/$@
	dh_installexamples -p$@ -P$(b)/$@
	dh_install -p$@
#	rm debian/$@/usr/sbin/nufw
#	dh_installmenu
	dh_installman -pnutcpc doc/nutcpc.1
	dh_installdebconf -p$@ -P$(b)/$@
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installcron
#	dh_installinfo
	dh_link -p$@ -P$(b)/$@
	dh_strip -p$@ -P$(b)/$@
	dh_compress -p$@ -P$(b)/$@
	dh_fixperms -p$@ -P$(b)/$@
#	dh_perl
#	dh_python
	dh_makeshlibs -p$@ -P$(b)/$@ -n
	dh_installdeb -p$@ -P$(b)/$@
	dh_shlibdeps -L libnuclient3 -l debian/libnuclient3/usr/lib -p$@ -P$(b)/$@
	dh_gencontrol -p$@ -P$(b)/$@
	dh_md5sums -p$@ -P$(b)/$@
	dh_builddeb -p$@ -P$(b)/$@

install: install-stamp

install-stamp:
	dh_testdir
	@echo "Installing $@"
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/nufw.
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp/
	$(MAKE) install-conf DESTDIR=$(CURDIR)/debian/tmp/ sysconfdir=$(CURDIR)/debian/tmp/etc/nufw/
#default certificates
#	cp $(CURDIR)/conf/certs/nufw-key.pem $(CURDIR)/debian/tmp/etc/nufw/certs
#	cp $(CURDIR)/conf/certs/nufw-cert.pem $(CURDIR)/debian/tmp/etc/nufw/certs
#	cp $(CURDIR)/conf/certs/nuauth-key.pem $(CURDIR)/debian/tmp/etc/nufw/certs
#	cp $(CURDIR)/conf/certs/nuauth-cert.pem $(CURDIR)/debian/tmp/etc/nufw/certs

	# move nuauth config files to /usr/share/nuauth, we will handle them using ucf in nuauth.postinst file
	install -d -m0755 $(CURDIR)/debian/tmp/usr/share/nuauth
	mv $(CURDIR)/debian/tmp/etc/nufw/nuauth.conf $(CURDIR)/debian/tmp/etc/nufw/nuauth.d $(CURDIR)/debian/tmp/usr/share/nuauth/
	for file in `find $(CURDIR)/debian/tmp/usr/share/nuauth/ -type f`; do \
	sed -e "s^#\? *nuauth_tls_key=.*nuauth_tls_key=\"/etc/nufw/certs/nuauth-key.pem\"" \
	    -e "s^#\? *nuauth_tls_cert=.*nuauth_tls_cert=\"/etc/nufw/certs/nuauth-cert.pem\"" \
	    -e "s^#\? *nuauth_tls_cacert=.*nuauth_tls_cacert=\"/etc/ssl/certs/ssl-cert-snakeoil.pem\"" \
	    -e "s^#\? *nuauth_tls_dh_params=.*nuauth_tls_dh_params=\"/etc/nufw/certs/nuauth-dh_params.pem\"" \
	    -e "s^ *plaintext_aclfile=.*plaintext_aclfile=\"/etc/nufw/acls.nufw\"" \
	    -e "s^ *plaintext_userfile=.*plaintext_userfile=\"/etc/nufw/users.nufw\"" \
	    -e "s^ *nuauth_user_check_module=.*nuauth_user_check_module=\"system\"" \
	    -e "s^ *nuauth_tls_request_cert=.*nuauth_tls_request_cert=1" \
	    -e "s^#\? *nuauth_tls_disable_nufw_fqdn_check=.*nuauth_tls_disable_nufw_fqdn_check=1" \
	     -i $$file; \
	done

	# do the same for nufw.conf
	install -d -m0755 $(CURDIR)/debian/tmp/usr/share/nufw
	mv $(CURDIR)/debian/tmp/etc/nufw/nufw.conf $(CURDIR)/debian/tmp/usr/share/nufw/
	for file in `find $(CURDIR)/debian/tmp/usr/share/nufw/ -type f`; do \
	sed -e "s^#\? *nufw_tls_key=.*nufw_tls_key=\"/etc/nufw/certs/nufw-key.pem\"" \
	    -e "s^#\? *nufw_tls_cert=.*nufw_tls_cert=\"/etc/nufw/certs/nufw-cert.pem\"" \
	    -e "s^#\? *nufw_tls_cacert=.*nufw_tls_cacert=\"/etc/ssl/certs/ssl-cert-snakeoil.pem\"" \
	     -i $$file; \
	done

	install -m0755 $(CURDIR)/scripts/nuaclgen $(CURDIR)/debian/tmp/usr/bin
	install -m0755 $(CURDIR)/scripts/nutop $(CURDIR)/debian/tmp/usr/bin
	install -m0755 $(CURDIR)/scripts/clean_conntrack.pl $(CURDIR)/debian/tmp/usr/sbin
	install -m0640 $(CURDIR)/conf/nuaclgen.conf $(CURDIR)/debian/tmp/etc/nufw/
	install -m0640 $(CURDIR)/conf/nutop.conf $(CURDIR)/debian/tmp/etc/nufw/

	rm -f $(CURDIR)/debian/tmp/usr/lib/nuauth/modules/*.la
	rm -f $(CURDIR)/debian/tmp/usr/lib/nuauth/modules/*.a
	rm -f $(CURDIR)/debian/tmp/usr/lib/nuclient/modules/*.la
	rm -f $(CURDIR)/debian/tmp/usr/lib/nuclient/modules/*.a
	rm -f $(CURDIR)/debian/tmp/lib/security/*.la
	rm -f $(CURDIR)/debian/tmp/lib/security/*.a

	find $(CURDIR)/debian/tmp/usr/ -name "*.pyc" -delete

	dh_install --list-missing --sourcedir=debian/tmp

	touch install-stamp


# Build architecture-dependent files here.
#binary-arch: build libnussl1 libnussl-dev nufw nuauth libnuclient3 libnuclient-dev nutcpc nuauth-extra nuauth-log-mysql nuauth-log-pgsql libpam-nufw nuauth-utils install
binary-common: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples
#	dh_installmenu
	dh_installdebconf
	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
	dh_installinit -u"start 40 2 3 4 5 . stop 89 0 1 6 ."
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
	dh_pysupport
	dh_makeshlibs -n
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common


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