#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DH_GOLANG_GO_GENERATE := 1
#export DH_GOLANG_INSTALL_ALL := 1  # test data
#export DH_GOLANG_INSTALL_EXTRA := vendor
export DH_RUBY = --gem-install
export DH_RUBY_USE_DH_AUTO_INSTALL_DESTDIR = debian/ruby-gitaly
export GEM2DEB_TEST_RUNNER = --check-dependencies
export RAILS_ENV = development # for bundle install --local test
export GITALY_EXECUTABLES = $(addprefix gitlab.com/gitlab-org/gitaly/v16/cmd/,$(notdir $(shell find ${CURDIR}/cmd -mindepth 1 -maxdepth 1 -type d -print)))
# All executables packed inside the Gitaly binary.
export GITALY_PACKED_EXECUTABLES = $(filter %gitaly-hooks %gitaly-git2go %gitaly-ssh %gitaly-lfs-smudge, ${GITALY_EXECUTABLES})

include /usr/share/dpkg/pkg-info.mk
## Set some build variables as per Makefile:
VERSION  ?= $(shell sh -c 'printf "$${1%%+ds*}"' -- "$(DEB_VERSION_UPSTREAM)")
PKG := gitlab.com/gitlab-org/gitaly/v16
MODULE_VERSION := v16
GO_LDFLAGS ?= -X $(PKG)/internal/version.version=$(VERSION) -X $(PKG)/internal/version.moduleVersion=$(MODULE_VERSION)
GO_BUILD_TAGS ?= static,system_libgit2

%:
	dh $@  --buildsystem=golang --with=golang --package=golang-gitlab-gitlab-org-gitaly-dev \
	--builddirectory=_build

override_dh_auto_clean:
	dh_auto_clean -O--buildsystem=golang -O--package=golang-gitlab-gitlab-org-gitaly-dev \
	-O--builddirectory=_build

override_dh_auto_configure:
	dh_auto_configure -O--buildsystem=golang -O--package=golang-gitlab-gitlab-org-gitaly-dev \
	-O--builddirectory=_build

override_dh_auto_build:
	dh_auto_build -O--buildsystem=golang -O--package=golang-gitlab-gitlab-org-gitaly-dev \
	-O--builddirectory=_build -- \
	   -ldflags "$(GO_LDFLAGS)" -tags "$(GO_BUILD_TAGS)" \
           $(GITALY_PACKED_EXECUTABLES)
	mkdir -p _build/src/${PKG}/_build/ && cp -a _build/bin _build/src/${PKG}/_build/
	dh_auto_build -O--buildsystem=golang -O--package=golang-gitlab-gitlab-org-gitaly-dev \
	-O--builddirectory=_build -- \
	   -ldflags "$(GO_LDFLAGS)" -tags "$(GO_BUILD_TAGS)" \
           gitlab.com/gitlab-org/gitaly/${MODULE_VERSION}/cmd/gitaly
	rm -rf _build/src/${PKG}/_build/

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
	# Test hangs, so don't even run it
	#-dh_auto_test -O--buildsystem=golang -O--package=golang-gitlab-gitlab-org-gitaly-dev \
	#-O--builddirectory=_build
endif

override_dh_auto_install:
	dh_auto_install -O--buildsystem=golang -O--package=golang-gitlab-gitlab-org-gitaly-dev \
	-O--no-binaries --builddirectory=_build --destdir=debian/golang-gitlab-gitlab-org-gitaly-dev
	$(RM) -rv debian/golang-gitlab-gitlab-org-gitaly-dev/usr/bin
	# Remove testdata, contains files that triggers more lintian errors
	for f in `find debian/ -name testdata`; do \
		$(RM) -rv $$f; \
	done