#!/usr/bin/make -f

export DH_VERBOSE = 1

# Generic hardening of binaries
# Note that passing -buildmode=pie separately is also needed.
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# Provides $(DEB_VERSION_UPSTREAM)
include /usr/share/dpkg/pkg-info.mk

# Control the scope of the entire build. The root of usql needs to be included
# so that the actual usql binary is included in dh_install, while the drivers
# whose dependencies are not in Debian are left out.
export DH_GOLANG_BUILDPKG = \
	github.com/xo/usql \
	github.com/xo/usql/drivers/metadata \
	github.com/xo/usql/drivers/metadata/informationschema \
	github.com/xo/usql/drivers/metadata/mysql \
	github.com/xo/usql/drivers/metadata/postgres \
	github.com/xo/usql/drivers/mymysql \
	github.com/xo/usql/drivers/mysql \
	github.com/xo/usql/drivers/sqlite3 \
	github.com/xo/usql/drivers/sqlite3/sqshared \
	github.com/xo/usql/env \
	github.com/xo/usql/handler \
	github.com/xo/usql/metacmd \
	github.com/xo/usql/metacmd/charts \
	github.com/xo/usql/rline \
	github.com/xo/usql/stmt \
	github.com/xo/usql/styles \
	github.com/xo/usql/text

# tblfmt vendors consolesize-go, which is ignored in workspace mode
CONSOLESIZE := github.com/nathan-fiscaletti/consolesize-go

# Map vendored modules to their copies in the build directory, as dh-go builds
# in workspace mode, which ignores vendor directories
export DH_GOLANG_MODULE_MAPPINGS = \
	github.com/jeandeaual/go-locale:github.com/xo/usql/vendor/github.com/jeandeaual/go-locale \
	github.com/kenshaw/colors:github.com/xo/usql/vendor/github.com/kenshaw/colors \
	github.com/kenshaw/rasterm:github.com/xo/usql/vendor/github.com/kenshaw/rasterm \
	github.com/yookoala/realpath:github.com/xo/usql/vendor/github.com/yookoala/realpath \
	$(CONSOLESIZE):DEBIAN/$(CONSOLESIZE)

# Decrease the scope of what is being built
export TAGS = \
	mymysql \
	mysql \
	sqlite \
	sqlite_app_armor \
	sqlite_fts5 \
	sqlite_introspect \
	sqlite_json1 \
	sqlite_math_functions \
	sqlite_stat4 \
	sqlite_vtable \
	no_clickhouse \
	no_csvq \
	no_oracle \
	no_sqlserver \
	no_moderncsqlite

# Pass TAGS to every go command, including the go list run to compute Built-Using
comma := ,
space := $(subst ,, )
export GOFLAGS = -tags=$(subst $(space),$(comma),$(strip $(TAGS)))

# Logo is embedded and thus must be included
export DH_GOLANG_INSTALL_EXTRA := text/logo.png

%:
	dh $@ --with=golang

# Copy vendored sources into build directory
execute_before_dh_auto_configure:
	cp -av debian/vendor ./
	# Remove extraneous files that don't belong to a Debian package
	find drivers/testdata -type f -name ".gitignore" -delete

execute_after_dh_clean:
	rm -rfv vendor/

# Provide tblfmt's vendored consolesize-go as a module for DH_GOLANG_MODULE_MAPPINGS
execute_after_dh_auto_configure:
	mkdir -p _build/src/DEBIAN/$(CONSOLESIZE)
	ln -sf /usr/share/gocode/src/github.com/xo/tblfmt/vendor/$(CONSOLESIZE)/*.go _build/src/DEBIAN/$(CONSOLESIZE)/
	echo "module $(CONSOLESIZE)" > _build/src/DEBIAN/$(CONSOLESIZE)/go.mod

# Customize build so that ldflags and PIE can be passed
override_dh_auto_build:
	dh_auto_build -- \
	  -ldflags="-linkmode=external -X github.com/xo/usql/text.CommandName=usql -X github.com/xo/usql/text.CommandVersion=$(DEB_VERSION_UPSTREAM)" \
		-buildmode=pie
