#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2010, Geza Kovacs <gkovacs@mit.edu>
# Copyright © 2011, Jonas Smedegaard <dr@jones.dk>
# Description: Debian packaging script for CoffeeScript
#
# 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

DEB_UPSTREAM_URL = http://githubredir.debian.net/github/jashkenas/coffee-script
DEB_UPSTREAM_TARBALL_BASENAME = $(DEB_UPSTREAM_TARBALL_VERSION)
DEB_UPSTREAM_WGET_OPTS += --no-check-certificate
DEB_UPSTREAM_TARBALL_MD5 = 9218a47356c0da6713a05162a2e10295

# suppress checking binary file, to not upset dpkg-source
DEB_COPYRIGHT_CHECK_IGNORE_REGEX = ^documentation/images/logo\.png|debian/(changelog|copyright(|_hints|_newhints))$

CDBS_DEPENDS_coffeescript = nodejs (>= 0.2.6)
CDBS_RECOMMENDS_libjs-coffeescript = javascript-common
CDBS_SUGGESTS_coffeescript = libjs-coffeescript
CDBS_SUGGESTS_libjs-coffeescript = coffeescript
CDBS_PROVIDES_coffeescript = libnode-coffeescript

DEB_INSTALL_DOCS_libjs-coffeescript += extras/EXTRAS
DEB_INSTALL_EXAMPLES_coffeescript += examples/*

# put aside upstream-shipped temp files during build but after copyright-check
pre-build:: debian/stamp-upstreamtmpstuff
debian/stamp-upstreamtmpstuff: debian/stamp-copyright-check
	for dir in $(upstreamtmpdirs); do \
		[ ! -e $$dir ] || [ -e $$dir.upstream ] || cp -a $$dir $$dir.upstream; \
	done
	touch $@
clean::
	for dir in $(upstreamtmpdirs); do \
		if [ -e $$dir.upstream ]; then \
			rm -rf $$dir; \
			mv $$dir.upstream $$dir; \
		fi; \
	done
	rm -f debian/stamp-upstreamtmpstuff

# Compile from itself
# TODO: add build:parser when jison is available in Debian
# TODO: add build:ultraviolet when ruby-uv is in Debian
# TODO: compile docs when docco is in Debian
# libnode-uglify versioning: library calls broken in earlier packaging
CDBS_BUILD_DEPENDS += , libnode-uglify (>= 1.0.4)
upstreamtmpdirs += lib extras
# Provide "cake" as "cake.coffeescript" to peacefully coexist with cakephp
cake = cake.coffeescript
build/coffeescript:: debian/stamp-build
debian/stamp-build:
	chmod +x bin/cake
	bin/cake build:full build:browser

	echo '#!/usr/bin/env node' > debian/$(cake)
	echo >> debian/$(cake)
	echo "require('../lib/coffee-script/lib/cake').run();" >> debian/$(cake)

	echo '#!/usr/bin/env node' > debian/coffee
	echo >> debian/coffee
	echo "require('../lib/coffee-script/lib/command').run();" >> debian/coffee

	touch $@
clean::
	rm -f debian/$(cake) debian/coffee
	rm -f debian/stamp-build

install/coffeescript::
	bin/cake --destdir debian/tmp --prefix /usr --nodedir /usr/lib/nodejs install
	rm -f debian/tmp/usr/bin/cake
	install -t debian/tmp/usr/bin/ debian/$(cake) debian/coffee

# generate manpage based on --help option of script itself
CDBS_BUILD_DEPENDS += , help2man
manpages = debian/coffee.1
DEB_INSTALL_MANPAGES_coffeescript = $(manpages)
common-binary-indep:: $(manpages)
$(manpages): install/coffeescript
$(manpages): debian/%.1 : bin/%
	help2man \
		--name="interpreter and compiler for the CoffeeScript language" \
		--version-string="$(DEB_NOEPOCH_VERSION)" \
		--no-info \
		--output=$@ \
		$<
clean::
	rm -f $(manpages)

binary-fixup/coffeescript::
	chmod -R a-x,a+X debian/coffeescript/usr/lib/coffee-script/lib/ debian/coffeescript/usr/lib/coffee-script/src/
