#!/bin/bash
# The whole thing, from nothing, the way a classroom would get it: a fresh
# machine, the built .deb, and the session started from what it installed.
# No developer install script anywhere in the path.
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
LAB="$ROOT/tools/lab/lab"
DEB="${1:-$(ls -t "$ROOT"/dist/ekklesia-classroom_*_amd64.deb | head -1)}"
NAME="${LAB_NAME:-classroom-v2-lab}"

echo "installing $(basename "$DEB")"
"$LAB" up >/dev/null

docker exec "$NAME" sh -c '
    printf "#!/bin/sh\nexit 101\n" > /usr/sbin/policy-rc.d
    chmod +x /usr/sbin/policy-rc.d
    apt-get update -qq >/dev/null 2>&1'

# apt-get install on the file, exactly as ekklesia-classroom-update does.
# Not dpkg -i and then apt-get -f: when the package conflicts with
# something installed (khelpcenter, from 0.2.6.2) dpkg refuses, and -f then
# "fixes" it by dropping the classroom package rather than the conflict —
# measured, and a lab that installs differently from a classroom tests the
# wrong thing.
docker exec "$NAME" sh -c "
    DEBIAN_FRONTEND=noninteractive apt-get install -y -qq '/srv/classroom/${DEB#$ROOT/}' \
        >/tmp/from-package-install.log 2>&1 || cat /tmp/from-package-install.log
    rm -f /usr/sbin/policy-rc.d"

state=$(docker exec "$NAME" dpkg-query -W -f='${db:Status-Abbrev}' ekklesia-classroom)
echo "package state: $state"

# And then the machine is restarted, the way a room takes an update: a
# new PID 1, a new user manager, SDDM autologging the classroom in. The
# session environment, the logind drop-in and the user services are all
# things that are only read at boot.
"$LAB" reboot

"$LAB" session >/dev/null
sleep 8

check() { printf '  %-46s %s\n' "$1" "$2"; }
env_dirs=$(docker exec -u classroom -e XDG_RUNTIME_DIR=/run/user/1000 \
    -e DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus "$NAME" \
    systemctl --user show-environment 2>/dev/null | grep '^XDG_CONFIG_DIRS=' || true)

check "package installed" "$state"
check "session config path" "${env_dirs:-NOT SET}"
check "plasmashell" "$(docker exec "$NAME" pgrep -c -x plasmashell || echo 0)"
check "kwin" "$(docker exec "$NAME" pgrep -c -x kwin_wayland || echo 0)"
check "panel applets" "$(docker exec "$NAME" sh -c 'grep -c "^plugin=" /home/classroom/.config/plasma-org.kde.plasma.desktop-appletsrc' || echo 0)"
check "panel restart policy" "$(docker exec -u classroom -e XDG_RUNTIME_DIR=/run/user/1000 \
    -e DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus "$NAME" \
    systemctl --user show plasma-plasmashell.service -p Restart --value)"
check "audio policy" "$(docker exec -u classroom -e XDG_RUNTIME_DIR=/run/user/1000 \
    -e DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus "$NAME" \
    systemctl --user is-active ekklesia-classroom-audio.service || true)"

# The same list of properties the upgraded machine has to satisfy. A
# fresh install and a migrated one are supposed to be indistinguishable
# from a teacher's chair, so they are asked the same questions.
"$ROOT/tools/lab/converged-check" ${CHECK_ARGS:-}
converged=$?

# And the one part of Wonder Ink that is a behaviour rather than a
# property: which local files it will open. Run against the command the
# package installed, not against the repository.
echo
echo "Wonder Ink, worksheets and local files"
"$ROOT/tools/lab/pdf-check"
pdf=$?

exit $(( converged | pdf ))
