Files
dotfiles-new/install/install.sh
T

200 lines
4.6 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 prompt
#
# Le logiciel est installé séparément dans :
# ~/.zsh/spaceship-prompt
#
# La configuration personnelle est gérée par Stow :
# ~/.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
# ------------------------------------------------------------
# Migration des configurations existantes
# ------------------------------------------------------------
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_if_needed() {
local target="$1"
# Rien n'existe : Stow pourra créer le lien.
if [[ ! -e "$target" && ! -L "$target" ]]; then
return
fi
# C'est déjà un symlink.
# On ne le déplace pas et on laisse Stow le gérer.
if [[ -L "$target" ]]; then
log "Symlink déjà présent : $target"
return
fi
create_backup_dir
local relative_path
relative_path="${target#"$HOME"/}"
local backup_path
backup_path="$BACKUP_DIR/$relative_path"
mkdir -p "$(dirname "$backup_path")"
log "Backup : $target -> $backup_path"
mv -- "$target" "$backup_path"
}
migrate_dotfiles() {
log "Vérification des configurations existantes..."
# Zsh
backup_if_needed "$HOME/.zshrc"
backup_if_needed "$HOME/.zshrc.perso"
backup_if_needed "$HOME/.zsh/spaceship.zsh"
# Zellij
backup_if_needed "$HOME/.config/zellij/config.kdl"
backup_if_needed "$HOME/.config/zellij/layouts/default.kdl"
backup_if_needed "$HOME/.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
# ------------------------------------------------------------
# Outils Rust
# ------------------------------------------------------------
log "Installation des outils Rust..."
#cargo install --locked bat bottom diskus procs cargo-update
# ------------------------------------------------------------
# Résumé
# ------------------------------------------------------------
if [[ -n "$BACKUP_DIR" ]]; then
log "Les anciennes configurations ont été sauvegardées dans :"
log " $BACKUP_DIR"
fi
log "Installation terminée avec succès."