#!/usr/bin/env bash

# GUARD
if ! command -v pacman &>/dev/null; then
  echo "pacman not found. this script is for arch linux." >&2
  exit 1
fi

# COLOURS
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[1;33m'
blue='\033[0;34m'
cyan='\033[0;36m'
bold='\033[1m'
dim='\033[2m'
nc='\033[0m'

# COUNTERS
_covered=0
_partial=0
_missing=0

# HELPERS
is_installed() {
  pacman -Qq "$1" &>/dev/null
}

print_section() {
  printf "\n${blue}${bold}%s${nc}\n" "$1"
  printf "${dim}--------------------------------------------------${nc}\n"
}

check_any() {
  local category="$1"
  local description="$2"
  shift 2
  local -a found=()
  for pkg in "$@"; do
    is_installed "$pkg" && found+=("$pkg")
  done

  if [[ ${#found[@]} -gt 0 ]]; then
    printf "  ${green}[ ok ]${nc}  ${bold}%-35s${nc}  ${dim}%s${nc}\n" "$category" "${found[0]}"
    _covered=$((_covered + 1))
  else
    printf "  ${red}[ !! ]${nc}  ${bold}%-35s${nc}\n" "$category"
    printf "           ${dim}%s${nc}\n" "$description"
    _missing=$((_missing + 1))
  fi
}

check_all() {
  local category="$1"
  local description="$2"
  shift 2
  local -a found=()
  local -a absent=()
  for pkg in "$@"; do
    if is_installed "$pkg"; then found+=("$pkg"); else absent+=("$pkg"); fi
  done

  if [[ ${#absent[@]} -eq 0 ]]; then
    printf "  ${green}[ ok ]${nc}  ${bold}%-35s${nc}\n" "$category"
    _covered=$((_covered + 1))
  elif [[ ${#found[@]} -gt 0 ]]; then
    printf "  ${yellow}[ /  ]${nc}  ${bold}%-35s${nc}\n" "$category"
    printf "           ${dim}missing: %s${nc}\n" "${absent[*]}"
    _partial=$((_partial + 1))
  else
    printf "  ${red}[ !! ]${nc}  ${bold}%-35s${nc}\n" "$category"
    printf "           ${dim}need all of: %s${nc}\n" "$*"
    _missing=$((_missing + 1))
  fi
}

# HEADER
clear
printf "${cyan}${bold}ARCH LINUX + HYPRLAND AUDIT${nc}\n"
printf "${dim}checking package categories in local database${nc}\n\n"

# CORE SYSTEM
print_section "CORE SYSTEM"
check_all "base system" "essential os foundation" base base-devel
check_any "kernel" "linux kernel flavour" linux linux-lts linux-zen linux-hardened
check_any "ucode" "processor microcode patches" intel-ucode amd-ucode
check_any "bootloader" "system startup manager" grub refind
check_any "filesystems" "fat/ntfs/ext4 utilities" dosfstools ntfs-3g e2fsprogs

# DISPLAY AND COMPOSITOR
print_section "DISPLAY AND COMPOSITOR"
check_any "compositor" "hyprland or alternatives" hyprland sway niri
check_any "session manager" "uwsm or greetd" uwsm ly greetd
check_all "xdg portals" "screen sharing and file pickers" xdg-desktop-portal-hyprland xdg-desktop-portal-gtk
check_any "gpu drivers" "mesa or nvidia proprietary" mesa nvidia nvidia-open

# DESKTOP COMPONENTS
print_section "DESKTOP COMPONENTS"
check_any "status bar" "waybar or eww" waybar eww ags
check_any "launcher" "app and command fuzzy search" rofi-wayland wofi fuzzel
check_any "notifications" "notification daemon" mako swaync dunst
check_any "locker" "screen locking utility" hyprlock swaylock
check_any "wallpaper" "wayland native wallpaper setter" hyprpaper swww
check_all "clipboard" "manager and history stack" wl-clipboard cliphist

# AUDIO AND NETWORK
print_section "AUDIO AND NETWORK"
check_all "pipewire" "full audio stack" pipewire pipewire-pulse pipewire-alsa wireplumber
check_any "network" "networkmanager or iwd" networkmanager iwd
check_any "firewall" "traffic control" ufw nftables

# SHELL AND EDITORS
print_section "SHELL AND EDITORS"
check_any "terminal" "wayland native terminal" kitty alacritty foot ghostty
check_any "shell" "bash or zsh" bash zsh fish
check_any "prompt" "shell prompt customiser" starship oh-my-posh
check_any "editor" "modal or simple text editor" nvim vim micro nano

# INPUT AND POWER
print_section "INPUT AND POWER"
check_any "brightness" "backlight control" brightnessctl light
check_any "power" "power management daemon" tlp auto-cpufreq
check_all "bluetooth" "bluetooth stack" bluez bluez-utils
check_any "bt-manager" "bluetooth frontend" blueman bluetui

# THEMING AND FONTS
print_section "THEMING AND FONTS"
check_any "nerd-font" "glyphs for terminal icons" ttf-jetbrains-mono-nerd ttf-hack-nerd
check_any "emoji" "emoji rendering support" noto-fonts-emoji
check_any "gtk-theme" "gtk appearance manager" nwg-look lxappearance

# SECURITY AND SECRETS
print_section "SECURITY AND SECRETS"
check_any "polkit" "auth agent for elevated tasks" polkit-kde-agent lxqt-policykit
check_any "keyring" "password and secret storage" gnome-keyring

# SUMMARY
_total=$((_covered + _partial + _missing))
printf "\n${blue}${bold}SUMMARY${nc}\n"
printf "${dim}--------------------------------------------------${nc}\n"
printf "  ${green}ok:      %d${nc}\n" "$_covered"
printf "  ${yellow}partial: %d${nc}\n" "$_partial"
printf "  ${red}missing: %d${nc}\n" "$_missing"
printf "  total:   %d\n\n" "$_total"
