#!/usr/bin/env bash
# The .deb postinst's decisions (zc#636), against stub incus/getent/id/usermod:
# init only an Incus with no pool, one group (incus-admin, else docker), the
# installing user only, and exit 0 whatever fails. Run by check-deb.sh.
#
#   test-postinst.sh [path/to/postinst]
set -euo pipefail
here="$(cd "$(dirname "$0")" && pwd)"
postinst="${1:-$here/../linux/postinst}"
tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT

# scenario <incus: none|pools|empty|down|initfail> <groups> <user-groups> [env...]
scenario() {
  local incus="$1" groups="$2" ugroups="$3"; shift 3
  local bin="$tmp/bin"; rm -rf "$bin" "$tmp/log"; mkdir -p "$bin"
  [[ "$incus" == none ]] || cat > "$bin/incus" <<EOF
#!/bin/sh
echo "incus \$*" >> "$tmp/log"
case "$incus:\$1" in
  pools:storage) echo default,dir,,CREATED ;;
  empty:storage|initfail:storage) ;;
  down:*) exit 1 ;;
  initfail:admin) exit 1 ;;
esac
EOF
  cat > "$bin/getent" <<EOF
#!/bin/sh
for g in $groups; do [ "\$2" = "\$g" ] && exit 0; done; exit 2
EOF
  cat > "$bin/id" <<EOF
#!/bin/sh
[ "\$1" = -nu ] && { echo alice; exit 0; }
echo "$ugroups"
EOF
  printf '#!/bin/sh\nshift; exec "$@"\n' > "$bin/timeout"
  cat > "$bin/usermod" <<EOF
#!/bin/sh
echo "usermod \$*" >> "$tmp/log"
EOF
  chmod +x "$bin"/*
  touch "$tmp/log"
  env -i PATH="$bin:/usr/bin:/bin" "$@" sh "$postinst" configure > "$tmp/out" 2>&1 \
    || { echo "FAIL: postinst exited non-zero ($incus)"; cat "$tmp/out"; exit 1; }
}
has() { grep -qF -- "$1" "$tmp/log" || { echo "FAIL [$name]: expected '$1' in:"; cat "$tmp/log" "$tmp/out"; exit 1; }; }
hasnt() { ! grep -qF -- "$1" "$tmp/log" || { echo "FAIL [$name]: unexpected '$1' in:"; cat "$tmp/log"; exit 1; }; }

name="fresh incus: init once, user joins incus-admin"
scenario empty "incus-admin docker" "alice" SUDO_USER=alice
has "incus admin init --minimal"; has "usermod -aG incus-admin alice"; hasnt "usermod -aG docker"

name="configured incus: no second init"
scenario pools "incus-admin" "alice" SUDO_USER=alice
hasnt "incus admin init"; has "usermod -aG incus-admin alice"

name="already a member: no usermod"
scenario pools "incus-admin" "alice incus-admin" SUDO_USER=alice
hasnt "usermod"

name="no incus (Debian 12 without backports): docker group"
scenario none "docker" "alice" SUDO_USER=alice
has "usermod -aG docker alice"

name="daemon down / init fails: install still succeeds"
scenario down "incus-admin" "alice" SUDO_USER=alice
hasnt "incus admin init"; grep -q "not reachable" "$tmp/out"
scenario initfail "incus-admin" "alice" SUDO_USER=alice
has "incus admin init --minimal"; grep -q "init failed" "$tmp/out"

name="GUI install through pkexec"
scenario pools "incus-admin" "alice" PKEXEC_UID=1000
has "usermod -aG incus-admin alice"

name="root shell, no installing user: nobody is added"
scenario pools "incus-admin" "alice" SUDO_USER=root
hasnt "usermod"; grep -q "sudo usermod -aG incus-admin" "$tmp/out"

name="upgrade/remove hooks do nothing"
rm -f "$tmp/log"; touch "$tmp/log"
env -i PATH="$tmp/bin:/usr/bin:/bin" SUDO_USER=alice sh "$postinst" abort-upgrade >/dev/null 2>&1
hasnt "incus"; hasnt "usermod"

echo "ok: postinst scenarios pass"
