#!/bin/ash
#
# Debug why submodule mirrors are missing in Gitea UI.
#
# Usage:
#   sudo ./gitea_diagnose_mirrors.sh
#   sudo ./gitea_diagnose_mirrors.sh /var/www/.../android_cast
#   sudo ./gitea_diagnose_mirrors.sh /var/www/.../android_cast/.gitmodules
#
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_submodule_lib.sh
. "$SCRIPT_DIR/gitea_submodule_lib.sh"

GITEA_OWNER="${GITEA_OWNER:-${GITEA_ORG:-AndroidCast}}"
GITEA_REF="${GITEA_REF:-HEAD}"
GITEA_SUBMODULES_CONF="${GITEA_SUBMODULES_CONF:-$SCRIPT_DIR/submodules.defaults.conf}"

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

_arg="${1:-.}"
_repo_dir=""
_gitmodules=""

case "$_arg" in
  *.gitmodules)
    _gitmodules="$(cd "$(dirname "$_arg")" && pwd)/$(basename "$_arg")"
    _parent="$(dirname "$_gitmodules")"
    [ -d "$_parent/.git" ] || [ -f "$_parent/.git" ] && _repo_dir="$(cd "$_parent" && pwd)" || _repo_dir="$_parent"
    ;;
  *)
    [ -d "$_arg" ] || { warn "not a directory: $_arg"; exit 1; }
    _repo_dir="$(cd "$_arg" && pwd)"
    _gitmodules="$_repo_dir/.gitmodules"
    ;;
esac

gitea_load_config || exit 1
gitea_ensure_cli_access || exit 1

log "ini=$GITEA_APP_INI  owner=$GITEA_OWNER"
log "checkout=$_repo_dir"
log "gitmodules=$_gitmodules"

if [ -r "$_gitmodules" ]; then
  log ".gitmodules present ($(wc -l < "$_gitmodules" | tr -d ' ') lines)"
else
  warn ".gitmodules MISSING on BE — submodule migrate cannot discover entries"
fi

if command -v git >/dev/null 2>&1 && [ -d "$_repo_dir/.git" ] || [ -f "$_repo_dir/.git" ]; then
  log "git branch: $(git -C "$_repo_dir" branch --show-current 2>/dev/null || echo '?')"
  log "git HEAD: $(git -C "$_repo_dir" rev-parse HEAD 2>/dev/null || echo '?')"
  for _p in third-party/libvpx third-party/opus third-party/speex; do
    _sha="$(git -C "$_repo_dir" rev-parse "$GITEA_REF:$_p" 2>/dev/null)" || _sha="(no gitlink)"
    log "  $GITEA_REF:$_p -> $_sha"
  done
else
  warn "not a git checkout — only .gitmodules / defaults.conf can be used"
fi

log "collect_entries (what migrate would mirror):"
gitea_submodule_collect_entries "$_repo_dir" "$GITEA_REF" "$_gitmodules" "$GITEA_SUBMODULES_CONF" \
  | while IFS= read -r _e; do
    [ -n "$_e" ] && log "  $_e"
  done

log "Gitea repos via API:"
if _names="$(gitea_list_repos_api "$GITEA_OWNER" 2>/dev/null)"; then
  echo "$_names" | while read -r _r; do
    [ -n "$_r" ] && log "  $GITEA_OWNER/$_r"
  done
  log "count=$(echo "$_names" | grep -c . 2>/dev/null || echo 0)"
else
  warn "API list failed"
fi

log "on-disk bare repos:"
if [ -d "${GITEA_REPO_ROOT:-/var/lib/gitea/data/gitea-repositories}/$GITEA_OWNER" ]; then
  ls -1 "${GITEA_REPO_ROOT}/$GITEA_OWNER" 2>/dev/null | while read -r _d; do
    log "  $GITEA_OWNER/$_d"
  done
else
  warn "no directory ${GITEA_REPO_ROOT}/$GITEA_OWNER"
fi

cat <<EOF

If count=1 (only android_cast under AndroidCast or admin):
  • gitea_attach_remote.sh only mirrors the main repo — run submodule step:
      sudo ./gitea_migrate_project.sh . --skip-main
    or:
      sudo ./gitea_mirror_submodules.sh .gitmodules
  • If collect_entries is empty: sync .gitmodules to BE or use submodules.defaults.conf

Gitea UI: profile "Repositories N" may exclude mirrors on some versions —
  try Explore → search libvpx, or Site Administration → Repositories.

EOF
