#!/bin/bash

# Check that the script is not run as root
function check_not_root() {
    if [ "$(id -u)" -eq 0 ]; then
        echo -e "${RED}[ERROR] This script shall not be executed as root${DEFAULT}" | tee -a "$LOG"
        exit 1
    fi
}

# Check that an internet connection is available
function check_internet() {
    if ping -q -c 1 -W 1 archlinux.org >/dev/null; then
        echo -e "${GREEN}Internet connection is available${DEFAULT}" | tee -a "$LOG"
    else
        echo -e "${RED}[ERROR] Internet connection is not available${DEFAULT}" | tee -a "$LOG"
        exit 1
    fi
}

# Check that the system is Arch Linux
function check_arch() {
    if [ -f "/etc/arch-release" ]; then
        echo -e "${GREEN}Arch Linux detected${DEFAULT}" | tee -a "$LOG"
    else
        echo -e "${RED}[ERROR] Arch Linux not detected${DEFAULT}" | tee -a "$LOG"
        exit 1
    fi
}

# Check that chezmoi is installed
function check_chezmoi() {
    if ! command -v chezmoi &>/dev/null; then
        echo -e "${RED}[ERROR] Chezmoi is not installed${DEFAULT}" | tee -a "$LOG"
        exit 1
    fi
}

# Secure installation of packages
function check_installation() {
    local command=$1
    local package=$2
    echo -e "${BLUE}Installing $package...${DEFAULT}" | tee -a "$LOG"
    
    if ! $command $package; then
        echo -e "${RED}[ERROR] Installation of $package failed${DEFAULT}" | tee -a "$LOG"
        return 1
    fi
    echo -e "${GREEN}$package installed successfully${DEFAULT}" | tee -a "$LOG"
    return 0
}

# Configuration backup function
function backup_config() {
    local path=$1
    if [ -e "$path" ]; then  # Vérifie si le fichier ou le répertoire existe
        local backup_path="${path}.backup.$(date +%Y%m%d_%H%M%S)"
        echo -e "${BLUE}Backing up $path to $backup_path${DEFAULT}" | tee -a "$LOG"
        mv "$path" "$backup_path"
    fi
}

# Final system configuration
function setup_final() {
    echo -e "${BLUE}Performing final system configuration...${DEFAULT}" | tee -a "$LOG"
    
    # Enable systemd services
    systemctl --user enable --now pipewire.service
    systemctl --user enable --now pipewire-pulse.service
    
    # Setting up Vim plugins
    echo -e "${GREEN}Installing Vim plugins...${DEFAULT}"
    vim -E -s -u "~/.vimrc" +PlugInstall +qall >/dev/null 2>&1

    # Setting up NeoVim plugins
    echo -e "${GREEN}Installing NeoVim plugins...${DEFAULT}"
    nvim +Lazy +MasonInstallAll +MasonUpdate +180sleep +qall
    
    # Clean up the system
    echo -e "${BLUE}Cleaning up the system...${DEFAULT}" | tee -a "$LOG"
    yay -Yc --noconfirm
}

# Display logo and information
function display_header() {
    echo -e "${BLUE}"
    cat <<"EOF"

                                  ▄
                                 ▄█▄
                                ▄███▄
                               ▄█████▄                          ██████   ██████  ████████ ███████ ██ ██      ███████ ███████
                              ▄███████▄                         ██   ██ ██    ██    ██    ██      ██ ██      ██      ██
                             ▄ ▀▀██████▄                        ██   ██ ██    ██    ██    █████   ██ ██      █████   ███████
                            ▄██▄▄ ▀█████▄                       ██   ██ ██    ██    ██    ██      ██ ██      ██           ██
                           ▄█████████████▄                  ██  ██████   ██████     ██    ██      ██ ███████ ███████ ███████
                          ▄███████████████▄
                         ▄█████████████████▄
                        ▄███████████████████▄                   ┌───────────────────────────────────────────────────────────┐
                       ▄█████████▀▀▀▀████████▄                  │                                                           │
                      ▄████████▀      ▀███████▄                 │ • "Welcome to the Arch Linux DotFiles Setup Script!"      │
                     ▄█████████        ████▀▀██▄                │                                                           │
                    ▄██████████        █████▄▄▄                 │ • Created by Thomas Brasdefer                             │
                   ▄██████████▀        ▀█████████▄              │ • Find me at hexasec.io or gitea.hexasec.io/tombdf        │
                  ▄██████▀▀▀              ▀▀██████▄             │    for more.                                              │
                 ▄███▀▀                       ▀▀███▄            └───────────────────────────────────────────────────────────┘
                ▄▀▀                               ▀▀▄

Notes.
    - Operation on non-Arch Linux based is not guaranteed.
    - NVIDIA GPUs are supported.
    - Do not run this script as root.
    - Do NOT interrupt the script, it will end by itself.
EOF
    echo -e "${DEFAULT}"
}
