#!/usr/bin/make -f

%:
	dh $@ --with autoreconf

BUILD_FLAGS =\
	--host=$(DEB_HOST_GNU_TYPE) \
	--build=$(DEB_BUILD_GNU_TYPE) \
	--prefix=/usr \
	--sysconfdir=/etc/zabbix \
	--libdir=/usr/lib/zabbix \
	--enable-ipv6 \
	--with-libpcre \
	--with-openssl \
	--with-ldap \
	--with-net-snmp \
	--with-openipmi \
	--with-libcurl \
	--with-unixodbc \
	--with-libxml2 \
	--with-libevent \
	--with-ssh

# dependencies for override_dh_auto_build
BUILD_TARGETS =

# dependencies for override_dh_install
INSTALL_TARGETS =

# dependencies for override_dh_installinit
INSTALLINIT_TARGETS =

# dependencies for override_dh_clean
CLEAN_TARGETS =

#
# zabbix-agent
#

update_conf_agentd:
	cp conf/zabbix_agentd.conf debian/conf/zabbix_agentd.conf
#	sed -i 's:^PidFile=/run:PidFile=/var/run:g' debian/conf/zabbix_agentd.conf

installinit_agent:
	dh_installinit -p zabbix-agent

clean_agent: run_dh_clean
	rm -f debian/conf/zabbix_agentd.conf

BUILD_TARGETS += update_conf_agentd
INSTALLINIT_TARGETS += installinit_agent
CLEAN_TARGETS += clean_agent

#
# zabbix-agent2
#
update_conf_agent2:
#	sed -i 's:^PidFile=/run:PidFile=/var/run:g' src/go/conf/zabbix_agent2.conf

#BUILD_TARGETS += update_conf_agent2

installinit_agent2:
#	dh_installinit -p zabbix-agent2

#INSTALLINIT_TARGETS += installinit_agent2

#
# zabbix-java-gateway
#

update_conf_java_gateway:
	cat src/zabbix_java/settings.sh | sed \
		-e 's|^PID_FILE=.*|PID_FILE="/var/run/zabbix/zabbix_java_gateway.pid"|g' \
		> debian/conf/zabbix_java_gateway.conf
	cat src/zabbix_java/lib/logback.xml | sed \
		-e 's|/tmp/zabbix_java.log|/var/log/zabbix/zabbix_java_gateway.log|g' \
		> debian/conf/zabbix_java_gateway_logback.xml

install_java_gateway: run_dh_install
	rm debian/zabbix-java-gateway/usr/share/zabbix-java-gateway/lib/logback.xml
	rm debian/zabbix-java-gateway/usr/share/zabbix-java-gateway/lib/logback-console.xml
	chmod 755 debian/zabbix-java-gateway/usr/sbin/zabbix_java_gateway

installinit_java_gateway:
	dh_installinit -p zabbix-java-gateway

clean_java_gateway: run_dh_clean
	rm -f debian/conf/zabbix_java_gateway.conf
	rm -f debian/conf/zabbix_java_gateway_logback.xml

BUILD_TARGETS += update_conf_java_gateway
INSTALL_TARGETS += install_java_gateway
INSTALLINIT_TARGETS += installinit_java_gateway
CLEAN_TARGETS += clean_java_gateway

#
# zabbix server
#

update_conf_server:
	cp conf/zabbix_server.conf debian/conf/zabbix_server.conf
#	sed -i 's:^PidFile=/run:PidFile=/var/run:g' debian/conf/zabbix_server.conf
#	sed -i 's:^SocketDir=/run:SocketDir=/var/run:g' debian/conf/zabbix_server.conf

installinit_server:
	dh_installinit --no-start -p zabbix-server-mysql --name=zabbix-server
	dh_installinit --no-start -p zabbix-server-pgsql --name=zabbix-server

clean_server: run_dh_clean
	rm -f debian/conf/zabbix_server.conf

BUILD_TARGETS += update_conf_server
INSTALLINIT_TARGETS += installinit_server
CLEAN_TARGETS += clean_server

#
# zabbix-proxy
#

update_conf_proxy:
	cp conf/zabbix_proxy.conf debian/conf/zabbix_proxy.conf
