#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
NAME		= pf
PREFIX		= /usr/local
INSTALL		= /usr/bin/install -c -D -m0644

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.

	touch configure-stamp


build: build-stamp

build-stamp: configure-stamp  
	dh_testdir


	touch $@

clean: 
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp


	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	# Install all except debian and t directory
	for i in `find * ! -path 'debian/*' ! -path 'debian' ! -path 't/*' ! -path 't' -type d`; do \
		install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/$$i; \
	done
	for i in `find * ! -path 'debian/*' ! -path 'debian' ! -path 't/*' ! -path 't' ! -type d`; do \
		$(INSTALL) $$i $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/$$i; \
	done
	for i in `find * -path 't*' -type d`; do \
		install -d -m0700 $(CURDIR)/debian/packetfence-test$(PREFIX)/$(NAME)/$$i; \
	done
	for i in `find * -path 't*' ! -type d`; do \
		$(INSTALL) $$i $(CURDIR)/debian/packetfence-test$(PREFIX)/$(NAME)/$$i; \
	done
	# generate translations
	# TODO this is duplicated in our RPM spec, we should aim to consolidate in a 'make' style step
	for TRANSLATION in de en es fr he_IL it nl pt_BR; do \
		/usr/bin/msgfmt $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/conf/locale/$$TRANSLATION/LC_MESSAGES/packetfence.po \
		--output-file $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/conf/locale/$$TRANSLATION/LC_MESSAGES/packetfence.mo; \
	done
	# precompile grammar
	perl -w -e 'use strict; use warnings; use Parse::RecDescent; use lib "./lib"; use pf::pfcmd::pfcmd; Parse::RecDescent->Precompile($$grammar, "pfcmd_pregrammar");'
	mv pfcmd_pregrammar.pm $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/lib/pf/pfcmd/
	# Install empty directories not in our tarball since the git move
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/addons
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/conf/users
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/conf/ssl
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/html/admin/mrtg
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/html/admin/scan/results
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/html/admin/traplog
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/raddb/sites-enabled
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/logs
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/conf
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/dhcpd
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/named
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/run
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/rrd
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/session
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/webadmin_cache
	#Configurator integration
	install -d -m0700 $(CURDIR)/debian/packetfence/etc/init.d
	install -d -m0700 $(CURDIR)/debian/packetfence/etc/default
	install -m0755 $(CURDIR)/debian/pfappserver.init $(CURDIR)/debian/packetfence/etc/init.d/pfappserver
	install -m0644 $(CURDIR)/debian/pfappserver.default $(CURDIR)/debian/packetfence/etc/default/pfappserver
	#PacketFence remote Snort sensor
	install -d -m0700 $(CURDIR)/debian/packetfence-remote-snort-sensor$(PREFIX)/$(NAME)/conf
	install -d $(CURDIR)/debian/packetfence-remote-snort-sensor$(PREFIX)/$(NAME)/var
	install -d $(CURDIR)/debian/packetfence-remote-snort-sensor$(PREFIX)/$(NAME)/sbin
	install -m0755 addons/pfdetect_remote/sbin/pfdetect_remote $(CURDIR)/debian/packetfence-remote-snort-sensor$(PREFIX)/$(NAME)/sbin
	install -m0600 addons/pfdetect_remote/conf/pfdetect_remote.conf $(CURDIR)/debian/packetfence-remote-snort-sensor$(PREFIX)/$(NAME)/conf
	#PacketFence pfcmd suid
	install -d -m0755 $(CURDIR)/debian/packetfence-pfcmd-suid$(PREFIX)/$(NAME)/bin
	gcc src/pfcmd.c -o $(CURDIR)/debian/packetfence-pfcmd-suid$(PREFIX)/$(NAME)/bin/pfcmd
	# Install our logrotate file into debian/ so dh_installlogrotate will be able to do it's magic
	install addons/logrotate $(CURDIR)/debian/packetfence.logrotate
# 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
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples
	dh_install
#	dh_installmenu
#	dh_installdebconf	
	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_python
	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link /usr/local/pf/conf/httpd.conf.apache22 /usr/local/pf/conf/httpd.conf \
			/usr/local/pf/conf/named.conf.bind97 /usr/local/pf/conf/named.conf \
			/usr/local/pf/raddb/sites-available/control-socket /usr/local/pf/raddb/sites-enabled/control-socket \
			/usr/local/pf/raddb/sites-available/default /usr/local/pf/raddb/sites-enabled/default \
			/usr/local/pf/raddb/sites-available/inner-tunnel /usr/local/pf/raddb/sites-enabled/inner-tunnel \
			/usr/local/pf/raddb/sites-available/packetfence /usr/local/pf/raddb/sites-enabled/packetfence \
			/usr/local/pf/raddb/sites-available/packetfence-soh /usr/local/pf/raddb/sites-enabled/packetfence-soh \
			/usr/local/pf/raddb/sites-available/packetfence-tunnel /usr/local/pf/raddb/sites-enabled/packetfence-tunnel
	dh_strip
	dh_compress
	# PERMISSIONS
	# Executables
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/addons/*.pl
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/addons/*.sh
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/addons/upgrade/*.pl
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/addons/snort/*.pl
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/addons/watchdog/*.sh
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/bin/*
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/html/admin/*.cgi
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/sbin/*
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/html/pfappserver/script/*.pl
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/html/captive-portal/*.cgi
	dh_fixperms -X$(NAME)/bin -X$(NAME)/sbin -X$(NAME)/addons \
                -X$(NAME)/html/admin/soh.cgi -X$(NAME)/conf/pfdetect_remote.conf \
                -X$(NAME)sbin/pfdetect_remote
	# Everything is owned by pf:pf
	chown pf:pf -R $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/
	# Set setuid root on bin/pfcmd
	chown pf:pf -R $(CURDIR)/debian/packetfence-pfcmd-suid$(PREFIX)/$(NAME)/
	chown root:root $(CURDIR)/debian/packetfence-pfcmd-suid$(PREFIX)/$(NAME)/bin/pfcmd
	chmod ug+s $(CURDIR)/debian/packetfence-pfcmd-suid$(PREFIX)/$(NAME)/bin/pfcmd
#	dh_perl
#	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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