#!/usr/bin/make -f

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

PKD  = $(abspath $(dir $(MAKEFILE_LIST)))
PKG  = $(word 2,$(shell dpkg-parsechangelog -l$(PKD)/changelog | grep ^Source))
VER ?= $(shell dpkg-parsechangelog -l$(PKD)/changelog | perl -ne 'print $$1 if m{^Version:\s*([\d\.]+)}')

export DH_GOPKG := github.com/appc/acbuild
#export DH_GOLANG_INSTALL_ALL := 1
#export DH_GOLANG_EXCLUDES :=
GOLDFLAGS ?= \
    -X $(DH_GOPKG)/lib.Version=$(VER)

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

override_dh_clean:
	dh_clean
	## 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:
	mkdir -p _build && cp -r vendor _build/
	dh_auto_configure

override_dh_auto_build:
	dh_auto_build -- \
            -ldflags "$(GOLDFLAGS)"

override_dh_auto_test:
	-ACBUILD_BIN="$(CURDIR)/_build/bin/acbuild" dh_auto_test

override_dh_auto_install:
	dh_auto_install --destdir=debian/tmp
	## Build man pages:
	dh_auto_build -- \
            -tags 'manpages' -ldflags "$(GOLDFLAGS)"
	cd _build && bin/acbuild gen-man-pages
