#!/usr/bin/env bash
# Install Tunnels plug-ins into USER-scope folders (~/.vst3, ~/.clap).
#
# Dual-purpose: shipped inside the Linux release tarball as install.sh (the
# portability path for non-Debian distros), and used by developers for a
# no-sudo local install. The Debian .deb installs SYSTEM-scope (/usr/lib/...)
# instead — don't keep both, or a DAW may load the stale one.
#
# Usage: install-linux.sh [SRC_DIR]   (SRC_DIR defaults to this script's dir)
set -euo pipefail

src="${1:-$(cd "$(dirname "$0")" && pwd)}"
vst3_dest="$HOME/.vst3"
clap_dest="$HOME/.clap"

# Bundles sit flat next to this script in the release tarball, or under
# per-format subdirs (VST3/, CLAP/) in a build tree — find either.
vst3_src="$(find "$src" -maxdepth 2 -name Tunnels.vst3 -print -quit 2>/dev/null || true)"
clap_src="$(find "$src" -maxdepth 2 -name Tunnels.clap -print -quit 2>/dev/null || true)"

[[ -n "$vst3_src" || -n "$clap_src" ]] || {
    echo "No Tunnels plug-ins found in $src" >&2
    exit 1
}

# Warn (don't fail) if a system-scope copy from the .deb is already present —
# two copies in different scopes can shadow or duplicate in a DAW's scan.
for sys in /usr/lib/vst3/Tunnels.vst3 /usr/lib/clap/Tunnels.clap; do
    [[ -e "$sys" ]] || continue
    echo "Warning: a system-scope copy exists at $sys (installed by the .deb)." >&2
    echo "         Remove it or uninstall the package to avoid a duplicate scan." >&2
done

if [[ -n "$vst3_src" ]]; then
    mkdir -p "$vst3_dest"
    rm -rf "$vst3_dest/Tunnels.vst3"
    cp -R "$vst3_src" "$vst3_dest/"
    echo "Installed $vst3_dest/Tunnels.vst3"
fi

if [[ -n "$clap_src" ]]; then
    mkdir -p "$clap_dest"
    rm -rf "$clap_dest/Tunnels.clap"
    cp -R "$clap_src" "$clap_dest/"
    echo "Installed $clap_dest/Tunnels.clap"
fi

echo "Done. Restart your DAW to scan."
