-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash_update
More file actions
632 lines (541 loc) Β· 19.5 KB
/
bash_update
File metadata and controls
632 lines (541 loc) Β· 19.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#!/bin/bash
# ==============================================================================
# UNIFIED SYSTEM UPDATE UTILITY
# ==============================================================================
# Cross-platform update management with clear, coloured output.
# Automatically detects OS and uses appropriate package managers.
#
# Supported Systems:
# - Arch Linux (pacman + AUR helpers: yay, paru)
# - Debian/Ubuntu (apt)
# - Raspberry Pi OS (apt + optional fastfetch updates)
# - RHEL/CentOS/Fedora (dnf/yum) - detection only, no auto-update
# - Flatpak (cross-platform, if installed)
# - Atuin (cross-platform, if script-installed to ~/.atuin/bin)
#
# Usage: update, cleanup, full_update
# ==============================================================================
# ==============================================================================
# LOGGING FUNCTIONS
# ==============================================================================
_update_get_width() {
local width
width=$(tput cols 2>/dev/null || echo 80)
# Ensure minimum width of 40 and maximum of 120
[[ $width -lt 40 ]] && width=40
[[ $width -gt 120 ]] && width=120
echo "$width"
}
_update_header() {
local width
width=$(_update_get_width)
local line
line=$(printf 'β%.0s' $(seq 1 "$width"))
echo -e "\n${BC_COLOR_BOLD}${BC_COLOR_CYAN}${line}${BC_COLOR_RESET}"
echo -e "${BC_COLOR_BOLD}${BC_COLOR_CYAN} $1${BC_COLOR_RESET}"
echo -e "${BC_COLOR_BOLD}${BC_COLOR_CYAN}${line}${BC_COLOR_RESET}"
}
_update_section() {
local width
width=$(_update_get_width)
local line
line=$(printf 'β%.0s' $(seq 1 "$width"))
echo -e "\n${BC_COLOR_BOLD}${BC_COLOR_BLUE}βΆ $1${BC_COLOR_RESET}"
echo -e "${BC_COLOR_BLUE}${line}${BC_COLOR_RESET}"
}
_update_info() {
echo -e "${BC_COLOR_GRAY} βΉ $1${BC_COLOR_RESET}"
}
_update_success() {
echo -e "${BC_COLOR_GREEN} β $1${BC_COLOR_RESET}"
}
_update_warn() {
echo -e "${BC_COLOR_YELLOW} β $1${BC_COLOR_RESET}"
}
_update_error() {
echo -e "${BC_COLOR_RED} β $1${BC_COLOR_RESET}"
}
_update_skip() {
echo -e "${BC_COLOR_MAGENTA} β $1${BC_COLOR_RESET}"
}
_update_command() {
echo -e "${BC_COLOR_CYAN} βΆ Command: ${BC_COLOR_BOLD}$1${BC_COLOR_RESET}"
if [[ -n "$2" ]]; then
echo -e "${BC_COLOR_GRAY} $2${BC_COLOR_RESET}"
fi
}
# ==============================================================================
# OS DETECTION
# ==============================================================================
_detect_os() {
if [[ -f /etc/os-release ]]; then
local os_id os_id_like
{ read -r os_id; read -r os_id_like; } < <(
# shellcheck source=/dev/null
. /etc/os-release 2>/dev/null
echo "${ID:-}"
echo "${ID_LIKE:-}"
)
case "$os_id" in
arch|manjaro|endeavouros)
echo "arch"
;;
debian|ubuntu|linuxmint|pop)
echo "debian"
;;
raspbian)
echo "raspbian"
;;
rhel|centos|fedora|rocky|almalinux)
echo "rhel"
;;
*)
# Check for derivatives
if [[ "$os_id_like" == *"arch"* ]]; then
echo "arch"
elif [[ "$os_id_like" == *"debian"* ]]; then
echo "debian"
elif [[ "$os_id_like" == *"rhel"* || "$os_id_like" == *"fedora"* ]]; then
echo "rhel"
else
echo "unknown"
fi
;;
esac
else
echo "unknown"
fi
}
_get_os_pretty_name() {
if [[ -f /etc/os-release ]]; then
# shellcheck source=/dev/null
. /etc/os-release 2>/dev/null
echo "${PRETTY_NAME:-Unknown OS}"
else
echo "Unknown OS"
fi
}
# ==============================================================================
# AUR HELPER DETECTION
# ==============================================================================
_detect_aur_helper() {
if command -v yay &>/dev/null; then
echo "yay"
elif command -v paru &>/dev/null; then
echo "paru"
else
echo ""
fi
}
# ==============================================================================
# ARCH LINUX UPDATE FUNCTIONS
# ==============================================================================
_update_arch_pacman() {
_update_section "π¦ Pacman - Official Repositories"
_update_info "Synchronising package databases and upgrading..."
_update_command "sudo pacman -Syu" "-S: install packages | -y: refresh database | -u: upgrade packages"
if sudo pacman -Syu; then
_update_success "Pacman update complete"
return 0
else
_update_error "Pacman update failed"
return 1
fi
}
_update_arch_aur() {
local aur_helper
aur_helper=$(_detect_aur_helper)
_update_section "π AUR - Arch User Repository"
if [[ -z "$aur_helper" ]]; then
_update_skip "No AUR helper found (yay/paru not installed)"
return 0
fi
_update_info "Using AUR helper: ${aur_helper}"
_update_info "Checking for AUR package updates..."
_update_command "$aur_helper -Sua" "-S: install packages | -u: upgrade packages | -a: AUR packages only"
if $aur_helper -Sua; then
_update_success "AUR update complete"
return 0
else
_update_error "AUR update failed"
return 1
fi
}
_cleanup_arch() {
_update_section "π§Ή Cleanup - Removing Orphaned Packages"
local -a orphans
mapfile -t orphans < <(pacman -Qdtq 2>/dev/null)
if [[ ${#orphans[@]} -eq 0 ]]; then
_update_success "No orphan packages found"
return 0
fi
_update_info "Found orphan packages:"
for pkg in "${orphans[@]}"; do
echo -e " ${BC_COLOR_YELLOW}β’ $pkg${BC_COLOR_RESET}"
done
_update_command "sudo pacman -Rns <packages>" "-R: remove packages | -n: remove config files | -s: remove dependencies"
if sudo pacman -Rns "${orphans[@]}"; then
_update_success "Orphan packages removed"
return 0
else
_update_error "Failed to remove orphan packages"
return 1
fi
}
# ==============================================================================
# DEBIAN/UBUNTU UPDATE FUNCTIONS
# ==============================================================================
_update_debian_apt() {
_update_section "π¦ APT - Package Updates"
_update_info "Updating package lists..."
_update_command "sudo apt update" "Update package lists from repositories"
if ! sudo apt update; then
_update_error "Failed to update package lists"
return 1
fi
_update_success "Package lists updated"
_update_info "Upgrading packages..."
_update_command "sudo apt upgrade -y" "-y: automatic yes to prompts | upgrade: install available package upgrades"
if sudo apt upgrade -y; then
_update_success "APT upgrade complete"
return 0
else
_update_error "APT upgrade failed"
return 1
fi
}
_cleanup_debian() {
_update_section "π§Ή Cleanup - APT Maintenance"
_update_info "Removing unused packages..."
_update_command "sudo apt autoremove -y" "-y: automatic yes | autoremove: remove unused packages and dependencies"
if ! sudo apt autoremove -y; then
_update_warn "Autoremove encountered issues"
else
_update_success "Unused packages removed"
fi
_update_info "Cleaning package cache..."
_update_command "sudo apt autoclean" "Remove old package files from cache (partial clean)"
if sudo apt autoclean; then
_update_success "Package cache cleaned"
return 0
else
_update_warn "Autoclean encountered issues"
return 1
fi
}
# ==============================================================================
# RASPBERRY PI SPECIFIC FUNCTIONS
# ==============================================================================
_update_raspbian_fastfetch() {
_update_section "π Fastfetch - Checking for Updates"
if ! command -v fastfetch &>/dev/null; then
_update_info "Fastfetch not installed, attempting to install..."
local install_new=true
local current_version="none"
else
local current_version
current_version=$(fastfetch --version 2>/dev/null | head -n1 | grep -oP '\d+\.\d+\.\d+' | head -n1)
local install_new=false
fi
# Get latest version from GitHub
local latest_version
latest_version=$(curl -s https://api.github.com/repos/fastfetch-cli/fastfetch/releases/latest 2>/dev/null | grep -oP '"tag_name": "\K[^"]*' | sed 's/^v//')
if [[ -z "$latest_version" ]]; then
_update_warn "Could not fetch latest fastfetch version from GitHub"
return 1
fi
_update_info "Current version: ${current_version:-not installed}"
_update_info "Latest version: ${latest_version}"
if [[ "$current_version" == "$latest_version" ]]; then
_update_success "Fastfetch is already up to date"
return 0
fi
_update_info "Downloading fastfetch ${latest_version}..."
local temp_file="/tmp/fastfetch-${latest_version}-$(date +%s).deb"
local download_url
download_url=$(curl -s https://api.github.com/repos/fastfetch-cli/fastfetch/releases/latest 2>/dev/null | grep "browser_download_url.*linux-aarch64.deb" | cut -d '"' -f 4)
if [[ -z "$download_url" ]]; then
_update_error "Could not find ARM64 download URL"
return 1
fi
if wget -q -O "$temp_file" "$download_url"; then
_update_info "Installing fastfetch..."
if sudo dpkg -i "$temp_file" && sudo apt install -f -qq; then
rm -f "$temp_file"
_update_success "Fastfetch updated to ${latest_version}"
return 0
else
rm -f "$temp_file"
_update_error "Failed to install fastfetch"
return 1
fi
else
rm -f "$temp_file" 2>/dev/null
_update_error "Failed to download fastfetch"
return 1
fi
}
# ==============================================================================
# RHEL/CENTOS/FEDORA FUNCTIONS
# ==============================================================================
_update_rhel_notice() {
_update_section "π’ RHEL/Enterprise Linux Detected"
_update_warn "This appears to be a managed enterprise system"
_update_info "System updates should be handled by your system administrator"
_update_info "Detected OS: $(_get_os_pretty_name)"
return 0
}
# ==============================================================================
# FLATPAK UPDATE FUNCTIONS
# ==============================================================================
_update_flatpak() {
_update_section "π¦ Flatpak - Application Updates"
if ! command -v flatpak &>/dev/null; then
_update_skip "Flatpak not installed"
return 0
fi
_update_info "Checking for flatpak updates..."
_update_command "flatpak update -y" "Update all installed flatpak applications"
if flatpak update -y; then
_update_success "Flatpak update complete"
return 0
else
_update_error "Flatpak update failed"
return 1
fi
}
_cleanup_flatpak() {
_update_section "π§Ή Flatpak - Removing Unused Runtimes"
if ! command -v flatpak &>/dev/null; then
_update_skip "Flatpak not installed"
return 0
fi
_update_info "Removing unused flatpak runtimes and extensions..."
_update_command "flatpak uninstall --unused -y" "Remove unused runtimes and extensions"
if flatpak uninstall --unused -y; then
_update_success "Flatpak cleanup complete"
return 0
else
_update_warn "Flatpak cleanup encountered issues"
return 1
fi
}
# ==============================================================================
# ATUIN UPDATE FUNCTIONS
# ==============================================================================
_update_atuin() {
# Only self-update if installed via the official install script (~/.atuin/bin).
# Package-manager installs (/usr/bin etc.) are handled by the system updater.
if command -v atuin &>/dev/null; then
local atuin_path
atuin_path=$(command -v atuin)
if [[ "$atuin_path" != "$HOME/.atuin/bin/atuin" ]]; then
bc_log_debug "Atuin managed by system package manager (${atuin_path}) β skipping self-update"
return 0
fi
fi
_update_section "π Atuin - Shell History Manager"
if ! command -v atuin &>/dev/null; then
_update_skip "Atuin not installed"
return 0
fi
_update_info "Updating script-installed atuin..."
_update_command "atuin update" "Self-update the atuin binary"
if atuin update; then
_update_success "Atuin update complete"
return 0
else
_update_error "Atuin update failed"
return 1
fi
}
# ==============================================================================
# PIXI UPDATE FUNCTIONS
# ==============================================================================
_update_pixi() {
if ! command -v pixi &>/dev/null; then
return 0
fi
_update_section "π¦ Pixi - Package Manager"
# Only self-update if installed under PIXI_HOME (i.e. not via a system package manager).
local pixi_path
pixi_path=$(command -v pixi)
local expected_path="${PIXI_HOME:-$HOME/.pixi}/bin/pixi"
if [[ "$pixi_path" != "$expected_path" ]]; then
_update_skip "Pixi managed by system package manager (${pixi_path})"
return 0
fi
_update_info "Updating script-installed pixi..."
_update_command "pixi self-update" "Self-update the pixi binary"
if pixi self-update; then
_update_success "Pixi update complete"
return 0
else
_update_error "Pixi update failed"
return 1
fi
}
# ==============================================================================
# CROSS-PLATFORM UPDATERS
# ==============================================================================
# Runs the tools common to all non-RHEL platforms (flatpak, atuin, pixi).
_update_cross_platform() {
local overall=0
_update_flatpak || overall=1
_update_atuin || overall=1
_update_pixi || overall=1
return $overall
}
# ==============================================================================
# MAIN UPDATE FUNCTIONS
# ==============================================================================
# Primary update function - updates system packages
update() {
local os_type
os_type=$(_detect_os)
_update_header "π System Update - ${BC_COLOR_BOLD}$(_get_os_pretty_name)${BC_COLOR_RESET}"
local overall=0
case "$os_type" in
arch)
_update_arch_pacman || overall=1
_update_arch_aur || overall=1
_update_cross_platform || overall=1
;;
debian|raspbian)
_update_debian_apt || overall=1
_update_cross_platform || overall=1
;;
rhel)
_update_rhel_notice
_update_pixi || overall=1
;;
*)
_update_error "Unknown operating system"
_update_info "Detected: $(_get_os_pretty_name)"
_update_info "Please update manually using your system's package manager"
return 1
;;
esac
if [[ $overall -eq 0 ]]; then
echo -e "\n${BC_COLOR_GREEN}${BC_COLOR_BOLD}β All updates completed successfully${BC_COLOR_RESET}\n"
else
echo -e "\n${BC_COLOR_YELLOW}${BC_COLOR_BOLD}β Updates completed with some issues${BC_COLOR_RESET}\n"
fi
return $overall
}
# Cleanup function - removes orphaned/unused packages
cleanup() {
local os_type
os_type=$(_detect_os)
_update_header "π§Ή System Cleanup - ${BC_COLOR_BOLD}$(_get_os_pretty_name)${BC_COLOR_RESET}"
local overall=0
case "$os_type" in
arch)
_cleanup_arch || overall=1
_cleanup_flatpak || overall=1
;;
debian|raspbian)
_cleanup_debian || overall=1
_cleanup_flatpak || overall=1
;;
rhel)
_update_rhel_notice
return 0
;;
*)
_update_error "Unknown operating system"
return 1
;;
esac
if [[ $overall -eq 0 ]]; then
echo -e "\n${BC_COLOR_GREEN}${BC_COLOR_BOLD}β Cleanup completed successfully${BC_COLOR_RESET}\n"
else
echo -e "\n${BC_COLOR_YELLOW}${BC_COLOR_BOLD}β Cleanup completed with some issues${BC_COLOR_RESET}\n"
fi
return $overall
}
# Full update - updates and cleans up
full_update() {
local os_type
os_type=$(_detect_os)
_update_header "π Full System Update - ${BC_COLOR_BOLD}$(_get_os_pretty_name)${BC_COLOR_RESET}"
_update_info "This will update all packages and perform cleanup"
# Run update
update
local update_result=$?
# Run cleanup
cleanup
local cleanup_result=$?
# Raspberry Pi specific: update fastfetch
if [[ "$os_type" == "raspbian" ]]; then
_update_raspbian_fastfetch
fi
# Summary
_update_header "π Update Summary"
if [[ $update_result -eq 0 ]]; then
_update_success "Package updates: Completed"
else
_update_error "Package updates: Failed"
fi
if [[ $cleanup_result -eq 0 ]]; then
_update_success "System cleanup: Completed"
else
_update_warn "System cleanup: Completed with issues"
fi
if [[ $update_result -eq 0 && $cleanup_result -eq 0 ]]; then
echo -e "\n${BC_COLOR_GREEN}${BC_COLOR_BOLD}β Full update completed successfully${BC_COLOR_RESET}\n"
return 0
else
echo -e "\n${BC_COLOR_YELLOW}${BC_COLOR_BOLD}β Full update completed with some issues${BC_COLOR_RESET}\n"
return 1
fi
}
alias full-update='full_update'
# Alias for full_update
update_all() {
full_update
}
alias update-all='update_all'
# ==============================================================================
# HELP FUNCTION
# ==============================================================================
update_help() {
local width
width=$(_update_get_width)
local line
line=$(printf 'β%.0s' $(seq 1 "$width"))
cat << EOF
${line}
π UNIFIED UPDATE SYSTEM - COMMAND REFERENCE
${line}
π COMMANDS
update Update system packages
cleanup Remove orphaned/unused packages
full_update Complete update + cleanup (+ fastfetch on Pi)
update_all Alias for full_update
update_help Show this help message
π¦ SUPPORTED SYSTEMS
Arch Linux pacman + AUR (yay/paru)
Debian/Ubuntu apt
Raspberry Pi apt + fastfetch auto-update
RHEL/CentOS Detection only (managed systems)
Flatpak Cross-platform (if installed)
Atuin Cross-platform, script-installed only (~/.atuin/bin)
Pixi Cross-platform, script-installed only (\$PIXI_HOME/bin)
π¨ OUTPUT COLOURS
Blue Section headers
White Informational messages
Green Success messages
Yellow Warnings
Red Errors
Magenta Skipped operations
π‘ EXAMPLES
update # Update packages only
cleanup # Clean up orphaned packages
full_update # Do everything
${line}
EOF
}
# Shorthand alias
alias uh='update_help'
alias update-help='update_help'