#!/bin/bash
set -euo pipefail

##########################
# Color helpers (printf-safe, no tput)
##########################
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
CYAN="\033[0;36m"
GRAY="\033[0;37m"
BOLD="\033[1m"
RESET="\033[0m"

# Disable colors if output is not a terminal
if [ ! -t 1 ]; then
    RED="" GREEN="" YELLOW="" CYAN="" GRAY="" BOLD="" RESET=""
fi

say_red()    { printf "\n"; printf "%b%s%b\n" "$RED" "$*" "$RESET"; }
say_green()    { printf "\n"; printf "%b%s%b\n" "$GREEN" "$*" "$RESET"; }
say_yellow()   { printf "\n"; printf "%b%s%b\n" "$YELLOW" "$*" "$RESET"; }
say_cyan()     { printf "\n"; printf "%b%s%b\n" "$CYAN" "$*" "$RESET"; }
say_gray()     { printf "\n"; printf "%b%s%b\n" "$GRAY" "$*" "$RESET"; }
say_bold()     { printf "\n"; printf "%b%s%b\n" "$BOLD" "$*" "$RESET"; }

##########################
# OS Detection
##########################
say_yellow "################### Start OS Detection"

source /etc/os-release

OS_ID="${ID,,}"
OS_LIKE="${ID_LIKE:-}"
OS_LIKE="${OS_LIKE,,}"
OS_VERSION="${VERSION_ID:-unknown}"
OS_PRETTY="$PRETTY_NAME"
OS=""

case "$OS_ID" in
    arch)   OS="arch" ;;
    debian) OS="debian" ;;
    ubuntu) OS="ubuntu" ;;
    fedora) OS="fedora" ;;
    *)
        if [[ "$OS_LIKE" == *"ubuntu"* ]]; then
            OS="ubuntu"
        elif [[ "$OS_LIKE" == *"arch"* ]]; then
            OS="arch"
        elif [[ "$OS_LIKE" == *"debian"* ]]; then
            OS="debian"
        elif [[ "$OS_LIKE" == *"fedora"* || "$OS_LIKE" == *"rhel"* ]]; then
            OS="fedora"
        fi
        ;;
esac

if [[ -n "$OS" ]]; then
    say_cyan "Detected OS: $OS ($OS_PRETTY)"
    say_cyan "Version: $OS_VERSION"
else
    say_red "ERROR: Unsupported or unknown Linux distribution."
    say_red "Detected: ID=$OS_ID, ID_LIKE=${OS_LIKE:-empty}"
    exit 1
fi

say_yellow "End Detection"

##########################
# Copy Dotfiles Logic
##########################
say_yellow "################### Start Copy Dotfiles"

# The directory where the script is located (containing arch, debian, etc. folders)
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
# The source directory for the detected OS
DOTFILES_SRC_DIR="$SCRIPT_DIR/$OS"
# The target directory
TARGET_DIR="/etc/skel"

# 1. Check if the source directory for the detected OS exists
if [[ ! -d "$DOTFILES_SRC_DIR" ]]; then
    say_red "ERROR: Distribution-specific dotfiles directory not found: $DOTFILES_SRC_DIR"
    exit 1
fi

say_cyan "Source Directory: $DOTFILES_SRC_DIR"
say_cyan "Target Directory: $TARGET_DIR"

# 2. Perform the copy operation
# The 'cp -r' command copies the *contents* of the source directory (DOTFILES_SRC_DIR)
# to the target directory (TARGET_DIR), overwriting existing files silently.
# The 'cp -rT' flag is often safer to ensure the contents are copied, not the directory itself.
# On systems that don't support 'T', using a trailing '/' on the source is safer: cp -r "$DOTFILES_SRC_DIR/" "$TARGET_DIR"

say_bold "Copying contents of $OS dotfiles to $TARGET_DIR..."

# Use rsync for a more robust and flexible copy/sync operation
# -a: archive mode (preserves permissions, ownership, timestamps)
# -v: verbose
# -r: recursive (rsync is usually recursive by default, but included for clarity)
# --delete: optional, removes files in TARGET_DIR that are not in DOTFILES_SRC_DIR (be careful with this!)

if command -v rsync >/dev/null 2>&1; then
    # Ensure a trailing slash on the source to copy contents *of* the directory
    sudo rsync -avz "$DOTFILES_SRC_DIR/" "$TARGET_DIR"
    say_green "Successfully synchronized dotfiles using rsync."
else
    # Fallback to cp if rsync is not available
    say_yellow "rsync not found. Falling back to 'cp -r'."
    sudo cp -r "$DOTFILES_SRC_DIR/." "$TARGET_DIR"
    say_green "Successfully copied dotfiles using cp."
fi

say_green "Dotfiles for $OS copied to $TARGET_DIR."

say_yellow "End Copy Dotfiles"

say_yellow "########################################################################"
say_yellow "FINAL SKEL"
say_yellow "Copying all files and folders from /etc/skel to ~"
say_yellow "First we make a backup of .config"
say_yellow "Wait for it ...."
say_yellow "########################################################################"

cp -Rf ~/.config ~/.config-backup-$(date +%Y.%m.%d-%H.%M.%S)
cp -arf /etc/skel/. ~

say_yellow "##############################################################"
say_yellow "###################  $(basename $0) done"
say_yellow "##############################################################"
