#!/bin/ash
#
# Import url-shortener into the AndroidCast Gitea org (pull mirror, team-visible, protected branches).
# Thin orchestrator — reuses gitea_attach_remote.sh, gitea_set_default_branch.sh,
# gitea_protect_branches.sh (same rules as android_cast).
#
# Canonical remote: git://f0xx.org/androidcast_project/url-shortener
# Gitea UI: …/git/AndroidCast/url-shortener
#
# Usage (Alpine BE as root):
#   sudo ./gitea_attach_url_shortener.sh
#
# Override:
#   GITEA_MIRROR_URL=git://f0xx.org/androidcast_project/url-shortener sudo ./gitea_attach_url_shortener.sh
#
# Prerequisites: app.ini [migrations] must allow f0xx.org (see scripts/gitea/README.md).
#
set -eu

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=gitea_ini.sh
. "$SCRIPT_DIR/gitea_ini.sh"
# shellcheck source=gitea_mirror_lib.sh
. "$SCRIPT_DIR/gitea_mirror_lib.sh"
# shellcheck source=gitea_org_lib.sh
. "$SCRIPT_DIR/gitea_org_lib.sh"

GITEA_ORG="${GITEA_ORG:-AndroidCast}"
GITEA_OWNER="${GITEA_OWNER:-$GITEA_ORG}"
GITEA_REPO="${GITEA_REPO:-url-shortener}"
GITEA_MIRROR_URL="${GITEA_MIRROR_URL:-git://f0xx.org/androidcast_project/url-shortener}"
GITEA_DEFAULT_BRANCH="${GITEA_DEFAULT_BRANCH:-next}"
GITEA_MIRROR_INTERVAL="${GITEA_MIRROR_INTERVAL:-10m}"
GITEA_PRIVATE="${GITEA_PRIVATE:-false}"
# Personal or legacy owners to re-home under AndroidCast (space-separated).
GITEA_TRANSFER_SOURCES="${GITEA_TRANSFER_SOURCES:-admin androidcast_project}"

log() { printf '[gitea-url-shortener] %s\n' "$*"; }
warn() { printf '[gitea-url-shortener] WARN: %s\n' "$*" >&2; }

case "${1:-}" in
  -h|--help|help)
    sed -n '2,18p' "$0"
    exit 0
    ;;
esac

gitea_load_config || exit 1
gitea_ensure_cli_access || exit 1

log "=== $GITEA_REPO → $GITEA_OWNER/$GITEA_REPO ==="
log "mirror: $GITEA_MIRROR_URL"
log "public: ${GITEA_ROOT_URL%/}/$GITEA_OWNER/$GITEA_REPO"

# 1) Org + Owners team (admin, foxx) — repos under the org are team-visible.
gitea_org_ensure_androidcast "$GITEA_ORG" || warn "org setup incomplete"

# 2) Transfer if mirror was created under a personal/legacy account.
for _from in $GITEA_TRANSFER_SOURCES; do
  [ "$_from" = "$GITEA_OWNER" ] && continue
  if gitea_repo_exists_api "$_from" "$GITEA_REPO"; then
    log "transfer $_from/$GITEA_REPO → $GITEA_OWNER/$GITEA_REPO"
    gitea_transfer_repo_api "$_from" "$GITEA_REPO" "$GITEA_OWNER" \
      && log "  transferred" \
      || warn "  transfer failed"
  fi
done

# 3) Pull mirror (attach when missing). Resolve PAT once for all API calls this run.
_gitea_token="$(gitea_resolve_push_token)" || true
[ -n "$_gitea_token" ] && GITEA_ADMIN_TOKEN="$_gitea_token" && export GITEA_ADMIN_TOKEN

if gitea_repo_exists_api "$GITEA_OWNER" "$GITEA_REPO"; then
  log "repo exists — trigger mirror sync"
  gitea_sync_mirror_api "$GITEA_OWNER" "$GITEA_REPO" || warn "mirror sync API failed — try gitea_sync_mirror.sh"
else
  log "attach pull mirror (API migrate) ..."
  if ! gitea_mirror_pull_repo "$GITEA_MIRROR_URL" "$GITEA_REPO" "$GITEA_OWNER" \
    "$GITEA_MIRROR_INTERVAL" ""; then
    warn "API migrate failed — trying gitea_attach_remote.sh fallback ..."
    GITEA_OWNER="$GITEA_OWNER" GITEA_REPO="$GITEA_REPO" \
      GITEA_MIRROR_URL="$GITEA_MIRROR_URL" \
      GITEA_MIRROR_INTERVAL="$GITEA_MIRROR_INTERVAL" \
      GITEA_PRIVATE="$GITEA_PRIVATE" \
      sh "$SCRIPT_DIR/gitea_attach_remote.sh" "$GITEA_MIRROR_URL" || exit 1
  fi
  gitea_sync_mirror_api "$GITEA_OWNER" "$GITEA_REPO" || warn "initial sync pending — check [migrations] / git daemon"
fi

# 4) Default branch next (integration; master when release branch exists).
GITEA_OWNER="$GITEA_OWNER" GITEA_REPO_NAMES="$GITEA_REPO" \
  GITEA_DEFAULT_BRANCH="$GITEA_DEFAULT_BRANCH" \
  sh "$SCRIPT_DIR/gitea_set_default_branch.sh" || warn "default branch set failed (sync mirror first)"

# 5) Branch protection — master + next when present on canonical remote (GIT_FLOW §7).
_branches=""
for _b in next master; do
  if gitea_git_remote_has_branch "$GITEA_MIRROR_URL" "$_b"; then
    _branches="$_branches $_b"
  else
    log "skip Gitea protection for $_b (not on $GITEA_MIRROR_URL yet)"
  fi
done
_branches="$(echo "$_branches" | sed 's/^ //')"
if [ -n "$_branches" ]; then
  GITEA_OWNER="$GITEA_OWNER" GITEA_REPO="$GITEA_REPO" \
    GITEA_PROTECT_BRANCHES="$_branches" \
    sh "$SCRIPT_DIR/gitea_protect_branches.sh" || warn "branch protection failed — run manually"
else
  warn "no branches to protect — mirror may be empty"
fi

cat <<EOF

url-shortener on Gitea:
  • ${GITEA_ROOT_URL%/}/$GITEA_OWNER/$GITEA_REPO
  • Org repo (private=$GITEA_PRIVATE) — visible to AndroidCast org members; Owners: $GITEA_ORG_OWNERS
  • Default branch: $GITEA_DEFAULT_BRANCH
  • Gitea protected branches:${_branches:+ }${_branches:- (none yet)}

Canonical FE git (optional pre-receive hook — same as android_cast):
  cp examples/git/hooks/pre-receive.android_cast.example \\
     /path/to/url-shortener.git/hooks/pre-receive
  chmod +x /path/to/url-shortener.git/hooks/pre-receive

Re-sync: sudo ./gitea_sync_mirror.sh url-shortener

EOF
