#!/usr/bin/make -f
# -*- makefile -*-

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

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# we don't really need to run upstream configure and make
override_dh_auto_configure:
	true
override_dh_auto_build:
	true

%:
	dh $@ --with apache2

# the sed command below to insert mpath is a workaround for
# Debian bug #837754
# when the bug is closed, the workaround can be removed and this package
# can be changed from arch: any to arch: all

override_dh_auto_install:
	dh_install \
		-X.htaccess \
		-Xmysql.php -Xpgsql.php \
                -XCOPYING -XLICENSE \
                -Xjquery.min.js \
		api usr/share/homer/web
	# must not try to start Kamailio until user manually configures the DB
	dh_installinit --name kamailio-homer --no-start
	install -D -T -m 0640 examples/sipcapture/sipcapture.kamailio $(CURDIR)/debian/homer-api-mysql/etc/homer/kamailio-homer.cfg
	install -D -T -m 0640 examples/pgsql/kamailio.cfg $(CURDIR)/debian/homer-api-postgresql/etc/homer/kamailio-homer.cfg
	for cfg in $(CURDIR)/debian/homer-api-mysql/etc/homer/kamailio-homer.cfg $(CURDIR)/debian/homer-api-postgresql/etc/homer/kamailio-homer.cfg ; \
	do \
		grep -q ^mpath $$cfg || sed -i "0,/^loadmodule.*/s!^loadmodule.*!mpath=\"/usr/lib/$(DEB_HOST_MULTIARCH)/kamailio/modules/\"\n&!" $$cfg ; \
	done
	install -D -T -m 0644 scripts/sipcapture.crontab $(CURDIR)/debian/homer-api/etc/cron.d/homer
	sed -i \
		-e 's/ root / homer /g' \
		-e 's!/usr/bin/homer_!/usr/lib/homer/libexec/homer_!g' \
		$(CURDIR)/debian/homer-api/etc/cron.d/homer
	install -D -T -m 0644 examples/sipcapture/sipcapture.rsyslogd $(CURDIR)/debian/homer-api/etc/rsyslog.d/homer.conf
	sed -i -e 's/homer$$/homer.log/g' $(CURDIR)/debian/homer-api/etc/rsyslog.d/homer.conf
	install -D -T -m 0640 -g www-data api/configuration_example.php $(CURDIR)/debian/homer-api/etc/homer/configuration.php
	install -D -T -m 0640 -g www-data api/preferences_example.php $(CURDIR)/debian/homer-api/etc/homer/preferences.php
	sed -i -e "s!^define('PROFILE_PARAM.*\$$!define('PROFILE_PARAM', '/var/lib/homer/store/profile');!g" \
		-e "s!^define('DASHBOARD_PARAM.*\$$!define('DASHBOARD_PARAM', '/var/lib/homer/store/dashboard');!g" \
		$(CURDIR)/debian/homer-api/etc/homer/preferences.php
	install -d $(CURDIR)/debian/homer-api/usr/lib/homer/libexec
	install -m 0755 scripts/homer_partremove $(CURDIR)/debian/homer-api/usr/lib/homer/libexec
	install -D -T -m 0640 scripts/rotation.ini $(CURDIR)/debian/homer-api-mysql/etc/homer/rotation.ini
	install -D -T -m 0640 scripts/pgsql/rotation.ini $(CURDIR)/debian/homer-api-postgresql/etc/homer/rotation.ini

override_dh_fixperms:
	dh_fixperms
	chmod a+x $(CURDIR)/debian/homer-api/usr/share/homer/examples/sipcapture/el6/sipcapture.init
	chmod a-x $(CURDIR)/debian/homer-api/usr/share/homer/web/apidoc/html/utils/send_sample_request.js
	install -o www-data -g www-data -m 0750 -d $(CURDIR)/debian/homer-api/var/lib/homer/store/profile
	install -o www-data -g www-data -m 0750 -d $(CURDIR)/debian/homer-api/var/lib/homer/store/dashboard

