#!/usr/bin/env bash
# Writes site/public/quickstart.yml, the quickstart as ONE Compose file with nothing to clone, served at
# https://pgrebac.com/quickstart.yml:
#
#   curl -fsSL https://pgrebac.com/quickstart.yml | docker compose -f - run --rm demo
#
# It runs a released pg_rebac: the rebacd image from ghcr.io and the SQL attached to the GitHub Release (checked
# against its SHA-256), with this directory's seed, demo and configuration inlined. examples/quickstart/
# docker-compose.yml is the same demo built from a clone instead, for trying a change from a checkout.
#
#   examples/quickstart/bundle.sh 0.1.1     # after a release: point the file at it
#   examples/quickstart/bundle.sh 0.1.1 dist/pg_rebac--0.1.1.sql   # before it: hash the SQL scripts/release-sql.sh made
#   examples/quickstart/bundle.sh --check   # CI: fail if the file is not what this directory would write
set -euo pipefail
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
out="$here/../../site/public/quickstart.yml"

check=0
if [ "${1:-}" = "--check" ]; then
  check=1
  version="$(sed -n 's/^# pg_rebac version: //p' "$out")"
  sha="$(sed -n 's/^# SQL SHA-256: //p' "$out")"
else
  version="${1:?usage: bundle.sh <released version> [its SQL file] | --check}"
  if [ -n "${2:-}" ]; then
    sha="$(shasum -a 256 "$2" | cut -d' ' -f1)"
  else
    sha="$(curl -fsSL "https://github.com/louvain-ai/pg_rebac/releases/download/v$version/pg_rebac--$version.sql" |
           shasum -a 256 | cut -d' ' -f1)"
  fi
fi

# A config's content, indented under `content: |`, with `$` doubled so Compose does not interpolate it.
inline() { sed -e 's/\$/$$/g' -e 's/^/      /' -e 's/^ *$//' "$here/$1"; }

render() {
  cat <<EOF
# pg_rebac in one command: Postgres with pgvector, SpiceDB, the rebacd daemon, and a demo that runs one vector search
# as two people, then revokes one of them.
#
#   curl -fsSL https://pgrebac.com/quickstart.yml | docker compose -f - run --rm demo
#   curl -fsSL https://pgrebac.com/quickstart.yml | docker compose -f - down -v  # stop it and forget everything
#
# Nothing listens on the host. The SpiceDB key and the Postgres password are throwaway values for this demo only.
# Generated by examples/quickstart/bundle.sh in github.com/louvain-ai/pg_rebac; edit the files there, not this one.
# pg_rebac version: $version
# SQL SHA-256: $sha
name: pg_rebac_quickstart
services:
  postgres:
    image: pgvector/pgvector:pg17
    environment:
      POSTGRES_PASSWORD: quickstart
    healthcheck:
      test: ["CMD", "pg_isready", "-h", "127.0.0.1", "-U", "postgres"]
      interval: 1s
      retries: 30

  spicedb:
    image: authzed/spicedb:v1.56.2
    command: ["serve", "--grpc-preshared-key", "quickstart-key", "--datastore-engine", "memory",
              "--datastore-bootstrap-files", "/quickstart/spicedb.yaml"]
    configs: [{ source: spicedb.yaml, target: /quickstart/spicedb.yaml }]
    healthcheck:
      test: ["CMD", "grpc_health_probe", "-addr=:50051"]
      interval: 1s
      retries: 30

  # The release's SQL, checked against its SHA-256 before anything installs it.
  download:
    image: curlimages/curl:8.17.0
    user: root
    volumes: ["sql:/sql"]
    entrypoint: ["sh", "-c", "curl -fsSL -o /sql/rebac.sql https://github.com/louvain-ai/pg_rebac/releases/download/v$version/pg_rebac--$version.sql && echo '$sha  /sql/rebac.sql' | sha256sum -c -s"]

  # Installs the SQL, exactly as you would on your own Postgres: psql -f rebac.sql.
  schema:
    image: pgvector/pgvector:pg17
    depends_on:
      postgres: { condition: service_healthy }
      download: { condition: service_completed_successfully }
    environment:
      PGOPTIONS: "-c client_min_messages=warning"
    volumes: ["sql:/sql:ro"]
    entrypoint: ["psql", "-v", "ON_ERROR_STOP=1", "-q", "postgres://postgres:quickstart@postgres/postgres",
                 "-f", "/sql/rebac.sql"]

  rebacd:
    image: ghcr.io/louvain-ai/rebacd:$version
    depends_on:
      schema: { condition: service_completed_successfully }
      spicedb: { condition: service_healthy }
    environment:
      DATABASE_URL: postgres://postgres:quickstart@postgres/postgres
      SPICEDB_TOKEN: quickstart-key
    configs: [{ source: rebac.toml, target: /etc/rebac.toml }]
    command: ["--config", "/etc/rebac.toml", "run", "--accept-schema"]

  demo:
    image: pgvector/pgvector:pg17
    depends_on:
      rebacd: { condition: service_started }
    environment:
      DB: postgres://postgres:quickstart@postgres/postgres
      PGOPTIONS: "-c client_min_messages=warning"
      QUICKSTART_DOWN: "curl -fsSL https://pgrebac.com/quickstart.yml | docker compose -f - down -v"
    configs:
      - { source: demo.sh, target: /quickstart/demo.sh }
      - { source: seed.sql, target: /quickstart/seed.sql }
      - { source: demo.sql, target: /quickstart/demo.sql }
    entrypoint: ["bash", "/quickstart/demo.sh"]

volumes:
  sql: {}

configs:
EOF
  for f in spicedb.yaml rebac.toml demo.sh seed.sql demo.sql; do
    printf '  %s:\n    content: |\n' "$f"
    inline "$f"
  done
}

if [ "$check" = 1 ]; then
  diff -u "$out" <(render) || { echo "site/public/quickstart.yml is stale: run examples/quickstart/bundle.sh $version" >&2; exit 1; }
else
  render > "$out"
  echo "wrote site/public/quickstart.yml for pg_rebac $version"
fi
