#!/usr/bin/env bash
# check-deps.sh — report missing commands used by this Hyprland setup
# Usage: ./scripts/check-deps.sh

set -euo pipefail

script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
repo_root="${script_dir%/scripts}"

header() {
  printf '\n== %s ==\n' "$1"
}

info() { printf '  - %s\n' "$1"; }

required=(
  'uwsm|launch wrapper used for apps'
  'swaync|notification daemon'
  'swaync-client|notification control client'
  'waybar|status bar'
  'xsettingsd|XSettings bridge for GTK apps'
  'hyprpaper|wallpaper daemon'
  'hypridle|idle manager'
  'hyprlock|lock screen'
  'polkit-gnome-authentication-agent-1|Polkit authentication agent required for permission prompts'
  'hyprctl|Hyprland control tool'
  'hyprsunset|color temperature daemon'
  'swayosd-server|overlay indicators'
  'swayosd-client|volume/brightness OSD client'
  'xdg-desktop-portal-hyprland|Wayland portal backend for screen sharing and sandboxed apps'
  'playerctl|media key handler'
  'wpctl|PipeWire volume control'
  'grimblast|screenshot helper'
  'kitty|default terminal emulator'
  'nautilus|default file manager'
)

optional=(
  'sunwait|enables real sunrise/sunset scheduling for hyprsunset'
  'brightnessctl|required on laptop for idle dimming'
  'nm-applet|NetworkManager tray (laptop)'
  'blueman-applet|Bluetooth tray (laptop)'
  'clipman|clipboard history for wl-paste hook'
  'wl-paste|clipboard watcher utility'
  'sherlock|launcher binding'
  'uuctl|uwsm controller binding'
  'emote|emoji picker binding'
  'satty|grimblast editor when building from source'
)

missing_required=()
missing_optional=()

check_list() {
  local -n list=$1
  local -n missing=$2
  local entry cmd desc
  for entry in "${list[@]}"; do
    IFS='|' read -r cmd desc <<<"$entry"
    if ! command -v "$cmd" >/dev/null 2>&1; then
      missing+=("$cmd|$desc")
    fi
  done
}

check_list required missing_required
check_list optional missing_optional

header "Required commands"
if ((${#missing_required[@]}==0)); then
  info "all required commands found"
else
  for entry in "${missing_required[@]}"; do
    IFS='|' read -r cmd desc <<<"$entry"
    info "missing: $cmd ($desc)"
  done
fi

header "Optional commands"
if ((${#missing_optional[@]}==0)); then
  info "all optional commands found"
else
  for entry in "${missing_optional[@]}"; do
    IFS='|' read -r cmd desc <<<"$entry"
    info "missing: $cmd ($desc)"
  done
fi

if ((${#missing_required[@]})); then
  exit 1
fi