#	sed -i 's:^PidFile=/run:PidFile=/var/run:g' debian/conf/zabbix_proxy.conf
#	sed -i 's:^SocketDir=/run:SocketDir=/var/run:g' debian/conf/zabbix_proxy.conf

installinit_proxy:
	dh_installinit --no-start -p zabbix-proxy-mysql --name=zabbix-proxy
	dh_installinit --no-start -p zabbix-proxy-pgsql --name=zabbix-proxy
	dh_installinit --no-start -p zabbix-proxy-sqlite3 --name=zabbix-proxy

clean_proxy: run_dh_clean
	rm -f debian/conf/zabbix_proxy.conf

BUILD_TARGETS += update_conf_proxy
INSTALLINIT_TARGETS += installinit_proxy
CLEAN_TARGETS += clean_proxy

#
# zabbix frontend
#

install_frontend: run_dh_install
	find debian/zabbix-frontend-php/usr/share/zabbix -name .htaccess | xargs rm -f
	find debian/zabbix-frontend-php/usr/share/zabbix/locale -name '*.po' | xargs rm -f
	find debian/zabbix-frontend-php/usr/share/zabbix/locale -name '*.sh' | xargs rm -f
	rm -f debian/zabbix-frontend-php/usr/share/zabbix/assets/fonts/DejaVuSans.ttf
	sed -i -r "s/(define\(.*_FONT_NAME.*)DejaVuSans/\1graphfont/" \
		debian/zabbix-frontend-php/usr/share/zabbix/include/defines.inc.php

INSTALL_TARGETS += install_frontend

#
# clean
#

override_dh_clean: run_dh_clean $(CLEAN_TARGETS)

run_dh_clean:
	dh_clean

#
# configure
#

override_dh_auto_configure: configure_with_sqlite3 configure_with_pgsql configure_with_mysql

define configure_with
	mkdir -p $(CURDIR)/debian/tmp.build-$(1)
	tar -cSf - --exclude ./bin --exclude ./build --exclude ./ui --exclude ./debian . \
		| tar -xSpf - -C $(CURDIR)/debian/tmp.build-$(1) && \
	cat $(CURDIR)/database/$(1)/schema.sql > $(CURDIR)/database/$(1)/create.sql && \
	cat $(CURDIR)/database/$(1)/images.sql >> $(CURDIR)/database/$(1)/create.sql && \
	cat $(CURDIR)/database/$(1)/data.sql >> $(CURDIR)/database/$(1)/create.sql && \
	dh_auto_configure --sourcedirectory=$(CURDIR)/debian/tmp.build-$(1) -- $(BUILD_FLAGS) $(2) --with-$(1)
endef

configure_with_sqlite3:
	$(call configure_with,sqlite3,--enable-agent --enable-java --enable-proxy)

configure_with_pgsql:
	$(call configure_with,postgresql,--enable-server --enable-proxy)

configure_with_mysql:
	$(call configure_with,mysql,--enable-server --enable-proxy)

#
# build
#

override_dh_auto_build: $(BUILD_TARGETS) build_with_sqlite build_with_pgsql build_with_mysql

define build_with
	cd $(CURDIR)/debian/tmp.build-$(1) && make \
		EXTERNAL_SCRIPTS_PATH=/usr/lib/zabbix/externalscripts \
		ALERT_SCRIPTS_PATH=/usr/lib/zabbix/alertscripts \
	&& cd -
endef

build_with_sqlite:
	$(call build_with,sqlite3)

build_with_pgsql:
	$(call build_with,postgresql)

build_with_mysql:
	$(call build_with,mysql)

#
# install
#

override_dh_install: run_dh_install $(INSTALL_TARGETS)

run_dh_install:
	dh_install

override_dh_installinit: $(INSTALLINIT_TARGETS)


override_dh_fixperms:
	dh_fixperms
	chmod 600 debian/zabbix-server-mysql/etc/zabbix/zabbix_server.conf
	chmod 600 debian/zabbix-server-pgsql/etc/zabbix/zabbix_server.conf
	chmod 600 debian/zabbix-proxy-mysql/etc/zabbix/zabbix_proxy.conf
	chmod 600 debian/zabbix-proxy-pgsql/etc/zabbix/zabbix_proxy.conf

