#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

%:
	dh $@ --with autotools-dev

override_dh_auto_build:
	dh_auto_build
	mkdir -p debian/generated-code/
	uglifyjs debian/missing-sources/bootstrap.ajaxpager-0.8.js > debian/generated-code/bootstrap.ajaxpager-0.8.min.js
	uglifyjs debian/missing-sources/bootstrap.js > debian/generated-code/bootstrap.min.js
	uglifyjs debian/missing-sources/cal-heatmap.js > debian/generated-code/cal-heatmap.min.js
	uglifyjs debian/missing-sources/d3.v2.js > debian/generated-code/d3.v2.min.js
	uglifyjs debian/missing-sources/jquery-1.10.2.js > debian/generated-code/jquery.min.js
	uglifyjs debian/missing-sources/jquery.peity.js > debian/generated-code/jquery.peity.min.js
	uglifyjs debian/missing-sources/jquery.sparkline.js > debian/generated-code/jquery.sparkline.min.js
	uglifyjs debian/missing-sources/serializeCFJSON-0.1.js > debian/generated-code/serializeCFJSON-0.1.min.js
	uglifyjs debian/missing-sources/typeahead.js > debian/generated-code/typeahead.min.js
	coffee -c -o debian/generated-code/ debian/missing-sources/gauge
	uglifyjs debian/generated-code/gauge.js > debian/generated-code/gauge.min.js
	cleancss debian/missing-sources/bootstrap-responsive.css > debian/generated-code/bootstrap-responsive.min.css
	cleancss httpdocs/bootstrap/css/bootstrap.css > debian/generated-code/bootstrap.min.css

override_dh_auto_install:

override_dh_strip:
	dh_strip --dbg-package=ntopng-dbg

override_dh_install-indep:
	dh_install --indep --exclude=redis.lua --exclude=font-awesome --exclude=bootstrap_3 --exclude=.source-map.js
	#rm -fr $(CURDIR)/debian/ntopng-data/usr/share/ntopng/httpdocs/bootstrap
	install -p -o root -g root -m 644 -t \
	    $(CURDIR)/debian/ntopng-data/usr/share/ntopng/scripts/lua/modules \
	    $(CURDIR)/third-party/redis-lua/src/redis.lua
	rm $(CURDIR)/debian/ntopng-data/usr/share/ntopng/httpdocs/js/README.gauge

override_dh_install-arch:
	dh_install --arch --exclude=redis.lua --exclude=font-awesome --exclude=bootstrap_3

override_dh_auto_clean:
	dh_auto_clean
	rm -fr debian/generated-code/

DEB_UPSTREAM_VERSION=$(shell dpkg-parsechangelog \
		     | sed -rne 's/^Version: ([^-]+).*/\1/p')
PACKAGE=ntopng
SRC_VERSION := $(shell dpkg-parsechangelog | sed -ne 's/^Version: \(\([0-9]\+\):\)\?\(.*\)-.*/\3/p')
SVN_REVISION := $(shell echo $(SRC_VERSION) | cut -d + -f 2 | sed 's/svn//' )
UPSTREAM_VERSION := $(shell echo $(SRC_VERSION) | cut -d + -f 1)
TARBALL=$(PACKAGE)_$(SRC_VERSION).orig.tar.gz
UPSTREAM_SVN_REPO = https://svn.ntop.org/svn/ntop/trunk/ntopng/

get-orig-source:
ifneq ($(findstring +svn,$(SRC_VERSION)),)
	rm -rf get-orig-source ../$(TARBALL)
	mkdir get-orig-source
	svn export -r $(SVN_REVISION) $(UPSTREAM_SVN_REPO) \
	    get-orig-source/$(PACKAGE)-$(SRC_VERSION).orig
	GZIP='--best --no-name' tar czf ../$(TARBALL) -C get-orig-source \
	     $(PACKAGE)-$(SRC_VERSION).orig
	rm -rf get-orig-source
else
    ifneq ($(findstring +dfsg,$(SRC_VERSION)),)
	uscan --verbose --force-download --download-version $(UPSTREAM_VERSION)
	rm -rf get-orig-source ../$(TARBALL)
	mkdir get-orig-source
	tar zxf ../$(PACKAGE)_$(UPSTREAM_VERSION).orig.tar.gz -C get-orig-source
	mv get-orig-source/* get-orig-source/$(PACKAGE)-$(SRC_VERSION).orig
	# Remove unused 3rd party libs
	rm -fr get-orig-source/*/httpdocs/bootstrap_3
	rm -fr get-orig-source/*/third-party/LuaJIT-*
	rm -fr get-orig-source/*/third-party/json-c
	rm -fr get-orig-source/*/third-party/rrdtool-*
	rm -fr get-orig-source/*/third-party/zeromq-*
	# Remove pre-compiled and auotmatically generated files
	find get-orig-source/ '(' -name .libs -or -name .svn -or \
	    -name autom4te.cache ')' -prune -exec rm -fr {} \;
	find get-orig-source/*/third-party/EWAHBoolArray/ \
	    -type f -perm /111 -delete
	find get-orig-source/ '(' \
	    -name '*.a' -or -name '*.la' -or \
	    -name '*.o' -or -name '*.lo' -or \
	    -name '*.so' -or -name '*.so.*' -or \
	    -name '*~' -or -name '#*#' -or \
	    -name 'Geo*.dat' ')' -delete
	# Remove unsed files without source.
	rm get-orig-source/*/httpdocs/js/highlight.min.js
	rm get-orig-source/*/httpdocs/js/prettify.js
	rm get-orig-source/*/httpdocs/css/prettify.css
	GZIP='--best --no-name' tar czf ../$(TARBALL) -C get-orig-source \
	     $(PACKAGE)-$(SRC_VERSION).orig
	rm -rf get-orig-source
    else
	uscan --verbose --download-version $(UPSTREAM_VERSION)
    endif
endif

.PHONY: override_dh_auto_install override_dh_strip override_dh_install-indep
.PHONY: override_dh_install-arch get-orig-source
