#!/usr/bin/env bash
# Run separately in the base and head checkout. Uses isolated state only.
set -euo pipefail
checkout=$1
output=$2
cd "$checkout"
vp i
T3CODE_DEV_SEED=0 T3CODE_DESKTOP_UPDATE_REPOSITORY=pingdotgg/t3code \
  vp run dist:desktop:artifact --platform linux --target dir --arch x64 \
  --build-version 0.1.8 --output-dir "$output/package"
mkdir -p "$output/home/userdata"
cat > "$output/home/userdata/desktop-settings.json" <<'JSON'
{"serverExposureMode":"network-accessible","localEnvironmentEnabled":true,"mainWindowBounds":{"width":1280,"height":800}}
JSON
# An extracted-style package: APPIMAGE unset, no resources/package-type marker.
# Supply the same release feed if a local build omitted it.
cat > "$output/package/linux-unpacked/resources/app-update.yml" <<'YAML'
provider: github
owner: pingdotgg
repo: t3code
releaseType: release
YAML
unset APPIMAGE
T3CODE_DEV_SEED=0 T3CODE_HOME="$output/home" T3CODE_PORT=13381 \
  T3CODE_TELEMETRY_ENABLED=false headless-desktop \
  "$output/package/linux-unpacked/t3code" --no-sandbox --disable-gpu \
  --remote-debugging-port=13383
# In another terminal in apps/web:
# APP_VERSION=0.1.9 T3CODE_DEV_SEED=0 T3CODE_PORT=13381 \
# T3CODE_SINGLE_ORIGIN_DEV=1 PORT=13382 vp dev --host 0.0.0.0
# Mint a token with the CLI's pair command, using --base-dir "$output/home".
# Pair the browser at port 13382. In onboarding choose Select none, then Start.
# Open /settings/access. Click Update to 0.1.9 and Confirm if offered.
# Capture the Version row. On head there is manual update guidance instead.
# Fetch /.well-known/t3/environment from port 13381 for the live descriptor.
# Electron CDP: window.desktopBridge.getUpdateState() reads the actual updater.
# Supported control: build --target deb --skip-build, dpkg-deb -x into output,
# then run its opt/T3 Code (Beta)/t3code with the same launch environment.
# Inspect resources/package-type: it contains deb. Repeat the descriptor and UI checks.
