#!/usr/bin/make -f

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

export DH_GOPKG := github.com/coreos/fleet
export DH_GOLANG_BUILDPKG := $(DH_GOPKG)/fleetd $(DH_GOPKG)/fleetctl

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\.]+)}')

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

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:
	dh_auto_configure
	perl -0nE 'say $$1 if m{^Files\-Excluded\:\s*(.*?)(?:\n\n|Files:|Comment:)}sm;' debian/copyright \
        | while read D; do D="$${D##Godeps/_workspace/src/}" ; printf "Removing third party bundle: $$D\n" ;\
            perl -pi -E "s{$(DH_GOPKG)/Godeps/_workspace/src/$$D}{$$D}msg;" `find _build/src -type f -name "*.go"` ;\
        done
	sed -i -e "s/0.0.0+wasnotbuiltproperly/$(VER)/" _build/src/$(DH_GOPKG)/version/version.go

override_dh_auto_install:
	dh_auto_install
	$(RM) -rv debian/fleet/usr/share/gocode
	mkdir debian/fleet/usr/sbin && mv -v debian/fleet/usr/bin/fleetd debian/fleet/usr/sbin/

