#!/bin/bash
# publish.sh — Creates the 2 repos on github.com/ignaciolagosruiz and pushes the
# pre-built code that Pipo already prepared in ~/ignaciolagosruiz-oss/.
#
# This script does NOT contain the token. You set it as an env var right
# before running it (the env var is not stored anywhere on disk).
#
# Usage:
#   export GITHUB_TOKEN=ghp_xxxxx...    # paste your token
#   bash publish.sh
#
# The token is only used to create the 2 repos via the GitHub API and
# to authenticate the git pushes. After the script finishes, you can
# `unset GITHUB_TOKEN` and the token is gone from the shell.

set -euo pipefail

if [ -z "${GITHUB_TOKEN:-}" ]; then
  echo "ERROR: set GITHUB_TOKEN first" >&2
  echo "  export GITHUB_TOKEN=ghp_xxxxx..." >&2
  exit 1
fi

if [ "${#GITHUB_TOKEN}" -lt 30 ]; then
  echo "ERROR: GITHUB_TOKEN looks too short to be a real GitHub PAT" >&2
  exit 1
fi

BASE="/home/ubuntu/nachlakes-oss"
REMOTE_BASE="https://x-access-token:${GITHUB_TOKEN}@github.com/ignaciolagosruiz"

# Sanity: confirm token works and points to the right account
echo "==> Verifying token..."
LOGIN=$(curl -sS -H "Authorization: token ${GITHUB_TOKEN}" \
  -H "Accept: application/vnd.github+json" \
  https://api.github.com/user | python3 -c "import json,sys;print(json.load(sys.stdin).get('login',''))")

if [ "$LOGIN" != "ignaciolagosruiz" ]; then
  echo "ERROR: token belongs to '$LOGIN', not 'ignaciolagosruiz'. Aborting." >&2
  exit 1
fi
echo "    token is for: $LOGIN ✓"

create_repo () {
  local NAME="$1"
  local DESC="$2"
  echo ""
  echo "==> Creating github.com/ignaciolagosruiz/$NAME..."
  HTTP_CODE=$(curl -sS -o /tmp/repo-create-${NAME}.json -w "%{http_code}" \
    -H "Authorization: token ${GITHUB_TOKEN}" \
    -H "Accept: application/vnd.github+json" \
    -X POST https://api.github.com/user/repos \
    -d "{\"name\":\"${NAME}\",\"description\":\"${DESC}\",\"private\":false,\"auto_init\":false}")

  if [ "$HTTP_CODE" = "201" ]; then
    echo "    created ✓"
  elif [ "$HTTP_CODE" = "422" ]; then
    if grep -q "name already exists" /tmp/repo-create-${NAME}.json 2>/dev/null; then
      echo "    already exists, continuing"
    else
      echo "    unexpected 422: $(cat /tmp/repo-create-${NAME}.json)" >&2
      exit 1
    fi
  else
    echo "    unexpected HTTP $HTTP_CODE: $(cat /tmp/repo-create-${NAME}.json)" >&2
    exit 1
  fi
}

create_repo "releasekit" "Opinionated release automation for Node.js / TypeScript projects. Conventional Commits, semver, changelog, GitHub release, npm publish."
create_repo "repo-stats" "A maintainer dashboard for any public GitHub repository. Stars, recent issues, PR throughput, release cadence, top contributors."

push_repo () {
  local NAME="$1"
  local DIR="$BASE/$NAME"
  echo ""
  echo "==> Pushing $NAME (from $DIR)..."
  cd "$DIR"

  if ! git remote get-url origin >/dev/null 2>&1; then
    git remote add origin "${REMOTE_BASE}/${NAME}.git"
  else
    git remote set-url origin "${REMOTE_BASE}/${NAME}.git"
  fi

  # Push main + tags
  git push -u origin main --tags 2>&1 | tail -8
}

push_repo "releasekit"
push_repo "repo-stats"

echo ""
echo "==> All done."
echo ""
echo "Verify:"
echo "  https://github.com/ignaciolagosruiz/releasekit"
echo "  https://github.com/ignaciolagosruiz/repo-stats"
echo ""
echo "Next: open ~/ignaciolagosruiz-oss/APPLICATION.md and submit the form at"
echo "        https://openai.com/form/codex-for-oss/"
echo ""
echo "After that, unset the token in your shell:"
echo "  unset GITHUB_TOKEN"
