#!/usr/bin/make -f

# enable verbose mode
export DH_VERBOSE=1

# enable all hardening build flags
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

%:
	dh $@ --parallel --with autoreconf,systemd

override_dh_clean:
	dh_clean
	find . -name Makefile.in -delete

override_dh_auto_configure:
	dh_auto_configure -- \
		--disable-dependency-tracking \
		--libdir=/usr/lib/lighttpd \
		--libexecdir="/usr/lib/lighttpd" \
		--with-attr \
		--with-dbi \
		--with-fam \
		--with-gdbm \
		--with-krb5 \
		--with-ldap \
		--with-geoip \
		--with-memcached \
		--with-lua=lua5.1 \
		--with-maxminddb \
		--with-mysql \
		--with-openssl \
		--with-pam \
		--with-pcre \
		--with-pgsql \
		--with-sasl \
		--with-webdav-locks \
		--with-webdav-props \
		$(if $(filter pkg.lighttpd.libunwind,$(DEB_BUILD_PROFILES)),--with-libunwind) \
		CFLAGS_FOR_BUILD="$(shell dpkg-buildflags --get CFLAGS)" \
		LDFLAGS_FOR_BUILD="$(shell dpkg-buildflags --get LDFLAGS)" \
		CPPFLAGS_FOR_BUILD="$(shell dpkg-buildflags --get CPPFLAGS)" \

override_dh_install:
	dh_install
	install -m644 debian/lighttpd.ufw.profile debian/lighttpd/etc/ufw/applications.d/lighttpd

override_dh_missing:
	dh_missing --fail-missing

DOCLESS_PACKAGES=\
	lighttpd-modules-ldap \
	lighttpd-modules-mysql \
	lighttpd-mod-authn-pam \
	lighttpd-mod-authn-sasl \
	lighttpd-mod-maxminddb \
	lighttpd-mod-vhostdb-dbi \
	lighttpd-mod-vhostdb-pgsql \

override_dh_installdocs:
	dh_installdocs $(foreach p,$(DOCLESS_PACKAGES),-p$(p)) --link-doc=lighttpd
	dh_installdocs $(foreach p,$(DOCLESS_PACKAGES),-N$(p))

override_dh_fixperms-arch:
	dh_fixperms
	chmod 0750 debian/lighttpd/var/log/lighttpd
	chown www-data:www-data \
			debian/lighttpd/var/cache/lighttpd \
			debian/lighttpd/var/cache/lighttpd/compress \
			debian/lighttpd/var/cache/lighttpd/uploads \
			debian/lighttpd/var/log/lighttpd

override_dh_installinit:
	dh_installinit --error-handler=true -- defaults 91 09

override_dh_gencontrol:
	set -e; for p in `dh_listpackages`; do \
		test -d debian/$$p/usr/lib/lighttpd || continue; \
		m=`ls debian/$$p/usr/lib/lighttpd | sed 'y/_/-/;s/^mod-\(.*\)\.so$$/lighttpd-mod-\1,/;ta;d;:a /^'"$$p"',$$/d;' | xargs`; \
		echo "lighttpd:ModuleProvides=$$m" >> debian/$$p.substvars; \
	done
	dh_gencontrol
