#!/bin/bash
# Install Arch Linux packages and setup for Sway

set -e

echo "Installing Sway requirements and packages for Arch Linux..."
echo "==========================================================="
echo ""
echo "This script will install packages and configure services for Sway."
echo "You can comment out sections you don't want to install."
echo ""

# === CORE SWAY PACKAGES ===
# Comment out this section if you already have Sway installed
# echo "=== Installing Core Sway Packages ==="
# echo "Installing: sway swaylock swayidle fuzzel foot i3status-rust"
# sudo pacman -S --needed sway swaylock swayidle fuzzel foot i3status-rust
# echo "✓ Core Sway packages installed"
# echo ""

# === ESSENTIAL SYSTEM PACKAGES ===
# Comment out this section if you don't want these system packages
echo "=== Installing Essential System Packages ==="
echo "Installing: file manager, screenshots, brightness, display config, terminal"
sudo pacman -S --needed thunar grim slurp brightnessctl wdisplays kitty
echo "✓ Essential system packages installed"
echo ""

# === NOTIFICATION SYSTEM ===
# Comment out this section if you don't want notifications
echo "=== Installing Notification System ==="
echo "Installing: mako (notification daemon)"
sudo pacman -S --needed mako
echo "✓ Notification system installed"
echo ""

# === AUDIO SYSTEM ===
# Comment out this section if you already have audio configured
# echo "=== Installing Audio System (PipeWire) ==="
# echo "Installing: pipewire, pipewire-pulse, pipewire-alsa, wireplumber"
# sudo pacman -S --needed pipewire pipewire-pulse pipewire-alsa wireplumber
# echo "✓ Audio system installed"
# echo ""

# === NETWORK AND BLUETOOTH ===
# Comment out this section if you don't want NetworkManager/Bluetooth
# echo "=== Installing Network and Bluetooth ==="
# echo "Installing: networkmanager, blueman, network-manager-applet"
# sudo pacman -S --needed networkmanager blueman network-manager-applet
# echo "✓ Network and Bluetooth packages installed"
# echo ""

# === WAYLAND INTEGRATION ===
# Comment out this section if you don't need screen sharing/file pickers
echo "=== Installing Wayland Integration ==="
echo "Installing: xdg-desktop-portal-wlr (screen sharing, file pickers)"
sudo pacman -S --needed xdg-desktop-portal-wlr
echo "✓ Wayland integration installed"
echo ""

# === FONTS ===
# Comment out this section if you don't want additional fonts
echo "=== Installing Fonts ==="
echo "Installing: ttf-font-awesome, ttf-dejavu, noto-fonts, noto-fonts-emoji"
sudo pacman -S --needed ttf-font-awesome ttf-dejavu noto-fonts noto-fonts-emoji
echo "✓ Fonts installed"
echo ""

# === VISUAL ENHANCEMENTS ===
# Comment out this section if you don't want icon themes
echo "=== Installing Visual Enhancements ==="
echo "Installing: papirus-icon-theme"
sudo pacman -S --needed papirus-icon-theme
echo "✓ Visual enhancements installed"
echo ""

# === UTILITIES ===
# Comment out this section if you don't want these utilities
echo "=== Installing Utilities ==="
echo "Installing: curl, libnotify, jq, pavucontrol, thunar-volman, gvfs"
sudo pacman -S --needed curl libnotify jq pavucontrol thunar-volman gvfs
echo "✓ Utilities installed"
echo ""

# === AUTHENTICATION AGENT (OPTIONAL) ===
# Comment out this section if you don't want GUI password prompts
echo "=== Installing Authentication Agent (Optional) ==="
echo "Choose one or comment out entirely:"
echo ""
echo "1. lxqt-policykit (recommended: lightweight Qt-based)"
echo "2. polkit-dumb-agent (minimal: command-line only)"
echo "3. Skip (no GUI password prompts)"
echo ""
read -p "Enter choice (1/2/3): " auth_choice

case $auth_choice in
    1)
        echo "Installing lxqt-policykit..."
        sudo pacman -S --needed lxqt-policykit
        echo "✓ lxqt-policykit installed"
        echo "Note: Auto-started in Sway config"
        ;;
    2)
        echo "Installing polkit-dumb-agent..."
        sudo pacman -S --needed polkit-dumb-agent
        echo "✓ polkit-dumb-agent installed"
        echo "Note: You'll need to update Sway config to use this instead"
        ;;
    3)
        echo "Skipping authentication agent"
        echo "Note: Comment out 'exec lxqt-policykit-agent' in Sway config"
        ;;
    *)
        echo "Invalid choice, skipping authentication agent"
        ;;
esac
echo ""

# === ENABLE SERVICES ===
# Comment out this section if you don't want to enable services
echo "=== Enabling Services ==="

# NetworkManager
echo "Enabling NetworkManager..."
if ! systemctl is-enabled NetworkManager >/dev/null 2>&1; then
    sudo systemctl enable NetworkManager
    echo "✓ NetworkManager enabled"
else
    echo "✓ NetworkManager already enabled"
fi

if ! systemctl is-active NetworkManager >/dev/null 2>&1; then
    sudo systemctl start NetworkManager
    echo "✓ NetworkManager started"
else
    echo "✓ NetworkManager already running"
fi

# PipeWire (user services)
# echo "Enabling PipeWire services..."
# systemctl --user enable pipewire pipewire-pulse wireplumber 2>/dev/null || true
# systemctl --user start pipewire pipewire-pulse wireplumber 2>/dev/null || true
# echo "✓ PipeWire services enabled and started"
# echo ""

# === CONFIGURATION NOTES ===
echo "=========================================="
echo "✅ Package installation complete!"
echo "=========================================="
echo ""
echo "🔧 Additional setup recommendations:"
echo ""
echo "1. Add to your shell rc file (~/.bashrc or ~/.zshrc):"
echo "   export XDG_CURRENT_DESKTOP=sway"
echo "   export XDG_SESSION_DESKTOP=sway"
echo ""
echo "2. Weather configuration:"
echo "   - Default location: Pärnu (wttr.in, no API key needed)"
echo "   - Shows weather + sunrise/sunset times"
echo "   - Left click: detailed weather notification"
echo "   - Right click: open wttr.in website"
echo "   - Change location: edit ~/.config/sway/scripts/i3status/wttrin-weather.sh"
echo ""
echo "3. Key applications configured:"
echo "   - Terminal: foot"
echo "   - File manager: thunar"
echo "   - Launcher: fuzzel"
echo "   - Display settings: wdisplays"
echo "   - Network: nm-connection-editor"
echo "   - Bluetooth: blueman-manager"
echo ""
echo "4. Alternative applications you might prefer:"
echo "   - Terminal: alacritty, kitty, wezterm"
echo "   - File manager: nautilus, dolphin, nemo, pcmanfm"
echo "   - Launcher: wofi, rofi-wayland, bemenu"
echo ""
echo "5. To customize apps, edit variables in ~/.config/sway/config:"
echo "   - set \$term foot"
echo "   - set \$filemanager thunar"
echo "   - set \$menu fuzzel"
echo ""
echo "6. For HiDPI displays, add to Sway config:"
echo "   output * scale 1.5"
echo ""
echo "🚀 Ready to use Sway!"
echo "   - Log out and select Sway from display manager"
echo "   - Or start from TTY with: sway"
