#!/usr/bin/make -f

debRreposname   := $(shell dpkg-parsechangelog | awk '/^Source:/ {print $$2}' | sed 's/r-\([a-z]\+\)-.*/\1/')
awkString       := "'/^(Package|Bundle):/ {print $$2 }'"
cranNameOrig    := $(shell awk "$(awkString)" DESCRIPTION)
cranName        := $(shell echo "$(cranNameOrig)" | tr A-Z a-z)
package         := r-$(debRreposname)-$(cranName)
debRdir         := usr/lib/R/site-library
debRlib         := $(CURDIR)/debian/$(package)/$(debRdir)

%:
	dh $@ --buildsystem R

override_dh_install:
	dh_install
	sed -i -e 's|http.*/jquery.min.js|file://usr/share/javascript/jquery/jquery.min.js|' \
		$(debRlib)/$(cranName)/www/reactive-graph.html
	find debian -name "LICENSE*" -delete
	rm -rf $(debRlib)/$(cranName)/www/shared/showdown/license.txt
	mkdir -p $(debRlib)/$(cranName)/www/shared/bootstrap/shim
	yui-compressor debian/missing-sources/respond.src.js > $(debRlib)/$(cranName)/www/shared/bootstrap/shim/respond.min.js
	mkdir -p $(debRlib)/$(cranName)/www/shared/selectize/js
	yui-compressor debian/missing-sources/selectize.js > $(debRlib)/$(cranName)/www/shared/selectize/js/selectize.min.js
	mkdir -p $(debRlib)/$(cranName)/www/shared/strftime
	yui-compressor debian/missing-sources/strftime.js > $(debRlib)/$(cranName)/www/shared/strftime/strftime-min.js
	# this fails: yui-compressor debian/missing-sources/babel-polyfill.js > $(debRlib)/$(cranName)/www/shared/babel-polyfill.min.js
	# see https://lists.debian.org/debian-mentors/2016/11/msg00102.html
	cp -a debian/missing-sources/babel-polyfill.min.js $(debRlib)/$(cranName)/www/shared/babel-polyfill.min.js
