#!/bin/bash
# Helper for creating a autopkgtest/ROCM-capable podman images
#
# Author: Christian Kastner <ckk@kvr.at>
# License: MIT
set -eu

# Option defaults
mirror="http://deb.debian.org/debian"
release="unstable"

function usage() {
	cat >&2 <<-EOF

	Create a container image suitable for running ROCm package autopkgtests.

	IMAGE, if not specified, defaults to rocm/debian:RELEASE.

	The container will have a regular (non-system) user 'user', who will be
	in all the necessary system groups.

	Synopsis:
	  $0 -h

	  $0 [-m MIRROR] [-r RELEASE] [-u USER] [IMAGE]

	Options:
	  -h          Show this help
	  -m MIRROR   Download packages from here. This will be also used within
	              the image. Ideally, this points to an APT cache on the host
	              machine, such as apt-cacher-ng or approx.
	  -r RELEASE  Release (default: '$release')
	              If RELEASE is 'experimental', APT sources for both 'unstable'
	              and 'experimental' will be added to the image.
                  If RELEASE contains a dash, APT sources for the "basename"
	              will be added, e.g.: 'bookworm-backports' will include
	              sources for 'bookworm' and 'bookworm-backports'.

	Examples:

	  # Configure the system for GPU-in-container use

	  \$ rocm-podman-setup -u <user>

	  # Creates rocm/debian:unstable

	  \$ $0

	  # Same image as above, but tagged rocm/othertag

	  \$ $0 rocm/othertag

	  # Creates rocm/debian:bookworm

	  \$ $0 -m http://10.1.2.3:9999/debian -r bookworm

	EOF
	exit 0
}

while getopts "hm:r:u:" OPTNAME
do
	case $OPTNAME in
	h)  usage;;
	m)  mirror="$OPTARG";;
	r)  release="$OPTARG";;
	?)  usage;;
	esac
done
shift $((OPTIND - 1))

release_base=$(echo "$release" | sed -r 's/-.+$//')
[ "$release_base" != "experimental" ] || release_base="unstable"

script="/usr/share/debootstrap/scripts/$release_base"
if grep -q archive.ubuntu.com "$script"
then
	distro="ubuntu"
else
	distro="debian"
fi
imagename="${1:-rocm/$distro:$release}"


# /etc/apt/sources.list within the VM
if [ "$distro" = "ubuntu" ]
then
		AUTOPKGTEST_APT_SOURCES=$(cat <<- EOF
		deb     $mirror $release          main restricted universe multiverse
		deb-src $mirror $release          main restricted universe multiverse
		deb     $mirror $release-updates  main restricted universe multiverse
		deb-src $mirror $release-updates  main restricted universe multiverse
		deb     $mirror $release-security main restricted universe multiverse
		deb-src $mirror $release-security main restricted universe multiverse
EOF
)
else
	if [ "$release" != "$release_base" ]
	then
		AUTOPKGTEST_APT_SOURCES=$(cat <<- EOF
		deb     $mirror $release_base main contrib non-free-firmware
		deb-src $mirror $release_base main contrib non-free-firmware

	deb     $mirror $release main contrib non-free-firmware
	deb-src $mirror $release main contrib non-free-firmware
EOF
)
	else
		AUTOPKGTEST_APT_SOURCES=$(cat <<- EOF
		deb     $mirror $release main contrib non-free-firmware
		deb-src $mirror $release main contrib non-free-firmware
EOF
)
	fi
fi
export AUTOPKGTEST_APT_SOURCES

if [ ! -e /usr/bin/autopkgtest-build-podman ]
then
	echo "autopkgtest not installed on this system. Aborting." >&2
	exit 1
elif podman image exists "$imagename"
then
	echo "Image $imagename already exists, refusing to overwrite." >&2
	exit 1
fi

# Render group doesn't exist in base images
renderGID="$(getent group render | cut -d: -f3)"
postcmd="groupadd -g $renderGID render"
postcmd+="; useradd -m -G render,video rocm"
# This looks pointless, but is necessitated by how autopkgtest runs tests
# internally (through su, which behaves differently in containers)
postcmd+="; usermod -a -G render,video root"

# See https://bugs.debian.org/1032487
# This can be removed with autopkgtest >= 5.30
umask 0022

autopkgtest-build-podman \
	-t "$imagename" \
	-m "$mirror" \
	--post-command="$postcmd"
