#!/usr/bin/env bash
# Synthetic local fixture for forgejo-loopback-settings.e2e.ts.
# Run from a new scratch directory; stop and remove the named container afterward.
set -euo pipefail
fixture_dir="$PWD"
curl -fsSL https://codeberg.org/forgejo-contrib/forgejo-cli/releases/download/v0.7.0/forgejo-cli-x86_64-linux.tar.gz -o fj.tar.gz
tar -xzf fj.tar.gz
container_id=$(docker run -d --name factory-pr-42949-forgejo -p 127.0.0.1:3000:3000 -e FORGEJO__security__INSTALL_LOCK=true -e FORGEJO__database__DB_TYPE=sqlite3 -e FORGEJO__server__ROOT_URL=http://localhost:3000/ -e FORGEJO__server__HTTP_PORT=3000 -e FORGEJO__service__DISABLE_REGISTRATION=true codeberg.org/forgejo/forgejo:11)
printf 'Container: %s\nWait for Forgejo to listen, then run the remaining commands.\n' "$container_id"
read -r -p 'Press Enter when ready: '
docker exec --user git "$container_id" forgejo admin user create --username owner --password 'Synthetic-proof-42949!' --email owner@example.invalid --admin --must-change-password=false
docker exec --user git "$container_id" forgejo admin user generate-access-token --username owner --token-name scenario --scopes all --raw > token
python3 - <<'PY'
import urllib.request,json
key=open('token').read().strip()
request=urllib.request.Request('http://localhost:3000/api/v1/user/repos',data=json.dumps({'name':'repo','auto_init':True,'default_branch':'main'}).encode(),headers={'Authorization':'token '+key,'Content-Type':'application/json'})
print('Repository creation:',urllib.request.urlopen(request).status)
PY
export XDG_DATA_HOME="$fixture_dir/data"
./fj --host http://localhost:3000 auth add-token < token
./fj --host http://localhost:3000 whoami
# HTTPS whoami fails with TLS InvalidContentType against this HTTP-only endpoint.
# From the main runner checkout, run scenarios:prove with:
# T3CODE_DEV_SEED=0 T3_PROOF_FIXTURE=<this directory>
# XDG_DATA_HOME=<this directory>/data
# T3_SCENARIO_INHERIT_ENV=T3CODE_DEV_SEED,T3_PROOF_FIXTURE,XDG_DATA_HOME
