#!/usr/bin/make -f

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

#DH_VERBOSE=1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
# Causes static variable to become having multiple instances, so we need to strip it away
# See https://github.com/singularityware/singularity/issues/1947
export DEB_LDFLAGS_MAINT_STRIP=-Wl,-Bsymbolic-functions

PKGDIR=debian/singularity-container
export SINGULARITY_CACHEDIR=$(PKGDIR)/var/lib/singularity/cache

export GOPATH=/usr/share/gocode:$(CURDIR)/go
SRCDIR = go/src/github.com/sylabs/singularity

export DESTDIR:=$(CURDIR)/$(PKGDIR)/

%:
	dh $@ --with golang

override_dh_auto_configure:
# prepare source directory the way the build system wants it
# https://www.sylabs.io/guides/3.0/user-guide/quick_start.html#compile-the-singularity-binary
	mkdir -p $(SRCDIR)
	find -maxdepth 1 ! -name debian ! -name go ! -name ".git*" ! -name "."\
	  -exec cp -r -t $(SRCDIR) {} +
# perform the configuration
	cd $(SRCDIR) \
	&& ./mconfig \
	-V $(DEB_VERSION_UPSTREAM) \
	--prefix=/usr \
	--sysconfdir=/etc \
	--libexecdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
	--localstatedir=/var/lib

override_dh_auto_build:
	make -C $(SRCDIR)/builddir

override_dh_auto_test:
	make -C $(SRCDIR)/builddir check

override_dh_auto_install:
	make -C $(SRCDIR)/builddir install
	: # move bash completions into now new standard location
	mv $(PKGDIR)/etc/bash_completion.d $(PKGDIR)/usr/share/bash-completion/completions

override_dh_installman:
	: # Very sloppy man pages for now
	debian/generate_manpages $(PKGDIR) $(DEB_VERSION)
	dh_installman

override_dh_fixperms:
	dh_fixperms
	chown root.root $(PKGDIR)/usr/lib/*/singularity/bin/*
	chmod 4755 $(PKGDIR)/usr/lib/*/singularity/bin/*-suid

override_dh_auto_clean:
	-test -d $(SRCDIR) && $(RM) -r go
	dh_auto_clean


# dh_golang uses `go list` to find the dependencies, then maps them
# to Debian packages. However, it wipes out the GOPATH and forces it
# to look only in <build-directory>/src. It's supposed to have symlinked
# the central libraries from /usr/share/gocode into here, but this doesn't
# appear to be happening (maybe since we aren't using `--buildsystem=golang`).
# I worked around this by using GNU stow to do the symlinking. I also stow
# the vendored libraries in to prevent dh_golang from failing.
override_dh_golang:
	stow vendor -t go/src
	ln -s /usr/share/gocode/src gocode
	stow gocode --defer '.*crypto' --defer '.*containernetworking' -t go/src/
	dh_golang --builddirectory=go
# clean up
	stow -D vendor -t go/src
	stow -D gocode -t go/src
	$(RM) gocode
