#!/usr/bin/env bash
# HP Envy menu helper: watch current DVD or existing video files.
# This does not decrypt/copy/rip media. It only opens the source in an installed player.

set -Eeuo pipefail

pause() {
  printf '\nPress Enter to return to the menu...'
  read -r _ || true
}

detect_dvd_source() {
  local candidate
  for candidate in /dev/dvd /dev/cdrom /dev/sr0 /dev/sr1 /dev/sr2; do
    [[ -e "$candidate" ]] && { printf '%s' "$candidate"; return 0; }
  done
  printf '/dev/sr0'
}

dvd_mount_for_source() {
  local source="$1"
  findmnt -rno SOURCE,TARGET,FSTYPE | awk -v src="$source" '$1==src && ($3=="udf" || $3=="iso9660") {print $2; exit}'
}

dvd_label_for_source() {
  local source="$1" label=""
  if [[ -b "$source" ]]; then
    label="$(lsblk -no LABEL "$source" 2>/dev/null | sed -n '1p' || true)"
  fi
  printf '%s' "$label"
}

player_cmd() {
  for c in vlc mpv celluloid totem; do
    if command -v "$c" >/dev/null 2>&1; then
      printf '%s' "$c"
      return 0
    fi
  done
  if command -v xdg-open >/dev/null 2>&1; then
    printf 'xdg-open'
    return 0
  fi
  return 1
}

launch_gui() {
  local cmd="$1" target="$2"
  if command -v systemd-run >/dev/null 2>&1; then
    systemd-run --user --collect --same-dir --setenv=DISPLAY="${DISPLAY:-:0}" --setenv=WAYLAND_DISPLAY="${WAYLAND_DISPLAY:-wayland-0}" "$cmd" "$target" >/dev/null 2>&1 &
  else
    "$cmd" "$target" >/dev/null 2>&1 &
  fi
}

watch_dvd() {
  local source mount label player target
  source="$(detect_dvd_source)"
  label="$(dvd_label_for_source "$source")"
  mount="$(dvd_mount_for_source "$source" || true)"
  player="$(player_cmd || true)"

  printf '\nCurrent DVD source: %s\n' "$source"
  [[ -n "${label// }" ]] && printf 'DVD label: %s\n' "$label"
  [[ -n "${mount// }" ]] && printf 'Mounted at: %s\n' "$mount"

  if [[ -z "$player" ]]; then
    printf '\nNo supported media player found. Install VLC from the menu first.\n' >&2
    pause
    return 1
  fi

  case "$player" in
    vlc) target="dvd:///$source" ;;
    mpv) target="dvd://" ;;
    celluloid|totem)
      if [[ -n "${mount// }" ]]; then target="$mount"; else target="$source"; fi
      ;;
    xdg-open)
      if [[ -n "${mount// }" ]]; then
        target="$mount"
      else
        printf '\nOnly xdg-open is available and the DVD is not mounted. Install VLC for direct DVD playback.\n' >&2
        pause
        return 1
      fi
      ;;
    *) target="$source" ;;
  esac

  printf '\nOpening with %s: %s\n' "$player" "$target"
  printf 'Note: if the disc is DRM/region restricted, the player may refuse playback. This helper does not bypass DRM.\n'
  launch_gui "$player" "$target"
  pause
}

watch_file() {
  local path player
  printf 'Existing video file path: '
  read -r path
  path="${path/#\~/$HOME}"
  if [[ ! -f "$path" ]]; then
    printf 'File not found: %s\n' "$path" >&2
    pause
    return 1
  fi
  player="$(player_cmd || true)"
  if [[ -z "$player" ]]; then
    printf 'No supported media player found. Install VLC from the menu first.\n' >&2
    pause
    return 1
  fi
  printf '\nOpening with %s: %s\n' "$player" "$path"
  launch_gui "$player" "$path"
  pause
}

install_vlc() {
  printf '\nThis installs VLC using apt. You may be asked for your sudo password.\n\n'
  sudo apt update && sudo apt install -y vlc
  pause
}

status() {
  printf '\nPlayback helper status\n'
  printf '======================\n\n'
  printf 'Available player: '
  player_cmd || printf 'none'
  printf '\n\nDVD devices/media:\n'
  lsblk -o NAME,PATH,TYPE,SIZE,MODEL,FSTYPE,LABEL,MOUNTPOINTS | awk 'NR==1 || $3=="rom" || $2 ~ /\/dev\/sr[0-9]+/ || $6=="udf" || $6=="iso9660"'
  pause
}

case "${1:-menu}" in
  dvd) watch_dvd ;;
  file) watch_file ;;
  install-vlc) install_vlc ;;
  status) status ;;
  menu|*)
    while true; do
      clear || true
      cat <<MENU
Watch Media Helper
==================

1. Watch current DVD
2. Watch existing video file
3. Install VLC media player
4. Playback status
q. Quit
MENU
      printf '\nChoose: '
      read -r choice || exit 0
      case "$choice" in
        1) watch_dvd ;;
        2) watch_file ;;
        3) install_vlc ;;
        4) status ;;
        q|Q|quit|exit) exit 0 ;;
        *) printf 'Invalid choice.\n'; sleep 1 ;;
      esac
    done
    ;;
esac
