#!/bin/bash
INSTLOG="install.log"
#-- package -----------------------------------------
pkg=(
    ## Other
    fnm
    fd
    devify
    pacman-contrib
    alsa-utils
    less
    wlroots
    tumbler
    polkit-gnome
    jq
    xdg-dbus-proxy

    ## Kernel
    linux-headers

    ## For detecting External device
    udiskie
    mtpfs
    jmtpfs
    gvfs-gphoto2
    gvfs-mtp

    wmctrl
    xdg-desktop-portal-gtk
    xsel
    bc

    ##Volume & brightness tool
    pavucontrol
    pamixer
    brightnessctl

    ## Image Viewer
    imv

    ## Editor
    neovim

    #Tools
    zathura
    zathura-pdf-mupdf
    zsh
    clipshit
    wl-clipboard
    swww
    kitty
    imagemagick
    yt-dlp
    ffmpegthumbnailer
    mpv-mpris
    playerctl
    wf-recorder
    unzip
    zip
    unrar
    xarchiver
    network-manager-applet
    fastfetch
    tmux
    btop
    lm_sensors
    fwupd
    libdbusmenu-gtk3
    libxdg-basedir
    cpio
    cmake
    meson

    ## Bluetooth
    bluez
    bluez-libs
    bluez-utils


    ## Thunar
    thunar
    thunar-volman
    thunar-archive-plugin
    file-roller

    ## fonts
    otf-font-awesome
    ttf-font-awesome
    ttf-material-symbols-variable-git
    ttf-meslo-nerd
    ttf-google-sans
    ttf-opensans
    ttf-roboto

    #hyprland
    hyprland
    adw-gtk3
    adw-gtk-theme
    bibata-cursor-theme-bin
    hyprpicker
    hyprpaper
    hyprshot
    power-profile-daemon
    hyprlock
    hypridle
    sddm
    xdg-user-dirs

    # AGS
    gnome-bluetooth-3.0
    aylurs-gtk-shell
    bun-bin
    dart-sass
    sass
    gtksourceview3
    gtksourceviewmm
    gtkmm3
    tinyxml2
    cairomm

    ## python
    python-pillow
    python-pip
    python-pywayland
    python-pyxdg
    python-materialyoucolor-git
    python-numpy

)
# Creating a new `packages` array
packages=(
    "${pkg[@]}"
)
#-- function -----------------------------------------
install_software() {
    package_name=$1
    if pacman -Q $package_name &>/dev/null; then
        echo -e "\e[90m$package_name is already installed. Skipping installation.\e[0m"
    else
        echo -en "\e[90mInstalling\e[0m \e[97m$package_name\e[0m..."
        paru -S --noconfirm $package_name &>>$INSTLOG &
        show_progress $!
    fi
}

installed() {
    echo -en "Installed package \e[97m$1\e[0m."
}

#-- express -----------------------------------------
clear
echo -e "\033[1;34m"
cat <<"EOF"

                                  ▄
                                 ▄█▄
                                ▄███▄
                               ▄█████▄                          ██████   ██████  ████████ ███████ ██ ██      ███████ ███████
                              ▄███████▄                         ██   ██ ██    ██    ██    ██      ██ ██      ██      ██
                             ▄ ▀▀██████▄                        ██   ██ ██    ██    ██    █████   ██ ██      █████   ███████
                            ▄██▄▄ ▀█████▄                       ██   ██ ██    ██    ██    ██      ██ ██      ██           ██
                           ▄█████████████▄                      ██████   ██████     ██    ██      ██ ███████ ███████ ███████
                          ▄███████████████▄
                         ▄█████████████████▄
                        ▄███████████████████▄                   ┌───────────────────────────────────────────────────────────┐
                       ▄█████████▀▀▀▀████████▄                  │                                                           │
                      ▄████████▀      ▀███████▄                 │  • "Welcome to the Arch Linux DotFiles Setup Script!"     │
                     ▄█████████        ████▀▀██▄                │                                                           │
                    ▄██████████        █████▄▄▄                 │  • Created with    by S4NKALP                             │
                   ▄██████████▀        ▀█████████▄              │ • Find me at [My GitHub/Facebook/Website]                 │
                  ▄██████▀▀▀              ▀▀██████▄             │    for more awesomeness.                                  │
                 ▄███▀▀                       ▀▀███▄            └───────────────────────────────────────────────────────────┘
                ▄▀▀                               ▀▀▄



