#!/usr/bin/make -f

CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

Makefile: configure
	dh_testdir
	# Add here commands to configure the package.
	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

build: build-stamp
build-stamp: Makefile
	dh_testdir

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

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp 
	[ ! -f Makefile ] || $(MAKE) distclean
	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 . && \
		mv menu ../../../../usr/share/xymon && ln -s ../../../../usr/share/xymon/menu .
	# 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
	# Make xymonserver.cfg use the settings we have configured during installation
	echo "include /etc/default/xymon-client" > $S/etc/xymon/xymonserver.cfg.debian
	sed -f debian/xymonserver-setup.sed $S/etc/xymon/xymonserver.cfg >> $S/etc/xymon/xymonserver.cfg.debian
	rm $S/etc/xymon/xymonserver.cfg
	mv $S/etc/xymon/xymonserver.cfg.debian $S/etc/xymon/xymonserver.cfg
	# Autogenerated on first install
	rm $S/etc/xymon/hosts.cfg

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
	# but install xymon also, since it is used so much.
	cd $C/usr/bin && ln -s ../lib/xymon/client/bin/xymoncmd xymoncmd
	cd $C/usr/bin && ln -s ../lib/xymon/client/bin/xymon xymon
	cp debian/xymon-client.default.dist $C/usr/share/xymon/xymon-client.default
	# 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

binary: binary-arch binary-indep

#binary-indep:
#	#################### Building dummy packages ########################
#	dh_testdir -i
#	dh_installchangelogs -i
#	dh_installdocs -i
#	dh_compress -i
#	dh_gencontrol -i
#	dh_builddeb -i

binary-arch: build install-server install-client
	#################### Building .deb files ########################
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a Changes
	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
	# use the old file names for now
	dh_installlogrotate --name=xymon-client -a
	dh_installinit --name=xymon -p'xymon' -- defaults 98 02
	dh_installinit --name=xymon-client -p'xymon-client' -- defaults 98 02
	dh_installman -a
	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
