#!/usr/bin/make -f

export DH_COMPAT=3

build:

	# Nothing to do

clean:

	dh_testdir
	dh_testroot
	dh_clean

	rm -f debian/rcf.init
	rm -f debian/changelog

install:

	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Create config file
	install --mode=700 /dev/null debian/rcf/etc/firewall.conf

	# Install functions
	install --mode=700 -d debian/rcf/etc/firewall/functions
	tar --exclude=CVS -cf - etc/firewall/functions | tar xf - -C debian/rcf

	# Install groups
	install --mode=700 -d debian/rcf/etc/firewall/groups
	tar --exclude=CVS -cf - etc/firewall/groups | tar xf - -C debian/rcf

	# Install modules
	install --mode=700 -d debian/rcf/etc/firewall/modules
	tar --exclude=CVS -cf - etc/firewall/modules | tar xf - -C debian/rcf

	# Install firewall script
	install -c --mode=755 fwinit debian/rcf.init

	# Create directories
	$(MAKE) --directory=debian/rcf --makefile=../../Makefile dir
	install -d debian/rcf/usr/share/rcf
	install -d -m 0700 -o root debian/rcf/usr/share/rcf/modules
	install -d -m 0600 -o root debian/rcf/usr/share/rcf/groups
	install -d -m 0600 -o root debian/rcf/usr/share/rcf/functions

	# Create links
	$(MAKE) --directory=debian/rcf --makefile=../../Makefile link

	# Remove unnecessary directories
	rm -rf debian/rcf/etc/rc.d

	# Fix permissions
	#chmod -R 0700 debian/rcf/etc/firewall/modules
	#chown -R 0:0 debian/rcf/etc/firewall/modules
	#chmod 0600 debian/rcf/etc/firewall/functions/*
	#chown 0:0 debian/rcf/etc/firewall/functions/*

	# Install RCF files in policy-compliant directories
	for dir in `ls -A debian/rcf/etc/firewall/modules`; \
	do \
		install -d -m 0700 -o root debian/rcf/etc/firewall/modules/$$dir debian/rcf/usr/share/rcf/modules/$$dir; \
		for subdir in `ls -A debian/rcf/etc/firewall/modules/$$dir`; \
		do \
			install -d -m 0700 -o root debian/rcf/etc/firewall/modules/$$dir/$$subdir debian/rcf/usr/share/rcf/modules/$$dir/$$subdir; \
			for file in `ls -A debian/rcf/etc/firewall/modules/$$dir/$$subdir`; \
			do \
				install -m 0700 -o root debian/rcf/etc/firewall/modules/$$dir/$$subdir/$$file debian/rcf/usr/share/rcf/modules/$$dir/$$subdir/$$file; \
			done; \
		done; \
	done
	install -m 0600 -o root debian/rcf/etc/firewall/groups/* debian/rcf/usr/share/rcf/groups
	install -m 0600 -o root debian/rcf/etc/firewall/functions/* debian/rcf/usr/share/rcf/functions

	# Remove obsolete files
	rm -rf debian/rcf/etc/firewall

	# Install RCF
	install -c --mode=700 --owner=root sbin/rcf debian/rcf/sbin

binary-indep: install

	dh_testdir
	dh_testroot
	dh_installdocs --noscripts
	dh_installinit --init-script=firewall --noscripts
	dh_installman usr/man/firewall.conf.5 usr/man/rcf-groups.5 usr/man/rcf-modules.5 usr/man/rcf.8
	dh_installchangelogs ChangeLog
	dh_compress
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-arch:

	# Nothing to do

binary: binary-indep

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

