#!/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


# 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

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

build: build-stamp

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

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	-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 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

binary-arch: build nufw nuauth libnuclient3 libnuclient-dev nutcpc nuauth-extra nuauth-log-mysql nuauth-log-pgsql libpam-nufw nuauth-utils

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
#	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-plaintext.nufw\"" \
	    -e "s^ *nuauth_user_check_module=.*nuauth_user_check_module=\"system\"" \
	     < $(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 -a
	@echo "Installing $@"
	dh_testroot -a
#	dh_clean -k -a
#	dh_installdirs -a

	# Add here commands to install the package into debian/nufw.
	#$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp/
#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

#	cp $(CURDIR)/conf/nufw.init $(CURDIR)/debian/tmp/etc/init.d/nufw
#	cp $(CURDIR)/conf/nuauth $(CURDIR)/debian/tmp/etc/init.d/nuauth
#	chmod 755 $(CURDIR)/debian/tmp/etc/init.d/*
#	ln -s $(CURDIR)/debian/tmp/usr/lib/nuauth $(CURDIR)/debian/tmp/usr/lib/nuauth
	touch $(maindir)/install-stamp


# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
#	dh_testdir $(dhbuildpackages)
#	dh_testroot $(dhbuildpackages)
#	dh_installchangelogs $(dhbuildpackages) ChangeLog
#	dh_installdocs $(dhbuildpackages)
#	dh_installexamples $(dhbuildpackages)
#	dh_install $(dhbuildpackages) --sourcedir=debian/tmp
##	dh_installmenu
#	dh_installdebconf $(dhbuildpackages)
##	dh_installlogrotate
##	dh_installemacsen
##	dh_installpam
##	dh_installmime
#	dh_installinit $(dhbuildpackages) -u"start 40 2 3 4 5 . stop 89 0 1 6 ."
##	dh_installcron
##	dh_installinfo
#	dh_installman -pnufw doc/nufw.1
#	dh_installman -pnuauth doc/nuauth.1
#	dh_installman -pnutcpc doc/nutcpc.1
#	dh_installman -pnuauth-utils doc/nuaclgen.1
#	dh_installman -pnuauth-utils doc/nutop.1
#	dh_link $(dhbuildpackages)
#	dh_strip $(dhbuildpackages)
#	dh_compress $(dhbuildpackages)
#	dh_fixperms $(dhbuildpackages)
##	dh_perl
##	dh_python
#	dh_makeshlibs $(dhbuildpackages) -n
#	dh_installdeb $(dhbuildpackages)
#	dh_shlibdeps $(dhbuildpackages)
#	dh_gencontrol $(dhbuildpackages)
#	dh_md5sums $(dhbuildpackages)
#	dh_builddeb $(dhbuildpackages)

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