#!/bin/ash
#
# Gitea user admin helpers (foxx pre-created by migrate — not self-registration).
#
# Usage:
#   sudo ./gitea_user_admin.sh list
#   sudo ./gitea_user_admin.sh reset-password foxx 'NewSecurePassword'
#   sudo ./gitea_user_admin.sh must-change-password foxx
#
set -eu

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

log() { printf '[gitea-user] %s\n' "$*"; }
die() { printf '[gitea-user] ERROR: %s\n' "$*" >&2; exit 1; }

gitea_load_config || exit 1
gitea_ensure_cli_access || exit 1

_cmd="${1:-}"
_user="${2:-}"
_pass="${3:-}"

case "$_cmd" in
  list)
    gitea_cli "admin" "user" "list"
    ;;
  reset-password)
    [ -n "$_user" ] && [ -n "$_pass" ] || die "usage: $0 reset-password USER PASSWORD"
    gitea_cli "admin" "user" "change-password" \
      "--username" "$_user" \
      "--password" "$_pass"
    log "password set for $_user (use Sign In, not Register)"
    ;;
  must-change-password)
    [ -n "$_user" ] || die "usage: $0 must-change-password USER"
    gitea_cli "admin" "user" "must-change-password" \
      "--username" "$_user"
    log "$_user must change password on next login"
    ;;
  -h|--help|help)
    sed -n '2,12p' "$0"
    ;;
  *)
    die "unknown command: $_cmd (try --help)"
    ;;
esac
