#!/usr/bin/make -f

# Transform version numbers so that the match debian precedence / rules.
# https://www.debian.org/doc/debian-policy/ch-controlfields.html#version
#
# 3.4.2-rc.1            ->  3.4.2~rc.1
# 3.4.2                 ->  3.4.2
# 3.4.2+522-gee98ef356  ->  3.4.2+522.gee98ef356
srcver = $(shell scripts/get-version | sed -e 's,\(^[^+]\+\)-,\1~,; s,-,.,g')
dist   = $(shell lsb_release -s -c)

OS_VERSION := $(shell grep ^VERSION_ID /etc/os-release | cut -d'=' -f2 | sed 's/\"//gI')
OS_MAJOR := $(shell grep ^VERSION_ID /etc/os-release | cut -d'=' -f2 | sed 's/\"//gI' | cut -d'.' -f1)

DH_VERBOSE=1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Set a package name for dh-golang equal to the repo name
# The name doesn't really matter as we aren't doing a GOPATH build
# and are using modules
export DH_GOPKG=singularity-ce
# Make sure all files are copied into the _build/src dir
export DH_GOLANG_INSTALL_ALL := 1

# Build out of the main source tree
BUILDDIR = $(CURDIR)/_build
SRCDIR = $(BUILDDIR)/src

# Force use of a GO in /usr/local/go - the distro Go is likely to be too old
export PATH := /usr/local/go/bin:$(PATH)

# Install to debian/tmp as dh_install will then split from there into
# multiple packages according to our <package>.install files.
export DESTDIR=$(CURDIR)/debian/tmp

# Use a GOPATH that's independent of anything that's already set
export GOPATH=$(CURDIR)/_build/gopath

%:
	dh $@ --builddirectory=$(BUILDDIR) --buildsystem=golang

override_dh_auto_clean:
# Go makes some pkg cache etc. read-only, chmod before we try to remove
	-chmod -R +w _build
	dh_auto_clean

override_dh_auto_configure:
# Override the changelog file by adding an entry for our git-derived version number
	dch -D unstable -v "$(srcver)-$(dist)" -M "See: https://github.com/sylabs/singularity/blob/main/CHANGELOG.md"
	dch -r "" -M

	dh_auto_configure --builddirectory=_build
# Reset permissions lost in the copy to _build/src on Ubuntu
	cd $(SRCDIR)/$(DH_GOPKG) && \
	chmod +x mconfig scripts/* makeit/* e2e/testdata/*.sh

# Configure the standard build
	cd $(SRCDIR)/$(DH_GOPKG) && \
	./mconfig -v \
	  -b builddir \
	  -V $(srcver)-$(dist) \
	  --prefix=/usr \
	  --sysconfdir=/etc \
	  --libexecdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
	  --localstatedir=/var/lib \
	  --mandir=/usr/share/man

override_dh_auto_build:
# build standard install
	cd $(SRCDIR)/$(DH_GOPKG) && \
	make -C builddir

override_dh_auto_test:

override_dh_auto_install:
# install standard build
	cd $(SRCDIR)/$(DH_GOPKG) && \
	make -C builddir install
# Apparmor userns profile needed on Ubuntu >=23.10, or unconfined placeholder for older versions.
	if [ $(OS_MAJOR) -gt 23 ] || [ "$(OS_VERSION)" = "23.10" ]; then \
		echo "Ubuntu 24.04 or newer - installing apparmor userns profile"; \
		install -D -m 644 debian/apparmor-userns $(DESTDIR)/etc/apparmor.d/singularity-ce; \
	else \
		echo "Ubuntu 23.04 or older - installing apparmor placeholder profile"; \
		install -D -m 644 debian/apparmor-placeholder $(DESTDIR)/etc/apparmor.d/singularity-ce; \
	fi;
	dh_apparmor --profile-name=singularity-ce

override_dh_fixperms:
# dh_install copies from debian/tmp into debian/<package name> in the multi-package
# flow, and we need to set the suid perms in the final location, as we are running
# after this copy/split has happened.
	dh_fixperms
# perms for standard build
	chown root.root $(CURDIR)/debian/singularity-ce/usr/lib/$(DEB_HOST_MULTIARCH)/singularity/bin/*
	chmod 4755 $(CURDIR)/debian/singularity-ce/usr/lib/$(DEB_HOST_MULTIARCH)/singularity/bin/*-suid
