#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

#python_share_dir = /usr/share/check_mk
#export PYBUILD_INSTALL_ARGS ?= \
#        --install-lib=${package_share_dir}/ \
#        --install-scripts=${package_share_dir}/



configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.
	mkdir agents
	tar -xzf agents.tar.gz -C agents
	mkdir docs
	tar -xzf doc.tar.gz -C docs

	touch configure-stamp


build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp

build-stamp: patch-stamp configure-stamp
	dh_testdir

	# Add here commands to compile the package.
	#cd agents ; $(MAKE)
	#docbook-to-man debian/check-mk.sgml > check-mk.1

	touch $@

clean: clean-patched unpatch
clean-patched:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	# Add here commands to clean up after the build process.
	rm -rf agents docs config livestatus.src livestatus.log

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	dh_installdebconf
	mkdir -p debian/tmp
	DESTDIR=debian/tmp python_lib_dir=/usr/share/check_mk/ ./setup.sh --yes
	## clean up binaries shipped by upstream
	# upstream agent deb and rpm
	rm -rf debian/tmp/usr/share/check_mk/agent/check-mk-agent*.deb
	rm -rf debian/tmp/usr/share/check_mk/agent/check-mk-agent*.rpm
	# java jar and classes (we don't rebuild these, help if you want them)
	rm -rf debian/tmp/usr/share/doc/check_mk/treasures/jasperreports
	# Windows binaries related (we don't rebuild these, help if you want them)
	rm -rf debian/tmp/usr/share/check_mk/agents/windows
	rm -rf debian/tmp/usr/share/doc/check_mk/windows
	rm -rf debian/tmp/usr/share/doc/check_mk/checks/windows_*
	rm -rf debian/tmp/usr/share/check_mk/checks/windows_*
	rm -rf debian/tmp/usr/share/doc/check_mk/treasures/windows_msi
	# remove deprecated stuff
	rm -rf debian/tmp/usr/share/doc/check_mk/treasures/deprecated
	# remove binary selinux policy file
	rm -rf debian/tmp/usr/share/doc/check_mk/treasures/selinux/omd.pp
	# remove webapps mkp package file and dir
	rm -rf debian/tmp/usr/share/doc/check_mk/treasures/webapp
	# remove sap agent with source tarball
	rm -rf debian/tmp/usr/share/check_mk/agents/sap
	# remove binary agent_modbus
	rm debian/tmp/usr/share/doc/check_mk/treasures/modbus/agent_modbus
	# remove waitmax binary, check_mk will use timeout if available
	rm debian/tmp/usr/share/check_mk/agents/waitmax
	rm debian/tmp/usr/share/check_mk/agents/z_os/waitmax
	# copy the livestatus stuff that setup.sh generated in it's own dir
	cp -a livestatus.src/debian/tmp/* debian/tmp/
	## config files
	# We need a "paths.py" file with edited paths for FHS and monitoring
	#  system changes
	# remove the provided one
	rm debian/tmp/usr/share/check_mk/cmk/paths.py
	cp debian/paths.py.icinga debian/tmp/usr/share/check_mk/cmk/paths.py
	# provide a safe apache config that references icinga
	mkdir -p debian/check-mk-config-icinga/etc/apache2/conf-available/
	cp debian/apache.icinga debian/check-mk-config-icinga/etc/apache2/conf-available/check-mk-multisite.conf
	# Adjust path names
	# sed -i 's#/nagios/cgi-bin/#/cgi-bin/icinga/#' debian/tmp/check-mk-config-icinga/etc/icinga/objects/check_mk_templates.cfg
	# Prepare agent files
	cp debian/tmp/usr/share/check_mk/agents/check_mk_agent.linux debian/check-mk-agent/usr/bin/check_mk_agent
	chmod +x debian/check-mk-agent/usr/bin/check_mk_agent
	# mkp wrapper script
	mkdir -p debian/check-mk-server/usr/bin
	cp debian/tmp/usr/bin/mkp debian/check-mk-server/usr/bin/
	chmod +x debian/check-mk-server/usr/bin/mkp
	# xinetd: provide config, but disabled by default
	cp debian/tmp/usr/share/check_mk/agents/cfg_examples/xinetd.conf debian/check-mk-agent/etc/xinetd.d/check_mk
	sed -i 's#disable\s*=\s*no#disable        = yes#' debian/check-mk-agent/etc/xinetd.d/check_mk
	# move checks manpages (to be installed in -server)
	mv debian/tmp/usr/share/doc/check_mk/checks debian/tmp/usr/share/check_mk/checks-man
	# remove installed ChangeLog to avoid duplicate
	rm debian/tmp/usr/share/doc/check_mk/ChangeLog
	# remove installed COPYING, redundant
	rm debian/tmp/usr/share/doc/check_mk/COPYING

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i
	dh_install -i
	dh_installdocs -i debian/README.Debian
	dh_installchangelogs -i ChangeLog
	dh_installexamples -i
	dh_install -i
	dh_python2 /usr/share/check_mk
	dh_installman -i
	dh_link -i
	dh_strip -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a ChangeLog
	dh_installdocs -a debian/README.Debian
	dh_installexamples -a
	dh_install -a
	chmod +x debian/check-mk-server/usr/share/check_mk/checks/*
	sed -i -e 's/nagiosadmin/icingaadmin/g' debian/check-mk-config-icinga/etc/check_mk/multisite.mk
#	dh_installmenu
#	dh_installdebconf
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
#	dh_perl
#	dh_makeshlibs
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

patch: patch-stamp
patch-stamp:
	@echo "Doing $@"
	dpatch  --with-cpp apply-all
	dpatch  --with-cpp cat-all >patch-stamp

unpatch:
	@echo "Doing $@"
	dpatch --with-cpp deapply-all
	rm -rf patch-stamp debian/patched

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