#!/usr/bin/make -f

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

Makefile: configure
	dh_testdir
	# Add here commands to configure the package.
	dh_autotools-dev_updateconfig
	USEXYMONPING=y \
	ENABLESSL=y \
	ENABLELDAP=y \
	ENABLELDAPSSL=y \
	XYMONUSER=xymon \
	XYMONTOPDIR=/usr/lib/xymon \
	XYMONVAR=/var/lib/xymon \
	XYMONHOSTURL=/xymon \
	CGIDIR=/usr/lib/xymon/cgi-bin \
	XYMONCGIURL=/xymon-cgi \
	SECURECGIDIR=/usr/lib/xymon/cgi-secure \
	SECUREXYMONCGIURL=/xymon-seccgi \
	HTTPDGID=www-data \
	XYMONLOGDIR=/var/log/xymon \
	XYMONHOSTNAME=localhost \
	XYMONHOSTIP=127.0.0.1 \
	MANROOT=/usr/share/man \
	INSTALLBINDIR=/usr/lib/xymon/server/bin \
	INSTALLETCDIR=/etc/xymon \
	INSTALLWEBDIR=/etc/xymon/web \
	INSTALLEXTDIR=/usr/lib/xymon/server/ext \
	INSTALLTMPDIR=/var/lib/xymon/tmp \
	INSTALLWWWDIR=/var/lib/xymon/www \
	./configure --server \
	--pcrelib $(shell pcre-config --libs) \
	--ssllib /usr/lib/$(DEB_HOST_MULTIARCH) \
	--ldaplib /usr/lib/$(DEB_HOST_MULTIARCH)

build-indep:

build build-arch: build-stamp
build-stamp: Makefile
	dh_testdir

	# Parallel building does not work as of 4.3.0~beta2
	PKGBUILD=1 $(MAKE) MAKEFLAGS=-j1

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) distclean
	dh_autotools-dev_restoreconfig
	dh_clean 
	debconf-updatepo

S=$(CURDIR)/debian/xymon
C=$(CURDIR)/debian/xymon-client

install-clean:
	dh_prep

install: install-server install-client
install-server: build install-clean
	#################### Installing server ########################
	dh_testdir
	dh_testroot
	dh_install -a
	dh_installdirs -a
	PKGBUILD=1 INSTALLROOT=$S/ $(MAKE) install
	# Static content in /usr/share
	cd $S/var/lib/xymon/www && \
		mv gifs ../../../../usr/share/xymon && ln -s ../../../../usr/share/xymon/gifs . && \
		mv help ../../../../usr/share/xymon && ln -s ../../../../usr/share/xymon/help .
	# Create static gifs
	cd $S/usr/share/xymon/gifs && mkdir static && \
		for gif in *.gif ; do \
			convert "$$gif[0]" xpm:- | convert xpm:- static/$$gif || exit 1 ; \
		done && \
		cd static && ln -s ../*.ico ../*.css .
	# We depend on the -client package
	rm -rf $S/usr/lib/xymon/client
	# This needs root
	chmod 4755 $S/usr/lib/xymon/server/bin/xymonping
	mv $S/etc/xymon/xymon-apache.conf \
		$S/etc/apache2/conf.d/xymon
	# We use a modified version of this as /etc/init.d/xymon
	rm $S/usr/lib/xymon/server/xymon.sh
	# Autogenerated on first install
	rm $S/etc/xymon/hosts.cfg
	# Fix permissions in /var/lib/xymon (dh_fixperms ignores /var)
	chmod a-x $S/var/lib/xymon/www/menu/*.gif $S/var/lib/xymon/www/menu/*.css

install-client: build install-clean
	#################### Installing client ########################
	dh_testdir
	dh_testroot
	dh_install -a
	dh_installdirs -a
	PKGBUILD=1 INSTALLROOT=$C/ $(MAKE) install-client
	cd $C/usr/lib/xymon/client && mv etc/* $C/etc/xymon && rmdir etc && ln -s ../../../../etc/xymon etc
	cd $C/usr/lib/xymon/client && rmdir logs && ln -s ../../../../var/log/xymon logs
	cd $C/usr/lib/xymon/client && rmdir tmp && ln -s ../../../../var/lib/xymon/tmp
	# the only command needed in /usr/bin is xymoncmd, its PATH includes our private .../bin
	cd $C/usr/bin && ln -s ../lib/xymon/client/bin/xymoncmd xymoncmd
	cp debian/xymon-client.default.template $C/usr/share/xymon/xymon-client.default.template
	# dynamic list of installed client extensions
	echo "include /var/run/xymon/clientlaunch-include.cfg" >> \
		$C/etc/xymon/clientlaunch.cfg
	rm $C/usr/lib/xymon/client/runclient.sh
ifneq (,$(findstring kfreebsd,$(shell dpkg --print-architecture)))
	# kfreebsd support
	install -m644 debian/kfreebsd.cfg $C/etc/xymon/xymonclient.d/kfreebsd.cfg
else
	# no kfreebsd support
endif

binary-indep:

binary binary-arch: build install-server install-client
	#################### Building .deb files ########################
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a
	dh_installdocs -a
	dh_installexamples -a
	# ignore missing dh_lintian for older dh versions
	-dh_lintian -a
	# move some files into the client package
	dh_movefiles --sourcedir=debian/xymon -a
	cd $S/usr/lib/xymon/server/bin && \
		for f in * ; do \
			if [ -f $C/usr/lib/xymon/client/bin/$$f ] ; then \
				rm -v $$f ; ln -s ../../client/bin/$$f ; \
			fi \
		done
	rmdir $S/usr/share/man/man7
	dh_installdebconf -a
	dh_installlogrotate -a
	dh_installinit -a -- defaults 98 02
	dh_installman -a
	#cd $C/usr/share/man/man1 && mv bb.1 xymon.1
	#cd $C/usr/share/man/man8 && ln -s ../man1/xymon.1 bb.8
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a -Xbin/xymonping
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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