#!/usr/bin/env bash
set -euo pipefail
# Usage: source setup-forgejo.sh <fresh-fixture-directory>
# Requires Docker, curl, tar, Python 3. Synthetic credentials, local fixture only.
fixture_dir=${1:?Provide a fresh fixture directory}
mkdir -p "$fixture_dir/bin" "$fixture_dir/xdg" "$fixture_dir/forgejo"
curl -fL https://codeberg.org/forgejo-contrib/forgejo-cli/releases/download/v0.7.0/forgejo-cli-x86_64-linux.tar.gz -o "$fixture_dir/fj.tar.gz"
tar -xzf "$fixture_dir/fj.tar.gz" -C "$fixture_dir/bin"
fixture_container=issue-33410-evidence
docker run -d --name "$fixture_container" -p 127.0.0.1:3410:3000 \
  -e FORGEJO__security__INSTALL_LOCK=true \
  -e FORGEJO__server__ROOT_URL=http://localhost:3410/ \
  -e FORGEJO__server__DOMAIN=localhost \
  -e FORGEJO__service__DISABLE_REGISTRATION=true \
  -v "$fixture_dir/forgejo:/data" codeberg.org/forgejo/forgejo:11
until curl -fs http://localhost:3410/api/v1/version >/dev/null; do sleep 1; done
docker exec --user git "$fixture_container" forgejo admin user create \
  --username tester --password 'Test-only-password-33410!' \
  --email tester@example.invalid --admin --must-change-password=false
docker exec --user git "$fixture_container" forgejo admin user generate-access-token \
  --username tester --token-name evidence --scopes all --raw > "$fixture_dir/token"
export XDG_DATA_HOME="$fixture_dir/xdg"
export PATH="$fixture_dir/bin:$PATH"
fj -H http://localhost:3410 auth add-token "$(cat "$fixture_dir/token")"
fj -H http://localhost:3410 whoami
# Expected failure: InvalidContentType, because this server speaks HTTP only.
fj -H https://localhost:3410 whoami || true
export T3CODE_DEV_SEED=0
# From a current-main checkout containing forgejo-http-login.e2e.ts:
# vp run scenarios:prove scripts/scenarios/forgejo-http-login.e2e.ts \
#   --base ddc4f576023b3bf7ead6c831510c1d72316bb1c2 \
#   --head e4941d02c4ecd824dd460d69d763f6346cf34043
# After inspecting the captures, upload them and this script with ghdrop upload.
# Cleanup after proof: docker stop "$fixture_container"; docker rm "$fixture_container"
