#!/bin/bash
# ==============================================================================================
# ================================= Arr Cleanup Launcher =======================================
# ==============================================================================================
#
# PURPOSE
# ─────────────────────────────────────────────────────────────────────────────
# Thin bash launcher for arr_cleanup.py. Handles everything bash is uniquely
# suited for: sourcing shell config, detect_hosts(), exporting env vars.
# All logic lives in Python.
#
# USAGE
# ─────────────────────────────────────────────────────────────────────────────
#   arr_cleanup.sh --arr lidarr [--dry-run] [--log] [--status]
#   arr_cleanup.sh --arr radarr [--i-know-what-im-doing] [--skip-strike-list]
#   arr_cleanup.sh --arr sonarr
#
# ADDING A NEW ARR
# ─────────────────────────────────────────────────────────────────────────────
#   1. Add HOST*_<ARR>_URL, API_KEY, MEDIA_ROOT, PATH_MAP to host*.conf
#   2. Add <ARR>_ORPHAN_AGE, MAX_DELETE_GB, EXTENSIONS, etc. to master.conf
#   3. Add a profile entry to ARR_PROFILES in arr_cleanup.py (6 values)
#   4. Add an export block for the new arr below (copy Sonarr block, change prefix)
#
# ==============================================================================================

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

source "$SCRIPT_DIR/../load_config.sh"

if [[ "$EUID" -ne 0 ]]; then
    echo "ERROR: Must be run as root" >&2
    exit 1
fi

acquire_lock

if ! command -v python3 >/dev/null 2>&1; then
    echo "ERROR: python3 not found — required for arr_cleanup" >&2
    exit 1
fi

detect_hosts

# ── Helper: serialize host-specific associative array to JSON ──────────────────
# Reads ${MY_ID}_${ARR_UPPER}_PATH_MAP and emits {"container_path":"host_path",...}
# Paths with double-quotes in names are not supported (not a real-world constraint).
_path_map_json() {
    local arr_upper="$1"
    local map_var="${MY_ID}_${arr_upper}_PATH_MAP"
    local json="{"
    local sep="" keys k v

    if ! declare -p "$map_var" 2>/dev/null | grep -q "declare -A"; then
        echo "{}"
        return
    fi

    eval "keys=(\"\${!${map_var}[@]}\")"
    for k in "${keys[@]}"; do
        eval "v=\"\${${map_var}[\$k]}\""
        json+="${sep}\"${k}\":\"${v}\""
        sep=","
    done
    json+="}"
    echo "$json"
}

# ── Host identity ──────────────────────────────────────────────────────────────
export MY_ID LOCAL_SERVER_NAME
export ARR_HOSTNAME
ARR_HOSTNAME=$(hostname)

# ── Notifications ──────────────────────────────────────────────────────────────
export NOTIFY_UNRAID
export EMBY_URL EMBY_API_KEY
export MY_DISCORD_WEBHOOK

# ── Shared ─────────────────────────────────────────────────────────────────────
export ARR_CLEANUP_STATS
export LOCK_DIR LOCK_WAIT_TIMEOUT

# ── Lidarr ─────────────────────────────────────────────────────────────────────
export LIDARR_URL LIDARR_API_KEY
export LIDARR_MEDIA_ROOT="${LIDARR_MUSIC_ROOT:-}"
export LIDARR_EXTENSIONS="${LIDARR_EXTENSIONS[*]:-}"
export LIDARR_PROTECTED_PATTERNS="${LIDARR_PROTECTED_PATTERNS[*]:-}"
export LIDARR_ORPHAN_AGE LIDARR_MAX_DELETE_GB
export LIDARR_MIN_TRACKED_PCT LIDARR_TRACKED_COUNT_FILE
export LIDARR_VERSION_MAJOR LIDARR_IMPORT_SCAN_TIMEOUT LIDARR_LOCK_WARN_AGE
export LIDARR_PATH_MAP_JSON
LIDARR_PATH_MAP_JSON=$(_path_map_json "LIDARR")

# ── Radarr ─────────────────────────────────────────────────────────────────────
export RADARR_URL RADARR_API_KEY
export RADARR_MEDIA_ROOT="${RADARR_MOVIES_ROOT:-}"
export RADARR_EXTENSIONS="${RADARR_EXTENSIONS[*]:-}"
export RADARR_PROTECTED_PATTERNS="${RADARR_PROTECTED_PATTERNS[*]:-}"
export RADARR_ORPHAN_AGE RADARR_MAX_DELETE_GB
export RADARR_MIN_TRACKED_PCT RADARR_TRACKED_COUNT_FILE
export RADARR_VERSION_MAJOR RADARR_IMPORT_SCAN_TIMEOUT RADARR_LOCK_WARN_AGE
export RADARR_PATH_MAP_JSON
RADARR_PATH_MAP_JSON=$(_path_map_json "RADARR")

# ── Sonarr ─────────────────────────────────────────────────────────────────────
export SONARR_URL SONARR_API_KEY
export SONARR_MEDIA_ROOT="${SONARR_TV_ROOT:-}"
export SONARR_EXTENSIONS="${SONARR_EXTENSIONS[*]:-}"
export SONARR_PROTECTED_PATTERNS="${SONARR_PROTECTED_PATTERNS[*]:-}"
export SONARR_ORPHAN_AGE SONARR_MAX_DELETE_GB
export SONARR_MIN_TRACKED_PCT SONARR_TRACKED_COUNT_FILE
export SONARR_VERSION_MAJOR SONARR_IMPORT_SCAN_TIMEOUT SONARR_LOCK_WARN_AGE
export SONARR_PATH_MAP_JSON
SONARR_PATH_MAP_JSON=$(_path_map_json "SONARR")

exec python3 "$SCRIPT_DIR/arr_cleanup.py" "$@"
