#!/bin/bash
# Black Diamond Distortion - 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 Distortion"

MODE="system"
UNINSTALL=false

for arg in "$@"; do
    case "$arg" in
        --user) MODE="user" ;;
        --uninstall) UNINSTALL=true ;;
        --help|-h)
            echo "Black Diamond Distortion - 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 installed files"
            exit 0
            ;;
        *)
            echo "Unknown option: $arg"
            echo "Run '$0 --help' for usage."
            exit 1
            ;;
    esac
done

# Paths must match what JUCE resolves at runtime in PluginProcessor.cpp:
#   commonApplicationDataDirectory -> /opt  (system factory presets)
#   userApplicationDataDirectory   -> ~/.config (user presets)
if [ "$MODE" = "system" ]; then
    VST3_DIR="/usr/lib/vst3"
    PRESETS_DIR="/opt/Aspen Instruments/Transfer Functions"
    PLUGIN_PRESETS_DIR="/opt/Aspen Instruments/Black Diamond Distortion"
else
    VST3_DIR="$HOME/.vst3"
    PRESETS_DIR="$HOME/.config/Aspen Instruments/Transfer Functions"
    PLUGIN_PRESETS_DIR="$HOME/.config/Aspen Instruments/Black Diamond Distortion"
fi

if [ "$UNINSTALL" = true ]; then
    echo "Removing ${PLUGIN_NAME}..."
    rm -rf "${VST3_DIR}/${PLUGIN_NAME}.vst3"
    rm -rf "${PRESETS_DIR}"
    rm -rf "${PLUGIN_PRESETS_DIR}"
    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}/"

# Install factory presets
if [ -d "${SCRIPT_DIR}/Transfer Functions" ]; then
    echo "Installing factory presets to: ${PRESETS_DIR}/"
    mkdir -p "${PRESETS_DIR}"
    cp -R "${SCRIPT_DIR}/Transfer Functions/"* "${PRESETS_DIR}/"
fi

# Install plugin presets
if [ -d "${SCRIPT_DIR}/Plugin Presets" ]; then
    echo "Installing plugin presets to: ${PLUGIN_PRESETS_DIR}/"
    mkdir -p "${PLUGIN_PRESETS_DIR}"
    cp -R "${SCRIPT_DIR}/Plugin Presets/"* "${PLUGIN_PRESETS_DIR}/"
fi

echo ""
echo "${PLUGIN_NAME} installed successfully."
echo "  VST3:    ${VST3_DIR}/${PLUGIN_NAME}.vst3"
[ -d "${PRESETS_DIR}" ] && echo "  Presets:  ${PRESETS_DIR}/"
[ -d "${PLUGIN_PRESETS_DIR}" ] && echo "  Plugin Presets: ${PLUGIN_PRESETS_DIR}/"
echo ""
echo "Your DAW should detect the plugin on its next scan."
