#!/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
	dh $@  --buildsystem=ruby --with=ruby --package=ruby-gitaly
	dh $@  --package=gitlab-common
	dh $@  --package=gitaly

override_dh_auto_clean:
	rm -rf ruby/proto/gitaly/*_pb.rb
	dh_auto_clean -O--buildsystem=golang -O--package=golang-gitlab-gitlab-org-gitaly-dev \
	-O--builddirectory=_build
	dh_auto_clean -O--buildsystem=ruby -O--package=ruby-gitaly

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/
	rm tools/protogem/Gemfile.lock
	RAILS_ENV=$(RAILS_ENV) BUNDLE_GEMFILE=tools/protogem/Gemfile \
	bundle install --local
	RAILS_ENV=$(RAILS_ENV) BUNDLE_GEMFILE=tools/protogem/Gemfile \
	bundle exec tools/protogem/build-proto-gem -o gitaly.gem -w .
	dh_auto_build -O--buildsystem=ruby -O--package=ruby-gitaly

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
	dh_auto_test -O--buildsystem=ruby -O--package=ruby-gitaly
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
	dh_auto_install -O--buildsystem=ruby -O--package=ruby-gitaly --destdir=debian/ruby-gitaly
	$(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
