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

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

include /usr/share/dpkg/pkg-info.mk

export DEB_BUILD_MAINT_OPTIONS = hardening=+all


%:
	dh $@ --with systemd

override_dh_auto_configure:
	./autogen.sh
	dh_auto_configure

override_dh_auto_build:
	# Make sure we rebuild the minified files from the upstream tarball.
	$(MAKE) -C httpdocs/js clean
	$(MAKE) -C httpdocs/js minify
	dh_auto_build
	mkdir -p debian/generated-code/
	coffee -c -o debian/generated-code/ debian/missing-sources/gauge.coffee

override_dh_auto_install:

override_dh_installdocs:
	dh_installdocs -pntopng
	dh_installdocs -pntopng-data --doc-main-package=ntopng

override_dh_strip:
	dh_strip --dbgsym-migration='ntopng-dbg (<< 2.4+dfsg1-1~)'

override_dh_install-indep:
	dh_install --indep \
	    --exclude=.source-map.js \
	    --exclude=EtherOUI.txt \
	    --exclude=Makefile \
	    --exclude=README.gauge \
	    --exclude=font-awesome \
	    --exclude=redis.lua \
	    --exclude=scripts/lua/modules/i18n \
	    --exclude=scripts/lua/modules/resty
	rm -fr $(CURDIR)/debian/ntopng-data/usr/share/ntopng/httpdocs/bootstrap
	find $(CURDIR)/debian/ntopng-data/usr/share -type f -exec chmod a-x {} \;
	rm -fr $(CURDIR)/debian/ntopng-data/usr/share/ntopng/httpdocs/ssl
	chmod +x $(CURDIR)/debian/ntopng-data/usr/share/ntopng/httpdocs/misc/ntopng-add-user.sh

override_dh_install-arch:
	dh_install --arch

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

PACKAGE=ntopng
UPSTREAM_VERSION := $(shell echo $(DEB_VERSION_UPSTREAM) | cut -d + -f 1)
TARBALL=$(PACKAGE)_$(DEB_VERSION_UPSTREAM).orig.tar.gz

get-orig-source:
	@echo DEB_VERSION_UPSTREAM $(DEB_VERSION_UPSTREAM)
	@echo UPSTREAM_VERSION $(UPSTREAM_VERSION)
ifneq ($(findstring +dfsg,$(DEB_VERSION_UPSTREAM)),)
	# Download upstream tarball and prepare for repackaging
	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)-$(DEB_VERSION_UPSTREAM).orig

	# Remove unused 3rd party libs
	rm -fr get-orig-source/*/httpdocs/css/Rickshaw
	rm -fr get-orig-source/*/third-party/hiredis
	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 unsed files without source.
	rm -f get-orig-source/*/httpdocs/js/pdfmake.js
	rm -f get-orig-source/*/httpdocs/js/vfs_fonts.js

	# Need to create orig tarball.
	GZIP='--best --no-name' tar czf ../$(TARBALL) -C get-orig-source \
	     $(PACKAGE)-$(DEB_VERSION_UPSTREAM).orig
	rm -rf get-orig-source
else
	# Download and use upstream vanilla tarball
	uscan --verbose --download-version $(UPSTREAM_VERSION)
endif

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