#!/usr/bin/make -f

# Enable this export to get more detailed information about what debhelper does
#export DH_VERBOSE=1

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

# 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)

# Squeeze backporters: --export=configure is not available on Squeeze
#DPKG_BUILDFLAGS := $(shell DEB_CFLAGS_MAINT_APPEND=-Wall dpkg-buildflags --export=configure)

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

# minimise needless linking
export DEB_LDFLAGS_MAINT_APPEND= -Wl,--as-needed

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

## Hurd and kFreeBSD only have Java 1.5 which can't compile java-gateway sources...
#ifneq ($(DEB_BUILD_ARCH_OS),hurd)
#ifneq ($(DEB_BUILD_ARCH_OS),kfreebsd)
#CONFFLAGS_COMMON += --enable-java
#endif
#endif
CONFFLAG_IPMI           = $(if $(shell dpkg --status libopenipmi-dev &>/dev/null),--with-openipmi)
CONFFLAG_SERVER_MYSQL   = --with-mysql --enable-server
CONFFLAG_SERVER_PGSQL   = --with-postgresql --enable-server
CONFFLAG_SERVER_SQLITE3 = --with-sqlite3
BUILD_PREFIX := $(CURDIR)/debian/tmp-build

# The Zabbix sources are compiled once for each database backends (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.
VARIANTS := MYSQL PGSQL SQLITE3

%:
	dh $@ --max-parallel=4 --with javahelper,linktree,apache2

