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

# DIST = jessie or stretch or ..
DIST = $(shell lsb_release -c -s)

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

	#copying example files with the extention
	for i in `find * -name "*.example" ! -path 'conf/pfconfig.conf*' ! -path 'conf/redis_cache.conf*'`; do \
		$(INSTALL) $$i "$(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/$$(dirname $$i)/$$(basename $$i .example)"; \
	done

	# Install all except debian and t directory
	for i in `find * ! -path 'debian/*' ! -path 'debian' ! -path 't/*' ! -path 't' ! -path 'lib/pfconfig/*' ! -path 'lib/pfconfig' ! -path 'sbin/pfconfig' -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' ! -path 'lib/pfconfig/*' ! -path 'lib/pfconfig' ! -path 'sbin/pfconfig' ! -path 'conf/pfconfig.conf*' ! -path 'conf/redis_cache.conf*' ! -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
	for i in `find * -path 'lib/pfconfig*' -type d`; do \
		install -d -m0700 $(CURDIR)/debian/packetfence-config$(PREFIX)/$(NAME)/$$i; \
	done
	for i in `find * -path 'lib/pfconfig/*' ! -type d`; do \
		$(INSTALL) $$i $(CURDIR)/debian/packetfence-config$(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 pl_PL 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
	# 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/certmanager
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/conf/ssl
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/conf/ssl/acme-challenge
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/raddb/sites-enabled
	install -d -m2770 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/logs
	install -d -m2770 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/conf
	install -d -m2770 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/cache
	install -d -m2770 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/cache/ntlm_cache_users
	install -d -m2770 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/var
	install -d -m2770 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/redis_queue
	install -d -m2770 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/redis_ntlm_cache
	install -d -m2770 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/run
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/rrd
	install -d -m2770 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/session
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/webadmin_cache
	install -d -m2770 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/cache/configfiles
	install -d -m2770 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/cache/configfilesdata
	install -d -m0700 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/ssl_mutex
	touch $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/cache_control
	#Configurator integration
	install -d -m0700 $(CURDIR)/debian/packetfence/lib/systemd/system
	install -d -m0700 $(CURDIR)/debian/packetfence/etc/systemd/system
	install -m0644 $(CURDIR)/debian/packetfence.target $(CURDIR)/debian/packetfence/etc/systemd/system
	install -m0644 $(CURDIR)/debian/packetfence-base.target $(CURDIR)/debian/packetfence/etc/systemd/system
	install -m0644 $(CURDIR)/debian/packetfence-cluster.target $(CURDIR)/debian/packetfence/etc/systemd/system
	install -d -m0700 $(CURDIR)/debian/packetfence/etc/systemd/system/packetfence-base.target.wants
	install -d -m0700 $(CURDIR)/debian/packetfence/etc/systemd/system/packetfence.target.wants
	install -d -m0700 $(CURDIR)/debian/packetfence/etc/systemd/system/packetfence-cluster.target.wants
	install -m0644 $(CURDIR)/debian/packetfence.slice $(CURDIR)/debian/packetfence/etc/systemd/system
	install -m0644 $(CURDIR)/debian/packetfence-base.slice $(CURDIR)/debian/packetfence/etc/systemd/system
	install -m0644 $(CURDIR)/debian/ifup@.service $(CURDIR)/debian/packetfence/etc/systemd/system
	install -d -m0700 $(CURDIR)/debian/packetfence/etc/default
	install -d -m0755 $(CURDIR)/debian/packetfence/etc/modprobe.d
	install -d -m0755 $(CURDIR)/debian/packetfence/etc/modules-load.d
	install -m0644 $(CURDIR)/debian/packetfence.modprobe $(CURDIR)/debian/packetfence/etc/modprobe.d/packetfence.conf
	install -m0644 $(CURDIR)/debian/packetfence.modules-load $(CURDIR)/debian/packetfence/etc/modules-load.d/packetfence.conf

	# copy the rsyslog systemd drop in
	install -d -m0755 $(CURDIR)/debian/packetfence/etc/systemd/system/rsyslog.service.d/
	install -m0600 $(CURDIR)/packetfence.rsyslog-drop-in.service  $(CURDIR)/debian/packetfence/etc/systemd/system/rsyslog.service.d/packetfence.conf
	#Sudoer
	install -oroot -groot -d -m0750 $(CURDIR)/debian/packetfence/etc/sudoers.d
	install -oroot -groot -m0440 $(CURDIR)/debian/packetfence.sudoers $(CURDIR)/debian/packetfence/etc/sudoers.d/packetfence
	rm -fr $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/html/pfappserver/root/static/alt
	#PacketFence remote ARP sensor
	install -d -m0700 $(CURDIR)/debian/packetfence-remote-arp-sensor$(PREFIX)/$(NAME)/conf
	install -d $(CURDIR)/debian/packetfence-remote-arp-sensor$(PREFIX)/$(NAME)/var
	install -d $(CURDIR)/debian/packetfence-remote-arp-sensor$(PREFIX)/$(NAME)/var/run
	install -d $(CURDIR)/debian/packetfence-remote-arp-sensor$(PREFIX)/$(NAME)/sbin
	install -m0755 addons/pfarp_remote/sbin/pfarp_remote $(CURDIR)/debian/packetfence-remote-arp-sensor$(PREFIX)/$(NAME)/sbin
	install -m0600 addons/pfarp_remote/conf/pfarp_remote.conf $(CURDIR)/debian/packetfence-remote-arp-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
	# ntlm-wrapper
	install -d -m0755 $(CURDIR)/debian/packetfence-ntlm-wrapper$(PREFIX)/$(NAME)/bin
	make bin/ntlm_auth_wrapper
	mv bin/ntlm_auth_wrapper $(CURDIR)/debian/packetfence-ntlm-wrapper$(PREFIX)/$(NAME)/bin
	# packetfence-config
	install -d $(CURDIR)/debian/packetfence-config$(PREFIX)/$(NAME)/sbin
	install -d -m0700 $(CURDIR)/debian/packetfence-config/lib/systemd/system
	install -d -m0700 $(CURDIR)/debian/packetfence-config$(PREFIX)/$(NAME)/conf
	install -d -m2770 $(CURDIR)/debian/packetfence-config$(PREFIX)/$(NAME)/var/cache/pfconfig
	install -m0755 sbin/pfconfig $(CURDIR)/debian/packetfence-config$(PREFIX)/$(NAME)/sbin
	install -m0600 conf/pfconfig.conf.example $(CURDIR)/debian/packetfence-config$(PREFIX)/$(NAME)/conf/pfconfig.conf
	# packetfence-redis-cache
	install -d -m0700 $(CURDIR)/debian/packetfence-redis-cache/lib/systemd/system
	install -d -m2770 $(CURDIR)/debian/packetfence-redis-cache$(PREFIX)/$(NAME)/var/run
	install -d -m0700 $(CURDIR)/debian/packetfence-redis-cache$(PREFIX)/$(NAME)/conf
	install -d -m2770 $(CURDIR)/debian/packetfence-redis-cache$(PREFIX)/$(NAME)/logs
	install -d -m2770 $(CURDIR)/debian/packetfence-redis-cache$(PREFIX)/$(NAME)/var/redis_cache
	install -m0600 conf/redis_cache.conf.example $(CURDIR)/debian/packetfence-redis-cache$(PREFIX)/$(NAME)/conf/redis_cache.conf

	# packetfence-tracking-config
	install -m0644 $(CURDIR)/debian/packetfence-tracking-config.path $(CURDIR)/debian/packetfence/lib/systemd/system/packetfence-tracking-config.path

	#Portal javascript/css
	rm -f $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/html/common/styles.css
	rm -f $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/html/common/styles.css.map
	make -C html/common/ vendor
	make -C html/common light-dist
	install -d -m0755 $(CURDIR)/debian/packetfence-captive-portal-javascript$(PREFIX)/$(NAME)/html/common
	install -m0644 $(CURDIR)/html/common/styles.css $(CURDIR)/debian/packetfence-captive-portal-javascript$(PREFIX)/$(NAME)/html/common/
	install -m0644 $(CURDIR)/html/common/styles.css.map $(CURDIR)/debian/packetfence-captive-portal-javascript$(PREFIX)/$(NAME)/html/common/
	# Admin javascript/css
	make -C html/pfappserver/root/static/ vendor
	make -C html/pfappserver/root/static/ light-dist
	make -C html/pfappserver/root/static.alt/ vendor
	make -C html/pfappserver/root/static.alt/ light-dist

	# Documentation
	make html
	# install html and images dirs in pfappserver for embedded doc
	install -d -m0755 $(CURDIR)/debian/packetfence-doc$(PREFIX)/$(NAME)/html/pfappserver/root/static/doc
	for i in `find "docs/html" "(" -name "*.html" -or -iname "*.js" ")" -type f`; do \
		install -m0644 $$i $(CURDIR)/debian/packetfence-doc$(PREFIX)/$(NAME)/html/pfappserver/root/static/doc/; \
	done
	# images
	make DESTDIR=$(CURDIR)/debian/packetfence-doc images

	install -d -m0755 $(CURDIR)/debian/packetfence-pfappserver-javascript$(PREFIX)/$(NAME)/html/pfappserver/root/static
	for i in `find * -wholename "*js/pfappserver.*" -o -wholename "*js/reports.*" -o -wholename "*js/auditing.*" -o -wholename "*js/configuration.*" -o -wholename "*js/vendor/bootstrap.min.js*" -o -wholename "*js/vendor/jquery.min.*" -o -wholename "*js/vendor/jquery-ui.min.js*" -o -wholename "*js/vendor/jquery-extra.min.js*" -o -wholename "*js/vendor/raphael.min.js*" -o -wholename "*js/vendor/ace/*.js" -o -wholename "*js/vendor/fitty.min.js*" -o -wholename "*css/styles.css" -o -wholename "*css/styles.css.map" -path 'html/pfappserver/root/static/*' -type f`; do \
		install -D -m0644 $$i $(CURDIR)/debian/packetfence-pfappserver-javascript$(PREFIX)/$(NAME)/$$i; \
	done
	install -d -m0755 $(CURDIR)/debian/packetfence-pfappserver-javascript$(PREFIX)/$(NAME)/html/pfappserver/root/static.alt
	for i in `find 'html/pfappserver/root/static.alt/dist' -type f`; do \
		install -D -m0644 $$i $(CURDIR)/debian/packetfence-pfappserver-javascript$(PREFIX)/$(NAME)/$$i; \
	done
	#Golang binary
	install -d -m0744 $(CURDIR)/debian/packetfence-golang-daemon$(PREFIX)/$(NAME)/sbin
	make -C go all
	make -C go DESTDIR=$(CURDIR)/debian/packetfence-golang-daemon copy


# 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_systemd_enable
	dh_installinit --no-restart-on-upgrade
	dh_installinit --name=packetfence-galera-autofix
	dh_installinit --name=packetfence-haproxy-admin
	dh_installinit --name=packetfence-haproxy-portal
	dh_installinit --name=packetfence-haproxy-db
	dh_installinit --name=packetfence-httpd.aaa
	dh_installinit --name=packetfence-httpd.admin
	dh_installinit --name=packetfence-httpd.collector
	dh_installinit --name=packetfence-httpd.dispatcher
	dh_installinit --name=packetfence-httpd.admin_dispatcher
	dh_installinit --name=packetfence-httpd.portal
	dh_installinit --name=packetfence-httpd.proxy
	dh_installinit --name=packetfence-httpd.webservices
	dh_installinit --name=packetfence-iptables
	dh_installinit --name=packetfence-keepalived
	dh_installinit --name=packetfence-mariadb
	dh_installinit --name=packetfence-netdata
	dh_installinit --name=packetfence-pfbandwidthd
	dh_installinit --name=packetfence-pfacct
	dh_installinit --name=packetfence-pfcertmanager
	dh_installinit --name=packetfence-pfdetect
	dh_installinit --name=packetfence-pfdhcplistener
	dh_installinit --name=packetfence-pfdns
	dh_installinit --name=packetfence-pffilter
	dh_installinit --name=packetfence-pfmon
	dh_installinit --name=packetfence-pfperl-api
	dh_installinit --name=packetfence-pfpki
	dh_installinit --name=packetfence-pfqueue
	dh_installinit --name=packetfence-pfsso
	dh_installinit --name=packetfence-radiusd-acct
	dh_installinit --name=packetfence-radiusd-auth
	dh_installinit --name=packetfence-radiusd-cli
	dh_installinit --name=packetfence-radiusd-eduroam
	dh_installinit --name=packetfence-radiusd-load_balancer
	dh_installinit --name=packetfence-radsniff
	dh_installinit --name=packetfence-redis_ntlm_cache
	dh_installinit --name=packetfence-redis_queue
	dh_installinit --name=packetfence-snmptrapd
	dh_installinit --name=packetfence-tc
	dh_installinit --name=packetfence-winbindd
	dh_installinit --name=packetfence-pfdhcp
	dh_installinit --name=packetfence-pfipset
	dh_installinit --name=packetfence-pfunified-api
	dh_installinit --name=packetfence-pfstats
	dh_installinit --name=packetfence-api-frontend
	dh_installinit --no-start --name=packetfence-tracking-config

	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link \
			/usr/local/pf/raddb/sites-available/dynamic-clients /usr/local/pf/raddb/sites-enabled/dynamic-clients \
			/usr/local/pf/raddb/sites-available/status /usr/local/pf/raddb/sites-enabled/status \
			/usr/local/fingerbank/lib/fingerbank /usr/local/pf/lib/fingerbank \
			/usr/local/pf/html/pfappserver/lib/pfappserver /usr/local/pf/lib/pfappserver \
			/usr/local/pf/html/captive-portal/lib/captiveportal /usr/local/pf/lib/captiveportal \
			/usr/local/pf/html/pfappserver/root/static.alt/dist /usr/local/pf/html/pfappserver/root/static/alt
	dh_strip
	dh_compress
	# PERMISSIONS
	chmod 0750 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/conf
	# 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/pfconfig/comparator/*.pl
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/addons/pfconfig/comparator/*.sh
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/addons/upgrade/*.pl
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/addons/upgrade/*.sh
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/addons/watchdog/*.sh
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/addons/monit/*.pl
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/addons/monit/monitoring-scripts/*.pl
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/addons/monit/monitoring-scripts/*.sh
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/addons/packages/*.sh
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/addons/dev-helpers/*.sh
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/addons/hostapd/*.sh
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/addons/eapol_test/*.sh
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/bin/*
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/bin/cluster/*
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/sbin/*
	chmod 0755 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/html/pfappserver/script/*.pl
	dh_fixperms -X$(NAME)/bin -X$(NAME)/sbin -X$(NAME)/addons \
                -X$(NAME)/conf/pfdetect_remote.conf \
                -X$(NAME)sbin/pfdetect_remote -X$(NAME)/conf/pfarp_remote.conf \
		-X$(NAME)sbin/pfarp_remote
	chmod 2775 $(CURDIR)/debian/packetfence$(PREFIX)/$(NAME)/var/run
	chown root:root $(CURDIR)/debian/packetfence-pfcmd-suid$(PREFIX)/$(NAME)/bin/pfcmd
	chmod ug+s $(CURDIR)/debian/packetfence-pfcmd-suid$(PREFIX)/$(NAME)/bin/pfcmd
	#pfconfig
	chmod 0755 $(CURDIR)/debian/packetfence-config$(PREFIX)/$(NAME)/sbin/*
#	dh_perl
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
ifeq ($(DIST),jessie)
	dh_gencontrol -- '-Vpacketfence:dist=openssl-blacklist,openssl-blacklist-extra,libapache2-mod-proxy-html,apache2-mpm-prefork'
endif
ifeq ($(DIST),stretch)
	dh_gencontrol -- '-Vpacketfence:dist=libclass-xsaccessor-perl,acl'
endif
	dh_md5sums
	dh_builddeb

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