Notes.
    - Operation on non-Arch Linux is not guaranteed.
    - Not supported on VMs
    - NVIDIA GPUs are not supported


EOF
echo -e "\033[0m"
echo "┌──────────────────────────────────────┐"
echo "│   Start Setup? (y/n)                 │"
echo "└──────────────────────────────────────┘"
read -n1 -rep $'\e[97m: : \e[32m1) \e[0my\e[32m 2) \e[0mn\e[32m \e[0m ' INST
if [[ $INST == "y" || $INST == "Y" ]]; then
    echo -e "\nStarting setup..."
else
    echo -e "\nSetup cancelled."
    exit 0
fi
#-- check step -----------------------------------------
current_step=1
total_steps=7 # Adjust this number based on the total number of checks you plan to perform
perform_check() {
    local message="$1"
    local cmd="$2"
    local success_msg="$3"
    local failure_msg="$4"
    local exit_on_fail=${5:-false}
    # Generate a random delay time between 1 and 3 seconds.
    local delay_time=$((RANDOM % 3 + 1))
    # Display step number
    echo -n "($current_step/$total_steps) $message"
    (
        sleep $delay_time
        eval "$cmd"
    ) &
    local pid=$!
    while ps | grep -Ew $pid &>/dev/null; do
        echo -n "."
        sleep 1
    done
    wait $pid
    if [ $? -eq 0 ]; then
        echo -en "\r\e[K"
        echo -e "$success_msg"
    else
        echo -en "\r\e[K"
        echo -e "$failure_msg"
        [ "$exit_on_fail" = true ] && exit 1
    fi
    # Increment the step count for the next check
    ((current_step++))
}
echo -e "Check step in progress:"
perform_check "Checking your system" \
    "grep -q 'Arch Linux' /etc/os-release" \
    "  - Check System is Arch Linux" \
    "  - Error System is not Arch Linux" \
    true
perform_check "Checking network connection" \
    "ping -c 1 8.8.8.8 &>/dev/null" \
    "  - Check Internet connection is available" \
    "  - Error No internet connection" \
    true
perform_check "Checking if git is installed" \
    "command -v git &>/dev/null" \
    "  - Check Git is installed" \
    "  - Error Git is not installed" \
    true
perform_check "Checking for VM environment" \
    "! grep -i -e 'VMware' -e 'VirtualBox' -e 'KVM' -e 'Hyper-V' /proc/cpuinfo &>/dev/null" \
    "  - Check This system is not running inside a VM" \
    "  - Error This script is not supported inside a VM." \
    true
perform_check "Checking for NVIDIA usage" \
    "! lspci 2>/dev/null | grep -i 'NVIDIA' &>/dev/null" \
    "  - Check NVIDIA GPU not detected" \
    "  - Error NVIDIA hardware is not supported." \
    true

# #-- Installing AUR -----------------------------------------
echo "Please enter password required"
sudo -v

if [ ! -f /sbin/paru ]; then
    echo -en "Configuring paru."
    git clone https://aur.archlinux.org/paru.git &>>$INSTLOG
    cd paru
    makepkg -si --noconfirm &>>../$INSTLOG
    cd ..
    rm -rf paru
fi

sleep 2
echo -e "[\e[32m   OK   \e[0m] All checks completed"
echo -e "Installing main components. This may take a while...\n\n\n\n\n\n\n\n\n\n\n\n"

