Files

283 lines
6.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
DOTFILES_DIR="$(cd -- "$SCRIPT_DIR/../dotfiles" && pwd)"
BACKUP_ROOT="$HOME/.dotfiles-backup"
BACKUP_DIR=""
log() {
printf '[*] %s\n' "$*"
}
warn() {
printf '[!] %s\n' "$*" >&2
}
die() {
printf '[✗] %s\n' "$*" >&2
exit 1
}
# ------------------------------------------------------------
# Vérifications
# ------------------------------------------------------------
if [[ "${EUID}" -eq 0 ]]; then
die "Ne lance pas ce script en root."
fi
if [[ ! -d "$DOTFILES_DIR" ]]; then
die "Répertoire dotfiles introuvable : $DOTFILES_DIR"
fi
# ------------------------------------------------------------
# Dépendances système
# ------------------------------------------------------------
log "Mise à jour des paquets apt..."
#sudo apt update
log "Installation des dépendances..."
#sudo apt install -y apt-transport-https curl git gnupg stow zsh fzf zoxide
# ------------------------------------------------------------
# Rust / Cargo
# ------------------------------------------------------------
if ! command -v cargo >/dev/null 2>&1; then
log "Cargo introuvable, installation de rustup..."
curl \
--proto '=https' \
--tlsv1.2 \
-sSf \
https://sh.rustup.rs |
sh -s -- -y
fi
if [[ -f "$HOME/.cargo/env" ]]; then
# shellcheck disable=SC1091
source "$HOME/.cargo/env"
fi
if ! command -v cargo >/dev/null 2>&1; then
echo "Cargo est toujours introuvable après l'installation de Rust."
exit 1
fi
# ------------------------------------------------------------
# Spaceship
#
# Logiciel :
# ~/.zsh/spaceship-prompt
#
# Configuration :
# ~/.zsh/spaceship.zsh
# ------------------------------------------------------------
SPACESHIP_DIR="$HOME/.zsh/spaceship-prompt"
if [[ -d "$SPACESHIP_DIR/.git" ]]; then
log "Spaceship déjà installé, mise à jour..."
git -C "$SPACESHIP_DIR" pull --ff-only
elif [[ -e "$SPACESHIP_DIR" ]]; then
die "$SPACESHIP_DIR existe mais n'est pas un dépôt Git."
else
log "Installation de Spaceship..."
mkdir -p "$HOME/.zsh"
git clone \
--depth=1 \
https://github.com/spaceship-prompt/spaceship-prompt.git \
"$SPACESHIP_DIR"
fi
# ------------------------------------------------------------
# Backup
# ------------------------------------------------------------
create_backup_dir() {
if [[ -n "$BACKUP_DIR" ]]; then
return
fi
local timestamp
timestamp="$(date '+%Y%m%d-%H%M%S')"
BACKUP_DIR="$BACKUP_ROOT/$timestamp"
mkdir -p "$BACKUP_DIR"
log "Backup créé dans : $BACKUP_DIR"
}
backup_path_for() {
local target="$1"
local relative_path
relative_path="${target#"$HOME"/}"
printf '%s/%s\n' "$BACKUP_DIR" "$relative_path"
}
backup_if_needed() {
local target="$1"
local expected_source="$2"
# Rien n'existe.
if [[ ! -e "$target" && ! -L "$target" ]]; then
log "Absent, Stow va le créer : $target"
return
fi
# --------------------------------------------------------
# Symlink
# --------------------------------------------------------
if [[ -L "$target" ]]; then
local current_source
current_source="$(readlink -f "$target")"
expected_source="$(readlink -f "$expected_source")"
if [[ "$current_source" == "$expected_source" ]]; then
log "Symlink déjà correct : $target"
return
fi
warn "Symlink existant mais incorrect : $target"
warn " actuel : $current_source"
warn " attendu : $expected_source"
create_backup_dir
local backup_path
backup_path="$(backup_path_for "$target")"
mkdir -p "$(dirname "$backup_path")"
log "Backup du symlink : $target -> $backup_path"
mv -- "$target" "$backup_path"
return
fi
# --------------------------------------------------------
# Fichier ou répertoire normal
# --------------------------------------------------------
create_backup_dir
local backup_path
backup_path="$(backup_path_for "$target")"
mkdir -p "$(dirname "$backup_path")"
log "Backup : $target -> $backup_path"
mv -- "$target" "$backup_path"
}
# ------------------------------------------------------------
# Migration des dotfiles
# ------------------------------------------------------------
migrate_dotfiles() {
log "Vérification des configurations existantes..."
# Zsh
backup_if_needed \
"$HOME/.zshrc" \
"$DOTFILES_DIR/zsh/dot-zshrc"
backup_if_needed \
"$HOME/.zshrc.perso" \
"$DOTFILES_DIR/zsh/dot-zshrc.perso"
backup_if_needed \
"$HOME/.zsh/spaceship.zsh" \
"$DOTFILES_DIR/zsh/dot-zsh/spaceship.zsh"
# Zellij
backup_if_needed \
"$HOME/.config/zellij/config.kdl" \
"$DOTFILES_DIR/zellij/dot-config/zellij/config.kdl"
backup_if_needed \
"$HOME/.config/zellij/layouts/default.kdl" \
"$DOTFILES_DIR/zellij/dot-config/zellij/layouts/default.kdl"
backup_if_needed \
"$HOME/.config/zellij/plugins/monocle.wasm" \
"$DOTFILES_DIR/zellij/dot-config/zellij/plugins/monocle.wasm"
}
migrate_dotfiles
# ------------------------------------------------------------
# Dotfiles avec GNU Stow
# ------------------------------------------------------------
log "Installation des dotfiles avec Stow..."
cd "$DOTFILES_DIR"
stow \
--dotfiles \
--restow \
--target="$HOME" \
zsh \
zellij
# ------------------------------------------------------------
# Installation des outils Cargo
# ------------------------------------------------------------
install_cargo_package() {
local package="$1"
local command="${2:-$1}"
if command -v "$command" >/dev/null 2>&1; then
log "$package déjà installé."
return
fi
log "Installation de $package..."
cargo install --locked "$package"
}
#install_cargo_package zellij
#install_cargo_package bat
#install_cargo_package bottom
#install_cargo_package diskus
#install_cargo_package procs
# cargo-update installe la commande `cargo-install-update`
if ! command -v cargo-install-update >/dev/null 2>&1; then
log "Installation de cargo-update..."
cargo install --locked cargo-update
else
log "cargo-update déjà installé."
fi
# ------------------------------------------------------------
# Résumé
# ------------------------------------------------------------
if [[ -n "$BACKUP_DIR" ]]; then
log "Anciennes configurations sauvegardées dans :"
log " $BACKUP_DIR"
else
log "Aucun backup nécessaire."
fi
log "Installation terminée avec succès."