#!/bin/ash
#
# Prepare AndroidCast Gitea org: team owners (admin + foxx), transfer legacy admin/* repos.
# Safe to re-run (idempotent). Run before or with gitea_migrate_project.sh.
#
# Usage (Alpine BE as root):
#   sudo ./gitea_migrate_org_setup.sh
#   GITEA_ORG=AndroidCast GITEA_ORG_OWNERS="admin foxx" sudo ./gitea_migrate_org_setup.sh
#
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_load_config || exit 1
gitea_ensure_cli_access || exit 1

gitea_org_log "org=$GITEA_ORG  owners=$GITEA_ORG_OWNERS  transfer_from=$GITEA_TRANSFER_FROM"
gitea_org_log "public URL: ${GITEA_ROOT_URL%/}/$GITEA_ORG"

gitea_org_ensure_androidcast "$GITEA_ORG" || exit 1

gitea_org_log "repos under $GITEA_ORG:"
gitea_list_repos_api "$GITEA_ORG" 2>/dev/null | while read -r _r; do
  [ -n "$_r" ] && gitea_org_log "  $GITEA_ORG/$_r"
done

cat <<EOF

Org setup done.
  • Owners team (full org/repo control): $GITEA_ORG_OWNERS
  • Set password or SSH key for foxx: Gitea UI → foxx → Settings
  • Mirror all project repos under org:
      GITEA_OWNER=$GITEA_ORG sudo ./gitea_migrate_project.sh /path/to/android_cast

EOF
