# Shared EULA gate for the Linux .deb preinst and install.sh.
# POSIX sh — Debian maintainer scripts cannot assume bash.
#
# tape_accept_license <license-file>
#   TAPE_ACCEPT_LICENSE=1  skip prompt (CI / scripted installs only)
#   GUI (zenity / kdialog / yad) when DISPLAY or WAYLAND_DISPLAY is set
#   Otherwise a terminal prompt. Fail closed if neither is available.

tape_accept_license() {
  license_file="${1:-}"
  if [ ! -f "$license_file" ]; then
    echo "Tape DSP: license file is missing." >&2
    exit 1
  fi

  if [ "${TAPE_ACCEPT_LICENSE:-}" = "1" ]; then
    echo "Tape DSP: license accepted via TAPE_ACCEPT_LICENSE=1"
    return 0
  fi

  accepted=0

  if [ -n "${DISPLAY:-}${WAYLAND_DISPLAY:-}" ]; then
    if command -v zenity >/dev/null 2>&1; then
      if zenity --text-info \
           --title="Tape DSP License" \
           --filename="$license_file" \
           --checkbox="I accept the Tape DSP Plugin License" \
           --ok-label="Install" \
           --cancel-label="Decline" \
           --width=680 --height=540; then
        accepted=1
      else
        echo "Tape DSP: install cancelled — license not accepted." >&2
        exit 1
      fi
    elif command -v kdialog >/dev/null 2>&1; then
      kdialog --title "Tape DSP License" --textbox "$license_file" 680 540 || true
      if kdialog --title "Tape DSP License" --yesno "Do you accept the Tape DSP Plugin License?"; then
        accepted=1
      else
        echo "Tape DSP: install cancelled — license not accepted." >&2
        exit 1
      fi
    elif command -v yad >/dev/null 2>&1; then
      if yad --text-info --filename="$license_file" \
           --title="Tape DSP License" \
           --button="Decline:1" --button="I accept:0" \
           --width=680 --height=540; then
        accepted=1
      else
        echo "Tape DSP: install cancelled — license not accepted." >&2
        exit 1
      fi
    fi
  fi

  if [ "$accepted" = 1 ]; then
    return 0
  fi

  if [ "${DEBIAN_FRONTEND:-}" = "noninteractive" ]; then
    echo "Tape DSP: you must accept the license to install." >&2
    echo "Read https://tapedsp.com/terms then install from a terminal or desktop." >&2
    echo "CI only: TAPE_ACCEPT_LICENSE=1" >&2
    exit 1
  fi

  if command -v whiptail >/dev/null 2>&1 && [ -r /dev/tty ]; then
    whiptail --title "Tape DSP License" --textbox "$license_file" 24 78 < /dev/tty || true
    if whiptail --title "Tape DSP License" --yesno "Do you accept the Tape DSP Plugin License?" 10 72 < /dev/tty; then
      return 0
    fi
    echo "Tape DSP: install cancelled — license not accepted." >&2
    exit 1
  fi

  if [ -t 1 ] || [ -r /dev/tty ]; then
    echo "Tape DSP Plugin End User License Agreement"
    echo "Also at https://tapedsp.com/terms"
    echo ""
    cat "$license_file"
    echo ""
    printf "Do you accept the Tape DSP Plugin License? [y/N] "
    ans=""
    if [ -r /dev/tty ]; then
      read -r ans < /dev/tty || ans=""
    elif [ -t 0 ]; then
      read -r ans || ans=""
    fi
    case "$ans" in
      y|Y|yes|YES) return 0 ;;
      *)
        echo "Tape DSP: install cancelled — license not accepted." >&2
        exit 1
        ;;
    esac
  fi

  echo "Tape DSP: you must accept the license to install." >&2
  echo "Open the .deb on a desktop so the license dialog can appear, or run dpkg from a terminal." >&2
  echo "Read https://tapedsp.com/terms" >&2
  exit 1
}
