-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
457 lines (405 loc) · 15.5 KB
/
install.sh
File metadata and controls
457 lines (405 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#!/usr/bin/env bash
# ==============================================================================
# install.sh — Dotfiles installer (macOS + Linux)
# https://github.com/tarto-dev/usefull-bash-alias
#
# Modes:
# ./install.sh Installation complète (fresh ou existant)
# ./install.sh --update Mise à jour des symlinks uniquement (git pull + relink)
# ==============================================================================
set -euo pipefail
# ------------------------------------------------------------------------------
# Config
# ------------------------------------------------------------------------------
REPO_URL="https://github.com/tarto-dev/usefull-bash-alias.git"
DOTFILES_DIR="$HOME/.dotfiles"
BACKUP_DIR="$HOME/.dotfiles_backup_$(date +%Y%m%d_%H%M%S)"
MODE="${1:-install}"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
RESET='\033[0m'
info() { echo -e "${CYAN}[info]${RESET} $*"; }
success() { echo -e "${GREEN}[ok]${RESET} $*"; }
warn() { echo -e "${YELLOW}[warn]${RESET} $*"; }
error() { echo -e "${RED}[error]${RESET} $*" >&2; }
OS="$(uname)"
# ------------------------------------------------------------------------------
# Detect package manager
# ------------------------------------------------------------------------------
install_pkg() {
if [[ "$OS" == "Darwin" ]]; then
brew install "$@"
elif command -v apt-get &>/dev/null; then
sudo apt-get install -y "$@"
elif command -v dnf &>/dev/null; then
sudo dnf install -y "$@"
elif command -v pacman &>/dev/null; then
sudo pacman -S --noconfirm "$@"
else
warn "Package manager not found — install manually: $*"
fi
}
# ------------------------------------------------------------------------------
# Homebrew (macOS)
# ------------------------------------------------------------------------------
install_homebrew() {
[[ "$OS" != "Darwin" ]] && return 0
if command -v brew &>/dev/null; then
success "Homebrew already installed"
return
fi
info "Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
}
# ------------------------------------------------------------------------------
# Oh My Zsh
# ------------------------------------------------------------------------------
install_ohmyzsh() {
if [[ -d "$HOME/.oh-my-zsh" ]]; then
success "Oh My Zsh already installed"
return
fi
info "Installing Oh My Zsh..."
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
}
# ------------------------------------------------------------------------------
# Powerlevel10k
# ------------------------------------------------------------------------------
install_p10k() {
local p10k_dir="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k"
if [[ -d "$p10k_dir" ]]; then
success "Powerlevel10k already installed"
return
fi
info "Installing Powerlevel10k..."
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git "$p10k_dir"
}
# ------------------------------------------------------------------------------
# OMZ plugins
# ------------------------------------------------------------------------------
install_omz_plugins() {
local custom="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins"
declare -A PLUGINS=(
[zsh-autosuggestions]="https://github.com/zsh-users/zsh-autosuggestions"
[zsh-syntax-highlighting]="https://github.com/zsh-users/zsh-syntax-highlighting"
[zsh-history-enquirer]="https://github.com/zthxxx/zsh-history-enquirer"
)
for plugin in "${!PLUGINS[@]}"; do
if [[ -d "$custom/$plugin" ]]; then
success "Plugin $plugin already installed"
else
info "Installing plugin $plugin..."
git clone --depth=1 "${PLUGINS[$plugin]}" "$custom/$plugin"
fi
done
}
# ------------------------------------------------------------------------------
# bat — called "batcat" on Debian/Ubuntu (name conflict with another package)
# Creates a "bat" symlink if needed
# ------------------------------------------------------------------------------
install_bat() {
if command -v bat &>/dev/null; then
success "bat already installed"
return
fi
if [[ "$OS" == "Darwin" ]]; then
brew install bat
return
fi
if command -v apt-get &>/dev/null; then
sudo apt-get install -y bat 2>/dev/null || sudo apt-get install -y batcat
# On Debian/Ubuntu, binary is "batcat" — create a symlink
if command -v batcat &>/dev/null && ! command -v bat &>/dev/null; then
mkdir -p "$HOME/.local/bin"
ln -sf "$(command -v batcat)" "$HOME/.local/bin/bat"
success "bat symlinked from batcat → ~/.local/bin/bat"
warn "Make sure ~/.local/bin is in your PATH (already set in .zshrc)"
fi
return
fi
install_pkg bat
}
# ------------------------------------------------------------------------------
# eza — not in standard apt repos, requires manual install on Linux
# Sources: GitHub releases (works on all distros)
# ------------------------------------------------------------------------------
install_eza() {
if command -v eza &>/dev/null; then
success "eza already installed"
return
fi
if [[ "$OS" == "Darwin" ]]; then
brew install eza
return
fi
# Linux: try deb repo first (Ubuntu/Debian), fallback to binary from GitHub
if command -v apt-get &>/dev/null; then
info "Installing eza via deb repo..."
sudo mkdir -p /etc/apt/keyrings
wget -qO- https://raw.githubusercontent.com/eza-community/eza/main/deb.asc \
| sudo gpg --dearmor -o /etc/apt/keyrings/gierens.gpg 2>/dev/null
echo "deb [signed-by=/etc/apt/keyrings/gierens.gpg] http://deb.gierens.de stable main" \
| sudo tee /etc/apt/sources.list.d/gierens.list > /dev/null
sudo chmod 644 /etc/apt/keyrings/gierens.gpg /etc/apt/sources.list.d/gierens.list
sudo apt-get update -q && sudo apt-get install -y eza \
&& success "eza installed via deb repo" && return
fi
# Fallback: binary from GitHub releases
info "Falling back to GitHub binary install for eza..."
local arch
arch="$(uname -m)"
local eza_arch
case "$arch" in
x86_64) eza_arch="x86_64-unknown-linux-gnu" ;;
aarch64) eza_arch="aarch64-unknown-linux-gnu" ;;
*)
warn "eza: unsupported arch $arch — install manually from https://github.com/eza-community/eza/releases"
return 1
;;
esac
local tmpdir
tmpdir="$(mktemp -d)"
local url="https://github.com/eza-community/eza/releases/latest/download/eza_${eza_arch}.tar.gz"
wget -qO "$tmpdir/eza.tar.gz" "$url" \
&& tar -xzf "$tmpdir/eza.tar.gz" -C "$tmpdir" \
&& sudo mv "$tmpdir/eza" /usr/local/bin/eza \
&& sudo chmod +x /usr/local/bin/eza \
&& success "eza installed to /usr/local/bin/eza" \
|| warn "eza binary install failed — install manually from https://github.com/eza-community/eza/releases"
rm -rf "$tmpdir"
}
# ------------------------------------------------------------------------------
# CLI tools
# ------------------------------------------------------------------------------
install_tools() {
info "Installing CLI tools..."
# eza et bat ont leur propre fonction (cas particuliers Linux)
install_bat
install_eza
local tools_common=(git fzf zoxide fortune)
local tools_macos=(ponysay lolcat thefuck)
local tools_linux=(thefuck)
for tool in "${tools_common[@]}"; do
if command -v "$tool" &>/dev/null; then
success "$tool already installed"
else
info "Installing $tool..."
install_pkg "$tool"
fi
done
if [[ "$OS" == "Darwin" ]]; then
for tool in "${tools_macos[@]}"; do
if command -v "$tool" &>/dev/null; then
success "$tool already installed"
else
info "Installing $tool..."
install_pkg "$tool"
fi
done
else
for tool in "${tools_linux[@]}"; do
if command -v "$tool" &>/dev/null; then
success "$tool already installed"
else
info "Installing $tool..."
install_pkg "$tool" 2>/dev/null || warn "$tool not available via package manager — install manually"
fi
done
# ponysay : pas dans apt, .deb depuis vcheng.org
if ! command -v ponysay &>/dev/null; then
info "Installing ponysay via .deb..."
local tmpdir ponysay_deb="ponysay_3.0.3+20210327-1_all.deb"
tmpdir="$(mktemp -d)"
wget -qO "$tmpdir/$ponysay_deb" "https://vcheng.org/ponysay/$ponysay_deb" \
&& sudo apt install -y "$tmpdir/$ponysay_deb" \
&& success "ponysay installed" \
|| warn "ponysay install failed — install manually: https://vcheng.org/ponysay/$ponysay_deb"
rm -rf "$tmpdir"
else
success "ponysay already installed"
fi
# lolcat : gem ou pip selon dispo
if ! command -v lolcat &>/dev/null; then
if command -v gem &>/dev/null; then
info "Installing lolcat via gem..."
gem install lolcat 2>/dev/null || warn "lolcat gem install failed"
else
info "Installing lolcat via pip..."
pip3 install lolcat 2>/dev/null || warn "lolcat install failed — try: gem install lolcat"
fi
else
success "lolcat already installed"
fi
# Clipboard fallback
if ! command -v xclip &>/dev/null && ! command -v xsel &>/dev/null; then
info "Installing xclip..."
install_pkg xclip
fi
fi
}
# ------------------------------------------------------------------------------
# Kaamelott fortunes
# ------------------------------------------------------------------------------
install_fortunes() {
local fortune_dir="$HOME/.local/share/fortunes-kaamelott"
if [[ -d "$fortune_dir" ]]; then
success "Kaamelott fortunes already installed"
return
fi
info "Installing Kaamelott fortunes..."
mkdir -p "$(dirname "$fortune_dir")"
git clone https://github.com/methatronc/fortunes-kaamelott "$fortune_dir"
if command -v strfile &>/dev/null; then
strfile "$fortune_dir/fortunes-kaamelott" 2>/dev/null || true
else
warn "strfile not found — run manually: strfile $fortune_dir/fortunes-kaamelott"
fi
}
# ------------------------------------------------------------------------------
# Clone / update dotfiles repo
# ------------------------------------------------------------------------------
setup_dotfiles_repo() {
if [[ -d "$DOTFILES_DIR/.git" ]]; then
info "Dotfiles repo already cloned, pulling latest..."
git -C "$DOTFILES_DIR" pull --ff-only || warn "git pull failed — check manually in $DOTFILES_DIR"
else
info "Cloning dotfiles repo to $DOTFILES_DIR..."
git clone "$REPO_URL" "$DOTFILES_DIR"
fi
}
# ------------------------------------------------------------------------------
# Backup existing files
# Backup uniquement les fichiers réels (pas les symlinks déjà en place)
# ------------------------------------------------------------------------------
backup_existing() {
# Fichiers à gérer : fichier réel existant OU symlink pointant ailleurs que dotfiles
local targets=(
"$HOME/.zshrc:$DOTFILES_DIR/zsh/.zshrc"
"$HOME/.gitconfig:$DOTFILES_DIR/git/.gitconfig"
"$HOME/.bash_aliases:"
"$HOME/.bash_functions:"
"$HOME/.bash_profile:"
"$HOME/.bashrc:"
)
local backed_up=false
for entry in "${targets[@]}"; do
local file="${entry%%:*}"
local expected_target="${entry##*:}"
# Fichier n'existe pas du tout → rien à faire
[[ ! -e "$file" ]] && continue
# C'est un symlink qui pointe déjà vers notre dotfiles → on le laisse
if [[ -L "$file" ]]; then
local current_target
current_target="$(readlink "$file")"
if [[ -n "$expected_target" && "$current_target" == "$expected_target" ]]; then
success "$file already linked to dotfiles, skipping"
continue
fi
# Symlink qui pointe ailleurs → on backup
fi
# Fichier réel ou symlink vers autre chose → backup
if [[ "$backed_up" == false ]]; then
mkdir -p "$BACKUP_DIR"
backed_up=true
warn "Backing up existing files to $BACKUP_DIR"
fi
mv "$file" "$BACKUP_DIR/$(basename "$file")"
success "Backed up $file → $BACKUP_DIR/"
done
if [[ "$backed_up" == true ]]; then
info "Backup complete. To restore: cp $BACKUP_DIR/* ~/"
fi
}
# ------------------------------------------------------------------------------
# Symlinks
# ------------------------------------------------------------------------------
link_dotfiles() {
info "Creating symlinks..."
ln -sf "$DOTFILES_DIR/zsh/.zshrc" "$HOME/.zshrc"
success "Linked .zshrc → $DOTFILES_DIR/zsh/.zshrc"
ln -sf "$DOTFILES_DIR/git/.gitconfig" "$HOME/.gitconfig"
success "Linked .gitconfig → $DOTFILES_DIR/git/.gitconfig"
# gitconfig.local : overrides locaux non versionnés
if [[ ! -f "$HOME/.gitconfig.local" ]]; then
cat > "$HOME/.gitconfig.local" << 'EOF'
# Local git overrides — not versioned
# Uncomment and fill to override .gitconfig values
# [user]
# email = perso@example.com
# name = Your Name
EOF
warn ".gitconfig.local created — fill it in for machine-specific overrides"
else
success ".gitconfig.local already exists, skipping"
fi
# SSH aliases : fichier local gitignored
if [[ ! -f "$DOTFILES_DIR/ssh/aliases.zsh" ]]; then
mkdir -p "$DOTFILES_DIR/ssh"
touch "$DOTFILES_DIR/ssh/aliases.zsh"
warn "ssh/aliases.zsh created — add your SSH aliases there (not versioned)"
else
success "ssh/aliases.zsh already exists, skipping"
fi
}
# ------------------------------------------------------------------------------
# Mode update : git pull + relink uniquement
# ------------------------------------------------------------------------------
run_update() {
echo ""
echo -e "${CYAN}╔══════════════════════════════════════╗${RESET}"
echo -e "${CYAN}║ dotfiles update ║${RESET}"
echo -e "${CYAN}╚══════════════════════════════════════╝${RESET}"
echo ""
if [[ ! -d "$DOTFILES_DIR/.git" ]]; then
error "Dotfiles not found at $DOTFILES_DIR — run install first"
exit 1
fi
setup_dotfiles_repo
link_dotfiles
echo ""
success "Update complete! Run: source ~/.zshrc"
echo ""
}
# ------------------------------------------------------------------------------
# Main
# ------------------------------------------------------------------------------
main() {
if [[ "$MODE" == "--update" ]]; then
run_update
return
fi
echo ""
echo -e "${CYAN}╔══════════════════════════════════════╗${RESET}"
echo -e "${CYAN}║ dotfiles installer ║${RESET}"
echo -e "${CYAN}╚══════════════════════════════════════╝${RESET}"
echo ""
# Détection système existant
if [[ -d "$DOTFILES_DIR/.git" ]]; then
warn "Existing dotfiles detected at $DOTFILES_DIR"
info "Running in upgrade mode — tools already installed will be skipped"
fi
install_homebrew
install_ohmyzsh
install_p10k
install_omz_plugins
install_tools
install_fortunes
setup_dotfiles_repo
backup_existing
link_dotfiles
echo ""
success "Installation complete!"
echo ""
info "Next steps:"
echo " 1. Add your SSH aliases to $DOTFILES_DIR/ssh/aliases.zsh"
echo " 2. Edit ~/.gitconfig.local for machine-specific git config"
echo " 3. Run: source ~/.zshrc"
echo " 4. Run p10k configure if this is a fresh install"
echo ""
info "To update dotfiles later: ./install.sh --update"
echo ""
}
main "$@"