override_dh_clean:
	@echo "Removing build directories in $(BUILD_PREFIX)"
	$(RM) -r debian/tmp*
	dh_clean \
            config.log debian/*.debhelper \
            frontends/php/js/vendors/jquery.js \
            frontends/php/js/vendors/jquery-ui.js \
            *.jar

# use --as-needed only if supported by dh-autoreconf (to simplify backporting)
DH_AS_NEEDED=$(shell dpkg --compare-versions $$(dpkg --status dh-autoreconf | grep Version | cut -d' ' -f2) ge 6 && echo --as-needed)
override_dh_autoreconf:
	dh_autoreconf $(DH_AS_NEEDED)

override_dh_auto_configure:
	## Copy the source directory (without the 'debian' directory) to build locations
	## to configure and compile the sources there for each database flavor.
	for i in $(VARIANTS); do \
            mkdir $(BUILD_PREFIX)-$$i ;\
            tar -cSf - --exclude=./.svn --exclude=./debian . \
            | tar -xSpf - -C $(BUILD_PREFIX)-$$i ;\
        done
	set -e ;\
$(foreach i,$(VARIANTS), \
    dh_auto_configure --sourcedirectory=$(BUILD_PREFIX)-$i --max-parallel=4 -- \
      $(CONFFLAGS_COMMON) $(CONFFLAG_IPMI) $(CONFFLAG_SERVER_$i) ;\
)

build-indep: override_dh_autoreconf
	## build styles:
	dh_auto_configure -- --enable-java
	$(MAKE) css
	## build translations:
	cd frontends/php/locale && ./update_po.sh
	cd frontends/php/locale && ./make_mo.sh
	## build zabbix-java-gateway.jar
	## override_jh_build:
	JAVA_HOME=/usr/lib/jvm/default-java \
        CLASSPATH=/usr/share/java/com.android.json.jar:/usr/share/java/logback-core.jar:/usr/share/java/logback-classic.jar:/usr/share/java/slf4j-api.jar \
        jh_build --no-javadoc --main=com.zabbix.gateway.JavaGateway zabbix-java-gateway.jar src/zabbix_java/src/com

override_dh_auto_build:
	set -e ;\
	$(foreach i,$(VARIANTS), \
        dh_auto_build --sourcedirectory=$(BUILD_PREFIX)-$i --max-parallel=4 \
    ;)

override_dh_auto_test:
#	set -e ;\
#	$(foreach i,$(VARIANTS), \
#        dh_auto_test --sourcedirectory=$(BUILD_PREFIX)-$i --max-parallel=4 \
#    ;)

override_dh_auto_install:
	@echo "Replacing sourceless (minified) bundled libraries..."
	cp -vfH debian/missing-sources/jquery.js  frontends/php/js/vendors/jquery.js
	cp -vfH debian/missing-sources/jquery-ui.js  frontends/php/js/vendors/jquery-ui.js
	## build man pages
	if [ -d "$(BUILD_PREFIX)-MYSQL/man" ]; then \
            dh_auto_install --sourcedirectory=$(BUILD_PREFIX)-MYSQL/man \
              --destdir=$(CURDIR)/debian/tmp ;\
        fi

override_dh_install:
	dh_install
	for i in debian/zabbix-*-*; do \
            find $$i -type f -name '*.sql' -printf 'compressing %p\n' \
                -exec gzip -n -9 '{}' \;;\
        done
	## Those files need to be renamed:
	if [ -d "$(CURDIR)/debian/$(PKG_GATEWAY_JAVA)" ]; then \
            cp -v src/zabbix_java/lib/logback.xml  $(CURDIR)/debian/$(PKG_GATEWAY_JAVA)/etc/zabbix/zabbix_java_gateway.logback.xml ;\
            cp -v src/zabbix_java/settings.sh  $(CURDIR)/debian/$(PKG_GATEWAY_JAVA)/etc/zabbix/zabbix_java_gateway.conf ;\
        fi
        ## Directories will be replaced with symlinks:
	if [ -d "$(CURDIR)/debian/$(PKG_FRONTEND)" ]; then \
            rmdir -v $(CURDIR)/debian/$(PKG_FRONTEND)/usr/share/zabbix/local/app/controllers || /bin/true ;\
            rmdir -v $(CURDIR)/debian/$(PKG_FRONTEND)/usr/share/zabbix/local/app/views || /bin/true ;\
        fi
        ## Remove .po files to save space in frontend-php:
	if [ -d "$(CURDIR)/debian/$(PKG_FRONTEND)" ]; then \
            $(RM) -v $(CURDIR)/debian/$(PKG_FRONTEND)/usr/share/zabbix/locale/*/LC_MESSAGES/*.po ;\
        fi

override_jh_depends:
	## make sure java-gateway.jar is executable. This is needed for jh_depends to generate correct {java:Depends}
	if [ -d "$(CURDIR)/debian/$(PKG_GATEWAY_JAVA)" ]; then \
            chmod -v +x $(CURDIR)/debian/$(PKG_GATEWAY_JAVA)/usr/sbin/*.jar ;\
        fi
	jh_depends -v -p$(PKG_GATEWAY_JAVA)

## jh_linkjars override is necessary with DH-11 due to the following error:
##   jh_linkjars -O--max-parallel=4 -Nzabbix-frontend-php -Nzabbix-java-gateway
##   Invalid option: N
override_jh_linkjars:
	jh_linkjars -v

override_dh_apache2:
	dh_apache2 --noenable

override_dh_installinit:
	dh_installinit -p $(PKG_AGENT)
	dh_installinit -p $(PKG_GATEWAY_JAVA)
	dh_installinit -p $(PKG_PROXY_MYSQL) --name zabbix-proxy --no-start
	dh_installinit -p $(PKG_PROXY_PGSQL) --name zabbix-proxy --no-start
	dh_installinit -p $(PKG_PROXY_SQLITE3) --name zabbix-proxy --no-start
	dh_installinit -p $(PKG_SERVER_MYSQL) --name zabbix-server --no-start
	dh_installinit -p $(PKG_SERVER_PGSQL) --name zabbix-server --no-start
ifdef PKG_SERVER_SQLITE3
	dh_installinit -p $(PKG_SERVER_SQLITE3) --name zabbix-server --no-start
endif

override_dh_installsystemd:
	dh_installsystemd -p $(PKG_AGENT)
	dh_installsystemd -p $(PKG_GATEWAY_JAVA)
	dh_installsystemd -p $(PKG_PROXY_MYSQL) --name zabbix-proxy --no-start
	dh_installsystemd -p $(PKG_PROXY_PGSQL) --name zabbix-proxy --no-start
	dh_installsystemd -p $(PKG_PROXY_SQLITE3) --name zabbix-proxy --no-start
	dh_installsystemd -p $(PKG_SERVER_MYSQL) --name zabbix-server --no-start
	dh_installsystemd -p $(PKG_SERVER_PGSQL) --name zabbix-server --no-start
ifdef PKG_SERVER_SQLITE3
	dh_installsystemd -p $(PKG_SERVER_SQLITE3) --name zabbix-server --no-start
endif