#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

b=$(CURDIR)/debian/build
VERSION:=$(shell dpkg-parsechangelog | awk '/Version: / { print $$2 }' | sed 's/+.*//')
BUILD_VERSION:=$(shell dpkg-parsechangelog | awk '/Version: / { print $$2 }')

build: build-stamp

build-stamp:
	dh_testdir
	egrep -q "\$VERSION = 'VERSION'" lib/admin.pm || \
                          (echo "Wrong version in lib/csc.pm" && exit 2)
	egrep -q "\$BUILD_VERSION = 'BUILD_VERSION'" lib/admin.pm || \
                          (echo "Wrong version in lib/csc.pm" && exit 2)
	mkdir -p pro/etc/ngcp-www-admin pro/etc/apache2/sites-available
	mkdir -p ce/etc/ngcp-www-admin ce/etc/apache2/sites-available
	tpage --define PRO=true   etc/admin.conf > pro/etc/ngcp-www-admin/admin.conf.tt2
	tpage                     etc/admin.conf > ce/etc/ngcp-www-admin/admin.conf.tt2
	tpage --define PRO=true   etc/admin.conf.services > pro/etc/ngcp-www-admin/admin.conf.services
	tpage                     etc/admin.conf.services > ce/etc/ngcp-www-admin/admin.conf.services
	tpage --define PRO=true   etc/apache.site > pro/etc/apache2/sites-available/ngcp-www-admin.tt2
	tpage                     etc/apache.site > ce/etc/apache2/sites-available/ngcp-www-admin.tt2
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	rm -rf $(b) pro ce
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

ngcp-www-admin:
	@echo "--- Building: $@"
	dh_installdirs       -p$@ -P$(b)/$@
	dh_installdocs       -p$@ -P$(b)/$@
	dh_installchangelogs -p$@ -P$(b)/$@
	dh_install           -p$@ -P$(b)/$@
	sed -i -e "s/VERSION = 'VERSION'/VERSION = '$(VERSION)'/" $(b)/$@/usr/share/ngcp-www-admin/lib/admin.pm
	sed -i -e "s/BUILD_VERSION = 'BUILD_VERSION'/BUILD_VERSION = '$(BUILD_VERSION)'/" $(b)/$@/usr/share/ngcp-www-admin/lib/admin.pm
	dh_installexamples   -p$@ -P$(b)/$@
	dh_installman        -p$@ -P$(b)/$@
	# drop svn files:
	find $(b) -type d -name \.svn -print0 | xargs -0 rm -rf
	# install lintian overrides:
	mkdir -p $(b)/$(@)/usr/share/lintian/overrides/
	cp -av debian/overrides $(b)/$(@)/usr/share/lintian/overrides/$@
	dh_link              -p$@ -P$(b)/$@
	dh_strip             -p$@ -P$(b)/$@
	dh_compress          -p$@ -P$(b)/$@
	dh_fixperms          -p$@ -P$(b)/$@
	#dh_perl              -p$@ -P$(b)/$@
	dh_makeshlibs        -p$@ -P$(b)/$@ -V
	dh_installdeb        -p$@ -P$(b)/$@
	dh_shlibdeps         -p$@ -P$(b)/$@
	dh_installdebconf    -p$@ -P$(b)/$@
	dh_gencontrol        -p$@ -P$(b)/$@
	dh_md5sums           -p$@ -P$(b)/$@
	dh_builddeb          -p$@ -P$(b)/$@

ngcp-templates-ce-www-admin:
	@echo "--- Building: $@"
	dh_installdirs       -p$@ -P$(b)/$@
	dh_link              -p$@ -P$(b)/$@
	dh_installdocs       -p$@ -P$(b)/$@
	dh_installchangelogs -p$@ -P$(b)/$@
	dh_install           -p$@ -P$(b)/$@
	dh_strip             -p$@ -P$(b)/$@
	dh_compress          -p$@ -P$(b)/$@
	dh_fixperms          -p$@ -P$(b)/$@
	dh_makeshlibs        -p$@ -P$(b)/$@ -V
	dh_installdeb        -p$@ -P$(b)/$@
	dh_shlibdeps         -p$@ -P$(b)/$@
	dh_installdebconf    -p$@ -P$(b)/$@
	dh_gencontrol        -p$@ -P$(b)/$@
	dh_md5sums           -p$@ -P$(b)/$@
	dh_builddeb          -p$@ -P$(b)/$@

ngcp-templates-pro-www-admin:
	@echo "--- Building: $@"
	dh_installdirs       -p$@ -P$(b)/$@
	dh_link              -p$@ -P$(b)/$@
	dh_installdocs       -p$@ -P$(b)/$@
	dh_installchangelogs -p$@ -P$(b)/$@
	dh_install           -p$@ -P$(b)/$@
	dh_strip             -p$@ -P$(b)/$@
	dh_compress          -p$@ -P$(b)/$@
	dh_fixperms          -p$@ -P$(b)/$@
	dh_makeshlibs        -p$@ -P$(b)/$@ -V
	dh_installdeb        -p$@ -P$(b)/$@
	dh_shlibdeps         -p$@ -P$(b)/$@
	dh_installdebconf    -p$@ -P$(b)/$@
	dh_gencontrol        -p$@ -P$(b)/$@
	dh_md5sums           -p$@ -P$(b)/$@
	dh_builddeb          -p$@ -P$(b)/$@

binary-all: build install

binary-indep: build install ngcp-www-admin \
	ngcp-templates-ce-www-admin ngcp-templates-pro-www-admin

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
