#!/usr/bin/make -f

export DH_GOLANG_BUILDPKG := github.com/scip-code/scip/cmd/scip

# cmd/scip embeds version.txt via //go:embed, and TestCLIReferenceInSync
# reads docs/CLI.md relative to the package directory. dh-golang only
# installs source files with known extensions by default.
export DH_GOLANG_INSTALL_EXTRA := cmd/scip/version.txt docs

# The upstream repository is a Go workspace (go.work) spanning several
# modules. Building against the system dependencies requires the regular
# module mode, so disable the workspace.
export GOWORK := off

# Reproducible drops the git revision suffix from "scip --version".
# buildmode=pie produces a position-independent executable (hardening).
export GOFLAGS := -ldflags=-X=main.Reproducible=true -buildmode=pie

# The Debian binary is installed as scip-code to avoid a filename conflict
# with the unrelated scip package (scipopt.org constraint integer
# programming solver). Upstream still calls the tool scip.
BINARY_NAME := scip-code

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

# golang-github-k0kubun-pp-dev declares module github.com/k0kubun/pp/v3 but
# installs its source at src/github.com/k0kubun/pp/, so GOPATH-mode imports
# of github.com/k0kubun/pp/v3 fail to resolve. Expose the v3 import path by
# replacing the dh-golang-created symlink with a real directory containing
# a v3 -> pp symlink to the system source.
override_dh_auto_configure:
	dh_auto_configure
	rm -f _build/src/github.com/k0kubun
	mkdir -p _build/src/github.com/k0kubun/pp
	ln -sfn /usr/share/gocode/src/github.com/k0kubun/colorstring \
	        _build/src/github.com/k0kubun/colorstring
	ln -sfn /usr/share/gocode/src/github.com/k0kubun/pp \
	        _build/src/github.com/k0kubun/pp/v3

override_dh_auto_install:
	dh_auto_install -- --no-source
	# dh-golang names the binary after the leaf of the Go import path
	# (cmd/scip -> scip). Rename it to avoid conflicting with the
	# existing Debian scip package.
	mv debian/scip-code/usr/bin/scip debian/scip-code/usr/bin/$(BINARY_NAME)

override_dh_auto_test:
	# The bindings/go/scip module is built and tested separately upstream;
	# here we only test the cmd/scip command.
	DH_GOLANG_EXCLUDES="bindings reprolang" dh_auto_test
