#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

SHOREWALLRC = $(CURDIR)/debian/shorewallrc

SRCDIR_SRWL       = $(CURDIR)/shorewall
SRCDIR_SRWL6      = $(CURDIR)/shorewall6
SRCDIR_SRWL6LITE  = $(CURDIR)/shorewall6-lite
SRCDIR_SRWLCORE   = $(CURDIR)/shorewall-core
SRCDIR_SRWLDOC    = $(CURDIR)/shorewall-docs-xml
SRCDIR_SRWLINIT   = $(CURDIR)/shorewall-init
SRCDIR_SRWLLITE   = $(CURDIR)/shorewall-lite
DESTDIR_SRWL      = $(CURDIR)/debian/shorewall
DESTDIR_SRWL6     = $(CURDIR)/debian/shorewall6
DESTDIR_SRWL6LITE = $(CURDIR)/debian/shorewall6-lite
DESTDIR_SRWLCORE  = $(CURDIR)/debian/shorewall-core
DESTDIR_SRWLDOC   = $(CURDIR)/debian/shorewall-doc
DESTDIR_SRWLINIT  = $(CURDIR)/debian/shorewall-init
DESTDIR_SRWLLITE  = $(CURDIR)/debian/shorewall-lite

%:
	dh $@

ifeq ($(filter nodoc,$(DEB_BUILD_OPTIONS)),)
override_dh_auto_build:
	sh debian/build-htmldocs
endif