#-- installation steps -----------------------------------------
package_count=${#packages[@]}
current_package=0
DISPLAY_LINES=10
CURSOR_MOVE=$((DISPLAY_LINES + 1))
# Display the header
display_header() {
    echo -e "\e[K:: Installing package ($current_package/$package_count)"
}
# Print the last DISPLAY_LINES actions
display_actions() {
    local start_index=$((current_package - DISPLAY_LINES))
    if [ $start_index -lt 0 ]; then
        start_index=0
    fi
    for ((i = 0; i < DISPLAY_LINES; i++)); do
        echo -en "\e[K"
        if [ $((start_index + i)) -lt $current_package ]; then
            echo "  Installing ${packages[start_index + i]}.."
        else
            echo ""
        fi
    done
}
# Manage the entire display
display_installation() {
    echo -en "\e[${CURSOR_MOVE}A"
    display_header
    display_actions
}
show_progress() {
    while ps | grep $1 &>/dev/null; do
        sleep 1
    done
}
# Initialize the display
display_installation
for SOFTWR in ${packages[@]}; do
    install_software $SOFTWR &>/dev/null
    ((current_package++))
    display_installation
done
sleep 1
echo -e "Installation step completed ..."
sleep 5
# Display installation completion for each package with a random delay
echo -e "\n==> Installation Summary: [packages]"
for SOFTWR in ${packages[@]}; do
    echo -e "  -> Installed $SOFTWR"
    # Generate a random delay between 0 and 0.3 seconds
    random_delay=$(awk -v min=0 -v max=0.3 'BEGIN{srand(); print min+rand()*(max-min)}')
    sleep $random_delay
done
sleep 2

# Function to generate random seconds
random_sleep() {
    random_delay=$(awk -v min=0 -v max=3 'BEGIN{srand(); print min+rand()*(max-min)}')
    sleep $random_delay
}

# Function to display simulated output
simulate_output() {
    echo -e "$1"
    random_sleep
}

#-- Create directories if they don't exist -----------------
echo "Making directories if they don't exist..."
directories=(
    "~/dotfiles"
    "~/.local/bin"
    "~/Pictures/wallpapers"
)

for dir in "${directories[@]}"; do
    if [ ! -d "$dir" ]; then
        mkdir -p "$dir"
        echo "Created directory: $dir"
    fi
done

echo ""
echo ""

#-- Copy Settings -----------------------------------------
echo "Copying .zshevn file ..."
for file in $HOME/hyrpland/misc/.zshevn; do
    if [ "$(basename "$file")" != "." ] && [ "$(basename "$file")" != ".." ]; then
        sudo cp "$file" $HOME/
    fi
done
echo "Copying bin ..."
for binn in $HOME/hyrpland/misc/bin/*; do
    if [ "$(basename "$binn")" != "." ] && [ "$(basename "$binn")" != ".." ]; then
        sudo cp -r "$binn" $HOME/.local/bin
    fi
done
echo "Copying fonts..."
for font in $HOME/hyrpland/assets/google-sans/; do
    if [ "$(basename "$font")" != "." ] && [ "$(basename "$font")" != ".." ]; then
        sudo cp -r "$font" /usr/share/fonts
    fi
done


sudo cp -r $HOME/hyprland/config/* $HOME/dofiles/
sudo cp -r $HOME/hyprland/wallpapers/* $HOME/Pictures/wallpapers
simulate_output "[   \e[32mOK\e[0m   ] Copied .zshevn dotfiles to $HOME"
simulate_output "[   \e[32mOK\e[0m   ] Copied binn dotfiles to $HOME/.local/bin"
simulate_output "[   \e[32mOK\e[0m   ] Copied font dotfiles to usr/share/fonts"
simulate_output "[   \e[32mOK\e[0m   ] Copied config to $HOME/dotfiles/"
simulate_output "[   \e[32mOK\e[0m   ] Copied config to $HOME/Pictures/wallpapers"

echo ""
echo ""

#-- Synchronize -----------------------------------------
echo "Synchronizing dotfiles .."
DOTFILES_DIR="$HOME/dotfiles"

CONFIG_DIRS=(
    "ags"
    "btop"
    "fastfetch"
    "gtk-3.0"
    "gtk-4.0"
    "hypr"
    "kitty"
    "mpv"
    "npm"
    "qimgv"
    "startship"
    "Thunar"
    "tmux"
    "zsh"
    "electron-flags.conf"
)
# Install .config directories
for dir in "${CONFIG_DIRS[@]}"; do
    source_path="$DOTFILES_DIR/config/$dir"
    target_path="$HOME/.config/$dir"

    # Create symlink
    ln -sf "$source_path" "$target_path"

    # Check if the symlink creation was successful
    if [ $? -eq 0 ]; then
        printf "%s%sCreated symlink for .config/%s\n" "$dir" "${BLD}" "${CGR}" "${CNC}"
    else
        printf "%s%sFailed to create symlink for .config/%s. Please check and fix the issue.\n" "$dir" "${BLD}" "${CGR}" "${CNC}"
        # You can choose to exit or handle the error in another way based on your requirements.
        exit 1
    fi
done
echo ""
echo ""

install_tela_nord_icons() {
    echo ":: Installing Tela Nord icons..."
    mkdir -p /tmp/install
    cd /tmp/install
    git clone https://github.com/vinceliuice/Tela-icon-theme
    cd Tela-icon-theme
    ./install.sh nord
    cd $HOME/dotfiles
}

setup_colors() {
    echo ":: Setting colors"
    python -O $HOME/dotfiles/material-colors/generate.py --color "#0000FF"
}

remove_gtk_buttons() {
    echo ":: Remove window close and minimize buttons in GTK"
    gsettings set org.gnome.desktop.wm.preferences button-layout ':'
}

setup_sensors() {
    sudo sensors-detect --auto > /dev/null
}

#-- Enabling the service -----------------------------------------
echo "┌──────────────────────────────────────┐"
echo "│   Powerprofile enabled ...           │"
echo "└──────────────────────────────────────┘"
echo ""
systemctl unmask power-profiles-daemon.service
systemctl start power-profiles-daemon.service
simulate_output "==> Starting PowerProfile..."
simulate_output "[   \e[32mOK\e[0m   ] Started PowerProfileDaemon service"
sleep 2

echo ""
echo ""
#-- Enabling the service -----------------------------------------
echo "┌──────────────────────────────────────┐"
echo "│   Bluetooth enabled ...              │"
echo "└──────────────────────────────────────┘"
echo ""
sudo systemctl enable --now bluetooth.service &>>$INSTLOG
simulate_output "==> Starting Bluetooth..."
simulate_output "    -> Running bluez"
simulate_output "    -> Running bluez-utils"
simulate_output "[   \e[32mOK\e[0m   ] Started Bluetooth service"
sleep 2

echo ""
echo ""
#-- SDDM Settings -----------------------------------------
echo "┌──────────────────────────────────────┐"
echo "│   SDDM enabled ...                   │"
echo "└──────────────────────────────────────┘"
echo ""
echo "Enabling the SDDM Service..."
sudo systemctl enable sddm &>>$INSTLOG
simulate_output "Enabling the SDDM Service..."
simulate_output "[   \e[32mOK\e[0m   ] Enabled SDDM Service"
sleep 2
echo "Setting theme for sddm..."
sudo mkdir -p /etc/sddm.conf.d
sudo tee /etc/sddm.conf.d/theme.conf <<EOF
[Theme]
Current=corners
EOF
sudo cp -f ~/dotfiles/extras/theme.conf /usr/share/sddm/themes/corners/
simulate_output "Setting theme for sddm..."
simulate_output "[   \e[32mOK\e[0m   ] SDDM theme set to 'corners'"

echo ""
echo ""
#-- GTK Settings -----------------------------------------
echo "┌──────────────────────────────────────┐"
echo "│   Setting GTK and icon themes...     │"
echo "└──────────────────────────────────────┘"
echo ""
simulate_output "Setting GTK and icon themes..."
simulate_output "[   \e[32mOK\e[0m   ] GTK theme set to 'adw-gtk3-dark'"
simulate_output "[   \e[32mOK\e[0m   ] Icon theme set to 'Tela-nord-dark'"
simulate_output "[   \e[32mOK\e[0m   ] curosr theme set to 'Bibata'"
gsettings set org.gnome.desktop.interface gtk-theme "adw-gtk3-dark"
gsettings set org.gnome.desktop.interface icon-theme "Tela-nord-dark"
gsettings set org.gnome.desktop.interface cursor-theme "Bibata-Orginal-Classic"

echo ""
echo ""
#-- shell changes -----------------------------------------
simulate_output "Changing default shell to Zsh..."
simulate_output "[   \e[32mOK\e[0m   ] Default shell set to Zsh"
if ! command -v zsh &>/dev/null; then
    echo "Zsh is not installed. Please install Zsh first."
    exit 1
fi
chsh -s /bin/zsh

echo ""

#-- Give permissions to scripts -----------------------------------------
simulate_output "Setting some files as executable..."
simulate_output "[   \e[32mOK\e[0m   ] Granted execute permissions to scripts"
sudo chmod +x ~/dotfiles/hypr/scripts/*
sudo chmod +x ~/dotfiles/ags/scripts/*
sudo sensors-detect --auto > /dev/null

echo ""
echo ""
#-- Script completion -----------------------------------------
echo "┌──────────────────────────────────────┐"
echo -e "│\e[97m  Start Hyprland? (y/n)               \e[0m│"
echo "└──────────────────────────────────────┘"
echo ""
echo "Setup is complete."

echo "Do you want to  Start?  y/N"
read -rep 'Choose: ' HYP
if [[ $HYP == "y" ]]; then
    simulate_output "Starting Hyprland..."
    simulate_output "[   \e[32mOK\e[0m   ] Started Hyprland"
    sleep 3
    exec sudo systemctl start sddm &>>$INSTLOG
else
    echo "Setup completed."
    exit
fi
echo ""
echo ""
echo "Good bye!, now you must reboot the system"
