#!/bin/sh
set -e

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

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 "^system"
check_out "^users"

# on an RPM based system we expect cockpit.socket not to be enabled by default;
# on a Debian-based system we do
if rpm -q cockpit >/dev/null 2>&1; then
    systemctl start cockpit.socket
fi

# 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 "skipping socket test with LXC on Debian 12, https://bugs.debian.org/1073815"
    exit 0
fi

echo " * socket unit is set up correctly, login page available"
OUT=$(curl --silent --show-error --insecure https://localhost:9090)
check_out "login-user-input.*User"
echo "smoke test passed"
