#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later

set -eu

suite=${suite:-$(debian-distro-info --stable)}
arch=$(dpkg --print-architecture)

if [ -z "${AUTOPKGTEST_TEST_UNINSTALLED-}" ]; then
    export AUTOPKGTEST_TEST_INSTALLED=yes
    build_incus=autopkgtest-build-incus
else
    build_incus=tools/autopkgtest-build-incus
fi

incus admin init --minimal

# Workaround for LP: #2109394.
apparmor_restrict=/proc/sys/kernel/apparmor_restrict_unprivileged_unconfined
if [ -r "$apparmor_restrict" ] && [ "$(cat "$apparmor_restrict")" = 1 ]; then
    incus profile set default raw.apparmor='signal peer=@{profile_name}//&unconfined, ptrace peer=@{profile_name}//&unconfined,'
fi

"$build_incus" "images:debian/$suite/$arch"

export AUTOPKGTEST_TEST_INCUS="autopkgtest/debian/$suite/$arch"
exec annotate-output ./tests/autopkgtest IncusRunner
