#!/usr/bin/make -f

# The Zabbix sources are compiled once for each VARIANT. The proxy and server
# packages always contain one kind of database support. The zabbix-agent and
# zabbix-frontend-php package do not have any kind of database support and
# are created from one of the above mentioned builds.

# extract
#
#     Copies the source directory into debian/tmp... to compile the
#     files there.
#
# patch
#
#     Apply Quilt patches.
#
# prepare
#
#     Magic autotools stuff (replacing config.auto and config.guess with
#     up-to-date versions from the autotools-dev package. And some black
#     magic so that our patch to remove -lcrypto is actually doing something.
#     
# configure
#
#     Configure the copied source tree for each database backend flavor.
#
# build
#
#     Compile the actual sources for each database backend flavor.
#
# Enable this export to get more detailed information about what debhelper does
#export DH_VERBOSE=1

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

## Include the 'quilt' patch manager using debian/patches
include /usr/share/quilt/quilt.make

## Define the names of the resulting binary packages
PKG_AGENT = zabbix-agent
PKG_GET = zabbix-get
PKG_SENDER = zabbix-sender
PKG_SERVER_MYSQL = zabbix-server-mysql
PKG_SERVER_PGSQL = zabbix-server-pgsql
PKG_SERVER_SQLITE3 = zabbix-server-sqlite3
PKG_FRONTEND = zabbix-frontend-php
PKG_PROXY_MYSQL = zabbix-proxy-mysql
PKG_PROXY_PGSQL = zabbix-proxy-pgsql
PKG_PROXY_SQLITE3 = zabbix-proxy-sqlite3
PKG_JAVA_GATEWAY = zabbix-java-gateway

# Define the directories where the binary packages' files are assembled
TMP_AGENT = $(CURDIR)/debian/$(PKG_AGENT)
TMP_GET = $(CURDIR)/debian/$(PKG_GET)
TMP_SENDER = $(CURDIR)/debian/$(PKG_SENDER)
TMP_SERVER_MYSQL = $(CURDIR)/debian/$(PKG_SERVER_MYSQL)
TMP_SERVER_PGSQL = $(CURDIR)/debian/$(PKG_SERVER_PGSQL)
TMP_SERVER_SQLITE3 = $(CURDIR)/debian/$(PKG_SERVER_SQLITE3)
TMP_FRONTEND = $(CURDIR)/debian/$(PKG_FRONTEND)
TMP_PROXY_MYSQL = $(CURDIR)/debian/$(PKG_PROXY_MYSQL)
TMP_PROXY_PGSQL = $(CURDIR)/debian/$(PKG_PROXY_PGSQL)
TMP_PROXY_SQLITE3 = $(CURDIR)/debian/$(PKG_PROXY_SQLITE3)
TMP_JAVA_GATEWAY = $(CURDIR)/debian/$(PKG_JAVA_GATEWAY)

# Deprecated: formerly this script didn't use dh_install
INSTALL = install
INSTALL_FILE    = $(INSTALL) -p    -oroot -groot -m644
INSTALL_PROGRAM = $(INSTALL) -p    -oroot -groot -m755
INSTALL_SCRIPT  = $(INSTALL) -p    -oroot -groot -m755
INSTALL_DIR     = $(INSTALL) -p -d -oroot -groot -m755

CFLAGS = -Wall -g

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

# Zabbix server and proxy packages will be built for these database backends
VARIANTS := PGSQL MYSQL SQLITE3

CONFFLAGS_COMMON  = --host=$(DEB_HOST_GNU_TYPE) \
                    --build=$(DEB_BUILD_GNU_TYPE) \
                    --prefix=/usr \
                    --mandir=\$${prefix}/share/man \
                    --infodir=\$${prefix}/share/info \
                    --sysconfdir=/etc/zabbix \
                    --enable-server \
                    --enable-agent \
                    --enable-proxy \
                    --enable-java \
                    --with-jabber \
                    --with-ldap \
                    --enable-ipv6 \
                    --with-net-snmp \
                    --with-openipmi \
                    --with-ssh2 \
                    --with-libcurl \
                    --with-iodbc

