#!/usr/bin/env bash
#
# Tape DSP — Linux installer (offline). Installs the effects pack (VST3 + LV2)
# from the .deb in this folder after the license is accepted. No loose plugin
# tarball is shipped. Works on any distro; re-run any time to reinstall/upgrade.
#
#   Per-user (default):   ./install.sh
#   System-wide:          sudo ./install.sh --system
#   Uninstall:            ./install.sh --uninstall
#   Accept license:       ./install.sh --accept-license
#                         (or TAPE_ACCEPT_LICENSE=1 after reading LICENSE.txt;
#                         non-interactive installs without that flag fail)
set -euo pipefail

HERE="$(cd "$(dirname "$0")" && pwd)"
MANIFEST="$HERE/tape-dsp-release-manifest.sh"
if [ -f "$MANIFEST" ]; then
  # shellcheck source=/dev/null
  source "$MANIFEST"
elif [ -f "$HERE/../pack-products.sh" ]; then
  # Development-tree fallback.
  # shellcheck source=../pack-products.sh
  source "$HERE/../pack-products.sh"
  tape_pack_load_release_selection
else
  echo "Tape DSP: release manifest not found next to installer." >&2
  exit 1
fi
find_deb() {
  shopt -s nullglob
  local files=( "$HERE"/tape-dsp_*_amd64.deb )
  shopt -u nullglob
  [ "${#files[@]}" -ge 1 ] || return 1
  printf '%s\n' "${files[0]}"
}

extract_deb() {
  local deb="$1" dest="$2"
  if command -v dpkg-deb >/dev/null 2>&1; then
    dpkg-deb -x "$deb" "$dest"
    return 0
  fi
  command -v ar >/dev/null 2>&1 || {
    echo "Tape DSP: need dpkg-deb or ar to read the .deb." >&2
    return 1
  }
  local nest data
  nest="$(mktemp -d /tmp/tapedsp-deb-ar.XXXXXX)"
  (cd "$nest" && ar x "$deb") || { rm -rf "$nest"; return 1; }
  data=""
  for f in "$nest"/data.tar.*; do
    [ -f "$f" ] && data="$f" && break
  done
  if [ -z "$data" ]; then
    rm -rf "$nest"
    echo "Tape DSP: .deb has no data archive." >&2
    return 1
  fi
  case "$data" in
    *.xz|*.lzma) tar -xJf "$data" -C "$dest" ;;
    *.gz)        tar -xzf "$data" -C "$dest" ;;
    *.zst)       tar --zstd -xf "$data" -C "$dest" ;;
    *)           tar -xf "$data" -C "$dest" ;;
  esac
  local ok=$?
  rm -rf "$nest"
  return "$ok"
}

MODE="user"; ACTION="install"; ACCEPT_LICENSE=0
for arg in "$@"; do
  case "$arg" in
    --system) MODE="system";;
    --uninstall) ACTION="uninstall";;
    --accept-license) ACCEPT_LICENSE=1;;
    -h|--help) echo "Usage: install.sh [--system] [--uninstall] [--accept-license]"; exit 0;;
  esac
done

if [ "$ACTION" != "uninstall" ]; then
  LICENSE="$HERE/LICENSE.txt"
  if [ ! -f "$LICENSE" ] && [ -f "$HERE/../LICENSE.txt" ]; then
    LICENSE="$HERE/../LICENSE.txt"
  fi
  if [ ! -f "$LICENSE" ]; then
    echo "Tape DSP: LICENSE.txt is missing next to this installer." >&2
    exit 1
  fi
  ACCEPT_LIB="$HERE/accept_license.sh"
  if [ ! -f "$ACCEPT_LIB" ] && [ -f "$HERE/../lib/accept_license.sh" ]; then
    ACCEPT_LIB="$HERE/../lib/accept_license.sh"
  fi
  if [ ! -f "$ACCEPT_LIB" ]; then
    echo "Tape DSP: accept_license.sh is missing next to this installer." >&2
    exit 1
  fi
  # shellcheck source=../lib/accept_license.sh
  . "$ACCEPT_LIB"
  if [ "$ACCEPT_LICENSE" = 1 ]; then
    TAPE_ACCEPT_LICENSE=1
  fi
  tape_accept_license "$LICENSE"
fi

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

NAMES=("${TAPE_RELEASE_DISPLAY_NAMES[@]}")

if [ "$ACTION" = "uninstall" ]; then
  for n in "${NAMES[@]}"; do
    rm -rf "$VST3_DEST/$n.vst3" "$LV2_DEST/$n.lv2"
  done
  echo "Tape DSP: removed."
  exit 0
fi

command -v tar >/dev/null 2>&1 || { echo "tar is required." >&2; exit 1; }
DEB="$(find_deb)" || {
  echo "Tape DSP: tape-dsp_*_amd64.deb is missing next to this installer." >&2
  exit 1
}

TMP="$(mktemp -d /tmp/tapedsp-install.XXXXXX)"
trap 'rm -rf "$TMP"' EXIT

echo "Tape DSP: installing bundled plugins…"
extract_deb "$DEB" "$TMP" || exit 1

mkdir -p "$VST3_DEST" "$LV2_DEST"
installed=0
for n in "${NAMES[@]}"; do
  if [ -d "$TMP/usr/lib/vst3/$n.vst3" ]; then
    echo "Tape DSP: installing $n.vst3 → $VST3_DEST"
    rm -rf "$VST3_DEST/$n.vst3"
    cp -R "$TMP/usr/lib/vst3/$n.vst3" "$VST3_DEST/"
    installed=$((installed + 1))
  fi
  if [ -d "$TMP/usr/lib/lv2/$n.lv2" ]; then
    echo "Tape DSP: installing $n.lv2 → $LV2_DEST"
    rm -rf "$LV2_DEST/$n.lv2"
    cp -R "$TMP/usr/lib/lv2/$n.lv2" "$LV2_DEST/"
  fi
done
[ "$installed" -gt 0 ] || {
  echo "Tape DSP: the .deb did not contain the expected plug-ins." >&2
  exit 1
}

echo "Tape DSP: installation complete."
