#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

#export DH_VERBOSE=1

# https://wiki.debian.org/HardeningWalkthrough
export DEB_BUILD_MAINT_OPTIONS  = hardening=+all
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

DH_GOPKG := github.com/google/fscrypt
TMP := $(CURDIR)/debian/tmp

# Partially borrowed from upstream Makefile
GO_LINK_FLAGS := -s -w
VERSION_FLAG := -X "main.version=v$(DEB_VERSION_UPSTREAM)"
GO_LINK_FLAGS += $(VERSION_FLAG) -extldflags "$(LDFLAGS)"
GO_FLAGS += --ldflags '$(GO_LINK_FLAGS)'

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

override_dh_auto_build:
	# Build the command line tool

	DH_GOLANG_BUILDPKG=$(DH_GOPKG)/cmd/... dh_auto_build -O--buildsystem=golang -- -buildmode=pie $(GO_FLAGS)

	# Build the PAM module as a C shared library.
	#
	# dh_auto_build doesn't support the ‘-o’ flag, so the module will be
	# named ‘.a’ while the original Makefile names is ‘.so’. We'll rename
	# it at install time.

	DH_GOLANG_BUILDPKG=$(DH_GOPKG)/pam_fscrypt/... dh_auto_build -O--buildsystem=golang -- -buildmode=c-shared $(GO_FLAGS)

override_dh_auto_install:
	# dh_auto_install will correctly install the ‘fscrypt’ binary and the Go
	# source files (if called without ‘--no-source’).
	dh_auto_install

	# dh_auto_install doesn't know how to deal with the shared library.
	# We have to install it manually.
	install -D \
		obj-$(DEB_HOST_MULTIARCH)/pkg/$(DEB_HOST_ARCH_OS)_$(DEB_HOST_ARCH_CPU)_shared/$(DH_GOPKG)/pam_fscrypt.a \
		$(TMP)/lib/$(DEB_HOST_MULTIARCH)/security/pam_fscrypt.so

	# Install the PAM module configuration file
	install -m 644 -D pam_fscrypt/config $(TMP)/usr/share/pam-configs/fscrypt

override_dh_auto_test:
	# Disable the test suite for now.
	# See: https://github.com/google/fscrypt/issues/99
