#!/bin/bash
# =============================================================================
# uninstall_padworks.command — desinstalador de Pad Works (macOS).
#
# Doble clic abre Terminal. Lista lo que va a borrar, pide confirmación y
# borra POR NOMBRE EXACTO los bundles de Pad Works en las dos Library:
#   /Library/Audio/Plug-Ins/{VST3,Components}      (sistema — pide sudo)
#   ~/Library/Audio/Plug-Ins/{VST3,Components}     (usuario)
# Después pregunta APARTE por los presets de usuario (~/Library/PadWorks).
# Nada más que esos nombres: ningún glob, ningún "*Pad Works*".
#
# Viaja dentro del zip de descarga junto al pkg (scripts/make_pkg_sku.sh) y
# está linkeado desde padworks.io/install ("Uninstalling"). bash 3.2.
#
# Uso:  doble clic · o  ./uninstall_padworks.command [--dry-run] [--yes]
#   --dry-run  sólo lista, no borra nada
#   --yes      no pregunta (para scripts/gates); los presets NO se borran con
#              --yes salvo que se pase también --presets
#   --presets  con --yes: borra también ~/Library/PadWorks
# =============================================================================
set -u

# Catálogo — espejo de PRODUCT_NAME en plugins/*/CMakeLists.txt. make_pkg_sku.sh
# verifica en preflight que cada plugin que empaqueta esté en esta lista.
NAMES=(
    "WIDE" "DRIFT" "FILTER" "CRUSH"
    "PUNCH"
    "GROWL" "REPEAT" "PUMP" "MORPHY"
    "SPACE"
    "BALANZE" "BALANZE Master"
    "DAMP"
)

DRY=0; YES=0; PRESETS=0
for a in "$@"; do
    case "$a" in
        --dry-run) DRY=1 ;;
        --yes)     YES=1 ;;
        --presets) PRESETS=1 ;;
        *) echo "unknown option: $a" >&2; exit 2 ;;
    esac
done

SYS_VST3="/Library/Audio/Plug-Ins/VST3"
SYS_AU="/Library/Audio/Plug-Ins/Components"
USR_VST3="$HOME/Library/Audio/Plug-Ins/VST3"
USR_AU="$HOME/Library/Audio/Plug-Ins/Components"
PRESET_DIR="$HOME/Library/PadWorks"

hr() { echo "------------------------------------------------------------"; }

echo ""
echo "  Pad Works — uninstaller"
hr

# --- 1. inventario por nombre exacto ----------------------------------------
SYS_FOUND=(); USR_FOUND=()
for n in "${NAMES[@]}"; do
    [ -d "$SYS_VST3/$n.vst3" ]      && SYS_FOUND[${#SYS_FOUND[@]}]="$SYS_VST3/$n.vst3"
    [ -d "$SYS_AU/$n.component" ]   && SYS_FOUND[${#SYS_FOUND[@]}]="$SYS_AU/$n.component"
    [ -d "$USR_VST3/$n.vst3" ]      && USR_FOUND[${#USR_FOUND[@]}]="$USR_VST3/$n.vst3"
    [ -d "$USR_AU/$n.component" ]   && USR_FOUND[${#USR_FOUND[@]}]="$USR_AU/$n.component"
done
TOTAL=$(( ${#SYS_FOUND[@]} + ${#USR_FOUND[@]} ))

if [ "$TOTAL" -eq 0 ]; then
    echo "  No Pad Works plug-ins found in /Library or ~/Library."
else
    echo "  These Pad Works plug-ins will be removed:"
    echo ""
    for b in ${SYS_FOUND[@]+"${SYS_FOUND[@]}"}; do echo "    $b"; done
    for b in ${USR_FOUND[@]+"${USR_FOUND[@]}"}; do echo "    $b"; done
    echo ""
    echo "  Your DAW sessions are not touched. Presets are asked about separately."
fi

if [ "$DRY" -eq 1 ]; then
    echo ""
    echo "  --dry-run: nothing removed."
    [ -d "$PRESET_DIR" ] && echo "  (presets folder present: $PRESET_DIR)"
    exit 0
fi

# --- 2. confirmación + borrado ---------------------------------------------
if [ "$TOTAL" -gt 0 ]; then
    if [ "$YES" -ne 1 ]; then
        echo ""
        printf "  Remove them? [y/N] "
        read -r ans
        case "$ans" in y|Y|yes|YES) ;; *) echo "  Nothing removed."; exit 0 ;; esac
    fi
    echo ""
    if [ "${#SYS_FOUND[@]}" -gt 0 ]; then
        echo "  Removing from /Library (administrator password required)…"
        # Un solo sudo con la lista explícita: el usuario ve exactamente qué.
        if ! sudo /bin/rm -rf "${SYS_FOUND[@]}"; then
            echo "  Could not remove from /Library (no password, or not an administrator)." >&2
            exit 1
        fi
    fi
    if [ "${#USR_FOUND[@]}" -gt 0 ]; then
        echo "  Removing from ~/Library…"
        /bin/rm -rf "${USR_FOUND[@]}" || { echo "  Could not remove from ~/Library." >&2; exit 1; }
    fi
    # Que el registro de Audio Units olvide los .component borrados.
    killall -9 AudioComponentRegistrar >/dev/null 2>&1 || true
    echo "  Plug-ins removed."
fi

# --- 3. presets de usuario: pregunta aparte --------------------------------
if [ -d "$PRESET_DIR" ]; then
    echo ""
    echo "  User presets folder: $PRESET_DIR"
    ls "$PRESET_DIR" 2>/dev/null | sed 's/^/    /'
    DO_PRESETS=0
    if [ "$YES" -eq 1 ]; then
        DO_PRESETS=$PRESETS
    else
        printf "  Also delete your user presets? [y/N] "
        read -r ans
        case "$ans" in y|Y|yes|YES) DO_PRESETS=1 ;; esac
    fi
    if [ "$DO_PRESETS" -eq 1 ]; then
        /bin/rm -rf "$PRESET_DIR" && echo "  Presets removed."
    else
        echo "  Presets kept."
    fi
fi

echo ""
echo "  Done. Restart your DAW (or rescan plug-ins)."
hr
exit 0
