#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2011 Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for node-postgres
#
# 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/class/makefile.mk
include /usr/share/cdbs/1/rules/debhelper.mk

libpkgname = node-pg

# Needed both by upstream build process and at runtime
common-depends = node-generic-pool

CDBS_BUILD_DEPENDS += , nodejs-dev, libpq-dev, $(common-depends)
CDBS_DEPENDS_$(libpkgname) = $(common-depends), nodejs

# suppress optional build-dependencies
CDBS_BUILD_DEPENDS_rules_upstream-tarball =

DEB_UPSTREAM_URL = http://githubredir.debian.net/github/brianc/node-postgres/
DEB_UPSTREAM_TARBALL_BASENAME = v$(DEB_UPSTREAM_TARBALL_VERSION)
DEB_UPSTREAM_WGET_OPTS += --no-check-certificate
DEB_UPSTREAM_TARBALL_MD5 = 4f2a2d1b90db93d059baf926dada245d

# TODO: Fix node-waf to support DESTDIR for install target
DEB_MAKE_ENVVARS = NODE_PATH="$(CURDIR)/debian/node-pg/usr/lib/nodejs/pg/native"
DEB_MAKE_BUILD_TARGET = build/default/binding.node
DEB_MAKE_CHECK_TARGET = test

DEB_INSTALL_DOCS_$(libpkgname) += README.md
DEB_INSTALL_EXAMPLES_$(libpkgname) += lib/defaults.js

common-install-arch:: debian/stamp-node-install
debian/stamp-node-install::
	node-waf install
	touch debian/stamp-node-install

binary-post-install/$(libpkgname)::
	mkdir -p "$(cdbs_curdestdir)/etc/nodejs/pg"
	mv "$(cdbs_curdestdir)/usr/lib/nodejs/pg/defaults.js" "$(cdbs_curdestdir)/etc/nodejs/pg/"
	ln -sT "/etc/nodejs/pg/defaults.js" "$(cdbs_curdestdir)/usr/lib/nodejs/pg/defaults.js"

# Node native modules has the uncommon extension .node, which prevents
# proper detection by dh_shlibdeps, so we do it manually
$(patsubst %,binary-strip-IMPL/%,$(DEB_ALL_PACKAGES)) ::
	$(if $(is_debug_package)$(filter nostrip,$(DEB_BUILD_OPTIONS)),,find "$(cdbs_curdestdir)" -name "*.node" -exec strip --remove-section=.comment --remove-section=.note --strip-unneeded {} +)
$(patsubst %,binary-predeb-IMPL/%,$(DEB_ALL_PACKAGES)) ::
	find "$(cdbs_curdestdir)" -name "*.node" -exec dpkg-shlibdeps -Tdebian/$(cdbs_curpkg).substvars {} +
	find "$(cdbs_curdestdir)" -name "*.node" -exec chmod 0644 {} +

clean::
	rm -rf build
	rm -f .lock-wscript debian/stamp-node-install
