-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·478 lines (402 loc) · 14.1 KB
/
setup.sh
File metadata and controls
executable file
·478 lines (402 loc) · 14.1 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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/bin/bash
set -euo pipefail
#==============================================================================
# CONFIGURATION
#==============================================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_DIR
readonly CONFIG_DIR="${HOME}/.config"
readonly FONT_DIR="${HOME}/.local/share/fonts"
#==============================================================================
# COLORS
#==============================================================================
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly BLUE='\033[0;34m'
readonly NC='\033[0m' # No Color
#==============================================================================
# GLOBAL VARIABLES
#==============================================================================
PACKAGE_MANAGER=""
PRIVILEGE_CMD=""
#==============================================================================
# LOGGING FUNCTIONS
#==============================================================================
log_info() {
printf "${BLUE}[INFO]${NC} %s\n" "$1"
}
log_success() {
printf "${GREEN}[SUCCESS]${NC} %s\n" "$1"
}
log_warning() {
printf "${YELLOW}[WARNING]${NC} %s\n" "$1"
}
log_error() {
printf "${RED}[ERROR]${NC} %s\n" "$1" >&2
}
#==============================================================================
# UTILITY FUNCTIONS
#==============================================================================
command_exists() {
command -v "$1" >/dev/null 2>&1
}
get_user_home() {
if [ -n "${SUDO_USER:-}" ]; then
getent passwd "$SUDO_USER" | cut -d: -f6
else
echo "$HOME"
fi
}
#==============================================================================
# RUN AS SUDO
#==============================================================================
run_privileged() {
if [[ "$PRIVILEGE_CMD" == "su -c" ]]; then
# su -c requires the entire command as a single string argument
su -c "$*"
else
# sudo and doas take arguments normally
$PRIVILEGE_CMD "$@"
fi
}
#==============================================================================
# SYSTEM DETECTION
#==============================================================================
detect_package_manager() {
local managers="nala apt dnf yum pacman zypper emerge xbps-install nix-env"
for manager in $managers; do
if command_exists "$manager"; then
PACKAGE_MANAGER="$manager"
log_info "Detected package manager: $manager"
return 0
fi
done
log_error "No supported package manager found"
return 1
}
detect_privilege_escalation() {
if command_exists sudo; then
PRIVILEGE_CMD="sudo"
elif command_exists doas && [ -f "/etc/doas.conf" ]; then
PRIVILEGE_CMD="doas"
else
PRIVILEGE_CMD="su -c"
fi
log_info "Using privilege escalation: $PRIVILEGE_CMD"
}
#==============================================================================
# VALIDATION FUNCTIONS
#==============================================================================
validate_requirements() {
local requirements="curl git"
local missing=""
for req in $requirements; do
if ! command_exists "$req"; then
missing="$missing $req"
fi
done
if [ -n "$missing" ]; then
log_error "Missing required commands:$missing"
return 1
fi
return 0
}
validate_permissions() {
if ! groups | grep -qE "(wheel|sudo|root)"; then
log_warning "User not in wheel/sudo/root; operations may prompt for password."
fi
if [ ! -w "$SCRIPT_DIR" ]; then
log_warning "No write permission to script directory: $SCRIPT_DIR (continuing)"
fi
return 0
}
#==============================================================================
# SETUP FUNCTIONS
#==============================================================================
setup_directories() {
log_info "Setting up directories..."
mkdir -p "$CONFIG_DIR" "$FONT_DIR"
log_info "Working from current directory: $SCRIPT_DIR"
}
#==============================================================================
# INSTALLATION FUNCTIONS
#==============================================================================
install_packages() {
local packages=(bash bash-completion tar bat tree multitail fastfetch wget unzip fontconfig)
if ! command_exists nvim; then
packages+=(neovim)
fi
if ! command_exists trash; then
packages+=(trash-cli)
fi
log_info "Installing packages..."
case "$PACKAGE_MANAGER" in
pacman)
run_privileged pacman -Syu --needed --noconfirm "${packages[@]}"
;;
nala|apt)
local apt_packages=()
for pkg in "${packages[@]}"; do
if [[ "$pkg" != "fastfetch" ]]; then
apt_packages+=("$pkg")
fi
done
run_privileged "$PACKAGE_MANAGER" update
log_info "Installing essential packages: ${apt_packages[*]}"
run_privileged "$PACKAGE_MANAGER" install -y "${apt_packages[@]}"
log_info "Attempting to install optional package: fastfetch"
run_privileged "$PACKAGE_MANAGER" install -y fastfetch || log_warning "fastfetch not found in repositories. Skipping."
;;
dnf|yum)
run_privileged "$PACKAGE_MANAGER" install -y "${packages[@]}"
;;
emerge)
local emerge_packages=(app-shells/bash-completion app-arch/tar sys-apps/bat app-text/tree app-text/multitail app-misc/fastfetch app-misc/trash-cli)
if ! command_exists nvim; then
emerge_packages+=(app-editors/neovim)
fi
run_privileged "$PACKAGE_MANAGER" -v --noreplace "${emerge_packages[@]}"
;;
xbps-install)
run_privileged "$PACKAGE_MANAGER" -Sy "${packages[@]}"
;;
nix-env)
# nix-env doesn't need privileges
local nix_packages=(nixpkgs.bash nixpkgs.bash-completion nixpkgs.gnutar nixpkgs.bat nixpkgs.tree nixpkgs.multitail nixpkgs.fastfetch nixpkgs.trash-cli)
if ! command_exists nvim; then
nix_packages+=(nixpkgs.neovim)
fi
nix-env -iA "${nix_packages[@]}"
;;
zypper)
run_privileged "$PACKAGE_MANAGER" install -y "${packages[@]}"
;;
*)
log_error "Unsupported package manager: $PACKAGE_MANAGER"
return 1
;;
esac
}
install_nerd_font() {
local font_name="MesloLGS Nerd Font"
if fc-list | grep -qi "meslo"; then
log_info "Nerd font already installed"
return 0
fi
log_info "Installing $font_name..."
local font_url="https://github.com/ryanoasis/nerd-fonts/releases/latest/download/Meslo.zip"
local temp_dir
temp_dir=$(mktemp -d)
if wget -q "$font_url" -O "$temp_dir/Meslo.zip"; then
unzip -q "$temp_dir/Meslo.zip" -d "$temp_dir"
mkdir -p "$FONT_DIR/MesloLGS"
find "$temp_dir" -name "*.ttf" -exec mv {} "$FONT_DIR/MesloLGS/" \;
fc-cache -fv >/dev/null 2>&1
log_success "Font installed successfully"
else
log_warning "Failed to download font"
fi
rm -rf "$temp_dir"
}
install_starship() {
if command_exists starship; then
log_info "Starship already installed"
return 0
fi
log_info "Installing Starship prompt..."
if curl -sS https://starship.rs/install.sh | sh -s -- -y; then
log_success "Starship installed successfully"
else
log_error "Failed to install Starship"
return 1
fi
}
install_fzf() {
if command_exists fzf; then
log_info "FZF already installed"
return 0
fi
local fzf_dir="$HOME/.fzf"
if [ -d "$fzf_dir" ]; then
log_info "FZF directory exists, updating..."
cd "$fzf_dir" && git pull
else
log_info "Installing FZF..."
git clone --depth 1 https://github.com/junegunn/fzf.git "$fzf_dir"
fi
"$fzf_dir/install" --all --no-update-rc
log_success "FZF installed successfully"
}
install_zoxide() {
if command_exists zoxide; then
log_info "Zoxide already installed"
return 0
fi
log_info "Installing Zoxide..."
if curl -sS https://raw.githubusercontent.com/ajeetdsouza/zoxide/main/install.sh | sh; then
log_success "Zoxide installed successfully"
else
log_error "Failed to install Zoxide"
return 1
fi
}
install_matugen() {
if command_exists matugen; then
log_info "Matugen already installed"
return 0
fi
log_info "Attempting to install optional package: Matugen..."
# Try to install via cargo first (most reliable method)
if command_exists cargo; then
# Hide cargo's output unless it fails, to keep the log clean
if cargo install matugen &>/dev/null; then
log_success "Matugen installed successfully via cargo"
return 0
else
log_info "Cargo installation failed, trying binary download..."
fi
fi
# Fallback to binary installation
local arch
arch=$(uname -m)
local os="linux"
case "$arch" in
x86_64) arch="x86_64" ;;
aarch64|arm64) arch="aarch64" ;;
*)
# This is not a fatal error, just warn and exit successfully.
log_warning "Unsupported architecture for matugen binary: $arch. Skipping."
return 0
;;
esac
local download_url="https://github.com/InioX/matugen/releases/latest/download/matugen-${os}-${arch}"
local install_dir="$HOME/.local/bin"
mkdir -p "$install_dir"
if wget -q "$download_url" -O "$install_dir/matugen"; then
chmod +x "$install_dir/matugen"
log_success "Matugen installed successfully from binary"
else
# This is also not a fatal error.
log_warning "Failed to download matugen binary. Skipping."
fi
# Always return 0 to ensure the script does not stop
return 0
}
#==============================================================================
# CONFIGURATION FUNCTIONS
#==============================================================================
setup_fastfetch_config() {
# First, check if fastfetch was actually installed before trying to configure it.
if ! command_exists fastfetch; then
log_warning "fastfetch is not installed. Skipping configuration."
return 0 # Exit the function successfully without doing anything.
fi
local user_home
user_home=$(get_user_home)
local fastfetch_dir="$user_home/.config/fastfetch"
local config_file="$fastfetch_dir/config.jsonc"
mkdir -p "$fastfetch_dir"
if [ -f "$SCRIPT_DIR/config.jsonc" ]; then
ln -sf "$SCRIPT_DIR/config.jsonc" "$config_file"
log_success "Fastfetch config linked"
else
log_warning "Fastfetch config file not found"
fi
}
setup_bash_config() {
local user_home
user_home=$(get_user_home)
local bashrc="$user_home/.bashrc"
local bash_profile="$user_home/.bash_profile"
local starship_config="$user_home/.config/starship.toml"
# Backup existing bashrc if it's a regular file and not a symlink
if [ -f "$bashrc" ] && [ ! -L "$bashrc" ]; then
log_info "Backing up existing .bashrc"
mv "$bashrc" "$bashrc.backup.$(date +%Y%m%d_%H%M%S)"
fi
# Link new configurations
if [ -f "$SCRIPT_DIR/.bashrc" ]; then
ln -sf "$SCRIPT_DIR/.bashrc" "$bashrc"
log_success "Bashrc configuration linked"
else
log_warning "Bashrc template not found; creating a minimal .bashrc"
cat > "$bashrc" << 'EOF'
# Minimal .bashrc created by mybash setup
export PATH="$HOME/.local/bin:$PATH"
if command -v starship >/dev/null 2>&1; then
eval "$(starship init bash)"
fi
EOF
fi
if [ -f "$SCRIPT_DIR/starship.toml" ]; then
ln -sf "$SCRIPT_DIR/starship.toml" "$starship_config"
log_success "Starship configuration linked"
else
log_warning "Starship config template not found"
fi
# Create bash_profile if needed
if [ ! -f "$bash_profile" ]; then
cat > "$bash_profile" << 'EOF'
# Source bashrc if it exists
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
EOF
log_success "Created .bash_profile"
fi
}
cleanup_project_files() {
log_info "Cleaning up project files..."
# Remove git directory
if [ -d "$SCRIPT_DIR/.git" ]; then
rm -rf "$SCRIPT_DIR/.git"
log_success "Removed .git directory"
fi
# Remove vscode directory
if [ -d "$SCRIPT_DIR/.vscode" ]; then
rm -rf "$SCRIPT_DIR/.vscode"
log_success "Removed .vscode directory"
fi
# Remove gitignore file
if [ -f "$SCRIPT_DIR/.gitignore" ]; then
rm -f "$SCRIPT_DIR/.gitignore"
log_success "Removed .gitignore file"
fi
# Remove README file
if [ -f "$SCRIPT_DIR/README.md" ]; then
rm -f "$SCRIPT_DIR/README.md"
log_success "Removed README.md file"
fi
log_success "Project cleanup completed"
}
#==============================================================================
# MAIN EXECUTION
#==============================================================================
main() {
log_info "Starting Linux Toolbox setup..."
# Validation phase
validate_requirements || exit 1
validate_permissions || exit 1
# Detection phase
detect_package_manager || exit 1
detect_privilege_escalation
# Setup phase
setup_directories || exit 1
# Installation phase
install_packages || exit 1
install_nerd_font
install_starship || exit 1
install_fzf || exit 1
install_zoxide || exit 1
install_matugen
# Configuration phase
setup_fastfetch_config
setup_bash_config || exit 1
# Cleanup phase
cleanup_project_files
log_success "Setup completed successfully!"
log_info "Please restart your shell or run 'source ~/.bashrc' to apply changes"
}
# Run main function
main "$@"