#!/bin/sh

# ───────────────────────────────────────────────────────────────────
#          ╭────────────────────────────────────────────────╮
#          │      Setup Script for janishutz Hyprland       │
#          ╰────────────────────────────────────────────────╯
# ───────────────────────────────────────────────────────────────────

echo "
                          _           _          _   _                  _                   _ 
     _             _     ( )         ( )_       ( ) ( )                (_ )                ( )
    (_)  _ _  ___ (_) ___| |__  _   _|  _)____  | |_| |_   _ _ _   _ __ | |   _ _  ___    _| |
    | |/ _  )  _  \ |  __)  _  \ ) ( ) | (_   ) |  _  | ) ( )  _ \(  __)| | / _  )  _  \/ _  |
    | | (_| | ( ) | |__  \ | | | (_) | |_ / /_  | | | | (_) | (_) ) |   | |( (_| | ( ) | (_| |
 _  | |\__ _)_) (_)_)____/_) (_)\___/ \__)____) (_) (_)\__  |  __/(_)  (___)\__ _)_) (_)\__ _)
( )_| |                                               ( )_| | |                               
 \___/                                                 \___/(_)                               

    => Setup script. This script is used to:
        -> Set up configs initially
        -> Regenerate the themes
        -> Install new versions
            
"

trap 'echo -e "\nCaught Ctrl+C, exiting..."; exit 130' SIGINT

# Read platform to install on (only if no platform file present in ~/.config/)
platform=""
if [[ -f ~/.config/platform ]]; then
    platform=$(cat ~/.config/platform)
else
    read -p "Choose the configs to install, Laptop or Desktop (l/D): " platform
    echo "$platform" >~/.config/platform
fi
platform=$(echo "$platform" | tr '[:upper:]' '[:lower:]')

# ────────────────────────────────────────────────────────────────────
# Get user preference for regenerating the styling
regen=""
read -p "Would you like to regenerate styling? (y/N) " regen
restart=""
if [[ "$regen" == "y" ]]; then
    restart="y"
    cd build
    node build.js
    if [ $? -ne 0 ]; then
        echo -e "\nExit signal received, exiting...."
        exit 130
    fi
    cd ..
else
    read -p "Apply full config? (y/N) " restart
fi

# ────────────────────────────────────────────────────────────────────
# hyprvim config
hyprvim=""
if [[ -f ~/.config/hyprvim ]]; then
    hyprvim=$(cat ~/.config/hyprvim)
else
    read -p "Would you like to use hyprvim? (Y/n) " hyprvim
fi
hyprvim=$(echo "$hyprvim" | tr '[:upper:]' '[:lower:]')
if [[ "$hyprvim" == "" ]]; then
    hyprvim="y"
fi
echo "$hyprvim" >~/.config/hyprvim

# ────────────────────────────────────────────────────────────────────
echo "=> Moving configs to correct destinations"
killall swaybg
cp -r ./config/fish ~/.config/
cp -r ./config/hypr ~/.config/
rm -rf ~/.config/rofi/
cp -r ./config/rofi ~/.config/
cp -r ./config/xdg-desktop-portal-termfilechooser/ ~/.config/
cp -r ./config/xdg-desktop-portal/ ~/.config/

# Depending on platform, remove one or the other config and rename remaining one
if [[ "$platform" == "d" ]]; then
    echo "==> Running on desktop"
    cp -f ~/.config/hypr/hyprland_desktop.conf ~/.config/hypr/hyprland.conf
else
    echo "==> Running on laptop"
    cp -f ~/.config/hypr/hyprland_laptop.conf ~/.config/hypr/hyprland.conf
fi

# Enable or disable "hyprvim" (using hyprland with vim-inspired modes)
if [[ "$hyprvim" == "y" ]]; then
    echo "==> Enabling hyprvim"
    mv -f ~/.config/hypr/hyprland/mode-binds.conf ~/.config/hypr/hyprland/binds.conf
else
    echo "==> Disabling hyprvim"
    rm -rf ~/.config/hypr/hyprland/modal-binds
    rm ~/.config/hypr/hyprland/mode-binds.conf
fi

# Done this way intentionally to control what is copied
cp -r ./config/kitty ~/.config/
cp -r ./config/fastfetch ~/.config/
cp -r ./config/lazygit ~/.config/
cp -r ./config/mpv ~/.config/
cp -r ./config/Thunar ~/.config/
cp -r ./config/wlogout/ ~/.config/
cp -r ./config/yazi ~/.config/
cp -r ./config/zathura ~/.config/
cp ./config/lint/indentconfig.yaml ~/.indentconfig.yaml

# ────────────────────────────────────────────────────────────────────

echo "
    => Installing yazi plugins
"
rm -rf ~/.config/yazi/plugins/*
ya pkg upgrade

if [[ "$restart" == "y" ]]; then
    echo "
    => Installing GTK Theme
"
    sudo rm -rf /usr/share/themes/Adaptive-Theme/
    sudo mkdir /usr/share/themes/Adaptive-Theme/
    sudo cp -r ./gtk-theme/dist/* /usr/share/themes/Adaptive-Theme/
    echo "
    => Installing System Configs
"
    sudo cp ./system/environment /etc/environment
    sudo cp ./system/greetd/pam /etc/pam.d/greetd
    sudo cp -r ./system/greetd/config.toml /etc/greetd/

    # if [[ "$platform" == "d" ]]; then
    # else
    #     echo "Setting up autologin config for greetd"
    #     sudo cp -r ./system/greetd/config-autologin.toml /etc/greetd/config.toml
    # fi
    hyprctl reload

    ./scripts/restart-bar
fi

# ────────────────────────────────────────────────────────────────────

hyprctl reload

echo "

        ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
        ██░▄▄▀██░▄▄▄░██░▀██░██░▄▄▄
        ██░██░██░███░██░█░█░██░▄▄▄
        ██░▀▀░██░▀▀▀░██░██▄░██░▀▀▀
        ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
    => All configs installed. Reboot
       for them to apply properly

"
