#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2010, Geza Kovacs <gkovacs@mit.edu>
# Copyright © 2011-2017, 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 3, 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/utils.mk
include /usr/share/cdbs/1/rules/debhelper.mk

pkg = $(DEB_SOURCE_PACKAGE)

# Need nodejs 6, and a nodejs providing /usr/bin/node (see bug#862051)
# TODO: simplify when bullseye is stable
deps = nodejs (>= 6.11.2~dfsg) | nodejs-legacy (>= 6)

CDBS_BUILD_DEPENDS += , $(deps)
CDBS_DEPENDS_$(pkg) = $(deps)
CDBS_RECOMMENDS_libjs-$(pkg) = javascript-common
CDBS_RECOMMENDS_$(pkg)-doc = libjs-coffeescript, libjs-jquery, libjs-underscore
CDBS_SUGGESTS_$(pkg) = libjs-$(pkg), coffeescript-doc
CDBS_SUGGESTS_libjs-$(pkg) = coffeescript
CDBS_PROVIDES_coffeescript = node-coffeescript

# copyright-check is done manually by invoking debian/copyright-check
CDBS_BUILD_DEPENDS_rules_utils_copyright-check =

# Provide "cake" as "cake.coffeescript" to peacefully coexist with cakephp
cake = cake.coffeescript

DEB_INSTALL_DOCS_ALL += README.md
DEB_INSTALL_EXAMPLES_$(pkg) += documentation/examples/*

# put aside upstream-shipped autogenerated files during build
DEB_UPSTREAM_CRUFT_COPY = lib

# Compile from itself (build twice to mimic build:full without testing)
# TODO: compile docs when docco is in Debian
CDBS_BUILD_DEPENDS += , node-uglify (>= 2.2), node-underscore, jison
build/$(pkg):: debian/stamp-build
debian/stamp-build:
	mkdir -p docs/v1/browser-compiler
	chmod +x bin/cake
	bin/cake build
	bin/cake build
	bin/cake build:browser
	$(if $(filter nocheck,$(DEB_BUILD_OPTIONS)),,bin/cake test)
	$(if $(filter nocheck,$(DEB_BUILD_OPTIONS)),,bin/cake test:browser)
	mv docs/v1/browser-compiler/coffee-script.js debian/coffee-script.min.js
	MINIFY=false bin/cake build:browser
	$(if $(filter nocheck,$(DEB_BUILD_OPTIONS)),,bin/cake test:browser)

	touch $@
clean::
	rm -f debian/coffee-script.min.js
	rm -rf docs/v1/browser-compiler
	rm -f debian/stamp-build

install/$(pkg)::
	mkdir -p $(cdbs_curdestdir)/usr/lib/coffee-script $(cdbs_curdestdir)/usr/bin
	cp -rf bin lib LICENSE README.md package.json src $(cdbs_curdestdir)/usr/lib/coffee-script
	ln -sfn /usr/lib/coffee-script/bin/coffee $(cdbs_curdestdir)/usr/bin/coffee
	ln -sfn /usr/lib/coffee-script/bin/cake $(cdbs_curdestdir)/usr/bin/$(cake)
	mkdir -p $(cdbs_curdestdir)/usr/lib/nodejs
	ln -sfn /usr/lib/coffee-script/lib/coffee-script $(cdbs_curdestdir)/usr/lib/nodejs/coffee-script

# generate manpage based on --help option of script itself
CDBS_BUILD_DEPENDS += , help2man
manpages = debian/coffee.1
DEB_INSTALL_MANPAGES_$(pkg) = $(manpages)
common-binary-indep:: $(manpages)
$(manpages): install/$(pkg)
$(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/$(pkg)::
	chmod -R a-x,a+X debian/$(pkg)/usr/lib/coffee-script/lib/ debian/$(pkg)/usr/lib/coffee-script/src/
