#!/usr/bin/env bash
set -euo pipefail

DOTFILES="$(cd "$(dirname "$0")" && pwd)"

# --- package manager detection ---

install_packages() {
  if command -v pacman &>/dev/null; then
    sudo pacman -S --needed "$@"
  elif command -v apt &>/dev/null; then
    sudo apt install -y "$@"
  elif command -v dnf &>/dev/null; then
    sudo dnf install -y "$@"
  else
    echo "No supported package manager found (pacman/apt/dnf)" >&2
    return 1
  fi
}

# --- dependencies ---

if command -v pacman &>/dev/null; then
  install_packages \
    git zsh neovim ripgrep fd fzf \
    clang rust-analyzer lua-language-server stylua cmake \
    tinymist websocat \
    ttf-firacode-nerd \
    hyprland hyprpaper hyprlock kanshi eww rofi ghostty dolphin \
    network-manager-applet blueman grim slurp swappy
elif command -v apt &>/dev/null; then
  install_packages \
    git zsh neovim ripgrep fd-find fzf \
    clangd rust-analyzer cmake \
    fonts-firacode \
    kanshi rofi dolphin \
    network-manager-gnome blueman grim slurp
elif command -v dnf &>/dev/null; then
  install_packages \
    git zsh neovim ripgrep fd-find fzf \
    clang-tools-extra rust-analyzer cmake \
    fira-code-fonts \
    hyprpaper kanshi rofi ghostty dolphin \
    network-manager-applet blueman grim slurp
fi

# --- symlinks ---

for dir in "$DOTFILES"/config/*/; do
  ln -sfn "$dir" "$HOME/.config/$(basename "$dir")"
done

# --- system config ---

sudo install -Dm644 "$DOTFILES/system/zshenv" /etc/zsh/zshenv