CONFFLAGS_SERVER_MYSQL = $(CONFFLAGS_COMMON) --with-mysql
CONFFLAGS_SERVER_PGSQL = $(CONFFLAGS_COMMON) --with-postgresql
CONFFLAGS_SERVER_SQLITE3 = $(CONFFLAGS_COMMON) --with-sqlite3

# The source package will be compiled for each database flavor in
# subdirectories of this directory:
BUILDDIR := $(CURDIR)/debian/tmp-build

# Replace config.auto and config.guess from the current autotools-dev package
# and reconfigure the autoconf magic to have the "-lcrypto" linking removed.
prepare: patch prepare-stamp
prepare-stamp:
	if test -e /usr/share/misc/config.guess ; then \
		if ! test -e config.guess.orig; then \
			mv config.guess config.guess.orig ; \
			cp --remove-destination /usr/share/misc/config.guess config.guess ; \
		fi ; \
	fi
	if test -e /usr/share/misc/config.sub ; then \
		if ! test -e config.sub.orig; then \
			mv config.sub config.sub.orig ; \
			cp --remove-destination /usr/share/misc/config.sub config.sub ; \
		fi ; \
	fi
	touch prepare-stamp

# Copy the source directory (without the 'debian' directory) to other locations
# to configure and compile the sources there for each database flavor.
extract: $(foreach s,$(VARIANTS),extract-$(s)-stamp)
extract-%-stamp:
	@echo "--------------------------"
	@echo "Copying source directory to $(BUILDDIR)-$(*) for building the $(*) variant there"
	@echo "--------------------------"
	mkdir $(BUILDDIR)-$(*)
	tar -cSf - --exclude=./.svn --exclude=./debian . | tar -xSpf - -C $(BUILDDIR)-$(*)
	touch $@

# Configure the flavors
configure: $(foreach s,$(VARIANTS),configure-$(s)-stamp)
configure-%-stamp: extract-%-stamp
	@echo "--------------------------"
	@echo "Configuring the $(*) flavor in $(BUILDDIR)-$(*)"
	@echo "--------------------------"
	dh_testdir
	cd $(BUILDDIR)-$(*) && ./configure $(CONFFLAGS_COMMON) $(CONFFLAGS_SERVER_$(*))
	touch $@

# Compile the flavors
build: prepare $(foreach s,$(VARIANTS),build-$(s)-stamp)
build-%-stamp: configure-%-stamp
	@echo "--------------------------"
	@echo "Compiling the $(*) flavor in $(BUILDDIR)-$(*)"
	@echo "--------------------------"
	dh_testdir
	$(MAKE) -C $(BUILDDIR)-$(*)
	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f *-stamp
	rm -f config.log
	[ ! -f Makefile ] || $(MAKE) distclean
	[ ! -f src/zabbix_agent/Makefile ] || $(MAKE) -C src/zabbix_agent/ clean
	[ ! -f src/zabbix_get/Makefile ] || $(MAKE) -C src/zabbix_get/ clean
	[ ! -f src/zabbix_sender/Makefile ] || $(MAKE) -C src/zabbix_sender/ clean
	[ ! -f src/zabbix_proxy/Makefile ] || $(MAKE) -C src/zabbix_proxy/ clean
	debconf-updatepo

	if test -e config.guess.orig ; then \
		mv config.guess.orig config.guess ; \
	fi ; 
	if test -e config.sub.orig ; then \
		mv config.sub.orig config.sub ; \
	fi ; \

	@echo Removing build directories in $(BUILDDIR)
	$(RM) -r debian/tmp*
	dh_clean

