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

DEBIAN_DIR = $(CURDIR)/debian
TMP     = $(DEBIAN_DIR)/$(PACKAGE)
SOURCE = $(TMP)/../

export DH_GOLANG_GO_GENERATE := 1
export DH_GOPKG := github.com/influxdb/influxdb

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

override_dh_auto_configure:
	dh_auto_configure

	# Include tests files
	cp -v $(SOURCE)/stress/stress.toml _build/src/$(DH_GOPKG)/stress/stress.toml

	# Install Web UI components
	cp -v -r $(SOURCE)/shared _build/src/$(DH_GOPKG)/shared

	# Forwarded usptream. There is no need for css files to be executable ;)
	chmod -v -x  _build/src/$(DH_GOPKG)/shared/admin/css/dropdowns-enhancement.css

	# Replace bundled javascript/css with our own
	mkdir -v _build/src/$(DH_GOPKG)/shared/admin/js/vendor

	ln -v -s /usr/share/javascript/bootstrap/js/bootstrap.min.js _build/src/$(DH_GOPKG)/shared/admin/js/vendor/bootstrap-3.3.5.min.js
	ln -v -s /usr/share/javascript/jquery/jquery.min.js _build/src/$(DH_GOPKG)/shared/admin/js/vendor/jquery-2.1.4.min.js
	uglifyjs $(TMP)/missing-sources/react-0.13.3.js --output _build/src/$(DH_GOPKG)/shared/admin/js/vendor/react-0.13.3.min.js
	ln -v -s /usr/share/javascript/bootstrap/css/bootstrap.css _build/src/$(DH_GOPKG)/shared/admin/css/bootstrap.css

	# glyphicons-halflings-regular.*
	rm --verbose _build/src/$(DH_GOPKG)/shared/admin/fonts/glyphicons-halflings-regular.*
	ln -s /usr/share/javascript/bootstrap/fonts/glyphicons-halflings-regular.*  _build/src/$(DH_GOPKG)/shared/admin/fonts/

	# Rebuild statik.go
	rm --verbose _build/src/$(DH_GOPKG)/statik/statik.go
	cd _build/src/$(DH_GOPKG) && golang-statik -src=./shared/admin
	go fmt _build/src/$(DH_GOPKG)/statik/statik.go

# influxdb version
DEB_VERSION := $(shell dpkg-parsechangelog -l$(DEBIAN_DIR)/changelog --show-field Version)
DEB_NOEPOCH_VERSION := $(shell echo $(DEB_VERSION) | cut -d: -f2-)
DEB_UPSTREAM_VERSION := $(shell echo $(DEB_NOEPOCH_VERSION) | sed 's/-[^-]*$$//' | sed 's/[+~].*$$//')

override_dh_auto_build:
	dh_auto_build -- -ldflags="-X main.version=$(DEB_UPSTREAM_VERSION)"

# Don't tests on unsupported architectures. Test are unstable.
# In the future, we should only skip cluster tests.
SKIP_TESTS := True

ifeq ($(DEB_HOST_ARCH_CPU),amd64)
SKIP_TESTS := False
endif

ifeq ($(DEB_HOST_ARCH_CPU),i386)
SKIP_TESTS := False
endif

override_dh_auto_test:
	if [ "$(SKIP_TESTS)" = "True" ]; then \
		echo "Skipping tests"; \
	else \
		dh_auto_test -- -timeout 12000s; \
	fi
