#!/usr/bin/make -f

source = $(shell find src/ -name '*.js')
timestamp = $(shell date --iso-8601=minutes --utc --date="$(shell dpkg-parsechangelog -S Date)" | sed -e 's/+0000/Z/')
version = $(shell dpkg-parsechangelog -SVersion | cut -d + -f 1)

override_dh_auto_build: dist/jquery.js dist/jquery.min.js dist/jquery.min.map
	dh_auto_build

build.js: debian/build.js
	cp $< $@

dist/jquery.js: build.js $(source)
	nodejs /usr/lib/nodejs/r.js -o build.js
	sed -i -e 's/@DATE/$(timestamp)/; s/@VERSION/$(version)/' $@
	sed -i -e 's/\/\/"use strict";/\/\//' $@

dist/jquery.min.js dist/jquery.min.map: dist/jquery.js
	uglifyjs \
		--source-map dist/jquery.min.map \
		--source-map-url jquery.min.map \
		--output dist/jquery.min.js \
		$^
	sed -i -e '/\/\/# sourceMappingURL=\S\+/ d' dist/jquery.min.js

override_dh_auto_clean:
	$(RM) -rf dist/
	$(RM) build.js
	dh_auto_clean

%:
	dh $@
