.dotfiles/utils/helpers.sh
tombdf 15b6581aa1
feat(install): add initial setup script
- Added install.sh script to automate the installation process.
- Included functions for initial checks, component installation, and
  configuration setup.
- Ensured the script runs with strict mode enabled for better error
  handling.
- Added user confirmation prompt before starting the setup.
- Displayed a completion message and prompt for system reboot.
2024-12-18 01:00:03 +01:00

162 lines
7.2 KiB
Bash

#!/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
}
# 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
# Configure git if necessary
if [ -z "$(git config --global user.name)" ]; then
echo -e "${BLUE}Configuring Git...${DEFAULT}"
read -p "Enter your Git name: " git_name
read -p "Enter your Git email: " git_email
git config --global user.name "$git_name"
git config --global user.email "$git_email"
fi
# 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}"
}
# Function to manage configurations files with stow
function setup_dotfiles() {
echo -e "${BLUE}Setting up configuration files...${DEFAULT}" | tee -a "$LOG"
# Check for existence of .dotfiles directory
if [ ! -d "$HOME/.dotfiles" ]; then
echo -e "${RED}[ERROR] .dotfiles directory not found${DEFAULT}" | tee -a "$LOG"
return 1
fi
# Backup existing configurations
local configs_to_backup=(
"$HOME/.config/hypr/"
"$HOME/.config/waybar/"
"$HOME/.config/nvim/"
"$HOME/.config/ohmyposh/"
"$HOME/.vimrc"
"$HOME/.bashrc"
"$HOME/.zshrc"
"$HOME/.config/fish/fish.config"
)
for config in "${configs_to_backup[@]}"; do
backup_config "$config"
done
# Apply configurations with stow
echo -e "${BLUE}Applying dotfiles with stow...${DEFAULT}" | tee -a "$LOG"
cd "$HOME/.dotfiles" || exit 1
# Unstow first in case of existing links
stow -D . 2>/dev/null || true
# Stow new configuration
if stow .; then
echo -e "${GREEN}Configuration files installed successfully${DEFAULT}" | tee -a "$LOG"
else
echo -e "${RED}[ERROR] Failed to install configuration files${DEFAULT}" | tee -a "$LOG"
return 1
fi
return 0
}