#!/usr/bin/make -f

b := $(shell pwd)/debian

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DPKG_BUILD_CFLAGS = $(shell DEB_BUILD_MAINT_OPTIONS=hardening=+all dpkg-buildflags --get CFLAGS)
DPKG_BUILD_CPPFLAGS = $(shell DEB_BUILD_MAINT_OPTIONS=hardening=+all dpkg-buildflags --get CPPFLAGS)
DPKG_BUILD_LDFLAGS = $(shell DEB_BUILD_MAINT_OPTIONS=hardening=+all dpkg-buildflags --get LDFLAGS)

SHELL = /bin/sh -e

clean:
	dh_testdir -a
	if test -f Makefile; then make distclean; fi
	find sources.orig -type f | while read f; do rm "$${f#sources.orig/}" && mv "$${f}" "$${f##sources.orig/}"; done
	rm -rf sources.orig
	rm -f keep install-stamp configure-stamp build-stamp
	find -type d -name autom4te.cache -print0 | xargs -0 -r rm -rf \;
	find -type d -name build -print0 | xargs -0 -r rm -rf \;
	rm -f html/index.php lib/snprintf.h t/Makefile
	dh_clean

configure: configure-stamp
configure-stamp:
	dh_testdir -a
	@echo "Doing $@"
	if test -f Makefile; then make clean; fi
	for f in ./config.sub ./config.guess; do [ -f "sources.orig/$${f}" ] || { mkdir -p "sources.orig/$${f%/*}" && mv "$${f}" "sources.orig/$${f}"; }; done
	ln -sf /usr/share/misc/config.sub .
	ln -sf /usr/share/misc/config.guess .
	rm -f config.cache
	CFLAGS='$(DPKG_BUILD_CFLAGS) $(DPKG_BUILD_CPPFLAGS)' LDFLAGS='$(DPKG_BUILD_LDFLAGS)' \
		./configure --prefix=/usr \
		--mandir=/usr/share/man \
		--bindir=/usr/sbin \
		--sbindir=/usr/lib/cgi-bin/nagios4 \
		--datadir=/usr/share/nagios4/htdocs \
		--sysconfdir=/etc/nagios4 \
		--infodir=/usr/share/info \
		--libexecdir=/usr/lib/nagios/plugins \
		--localstatedir=/var/lib/nagios4 \
		--enable-event-broker \
		--with-nagios-user=nagios \
		--with-nagios-group=nagios \
		--with-command-user=nagios \
		--with-command-group=nagios \
		--with-init-dir=/etc/init.d \
		--with-lockfile=/var/run/nagios4/nagios4.pid \
		--with-mail=/usr/bin/mail \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--host=$(DEB_HOST_GNU_TYPE) \
		--with-htmurl=/nagios4 \
		--with-cgiurl=/cgi-bin/nagios4
	touch configure-stamp

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: configure-stamp
	dh_testdir -a
	@echo "Doing $@"
	$(MAKE) all
	make -C contrib
	touch build-arch-stamp

install: install-stamp
install-stamp: build-stamp
	@echo "Doing $@"
	dh_testdir -a
	dh_testroot -a
	dh_installdirs -a -i
	# zero out INSTALL_OPTS to prevent chowning at build-time
	make "DESTDIR=$(b)/tmp" COMMAND_OPTS= INSTALL_OPTS= install-unstripped
	# move some arch-indep stuff to the other packages
	mv debian/tmp/usr/sbin/nagios "$(b)/nagios4-core/usr/sbin/nagios4"
	mv debian/tmp/usr/sbin/nagiostats "$(b)/nagios4-core/usr/sbin/nagios4stats"
	dh_link		     -i
	dh_lintian
	dh_installdocs       -i
	dh_installman        -a -pnagios4
	dh_installlogrotate
	dh_installchangelogs -i
	dh_install           -i
	dh_installinit 	     --name nagios4 -- defaults 30 18
	dh_installexamples
	touch install-stamp

binary: binary-arch binary-indep
binary-arch: install-stamp
	@echo "Doing $@"
	dh_testdir           -a
	dh_testroot          -a
	dh_link		     -a
	dh_installchangelogs -a
	dh_installdocs       -a
	dh_installdebconf    -a 
	dh_install           -a
	chmod 600 ${b}/nagios4-common/etc/nagios4/resource.cfg
	dh_strip             -a  --dbg-package=nagios4-dbg
	dh_compress          -a -X.php
	dh_fixperms          -a
	dh_makeshlibs        -a -V
	dh_installdeb        -a
	dh_perl		     -a
	dh_shlibdeps         -a
	dh_gencontrol        -a
	dh_md5sums           -a
	dh_builddeb          -a

binary-indep: install-stamp
	dh_link		     -i
	dh_compress          -i
	dh_fixperms          -i -Xnagios4/resource.cfg
	dh_installdebconf    -i
	dh_perl		     -i
	dh_installdeb        -i
	dh_gencontrol        -i
	dh_md5sums           -i
	dh_builddeb          -i

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