#!/bin/ash
#
# Remove stale gitea script temp dirs under /tmp (mirror clones, work dirs, json).
# Safe to run anytime; does not touch running jobs younger than GITEA_TMP_MAX_AGE_MIN.
#
# Usage:
#   sudo ./gitea_cleanup_tmp.sh
#   GITEA_TMP_MAX_AGE_MIN=0 sudo ./gitea_cleanup_tmp.sh   # aggressive (all ages)
#
set -eu

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

_before="$(find /tmp -maxdepth 1 \( -name 'gitea-migrate-*' -o -name 'gitea-submodules-*' \
  -o -name 'gitea-attach-*' -o -name 'gitea-mirror-*' -o -name 'gitea-*.json' \) 2>/dev/null | wc -l | tr -d ' ')"
gitea_prune_stale_tmp
_after="$(find /tmp -maxdepth 1 \( -name 'gitea-migrate-*' -o -name 'gitea-submodules-*' \
  -o -name 'gitea-attach-*' -o -name 'gitea-mirror-*' -o -name 'gitea-*.json' \) 2>/dev/null | wc -l | tr -d ' ')"
printf '[gitea-cleanup] max_age_min=%s  before=%s after=%s\n' \
  "${GITEA_TMP_MAX_AGE_MIN:-60}" "$_before" "$_after"
