#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright 2010-2011, 2013-2014, 2016 Jonas Smedegaard <dr@jones.dk>
# Copyright 2012 Andrew Harvey <andrew.harvey4@gmail.com>
# Description: Main Debian packaging script for Leaflet.markercluster
#
# 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

# package variables
pkg = libjs-leaflet-markercluster
install-stem = leaflet

# Build-depend unversioned on debhelper
#  TODO: Drop when adopted in cdbs
CDBS_BUILD_DEPENDS_rules_debhelper_v9 = debhelper

CDBS_BUILD_DEPENDS +=, node-uglify (>= 2), node-jake
CDBS_DEPENDS_$(pkg) = libjs-leaflet (>= 0.7.1)
CDBS_RECOMMENDS_$(pkg) = javascript-common


#
# Installation
#

js-destwebdir = /javascript/$(install-stem)
js-destdir = /usr/share$(js-destwebdir)
exampledir = $(cdbs_curdestdir)usr/share/doc/$(cdbs_curpkg)/examples

# install files to the js-dest-dir as defined by javascript-common
DEB_DH_INSTALL_ARGS_$(pkg) = dist/* $(js-destdir)

# install documentation
DEB_INSTALL_DOCS_$(pkg) = README.md

# install upstream changelog
DEB_INSTALL_CHANGELOGS_$(pkg) = CHANGELOG.md

DEB_INSTALL_EXAMPLES_$(pkg) = example/*


#
# Build
#

# rule for building project from source
build/$(pkg):: debian/stamp-jake
debian/stamp-jake:
	jake
	touch $@
clean::
	rm -f debian/stamp-jake
	rm -f dist/*.js

# drop examples needing DFSG-nonfree or raw source files
binary-post-install/$(pkg)::
	rm -rf $(exampledir)/old-bugs
	rm -rf $(exampledir)/*-realworld-*

# use common Debian relative paths (not CDN or source-relative)
binary-fixup/$(pkg)::
	perl -pi \
		-e '$$cdn=qr!http://cdn.leafletjs.com/leaflet-[^/]*!;' \
		-e '$$src=qr!\.\./dist!;' \
		-e 's,(href|src)="\K($$cdn|$$src),$(js-destwebdir),g' \
		$(exampledir)/*.html
