#!/usr/bin/make -f

pkgsrc = $(shell LC_ALL=C dpkg-parsechangelog --show-field Source )
pkgver = $(shell LC_ALL=C dpkg-parsechangelog --show-field Version )

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

# Needed by debchange to set Name and EMAIL in changelog
# DEBFULLNAME is filtered out by debuild
# use DEB_FULLNAME instead, which will set DEBFULLNAME
ifdef DEB_FULLNAME
export DEBFULLNAME=$(DEB_FULLNAME)
endif
# DEBEMAIL is not filtered out by debuild 

# request verbose debhelper here or from environment
DH_VERBOSE=1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# if verbose is requested do mconfig in verbose mode, too.
ifdef DH_VERBOSE
SC_VERBOSE = -v
endif

# For locally compiled go
MINGO_VERSION = $(shell scripts/get-min-go-version)
GOROOT = $${TMPDIR:-/tmp}/appdebgo/go
GOCACHE = $${TMPDIR:-/tmp}/appdebgo/cache

# get version via script
# Transform version numbers so that they 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
SC_VERSION = $(shell scripts/get-version | sed -e 's,\(^[^+]\+\)-,\1~,; s,-,.,g')
NEW_VERSION = $(shell dpkg --compare-versions $(SC_VERSION) gt $(pkgver) && echo $(SC_VERSION) )

# these can be overwritten by environment variables
DEB_SC_PROFILE ?= release
DEB_SC_BUILDDIR ?= builddir

# see mconfig for options
# set environment variables to disable options
# NOALL, NONETWORK, NOSECCOMP, NOLIBSUBID
SC_OPTIONS =
ifdef DEB_NOALL
SC_OPTIONS = --without-network --without-seccomp --without-libsubid
else
ifdef DEB_NONETWORK
SC_OPTIONS += --without-network
endif
ifdef DEB_NOSECCOMP
SC_OPTIONS += --without-seccomp
endif
ifdef DEB_NOLIBSUBID
SC_OPTIONS += --without-libsubid
endif
endif

MAKEPARALLEL = $(shell nproc --ignore=2 || echo 1 )
override pkgdir = debian/tmp
override apppkgdir = debian/apptainer

export APPTAINER_CACHEDIR=$(pkgdir)/var/lib/apptainer/cache

%:
	dh $@  --with=autoreconf

override_dh_auto_configure:
	@export GOPATH=$(GOROOT)/bin PATH=$(GOROOT)/bin:$$PATH GOCACHE=$(GOCACHE); \
	  set -ex; \
	  if ! ./mlocal/scripts/check-min-go-version go $(MINGO_VERSION); then \
	    if [ -d $(GOROOT) ]; then rm -rf $(GOROOT); fi; \
	    mkdir -p $(GOROOT); \
	    HERE=$$PWD; \
	    cd $(GOROOT)/..; \
	    tar -xf $$HERE/debian/go$(MINGO_VERSION).src.tar.gz; \
	    cd go/src; \
	    ./make.bash; \
	    cd $$HERE; \
	  fi; \
	  ./scripts/compile-dependencies debian
ifneq ($(NEW_VERSION),)
	$(warning "Setting new version in debian changelog: $(NEW_VERSION)")
	@debchange -v $(NEW_VERSION)$(VERSION_POSTFIX) "Version $(NEW_VERSION)" && debchange -m -r ""
endif
	@PATH=$(GOROOT)/bin:$$PATH ./mconfig $(SC_VERBOSE) -b $(DEB_SC_BUILDDIR) -P $(DEB_SC_PROFILE) --with-suid $(SC_OPTIONS) \
                --prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var/lib \
		--mandir=/usr/share/man

override_dh_auto_build:
	@PATH=$(GOROOT)/bin:$$PATH GOCACHE=$(GOCACHE) dh_auto_build -Smakefile --parallel --max-parallel=$(MAKEPARALLEL) -D$(DEB_SC_BUILDDIR)

override_dh_auto_install:
	@dh_auto_install -Smakefile -D$(DEB_SC_BUILDDIR)
	@./scripts/install-dependencies $(pkgdir)/usr/libexec
# Apparmor userns profile needed on Ubuntu 24.04, or unconfined placeholder for older versions.	
	if ([ $(OS_MAJOR) -gt 23 ] || [ "$(OS_VERSION)" = "23.10" ]) && [ "$(OS_NAME)" = "Ubuntu" ]; then \
		echo "Ubuntu 23.10 or newer - installing apparmor userns profile"; \
		install -D -m 644 dist/debian/apparmor-userns $(pkgdir)/etc/apparmor.d/apptainer; \
	else \
		echo "other debian/ubuntu distros - installing apparmor placeholder profile"; \
		install -D -m 644 dist/debian/apparmor-placeholder $(pkgdir)/etc/apparmor.d/apptainer; \
	fi;
	dh_apparmor --profile-name=apptainer

override_dh_install:
	@dh_install -papptainer-suid usr/libexec/apptainer/bin/starter-suid
	@rm -f $(pkgdir)/usr/libexec/apptainer/bin/starter-suid
	@dh_install -papptainer

override_dh_auto_test:

override_dh_fixperms:
	@dh_fixperms
	@EXE=`echo $(apppkgdir)-suid/usr/libexec/apptainer/bin/*-suid`; \
	  if [ -f "$$EXE" ]; then \
	    chmod 4755 $$EXE; \
	  fi

override_dh_clean:
	@rm -rf -- $(DEB_SC_BUILDDIR)
	@dh_clean
	@./scripts/clean-dependencies

# vim:ft=make:noet:sts=0
