#!/usr/bin/env bash

packages=(
    "bluez"
    "bluez-utils"
    "blueman"
    "brightnessctl"
    "bun"
    "discord"
    "betterdiscord-installer-bin"
    "dunst"
    "easyeffects"
    "fastfetch"
    "flatpak"
    "git"
    "go"
    "gum"
    "gvfs"
    "hicolor-icon-theme"
    "htop"
    "hyprland"
    "hyprlock"
    "hyprpaper"
    "hyprpolkitagent"
    "jetbrains-toolbox"
    "jq"
    "kitty"
    "networkmanager"
    "qt5-wayland"
    "qt5ct"
    "qt6-wayland"
    "qt6ct"
    "rofi"
    "scite"
    "thunar"
    "thunar-archive-plugin"
    "thunar-media-tags-plugin"
    "thunar-shares-plugin"
    "thunar-vcs-plugin"
    "thunar-volman"
    "ttf-fira-code"
    "ttf-fira-sans"
    "ttf-firacode-nerd"
    "unzip"
    "vim"
    "vivaldi"
    "waybar"
    "wget"
    "wireplumber"
    "wlogout"
    "woff2-font-awesome"
    "xdg-desktop-portal-hyprland"
    "steam"
    "millennium"
    "mangohud"
    "goverlay"
)

packagesToRemove=(
    "cachyos-micro-settings"
    "micro"
    "dolphin"
)

_checkCommandExists() {
    cmd="$1"
    if ! command -v "$cmd" >/dev/null; then
        echo 1
        return
    fi
    echo 0
    return
}

_isInstalled() {
    package="$1"
    check="$(sudo pacman -Qs --color always "${package}" | grep "local" | grep "${package} ")"
    if [ -n "${check}" ]; then
        echo 0
        return #true
    fi
    echo 1
    return #false
}

_installPackages() {
    for pkg; do
        if [[ $(_isInstalled "${pkg}") == 0 ]]; then
            echo "  .. ${pkg} is already installed."
            continue
        fi
        echo "Package not installed: ${pkg}"
        paru --noconfirm -S "${pkg}"
    done
}

_removePackages() {
    for pkg; do
        if [[ ! $(_isInstalled "${pkg}") == 0 ]]; then
            echo "  .. ${pkg} is not installed."
            continue
        fi
        paru --noconfirm -R "${pkg}"
    done
}

_generateThemes() {
    cd sithego || exit
    go mod download
    go run .
    cd ..
}

_copyThemes() {
  for folder in dotfiles/.themes/*; do
    destDir="$HOME/.themes/$(basename "$folder")"
    if [ -d "$destDir" ]; then
       echo "  .. creating Backup for $HOME/.themes/$(basename "$folder")"
       backupDir="$HOME/.backup/arindOS/$(date +%Y%m%d_%H%M%S)/.themes"
       mkdir -p "$backupDir"
       mv "$destDir" "$backupDir/$(basename "$folder")"
    fi
       cp -r "$folder" "$destDir"
  done
}

_copyScripts() {
    cp -rv "dotfiles/.local/" "$HOME"
}

_copyIcons() {
  for folder in dotfiles/.icons/*; do
    destDir="$HOME/.icons/$(basename "$folder")"
    if [ -d "$destDir" ]; then
       echo "  .. creating Backup for $HOME/.icons/$(basename "$folder")"
       backupDir="$HOME/.backup/arindOS/$(date +%Y%m%d_%H%M%S)/.icons"
       mkdir -p "$backupDir"
       mv "$destDir" "$backupDir/$(basename "$folder")"
    fi
       cp -r "$folder" "$destDir"
  done
}

_copyConfig() {
  for folder in dotfiles/.config/*; do
    destDir="$HOME/.config/$(basename "$folder")"
    if [ -d "$destDir" ]; then
       echo "  .. creating Backup for $HOME/.config/$(basename "$folder")"
       backupDir="$HOME/.backup/arindOS/$(date +%Y%m%d_%H%M%S)/.config"
       mkdir -p "$backupDir"
       mv -v "$destDir" "$backupDir/$(basename "$folder")"
    fi
    cp -rvf "$folder" "$destDir"
  done
  # use envsubst for qt6ct.conf
  envsubst < dotfiles/.config/qt6ct/qt6ct.conf > "$HOME/.config/qt6ct/qt6ct.conf"
  envsubst < dotfiles/.config/waybar/style.css > "$HOME/.config/waybar/style.css"
  # copy qt6 to qt5
  mkdir -p "$HOME/.config/qt5ct"
  cp -v "$HOME/.config/qt6ct/qt6ct.conf" "$HOME/.config/qt5ct/qt5ct.conf"
}

if [[ $(_checkCommandExists "paru") != 0 ]]; then
    echo "..The installer requires paru. paru will be installed now"
    sudo pacman -Sy paru
fi

clear

echo ":::::::::::::::::::::::::::"
echo "::                       ::"
echo ":: Installing arindOS    ::"
echo "::                       ::"
echo ":::::::::::::::::::::::::::"

echo ":::::::::::::::::::::::::::"
echo ":: Removing packages     ::"
echo ":::::::::::::::::::::::::::"
_removePackages "${packagesToRemove[@]}"
echo ":::::::::::::::::::::::::::"
echo ":: Installing packages   ::"
echo ":::::::::::::::::::::::::::"
_installPackages "${packages[@]}"
echo ":::::::::::::::::::::::::::"
echo ":: Generating themes     ::"
echo ":::::::::::::::::::::::::::"
_generateThemes
_copyThemes
_copyIcons
_copyConfig
_copyScripts

hyprctl reload
killall -9 waybar
sleep 1
waybar 2>&1 &
echo ":::::::::::::::::::::::::::"
echo ":: Installation complete ::"
echo ":::::::::::::::::::::::::::"

clear