#!/bin/bash
# Black Diamond Probe - Linux Installer
#
# Usage:
#   sudo ./install.sh              System-wide install (recommended)
#   ./install.sh --user            User-local install (~/.vst3)
#   sudo ./install.sh --uninstall  Remove system-wide installation
#   ./install.sh --user --uninstall Remove user-local installation
set -e

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

MODE="system"
UNINSTALL=false

for arg in "$@"; do
    case "$arg" in
        --user) MODE="user" ;;
        --uninstall) UNINSTALL=true ;;
        --help|-h)
            echo "Black Diamond Probe - Linux Installer"
            echo ""
            echo "Usage: $0 [--user] [--uninstall]"
            echo ""
            echo "  (default)    System-wide install to /usr/lib/vst3 (requires sudo)"
            echo "  --user       Install to ~/.vst3 (no sudo required)"
            echo "  --uninstall  Remove the installed VST3 bundle"
            exit 0
            ;;
        *)
            echo "Unknown option: $arg"
            echo "Run '$0 --help' for usage."
            exit 1
            ;;
    esac
done

if [ "$MODE" = "system" ]; then
    VST3_DIR="/usr/lib/vst3"
else
    VST3_DIR="$HOME/.vst3"
fi

if [ "$UNINSTALL" = true ]; then
    echo "Removing ${PLUGIN_NAME}..."
    rm -rf "${VST3_DIR}/${PLUGIN_NAME}.vst3"
    # Note: captured Transfer Function presets in
    #   /opt/Aspen Instruments/Transfer Functions  (system)
    #   ~/.config/Aspen Instruments/Transfer Functions  (user)
    # are user data and intentionally left in place.
    echo "Uninstall complete."
    exit 0
fi

# Check for root on system-wide install
if [ "$MODE" = "system" ] && [ "$(id -u)" -ne 0 ]; then
    echo "System-wide install requires root. Run with sudo or use --user."
    exit 1
fi

# Install VST3
echo "Installing VST3 to: ${VST3_DIR}/"
mkdir -p "${VST3_DIR}"
cp -R "${SCRIPT_DIR}/${PLUGIN_NAME}.vst3" "${VST3_DIR}/"

echo ""
echo "${PLUGIN_NAME} installed successfully."
echo "  VST3:    ${VST3_DIR}/${PLUGIN_NAME}.vst3"
echo ""
echo "Your DAW should detect the plugin on its next scan."
