#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright 2010-2011, 2013-2014, 2016-2018 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/debhelper.mk

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

CDBS_BUILD_DEPENDS +=, node-uglify, node-jake
CDBS_DEPENDS_$(pkg) = libjs-leaflet (>= 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) = debian/js/* dist/*.css $(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
	mkdir -p debian/js
	mv -f dist/leaflet.markercluster.js debian/js/leaflet.markercluster.min.js
	mv -f dist/leaflet.markercluster-src.js debian/js/leaflet.markercluster.js
	touch $@
clean::
	rm -f debian/stamp-jake
	rm -rf debian/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!https://unpkg.com/leaflet\@[^/]*/dist/!;' \
		-e '$$src=qr!\.\./dist!;' \
		-e 's,(href|src)="\K($$cdn|$$src),$(js-destwebdir),g' \
		$(exampledir)/*.html
