#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk
VER  ?= $(shell sh -c 'printf "$${1%%+dfsg*}"' -- "$(DEB_VERSION_UPSTREAM)")

export DH_GOLANG_GO_GENERATE := 1
export DH_GOPKG = gitlab.com/gitlab-org/gitlab-runner
export DH_GOPKG_common = $(DH_GOPKG)/common
#export DH_GOLANG_INSTALL_EXTRA := vendor
#export DH_GOLANG_INSTALL_ALL := 1
#export DH_GOLANG_EXCLUDES = apps/gitlab-runner-helper

## Set some build variables as per Makefile:
GO_LDFLAGS ?= \
    -X $(DH_GOPKG_common).VERSION=$(VER) \
    -X $(DH_GOPKG_common).REVISION=$(VER)
#    -X $(DH_GOPKG_common).NAME=$(DEB_SOURCE)
#    -X $(DH_GOPKG_common).BUILT=$(BUILT)
#    -X $(DH_GOPKG_common).BRANCH=$(BRANCH)

MKPI=debian/mk-prebuilt-images.sh

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

override_dh_clean:
	dh_clean $(MKPI)
	## 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`
	-find vendor -type d -empty -delete -print
	-find . -type f -name '*.generated.go' -print -delete ## delete .generated.go files

$(MKPI):
	sed -e 's/__VERSION__/$(VER)/g' $@.in > $@
	chmod --reference=$@.in $@

GITDIR=_build/src/$(DH_GOPKG)/tmp/gitlab-test
$(GITDIR):
	mkdir -v -p $@
	cd $@ && git init

override_dh_auto_configure: $(MKPI) $(GITDIR)
	dh_auto_configure

override_dh_auto_build:
	dh_auto_build --parallel -- \
            -ldflags "$(GO_LDFLAGS)"

override_dh_auto_install:
	dh_auto_install --destdir=debian/tmp

override_dh_auto_test:
	mkdir -v -p $(CURDIR)/debian/tmp
	HOME=$(CURDIR)/debian/tmp \
        dh_auto_test -- \
            -ldflags "$(GO_LDFLAGS)"
#-test.short