# Build architecture-independent files here.
binary-indep: build
	dh_testdir -i
	dh_testroot -i
	dh_prep -i
	dh_installdirs -i
	dh_install -i
	# --------------------------------
	# Rename during install
	# zabbix-frontend-php
	cp debian/conf/apache.conf $(TMP_FRONTEND)/etc/zabbix/apache.conf
	# delete unnecessary .htaccess. the configuration is in apache.conf
	find $(TMP_FRONTEND)/usr/share/zabbix -name .htaccess | xargs rm -rf
	# --------------------------------
	# zabbix-frontend-php
	# fonts come from the respective Debian packages.
	# it gets linked later with dh_link.
	rm -r $(TMP_FRONTEND)/usr/share/zabbix/fonts/DejaVuSans.ttf
	rm -f $(TMP_FRONTEND)/usr/share/zabbix/conf/COPYING
	# --------------------------------
	dh_installdocs -i
	dh_installchangelogs -i ChangeLog
	dh_installexamples -i
	dh_installman -i
	dh_installdebconf -i
	#dh_installinit -i
	dh_installlogrotate -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	# --------------------------------
	# zabbix-frontend-php
	# remove executable permissions on PNG file
	find $(TMP_FRONTEND) -type f -name '*.png' -print0 | xargs -0r chmod a-x
	# --------------------------------
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build
	dh_testdir -a
	dh_testroot -a
	dh_prep -a
	dh_installdirs -a
	# create temporary config files
	mkdir debian/templates
	# zabbix-agentd.conf
	cat conf/zabbix_agentd.conf | sed \
		-e '/^# PidFile=/a \\nPidFile=/var/run/zabbix/zabbix_agentd.pid' \
		-e 's|^LogFile=.*|LogFile=/var/log/zabbix/zabbix_agentd.log|g' \
		-e '/^# LogFileSize=.*/a \\nLogFileSize=0' \
		-e '/^# Include=$$/a \\nInclude=/etc/zabbix/zabbix_agentd.d/' \
		> debian/templates/zabbix_agentd.conf
	# zabbix-server.conf
	cat conf/zabbix_server.conf |sed \
		-e '/^# PidFile=/a \\nPidFile=/var/run/zabbix/zabbix_server.pid' \
		-e 's|^LogFile=.*|LogFile=/var/log/zabbix/zabbix_server.log|g' \
		-e '/^# LogFileSize=/a \\nLogFileSize=0' \
		-e '/^# AlertScriptsPath=/a \\nAlertScriptsPath=/usr/lib/zabbix/alertscripts' \
		-e '/^# ExternalScripts=/a \\nExternalScripts=/usr/lib/zabbix/externalscripts' \
		-e '/^# DBHost=/a \\nDBHost=_DBC_DBSERVER_' \
		-e 's|^DBName=zabbix|DBName=_DBC_DBNAME_|g' \
		-e 's|^DBUser=root|DBUser=_DBC_DBUSER_|g' \
		-e '/^# DBPassword/a \\nDBPassword=_DBC_DBPASS_' \
		-e '/^# DBSocket=/a \\nDBSocket=/var/run/mysqld/mysqld.sock' \
		-e '/^# FpingLocation=/a \\nFpingLocation=/usr/bin/fping' \
		-e '/^# Fping6Location=/a \\nFping6Location=/usr/bin/fping6' \
		> debian/templates/zabbix_server.conf
	# zabbix-proxy.conf
	cat conf/zabbix_proxy.conf | sed \
		-e '/^# PidFile=/a \\nPidFile=/var/run/zabbix/zabbix_proxy.pid' \
		-e 's|^LogFile=.*|LogFile=/var/log/zabbix/zabbix_proxy.log|g' \
		-e '/^# LogFileSize=/a \\nLogFileSize=0' \
		-e '/^# ExternalScripts=/a \\nExternalScripts=/usr/lib/zabbix/externalscripts' \
		-e '/^# DBHost=/a \\nDBHost=_DBC_DBSERVER_' \
		-e 's|^DBName=.*|DBName=_DBC_DBNAME_|g' \
		-e 's|^DBUser=root|DBUser=zabbix|g' \
		-e '/^# DBPassword/a \\nDBPassword=_DBC_DBPASS_' \
		-e '/^# DBSocket=/a \\nDBSocket=/var/run/mysqld/mysqld.sock' \
		-e '/^# FpingLocation=/a \\nFpingLocation=/usr/bin/fping' \
		-e '/^# Fping6Location=/a \\nFping6Location=/usr/bin/fping6' \
		> debian/templates/zabbix_proxy.conf
    # zabbix-java-gateway.conf
	cat src/zabbix_java/settings.sh | sed \
		-e 's|^PID_FILE=.*|PID_FILE="/var/run/zabbix/zabbix_java_gateway.pid"|g' \
		> debian/templates/zabbix_java_gateway.conf
	dh_install -a
	# install config files
	# zabbix-agent
	cp debian/templates/zabbix_agentd.conf $(TMP_AGENT)/usr/share/$(PKG_AGENT)/zabbix_agentd.conf
	# zabbix-server-mysql
	cp debian/templates/zabbix_server.conf $(TMP_SERVER_MYSQL)/usr/share/$(PKG_SERVER_MYSQL)/zabbix_server.conf
	cat database/mysql/schema.sql database/mysql/images.sql database/mysql/data.sql > $(TMP_SERVER_MYSQL)/usr/share/dbconfig-common/data/$(PKG_SERVER_MYSQL)/install/mysql
	# zabbix-server-pgsql
	cp debian/templates/zabbix_server.conf $(TMP_SERVER_PGSQL)/usr/share/$(PKG_SERVER_PGSQL)/zabbix_server.conf
	cat database/postgresql/schema.sql database/postgresql/images.sql database/postgresql/data.sql > $(TMP_SERVER_PGSQL)/usr/share/dbconfig-common/data/$(PKG_SERVER_PGSQL)/install/pgsql
	# zabbix-proxy-mysql
	cp debian/templates/zabbix_proxy.conf $(TMP_PROXY_MYSQL)/usr/share/zabbix-proxy/zabbix_proxy.conf
	cp database/mysql/schema.sql $(TMP_PROXY_MYSQL)/usr/share/dbconfig-common/data/$(PKG_PROXY_MYSQL)/install/mysql
	# zabbix-proxy-pgsql
	cp debian/templates/zabbix_proxy.conf $(TMP_PROXY_PGSQL)/usr/share/zabbix-proxy/zabbix_proxy.conf
	cp database/postgresql/schema.sql $(TMP_PROXY_PGSQL)/usr/share/dbconfig-common/data/$(PKG_PROXY_PGSQL)/install/pgsql
	# zabbix-proxy-sqlite3
	cat debian/templates/zabbix_proxy.conf | sed -e 's|^DBName=_DBC_DBNAME_|DBName=_DBC_BASEPATH_/_DBC_DBNAME_|g' > $(TMP_PROXY_SQLITE3)/usr/share/zabbix-proxy/zabbix_proxy.conf
	cp database/sqlite3/schema.sql $(TMP_PROXY_SQLITE3)/usr/share/dbconfig-common/data/$(PKG_PROXY_SQLITE3)/install/sqlite3
	sed -i 's|/tmp/zabbix_java.log|/var/log/zabbix/zabbix_java_gateway.log|g' debian/zabbix-java-gateway/usr/sbin/zabbix_java/lib/logback.xml
	# --------------------------------
	dh_installdocs -a
	dh_installchangelogs -a ChangeLog
	dh_installexamples -a
	dh_installman -a
	dh_installdebconf -a
	#dh_installinit -a
	dh_installinit -p $(PKG_SERVER_MYSQL) --name zabbix-server
	dh_installinit -p $(PKG_SERVER_PGSQL) --name zabbix-server
	dh_installinit -p $(PKG_SERVER_SQLITE3) --name zabbix-server
	dh_installinit -p $(PKG_PROXY_MYSQL) --name zabbix-proxy
	dh_installinit -p $(PKG_PROXY_PGSQL) --name zabbix-proxy
	dh_installinit -p $(PKG_PROXY_SQLITE3) --name zabbix-proxy
	dh_installinit -p $(PKG_JAVA_GATEWAY)
	dh_installinit -p $(PKG_AGENT)

	dh_installlogrotate -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_strip -a
	dh_makeshlibs -a
	dh_shlibdeps -a
	dh_installdeb -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-arch binary-indep 

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

