#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2011-2012 Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for buddycloud-server
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

-include /usr/share/cdbs/1/rules/upstream-tarball.mk
include /usr/share/cdbs/1/rules/utils.mk
include /usr/share/cdbs/1/rules/debhelper.mk

daemon-name = buddycloud-server
daemon-pkgname = $(daemon-name)

# suppress optional build-dependencies
CDBS_BUILD_DEPENDS_rules_upstream-tarball =
CDBS_BUILD_DEPENDS_rules_utils_copyright-check =

CDBS_BUILD_DEPENDS += , coffeescript (>= 1.1.1)

# All runtime dependencies needed at build time too to create manpage
deps = nodejs, node-jsconfig, node-async
CDBS_BUILD_DEPENDS += , $(deps)
CDBS_DEPENDS_$(daemon-pkgname) = $(deps)

CDBS_DEPENDS_$(daemon-pkgname) += , node-node-xmpp, node-node-stringprep, node-node-expat
CDBS_DEPENDS_$(daemon-pkgname) += , node-ltx, node-node-uuid, node-pg, node-underscore.logger
CDBS_DEPENDS_$(daemon-pkgname) += , node-pg, node-underscore.logger, node-ain2, node-glob
CDBS_DEPENDS_$(daemon-pkgname) += , lsb-base, adduser

DEB_UPSTREAM_URL = http://githubredir.debian.net/github/buddycloud/buddycloud-server
DEB_UPSTREAM_TARBALL_BASENAME = v$(DEB_UPSTREAM_TARBALL_VERSION)
DEB_UPSTREAM_WGET_OPTS += --no-check-certificate
DEB_UPSTREAM_TARBALL_MD5 = f95d779f04cee71b8ebedc1e5abe6305

DEB_INSTALL_DOCS_ALL += README.md
DEB_INSTALL_EXAMPLES_$(daemon-pkgname) += config.js.example

# Custom config file handling:
#  * source patched to load config from code dir (not oddly one dir out)
#  * Add dummy config to support help2man usage from staging area
build/$(daemon-pkgname)::
	coffee -o lib -c src
	touch lib/config.js
	perl -p \
		-e 's,^(    file: "/var/log/).*(\.log")$$,$$1$(daemon-pkgname)/$(daemon-name)$$2,;' \
		-e 's|^(    host: '"'"')localhost('"'"',)$$|$$1/var/run/postgresql$$2|;' \
		-e 's|^(    password: '"'"')tellnoone('"'"',)$$|$$1$$2|;' \
		config.js.example > debian/config.js
	@if ! fgrep -xq '    file: "/var/log/$(daemon-pkgname)/$(daemon-name).log"' debian/config.js; then \
		echo "Failed to adjust logfile setting in debian/config.js."; \
		exit 1; \
	fi
	@if ! fgrep -xq '    host: '"'"'/var/run/postgresql'"'"',' debian/config.js; then \
		echo "Failed to adjust host setting in debian/config.js."; \
		exit 1; \
	fi
	@if ! fgrep -xq '    password: '"''"',' debian/config.js; then \
		echo "Failed to adjust passwrod setting in debian/config.js."; \
		exit 1; \
	fi
clean::
	rm -rf lib
	rm -f debian/config.js

# generate manpage based on --help option of script itself
CDBS_BUILD_DEPENDS += , help2man
manpages = debian/buddycloud-server.1
DEB_INSTALL_MANPAGES_$(daemon-pkgname) = $(manpages)
common-binary-indep:: $(manpages)
$(manpages): build/$(daemon-pkgname)
$(manpages): debian/%.1 : bin/%
	help2man \
		--name="buddycloud channels service for XMPP" \
		--version-string="$(DEB_NOEPOCH_VERSION)" \
		--no-info \
		--no-discard-stderr \
		--output=$@ \
		$<
clean::
	rm -f $(manpages)

# Install initscript with debhelper to add pre- and postinst routines
#  * Skip runlevels 0 and 6: faster leaving to internal sysV cleanup
CDBS_RECOMMENDS_$(daemon-pkgname) += , logrotate
DEB_UPDATE_RCD_PARAMS = start 20 2 3 4 5 . stop 20 1 .
install/$(daemon-pkgname)::
	cp -f _etc_init.d_buddycloud-server 'debian/$(daemon-pkgname).init'
clean::
	rm -f 'debian/$(daemon-pkgname).init'

CDBS_DEPENDS_$(daemon-pkgname) += , dbconfig-common
CDBS_RECOMMENDS_$(daemon-pkgname) += , postgresql-client, postgresql
install/$(daemon-pkgname)::
	install -D -m 0644 postgres.sql \
		'$(cdbs_curdestdir)/usr/share/dbconfig-common/data/buddycloud-server/install/postgres'

# TODO: drop when using muffin to install
verfile=$(cdbs_curdestdir)/usr/share/$(daemon-pkgname)/main.js
binary-post-install/$(daemon-pkgname)::
	perl -i -pe 's/^(\s*version = )require.*/$$1'"'$(DEB_UPSTREAM_VERSION)'"'/' '$(verfile)'
	@if ! fgrep -xq '  version = '"'$(DEB_UPSTREAM_VERSION)'" '$(verfile)'; then \
		echo "Failed to hardcode version."; \
		exit 1; \
	fi
