#!/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_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

# parallel build by default on linux
ifeq ($(DEB_HOST_ARCH_OS),linux)
    ifeq ($(findstring parallel=,$(DEB_BUILD_OPTIONS)),)
        export DEB_BUILD_OPTIONS+=parallel=$(shell getconf _NPROCESSORS_ONLN)
    endif
endif

# 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-server \
                    --enable-ipv6 \
                    --with-jabber \
                    --with-ldap \
                    --with-libcurl \
                    --with-net-snmp \
                    --with-ssh2

CONFFLAG_IPMI           = $(if $(shell dpkg --status libopenipmi-dev &>/dev/null),--with-openipmi)
CONFFLAG_SERVER_MYSQL   = --with-mysql
CONFFLAG_SERVER_PGSQL   = --with-postgresql
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 $@ --parallel --max-parallel=4 --with autoreconf

override_dh_clean:
	@echo Removing build directories in $(BUILD_PREFIX)
	$(RM) -r debian/tmp* config.log
	dh_clean

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

override_dh_auto_configure:
	$(foreach i,$(VARIANTS), \
        dh_auto_configure --sourcedirectory=$(BUILD_PREFIX)-$i --max-parallel=4 -- \
            $(CONFFLAGS_COMMON) $(CONFFLAG_IPMI) $(CONFFLAG_SERVER_$i) \
    ;)

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

override_dh_auto_install:
	## build man pages
	dh_auto_install --sourcedirectory=$(BUILD_PREFIX)-MYSQL/man \
          --destdir=$(CURDIR)/debian/tmp

override_dh_install:
	dh_install
	for i in debian/zabbix-*-*; do \
	    find $$i -type f -name '*.sql' -printf 'compressing %p\n' \
	        -exec gzip -9 '{}' \;;\
	done

override_dh_installinit:
	dh_installinit -p $(PKG_AGENT)
	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_SERVER_MYSQL) --name zabbix-server
	dh_installinit -p $(PKG_SERVER_PGSQL) --name zabbix-server
	dh_installinit -p $(PKG_SERVER_SQLITE3) --name zabbix-server

override_dh_builddeb:
	dh_builddeb -- -Zxz

PKG := zabbix
DTYPE := +dfsg
DPATH := $(abspath $(dir $(MAKEFILE_LIST)))
VER ?= $(shell dpkg-parsechangelog -l$(DPATH)/changelog | perl -ne 'print $$1 if m{Version:\s*(?:\d:)?([\d\.]+)}')
.PHONY: get-orig-source
get-orig-source: $(PKG)_$(VER)$(DTYPE).orig.tar.xz
	@

$(PKG)_$(VER)$(DTYPE).orig.tar.xz:
	@echo "# Downloading..."
	uscan --noconf --verbose --rename --destdir=$(CURDIR) --check-dirname-level=0 --force-download --download-version $(VER) $(DPATH)
	@echo "# Extracting..."
	mkdir $(PKG)-$(VER) \
	&& tar xf $(PKG)_$(VER).orig.tar.* --directory $(PKG)-$(VER) --strip-components 1 \
	|| $(RM) -r $(PKG)-$(VER)
	@echo "# Clean-up..."
	cd $(PKG)-$(VER) \
	&& find . -type f -name '*.exe' -delete -printf 'removing %p\n' \
	&& $(RM) -r -v \
            frontends/php/images/flash \
            src/zabbix_java/lib/*.jar
	@echo "# Packing..."
	tar cJf "$(PKG)_$(VER)$(DTYPE).orig.tar.xz" "$(PKG)-$(VER)" \
	&& $(RM) -r "$(PKG)-$(VER)"
