#!/bin/sh
set -e

trap 'if [ "$?" -ne 0 ]; then systemctl --all | grep cockpit; journalctl -b; fi' EXIT

# HACK: debian stable kernel+LXC (running on ci.d.n) breaks DynamicUser=
# https://bugs.debian.org/1073815
if uname -r | grep -q ^6.1 && systemd-detect-virt --container; then
    echo " * working around broken DynamicUser= support on Debian 12 kernel+LXC"
    adduser --system --group --home /nonexistent --no-create-home --quiet cockpit-ws
    mkdir -p /etc/systemd/system/cockpit-ws-user.service.d
    printf '[Service]\nDynamicUser=no\n' > /etc/systemd/system/cockpit-ws-user.service.d/no-dynamic.conf
    systemctl daemon-reload
fi

check_out() {
    echo "$OUT" | grep -q "$1" || {
        echo "output does not match '$1'" >&2
        exit 1
    }
}

echo " * bridge works and has expected packages"
OUT=$(cockpit-bridge --packages)
echo "$OUT"
check_out "^base1.* /usr/share/cockpit/base1"
check_out "^podman.* /usr/share/cockpit/podman"

# create test user
if getent passwd test > /dev/null; then
    # defaults to nologin in autopkgtest environments
    chsh --shell /bin/bash test
else
    useradd --create-home --shell /bin/bash test
fi
echo 'test:foobar' | chpasswd

# We expect cockpit.socket to be enabled by default in cockpit-ws
echo " * socket unit is set up correctly, login page works"
curl  --silent --show-error -u test:foobar --cookie-jar /tmp/cookies --insecure https://localhost:9090/cockpit/login
echo

echo " * load podman page"
OUT=$(curl  --silent --show-error --cookie /tmp/cookies --insecure https://localhost:9090/cockpit/@localhost/podman/index.html)
check_out "<title.*Podman containers"
echo "smoke test passed"
