#!/bin/bash
set -e

echo "=== Hyprland Environment Installer (legacy — hyprlang config) ==="

# 1. Update system and install Flatpak
echo "Updating system and installing Flatpak..."
sudo pacman -Syu --noconfirm --needed flatpak

# 2. Install required packages
echo "Installing required packages..."
sudo pacman -Syu --noconfirm --needed \
    hyprland hyprcursor wl-clipboard hyprpaper hyprlock wofi kitty dunst \
    nwg-dock-hyprland nwg-drawer nwg-menu nwg-look \
    python cmake meson cpio pkgconf ruby-pkg-config \
    hyprsunset hypridle ksshaskpass \
    nm-connection-editor network-manager-applet blueman bluez \
    pipewire alsa-utils firefox greetd-tuigreet \
    grim slurp gst-plugin-pipewire imagemagick \
    nerd-fonts otf-font-awesome \
    pipewire-alsa pipewire-jack pipewire-pulse \
    qt5-wayland qt6-wayland swww ttf-jetbrains-mono wireplumber \
    qt6ct xdg-desktop-portal-hyprland xdg-utils \
    xorg-server xorg-xinit papirus-icon-theme \
    cool-retro-term qalculate-gtk iwd dbus \
    thunar tumbler thunar-archive-plugin thunar-shares-plugin thunar-volman \
    hyprpicker pcmanfm-qt udisks2 ly kew \
    hyprpolkitagent pavucontrol playerctl wf-recorder sound-theme-freedesktop

# 3. Enable essential services
echo "Enabling essential services..."
sudo systemctl enable NetworkManager.service
sudo systemctl disable getty@tty1.service || true
sudo systemctl enable ly@tty1.service
sudo systemctl enable udisks2.service

# 4. Install AUR packages
echo "Installing AUR packages..."
rustup default stable
yay -Syu --answerdiff None --answerclean All --noconfirm \
    hyprland-workspaces vicinae-bin bluetuith wvkbd iwmenu pinta \
    walker-bin ulauncher bzmenu udiskie \
    wofi-calc bri chamel

# 5. EWW bar selection and compilation
echo "Setting up EWW bar..."
rm -rf ~/.config/eww
read -n1 -p "Install eww bar for PC, Notebook or Tablet [P/N/T]: " doit
echo
case $doit in
    n|N) cp -rf ~/Dotfiles/desktopenvs/hyprland/eww/ ~/.config/ ;;
    p|P) cp -rf ~/Dotfiles/desktopenvs/hyprland/eww-nobattery/ ~/.config/eww ;;
    t|T) cp -rf ~/Dotfiles/desktopenvs/hyprland/eww-touch/ ~/.config/eww ;;
    *)   echo "No valid choice — skipping EWW copy. Run manually later." ;;
esac

echo "Compiling EWW..."
mkdir -p ~/install-tmp
cd ~/install-tmp
git clone https://github.com/elkowar/eww
cd eww
cargo build --release --no-default-features --features=wayland
chmod +x target/release/eww
sudo cp target/release/eww /usr/bin/
cd ~

# 6. Theme and icon setup
sudo cp -r ~/Dotfiles/gtk-themes/cyberqueer /usr/share/themes
sudo cp ~/Dotfiles/desktopenvs/hyprland/btop/themes/cyberqueer.theme /usr/share/btop/themes
sudo cp -f ~/Dotfiles/etc-ly-config.ini /etc/ly/config.ini
sudo ln -sf /usr/bin/kitty /usr/bin/xdg-terminal-exec
sudo ln -sf /usr/bin/ksshaskpass /usr/lib/ssh/ssh-askpass

# 7. Cursor setup
mkdir -p ~/.icons
wget -O ~/install-tmp/Nordzy-cursors-lefthand.tar.gz \
    https://github.com/guillaumeboehm/Nordzy-cursors/releases/download/v2.3.0/Nordzy-cursors-lefthand.tar.gz
tar -zxf ~/install-tmp/Nordzy-cursors-lefthand.tar.gz -C ~/.icons/

# 8. Enable Bluetooth and wireless services
sudo systemctl enable bluez
sudo systemctl enable bluetooth.service
sudo systemctl enable iwd.service

# 9. Hyprland plugins — must be run from inside a live Hyprland session
# Run manually after first login:
#   hyprpm update
#   hyprpm add https://github.com/hyprwm/hyprland-plugins

# 10. Copy configs
echo "Copying configs..."
CONFIGS=(kitty mimeapps.list vicinae walker ulauncher hypr xfce4 wofi dunst alacritty nwg-dock-hyprland nwg-drawer nwg-panel scripts btop gtk-3.0)
for cfg in "${CONFIGS[@]}"; do
    rm -rf ~/.config/"$cfg"
    cp -r ~/Dotfiles/desktopenvs/hyprland/"$cfg" ~/.config/
done

cp ~/Dotfiles/desktopenvs/hyprland/hypr-usr/* ~/.config/
cp ~/Dotfiles/colors.conf ~/.config/colors.conf

# 11. Wallpaper and resources
mkdir -p ~/Pictures
cp ~/Dotfiles/resources/fflogo.svg ~/Pictures/fflogo.svg
wget "https://cloud.abdelbaki.eu/apps/theming/image/background?v=15" -O ~/Pictures/background.jpg

# 12. Python venv for scripts
python -m venv ~/.config/python-script
~/.config/python-script/bin/pip install speedtest-cli requests pint simpleeval parsedatetime

# 13. Udiskie icon fix
echo "Applying Udiskie icon fix..."
PAPIRUS_DIR="/usr/share/icons/Papirus-Dark/status"
HICOLOR_DIR="/usr/share/icons/hicolor/scalable/status"
if [ -d "$PAPIRUS_DIR" ]; then
    sudo ln -sf "$PAPIRUS_DIR/checkbox-checked.svg"   "$HICOLOR_DIR/udiskie-checkbox-checked.svg"
    sudo ln -sf "$PAPIRUS_DIR/checkbox-unchecked.svg" "$HICOLOR_DIR/udiskie-checkbox-unchecked.svg"
    sudo gtk-update-icon-cache -f -t /usr/share/icons/hicolor
else
    echo "Papirus-Dark not found — skipping udiskie icon fix."
fi

# 14. Enable udiskie
sudo systemctl enable udiskie.service
sudo systemctl start udiskie.service

# 15. Install config updater and theme script
mkdir -p ~/.config/config-updater
ln -sf ~/Dotfiles/desktopenvs/hyprland/config-updater/updater.conf ~/.config/config-updater/updater.conf
ln -sf ~/Dotfiles/desktopenvs/hyprland/config-updater/update-configs.sh ~/update-configs.sh
cp ~/Dotfiles/apply-theme.sh ~/apply-theme.sh
chmod +x ~/apply-theme.sh

# 16. WallRizz (run manually after login — requires a running desktop session)
# curl -sL "$(curl -s https://api.github.com/repos/5hubham5ingh/WallRizz/releases/latest \
#     | grep -Po '"browser_download_url": "\K[^"]+' | grep WallRizz)" | tar -xz \
#     && sudo mv WallRizz /usr/bin/

echo "=== Hyprland installation complete. Reboot to start. ==="