ifeq ($(filter nodoc,$(DEB_BUILD_OPTIONS)),)
override_dh_auto_clean:
	rm -fr shorewall-docs-xml/html
	rm -fr shorewall-docs-xml/manpages/*.html
endif

override_dh_install: install_shorewall       \
                     install_shorewall6      \
                     install_shorewall6_lite \
                     install_shorewall_core  \
                     install_shorewall_doc   \
                     install_shorewall_init  \
                     install_shorewall_lite

install_shorewall:
	DESTDIR=$(DESTDIR_SRWL) $(SRCDIR_SRWL)/install.sh $(SHOREWALLRC)
	rmdir $(DESTDIR_SRWL)/sbin
	rmdir $(DESTDIR_SRWL)/usr/share/shorewall/deprecated

install_shorewall6:
	DESTDIR=$(DESTDIR_SRWL6) $(SRCDIR_SRWL6)/install.sh $(SHOREWALLRC)
	rmdir $(DESTDIR_SRWL6)/usr/share/shorewall/Shorewall
	rmdir $(DESTDIR_SRWL6)/usr/share/shorewall
	rmdir $(DESTDIR_SRWL6)/usr/share/shorewall6/deprecated/

install_shorewall6_lite:
	DESTDIR=$(DESTDIR_SRWL6LITE) $(SRCDIR_SRWL6LITE)/install.sh $(SHOREWALLRC)

install_shorewall_core:
	DESTDIR=$(DESTDIR_SRWLCORE) $(SRCDIR_SRWLCORE)/install.sh $(SHOREWALLRC)
	find $(DESTDIR_SRWLCORE) -type d -empty -delete

install_shorewall_doc:
ifeq ($(filter nodoc,$(DEB_BUILD_OPTIONS)),)
	install -m0755 -d $(DESTDIR_SRWLDOC)/usr/share/doc/shorewall-doc/html
	install -m0755 -d $(DESTDIR_SRWLDOC)/usr/share/doc/shorewall-doc/html/images
	install -m0755 -d $(DESTDIR_SRWLDOC)/usr/share/doc/shorewall-doc/manpages
	install $(SRCDIR_SRWLDOC)/html/*     $(DESTDIR_SRWLDOC)/usr/share/doc/shorewall-doc/html
	install $(SRCDIR_SRWLDOC)/images/*   $(DESTDIR_SRWLDOC)/usr/share/doc/shorewall-doc/html/images
	install $(SRCDIR_SRWLDOC)/manpages/* $(DESTDIR_SRWLDOC)/usr/share/doc/shorewall-doc/manpages
endif

install_shorewall_init:
	DESTDIR=$(DESTDIR_SRWLINIT) $(SRCDIR_SRWLINIT)/install.sh $(SHOREWALLRC)

install_shorewall_lite:
	DESTDIR=$(DESTDIR_SRWLLITE) $(SRCDIR_SRWLLITE)/install.sh $(SHOREWALLRC)

override_dh_installinit:
	dh_installinit -pshorewall       --only-scripts --no-enable --no-start
	dh_installinit -pshorewall6      --only-scripts --no-enable --no-start
	dh_installinit -pshorewall6-lite --only-scripts --no-enable --no-start
	dh_installinit -pshorewall-init  --only-scripts --no-enable --no-start
	dh_installinit -pshorewall-lite  --only-scripts --no-enable --no-start

override_dh_installsystemd:
	dh_installsystemd -pshorewall       --no-stop-on-upgrade --no-enable --no-start
	dh_installsystemd -pshorewall6      --no-stop-on-upgrade --no-enable --no-start
	dh_installsystemd -pshorewall6-lite --no-stop-on-upgrade --no-enable --no-start
	dh_installsystemd -pshorewall-init  --no-stop-on-upgrade --no-enable --no-start
	dh_installsystemd -pshorewall-lite  --no-stop-on-upgrade --no-enable --no-start

override_dh_fixperms: fixperms_shorewall       \
                      fixperms_shorewall6      \
                      fixperms_shorewall6_lite \
                      fixperms_shorewall_lite
	dh_fixperms -pshorewall                \
	            -Xetc/shorewall            \
	            -Xvar/lib/shorewall
	dh_fixperms -pshorewall6               \
	            -Xetc/shorewall6           \
	            -Xvar/lib/shorewall6
	dh_fixperms -pshorewall6-lite          \
	            -Xetc/shorewall6-lite      \
	            -Xvar/lib/shorewall6-lite
	dh_fixperms -pshorewall-core
ifeq ($(filter nodoc,$(DEB_BUILD_OPTIONS)),)
	dh_fixperms -pshorewall-doc
endif
	dh_fixperms -pshorewall-init
	dh_fixperms -pshorewall-lite           \
	            -Xetc/shorewall-lite       \
	            -Xvar/lib/shorewall-lite

fixperms_shorewall:
	# Temporary zones are only available to root
	chmod 750 $(DESTDIR_SRWL)/var/lib/shorewall
	# By default, configuration is not world-readable
	chmod 640 $(DESTDIR_SRWL)/etc/shorewall/*
	# These should not be executable
	chmod 644 $(DESTDIR_SRWL)/etc/shorewall/shorewall.conf
	if [ -f $(DESTDIR_SRWL)/etc/shorewall/Makefile ] ; then chmod 644 $(DESTDIR_SRWL)/etc/shorewall/Makefile ; fi
	# Global configuration has to be fully readable
	chmod 644 $(DESTDIR_SRWL)/usr/share/shorewall/*
	chmod 755 $(DESTDIR_SRWL)/usr/share/shorewall/compiler.pl
	chmod 755 $(DESTDIR_SRWL)/usr/share/shorewall/getparams
	chmod 755 $(DESTDIR_SRWL)/usr/share/shorewall/Shorewall/
	chmod 755 $(DESTDIR_SRWL)/usr/share/shorewall/configfiles/

fixperms_shorewall6:
	# Temporary zones are only available to root
	chmod 750 $(DESTDIR_SRWL6)/var/lib/shorewall6
	# By default, configuration is not world-readable
	chmod 640 $(DESTDIR_SRWL6)/etc/shorewall6/*
	# These should not be executable
	chmod 644 $(DESTDIR_SRWL6)/etc/shorewall6/shorewall6.conf
	if [ -f $(DESTDIR_SRWL6)/etc/shorewall6/Makefile ] ; then chmod 644 $(DESTDIR_SRWL6)/etc/shorewall6/Makefile ; fi
	# Global configuration has to be fully readable
	chmod 644 $(DESTDIR_SRWL6)/usr/share/shorewall6/*
	chmod 755 $(DESTDIR_SRWL6)/usr/share/shorewall6/configfiles/

fixperms_shorewall6_lite:
	# Temporary zones are only available to root
	chmod 750 $(DESTDIR_SRWL6LITE)/var/lib/shorewall6-lite
	# These should not be executable
	chmod 644 $(DESTDIR_SRWL6LITE)/etc/shorewall6-lite/shorewall6-lite.conf
	# Global configuration has to be fully readable
	chmod 644 $(DESTDIR_SRWL6LITE)/usr/share/shorewall6-lite/*
	# Must be executable
	chmod 755 $(DESTDIR_SRWL6LITE)/usr/share/shorewall6-lite/shorecap

fixperms_shorewall_lite:
	# Temporary zones are only available to root
	chmod 750 $(DESTDIR_SRWLLITE)/var/lib/shorewall-lite
	# These should not be executable
	chmod 644 $(DESTDIR_SRWLLITE)/etc/shorewall-lite/shorewall-lite.conf
	# Global configuration has to be fully readable
	chmod 644 $(DESTDIR_SRWLLITE)/usr/share/shorewall-lite/*
	# Must be executable
	chmod 755 $(DESTDIR_SRWLLITE)/usr/share/shorewall-lite/shorecap

.PHONY: install_shorewall        \
        install_shorewall6       \
        install_shorewall6_lite  \
        install_shorewall_core   \
        install_shorewall_doc    \
        install_shorewall_init   \
        fixperms_shorewall       \
        fixperms_shorewall6      \
        fixperms_shorewall6_lite \
        fixperms_shorewall_lite
