#!/bin/bash
set -eu
# Run under sudo; all writes remain in this fixture directory or private namespaces.
root=$(cd -- "$(dirname -- "$0")" && pwd)
side=$1
mode=$2
playwright=$3
account=${SUDO_USER:?Run this launcher with sudo}
account_home=$(getent passwd "$account" | cut -d: -f6)
export root side mode playwright account account_home
exec unshare --mount --uts --fork bash -c '
set -eu
mount --make-rprivate /
mount --bind "$root/desktop-home/.ssh" "$account_home/.ssh"
hostname desktop-demo
exec runuser -u "$account" -- env \
 HOME="$root/desktop-home" SHELL=/bin/bash \
 XDG_CONFIG_HOME="$root/$side-$mode-config" XDG_DATA_HOME="$root/$side-$mode-data" \
 T3CODE_HOME="$root/$side-$mode-state" T3CODE_DEV_SEED=0 T3CODE_DISABLE_AUTO_UPDATE=1 \
 T3CODE_RELEASE_BASE_URL=http://10.231.96.1:18996 \
 SSH_AUTH_SOCK="$root/agent.sock" PLAYWRIGHT_PATH="$playwright" \
 DESKTOP_EXE="$root/$side-package/linux-unpacked/t3code" \
 PROFILE_ONLY="$([ "$mode" = profile ] && echo 1 || echo 0)" \
 PROOF_OUTPUT="$root/$side-$mode-run" \
 "$account_home/.local/bin/headless-desktop" "$account_home/.local/share/vite-plus/bin/node" "$root/desktop-proof.cjs"
'
