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

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

#export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-pie

export DH_GOPKG := github.com/grafana/grafana

TS=$(shell find public -type f -name '*.ts' -print)
JS=$(TS:ts=js)

%:
	dh $@ --buildsystem=golang --with=golang,systemd --builddirectory=_build --parallel

override_dh_clean:
	dh_clean $(JS)
	## Remove Files-Excluded (when built from checkout or non-DFSG tarball):
	$(RM) -rv `perl -0nE 'say $$1 if m{^Files\-Excluded\:\s*(.*?)(?:\n\n|Files:|Comment:)}sm;' debian/copyright`

override_dh_auto_configure:
	dh_auto_configure
	##
	## symlink github.com/macaron-contrib (legacy, to remove after 2.7.0):
	#$(RM) -f _build/src/github.com/macaron-contrib
	mkdir -p _build/src/github.com/macaron-contrib
	-ln -sfvr _build/src/$(DH_GOPKG)/Godeps/_workspace/src/github.com/macaron-contrib/*  _build/src/github.com/macaron-contrib/
	##
	## symlink github.com/go-macaron/*:
	$(RM) -f _build/src/github.com/go-macaron
	mkdir -p _build/src/github.com/go-macaron
	-ln -sv  /usr/share/gocode/src/github.com/go-macaron/*  _build/src/github.com/go-macaron/
	-ln -sfvr _build/src/$(DH_GOPKG)/Godeps/_workspace/src/github.com/go-macaron/*  _build/src/github.com/go-macaron/
	##
	## symlink src/gopkg.in/*:
	$(RM) -f _build/src/gopkg.in
	mkdir -p _build/src/gopkg.in
	-ln -sv  /usr/share/gocode/src/gopkg.in/*  _build/src/gopkg.in/
	-ln -sfvr _build/src/$(DH_GOPKG)/Godeps/_workspace/src/gopkg.in/*  _build/src/gopkg.in/

build-indep:
	## Build CSS: http://lesscss.org/usage/index.html
	-mkdir -p public/css
	cd public/css \
        && lessc --include-path=../less --include-path=../vendor/bootstrap/less ../less/bootstrap.dark.less grafana.dark.min.css \
        && lessc --include-path=../less --include-path=../vendor/bootstrap/less ../less/bootstrap.light.less grafana.light.min.css
	tsc --module amd $(TS)
## Probably should be
##	tsc -p .
## but doesn't work due to
##   error TS6053: File 'public/app/features/dashboard/timepicker/timerange.ts' not found.

override_dh_install:
	dh_install -XLICENSE -Xlicense.json
	## rename config file:
	mv -v debian/grafana/etc/grafana/sample.ini  debian/grafana/etc/grafana/grafana.ini

override_dh_link:
	## link minified .CSS and .JS with to their uncompressed versions:
	find $(CURDIR)/debian/grafana-data/usr -type f -name "*[-.]min.css" -or -name "*[-.]min.js" | \
        while read MF; do \
            UF="$$(echo $${MF} | perl -pE 's{[\.-]min\.(js|css)}{.$$1};')" ;\
            if [ -f "$${UF}" ] || [ -n "$$(readlink \"$${UF}\")" ]; then \
                ln -svfr "$${UF}" "$${MF}" ;\
            fi ;\
        done
	dh_link

override_dh_fixperms:
	dh_fixperms
	## executable-not-elf-or-script:
	-chmod -R -c a-x+X \
            debian/grafana-data/usr/share/grafana/public

override_dh_installchangelogs:
	dh_installchangelogs CHANGELOG.md

#override_dh_installinit:
#	dh_installinit --no-start

#override_dh_systemd_start:
#	dh_systemd_start --no-start

#override_dh_systemd_enable:
#	dh_systemd_enable --no-enable

override_dh_auto_test:
	#-dh_auto_test
