#!/bin/sh
# Starts Unleash, flagd and GO Feature Flag in Docker and checks which flag keys each accepts.
# Usage: sh benchmarks/naming/run.sh > benchmarks/naming/results.json
# Set SKIP_UNLEASH=1 to test only flagd and GO Feature Flag (no Postgres needed).
set -e
DIR=$(cd "$(dirname "$0")" && pwd)
cd "$DIR"
npm ci --silent >&2
node probe.mjs files
cat > .work/goff-proxy.yaml <<YAML
retriever:
  kind: file
  path: /goff/flags.yaml
YAML
cleanup() { docker rm -f -v nm-unleash nm-db nm-flagd nm-goff >/dev/null 2>&1 || true; docker network rm nm-net >/dev/null 2>&1 || true; }
trap cleanup EXIT
cleanup
docker network create nm-net >/dev/null
docker run -d --name nm-flagd -p 8016:8016 -v "$DIR/.work/flags.json:/etc/flagd/flags.json:ro" \
  ghcr.io/open-feature/flagd:v0.18.2 start --uri file:/etc/flagd/flags.json >/dev/null
docker run -d --name nm-goff -p 1031:1031 -v "$DIR/.work:/goff:ro" gofeatureflag/go-feature-flag:v1.58.1 >/dev/null
if [ -z "$SKIP_UNLEASH" ]; then
docker run -d --name nm-db --network nm-net -e POSTGRES_DB=db -e POSTGRES_HOST_AUTH_METHOD=trust postgres:15 >/dev/null
until docker exec nm-db pg_isready -U postgres >/dev/null 2>&1; do sleep 1; done
docker run -d --name nm-unleash --network nm-net -p 4242:4242 -e DATABASE_URL=postgres://postgres@nm-db/db \
  -e DATABASE_SSL=false -e LOG_LEVEL=warn -e 'INIT_ADMIN_API_TOKENS=*:*.unleash-insecure-admin-api-token' \
  unleashorg/unleash-server:8.3.1 >/dev/null
until curl -sf localhost:4242/health >/dev/null; do sleep 1; done
fi
sleep 2
node probe.mjs
