-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsys-admin.sh
More file actions
executable file
Β·2054 lines (1761 loc) Β· 69.8 KB
/
sys-admin.sh
File metadata and controls
executable file
Β·2054 lines (1761 loc) Β· 69.8 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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#
# System Administration Script for Linux Configuration & Installation Suite
#
set -euo pipefail
# ============================================================================
# CONFIGURATION
# ============================================================================
readonly SCRIPT_VERSION="1.0.0"
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly LOG_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/sys-admin"
readonly LOG_FILE="${LOG_DIR}/sys-admin-$(date +%Y%m%d-%H%M%S).txt"
# Colors (use $'...' so the escape sequences are actual control chars)
readonly RED=$'\033[0;31m'
readonly GREEN=$'\033[0;32m'
readonly YELLOW=$'\033[1;33m'
readonly BLUE=$'\033[0;34m'
readonly MAGENTA=$'\033[0;35m'
readonly CYAN=$'\033[0;36m'
readonly WHITE=$'\033[1;37m'
readonly BOLD=$'\033[1m'
readonly DIM=$'\033[2m'
readonly NC=$'\033[0m'
# Distro detection results
DISTRO_ID=""
DISTRO_VERSION=""
DISTRO_FAMILY=""
PACKAGE_MANAGER=""
# PID of background sudo keepalive (set by check_sudo, cleaned up by trap)
sudo_keepalive_pid=""
# ============================================================================
# CLEANUP TRAP
# ============================================================================
cleanup() {
if [[ -n "$sudo_keepalive_pid" ]] && kill -0 "$sudo_keepalive_pid" 2>/dev/null; then
kill "$sudo_keepalive_pid" 2>/dev/null || true
fi
debug "Cleanup completed"
}
trap cleanup EXIT INT TERM HUP
setup_logging() {
mkdir -p "$LOG_DIR"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] === Sys Admin v${SCRIPT_VERSION} Started ===" >> "$LOG_FILE"
}
# ============================================================================
# UTILITY FUNCTIONS
# ============================================================================
_log_to_file() { echo "[$(date '+%H:%M:%S')] $*" >> "$LOG_FILE" 2>/dev/null || true; }
log() { echo -e "${BLUE}[INFO]${NC} $1"; _log_to_file "[INFO] $1"; }
success() { echo -e "${GREEN}[β]${NC} $1"; _log_to_file "[OK] $1"; }
warn() { echo -e "${YELLOW}[!]${NC} $1"; _log_to_file "[WARN] $1"; }
error() { echo -e "${RED}[β]${NC} $1" >&2; _log_to_file "[ERR] $1"; }
fatal() { error "$1"; exit 1; }
debug() { [[ "${DEBUG:-0}" == "1" ]] && echo -e "${DIM}[DEBUG]${NC} $1" || true; }
center_text() {
local text="$1"
local width="${2:-80}"
local padding=$(( (width - ${#text}) / 2 ))
printf "%*s%s\n" "$padding" "" "$text"
}
draw_line() {
local char="${1:-=}"
local width="${2:-80}"
printf '%*s\n' "$width" '' | tr ' ' "$char"
}
press_any_key() {
echo
read -n 1 -s -r -p "Press any key to continue..."
echo
}
show_main_banner() {
clear
echo -e "${CYAN}${BOLD}"
draw_line "=" 80
cat << 'EOF'
βββββββ ββ ββ βββββββ βββββ ββββββ βββ βββ ββ βββ ββ
ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββββ ββ ββββ ββ
βββββββ ββββ βββββββ βββββββ ββ ββ ββ ββββ ββ ββ ββ ββ ββ
ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ
βββββββ ββ βββββββ ββ ββ ββββββ ββ ββ ββ ββ ββββ
Linux Configuration & Installation Suite
EOF
draw_line "=" 80
echo -e "${NC}"
}
show_section_banner() {
local title="$1"
echo
echo -e "${MAGENTA}${BOLD}"
draw_line "β" 70
center_text "$title" 70
draw_line "β" 70
echo -e "${NC}"
}
# ============================================================================
# DISTRO DETECTION
# ============================================================================
detect_distro() {
show_section_banner "SYSTEM DETECTION"
log "Detecting Linux distribution..."
if [[ -f /etc/os-release ]]; then
# Read only the variables we need β avoid leaking arbitrary vars into
# the script's environment from os-release.
eval "$(sed -n 's/^\(ID\|VERSION_ID\|VERSION\|NAME\)=/local os_\1=/p' /etc/os-release)"
DISTRO_ID="${os_ID:-unknown}"
# Try multiple sources for version info
DISTRO_VERSION="${os_VERSION_ID:-}"
if [[ -z "$DISTRO_VERSION" && -n "${os_VERSION:-}" ]]; then
DISTRO_VERSION="$os_VERSION"
fi
if [[ -z "$DISTRO_VERSION" ]] && command -v lsb_release &>/dev/null; then
DISTRO_VERSION="$(lsb_release -rs 2>/dev/null || true)"
fi
# Detect distro family
case "$DISTRO_ID" in
arch|manjaro|cachyos|endeavouros|garuda|artix)
DISTRO_FAMILY="arch"
PACKAGE_MANAGER="pacman"
;;
ubuntu|debian|linuxmint|pop|elementary|zorin|deepin)
DISTRO_FAMILY="debian"
PACKAGE_MANAGER="apt"
;;
fedora|rhel|centos|rocky|alma|nobara)
DISTRO_FAMILY="fedora"
PACKAGE_MANAGER="dnf"
;;
opensuse*|suse|tumbleweed)
DISTRO_FAMILY="suse"
PACKAGE_MANAGER="zypper"
;;
gentoo|funtoo)
DISTRO_FAMILY="gentoo"
PACKAGE_MANAGER="emerge"
;;
void)
DISTRO_FAMILY="void"
PACKAGE_MANAGER="xbps-install"
;;
alpine)
DISTRO_FAMILY="alpine"
PACKAGE_MANAGER="apk"
;;
nixos)
DISTRO_FAMILY="nixos"
PACKAGE_MANAGER="nix-env"
;;
*)
DISTRO_FAMILY="unknown"
PACKAGE_MANAGER="unknown"
;;
esac
success "Detected: ${os_NAME:-$DISTRO_ID}${DISTRO_VERSION:+ $DISTRO_VERSION}"
success "Family: $DISTRO_FAMILY"
success "Package Manager: $PACKAGE_MANAGER"
else
warn "Could not detect distribution (no /etc/os-release)"
DISTRO_ID="unknown"
DISTRO_FAMILY="unknown"
fi
# Additional system info
log "Kernel: $(uname -r)"
log "Architecture: $(uname -m)"
log "Hostname: $(cat /etc/hostname 2>/dev/null || uname -n)"
# Detect init system (prefer checking PID 1 or systemd runtime dir)
if [ -d /run/systemd/system ] || [[ "$(ps -p 1 -o comm=)" == "systemd" ]]; then
success "Init system: systemd"
else
warn "systemd not detected"
fi
# Check for common tools
local tools=(curl wget git sudo)
for tool in "${tools[@]}"; do
if command -v "$tool" &>/dev/null; then
debug "$tool: available"
else
warn "$tool: not found"
fi
done
echo
}
# ============================================================================
# PACKAGE MANAGEMENT
# ============================================================================
check_package() {
local package="$1"
case "$PACKAGE_MANAGER" in
pacman)
pacman -Qi "$package" &>/dev/null
;;
apt)
dpkg -l "$package" 2>/dev/null | grep -q "^ii"
;;
dnf)
rpm -q "$package" &>/dev/null
;;
zypper)
rpm -q "$package" &>/dev/null
;;
*)
command -v "$package" &>/dev/null
;;
esac
}
# ============================================================================
# BUILT-IN FUNCTIONS
# ============================================================================
builtin_system_update() {
show_section_banner "SYSTEM UPDATE"
cat << EOF
${YELLOW}Description:${NC}
Updates system packages using the native package manager.
${YELLOW}Detected package manager:${NC} $PACKAGE_MANAGER
EOF
read -p "Continue with system update? [y/N]: " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
warn "Skipped system update"
return
fi
log "Updating system packages..."
echo
case "$PACKAGE_MANAGER" in
pacman)
sudo pacman -Syu
;;
apt)
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get autoremove -y
sudo apt-get clean
;;
dnf)
sudo dnf upgrade -y
sudo dnf autoremove -y
;;
zypper)
sudo zypper refresh
sudo zypper update -y
;;
emerge)
sudo emerge --sync
sudo emerge -uDN @world
;;
xbps-install)
sudo xbps-install -Su
;;
apk)
sudo apk update
sudo apk upgrade
;;
nix-env)
nix-channel --update
nix-env -u
;;
*)
error "Unsupported package manager: $PACKAGE_MANAGER"
;;
esac
success "System update completed"
press_any_key
}
# ============================================================================
# USB DEVICE MANAGEMENT HELPERS
# ============================================================================
list_usb_devices() {
# Lists all USB devices with vendor/product IDs
# Returns: empty if lsusb not available
if ! command -v lsusb &>/dev/null; then
return
fi
lsusb
}
configure_tlp_usb_blacklist() {
show_section_banner "TLP USB DEVICE BLACKLIST CONFIGURATION"
cat << EOF
${YELLOW}Description:${NC}
Prevent TLP from suspending specific USB devices (like Bluetooth adapters).
Uses USB_BLACKLIST configuration to keep devices powered during idle.
${YELLOW}Why this matters:${NC}
β’ Prevents Bluetooth disconnection during AC power
β’ Keeps USB devices like wireless adapters functional
β’ Preserves keyboard/mouse responsiveness
β’ No negative impact on battery life when plugged in
EOF
log "Scanning for USB devices..."
echo
# Get list of USB devices
local usb_list
usb_list=$(list_usb_devices)
if [[ -z "$usb_list" ]]; then
warn "Could not detect USB devices (lsusb not available)"
return
fi
# Detect Bluetooth devices from the already-fetched list (no second lsusb call)
local bt_devices
bt_devices=$(echo "$usb_list" | grep -iE "(bluetooth|bcm|intel.*wireless|qualcomm|realtek|atheros)" || true)
echo -e "${BOLD}${CYAN}Available USB Devices:${NC}"
echo
local -a device_ids
local counter=1
while IFS= read -r line; do
if [[ -z "$line" ]]; then continue; fi
local id=$(echo "$line" | sed -n 's/.*ID \([0-9a-f]*:[0-9a-f]*\).*/\1/p')
local name=$(echo "$line" | sed 's/.*ID [0-9a-f:]*[[:space:]]*//' || true)
if echo "$line" | grep -qiE "(bluetooth|bcm|intel.*wireless|qualcomm|realtek|atheros)"; then
printf " ${GREEN}%2d)${NC} [${CYAN}%s${NC}] ${BOLD}%s${NC} ${DIM}(Bluetooth)${NC}\n" "$counter" "$id" "$name"
else
printf " ${GREEN}%2d)${NC} [${CYAN}%s${NC}] %s\n" "$counter" "$id" "$name"
fi
device_ids+=("$id")
counter=$((counter + 1))
done <<< "$usb_list"
echo
echo -e " ${GREEN}0)${NC} Skip USB blacklist configuration"
echo
# Ask user for selection
read -p "Select devices to blacklist (comma-separated: 1,3,5 or 'all' for Bluetooth): " -r selection
if [[ "$selection" == "0" || -z "$selection" ]]; then
warn "Skipped USB blacklist configuration"
return
fi
# Validate format before parsing (prevent injection into TLP config)
if [[ ! "$selection" =~ ^([0-9]+(,[0-9]+)*|all)$ ]]; then
error "Invalid input β use numbers like 1,3 or 'all'"
return
fi
local -a selected_ids
local tlp_blacklist="USB_BLACKLIST="
local usb_autosuspend_blacklist="USB_AUTOSUSPEND_BLACKLIST="
if [[ "$selection" == "all" ]]; then
# Use all Bluetooth devices
log "Selecting all Bluetooth USB devices..."
while IFS= read -r line; do
local id=$(echo "$line" | sed -n 's/.*ID \([0-9a-f]*:[0-9a-f]*\).*/\1/p')
if [[ -n "$id" ]]; then
selected_ids+=("$id")
fi
done <<< "$bt_devices"
else
# Parse comma-separated selection
IFS=',' read -ra selections <<< "$selection"
for sel in "${selections[@]}"; do
sel=$(echo "$sel" | xargs) # Trim whitespace
if [[ "$sel" =~ ^[0-9]+$ ]] && (( sel > 0 && sel <= ${#device_ids[@]} )); then
selected_ids+=("${device_ids[$((sel-1))]}")
fi
done
fi
if [[ ${#selected_ids[@]} -eq 0 ]]; then
warn "No valid devices selected"
return
fi
# Build TLP configuration
for id in "${selected_ids[@]}"; do
tlp_blacklist+="$id "
usb_autosuspend_blacklist+="$id "
done
echo
success "Selected devices to blacklist:"
for id in "${selected_ids[@]}"; do
echo " β’ $id"
done
echo
# Back up TLP config
log "Backing up /etc/tlp.conf..."
sudo cp /etc/tlp.conf "/etc/tlp.conf.backup.$(date +%s)" || warn "Could not backup tlp.conf"
# Modify TLP configuration
log "Updating /etc/tlp.conf with USB blacklist..."
# Check if settings already exist and comment them out
sudo sed -i.bak '/^USB_BLACKLIST=/s/^/#&/; /^USB_AUTOSUSPEND_BLACKLIST=/s/^/#&/' /etc/tlp.conf 2>/dev/null || true
# Add new blacklist configuration at end of file
{
echo ""
echo "# ============================================================================"
echo "# USB Device Blacklist Configuration (Added by Sys Admin)"
echo "# ============================================================================"
echo "# Prevent TLP from suspending these USB devices"
echo "$tlp_blacklist"
echo "$usb_autosuspend_blacklist"
echo ""
} | sudo tee -a /etc/tlp.conf > /dev/null
success "TLP configuration updated"
# Validate the updated configuration
if tlp-stat -c &>/dev/null 2>&1; then
success "TLP configuration validated"
else
error "TLP configuration validation failed"
local backup
backup=$(ls -t /etc/tlp.conf.backup.* 2>/dev/null | head -1)
if [[ -n "$backup" ]]; then
warn "Restoring from backup: $backup"
sudo cp "$backup" /etc/tlp.conf
sudo systemctl restart tlp 2>/dev/null || true
success "Previous configuration restored"
fi
fi
echo
log "USB blacklist configuration added:"
sudo grep -A 5 "# USB Device Blacklist" /etc/tlp.conf | sed 's/^/ /'
echo
log "Restarting TLP to apply changes..."
sudo tlp start || warn "Could not restart TLP"
success "TLP restarted"
echo
}
configure_bluetooth_autoenable() {
show_section_banner "BLUETOOTH AUTO-ENABLE CONFIGURATION"
cat << EOF
${YELLOW}Description:${NC}
Ensures Bluetooth automatically enables on boot, even if powered off previously.
Prevents Bluetooth from staying disabled due to power management or rfkill blocks.
EOF
if [[ ! -f /etc/bluetooth/main.conf ]]; then
warn "Bluetooth configuration file not found at /etc/bluetooth/main.conf"
return
fi
read -p "Configure Bluetooth auto-enable? [y/N]: " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
warn "Skipped Bluetooth auto-enable configuration"
return
fi
log "Updating /etc/bluetooth/main.conf..."
# Already correct β skip to avoid duplicating lines on re-run
if sudo grep -q '^AutoEnable=true' /etc/bluetooth/main.conf 2>/dev/null; then
success "AutoEnable=true is already set"
return
fi
# Backup config
sudo cp /etc/bluetooth/main.conf "/etc/bluetooth/main.conf.backup.$(date +%s)" || warn "Could not backup main.conf"
# Check if [Policy] section exists
if sudo grep -q '^\[Policy\]' /etc/bluetooth/main.conf; then
log "Found [Policy] section - updating AutoEnable setting..."
# Comment out any existing AutoEnable line, then insert after [Policy]
sudo sed -i '/^#*AutoEnable=/s/^/#/' /etc/bluetooth/main.conf
sudo sed -i '0,/^\[Policy\]/{ /^\[Policy\]/a AutoEnable=true
}' /etc/bluetooth/main.conf
else
log "Adding [Policy] section with AutoEnable..."
printf '\n[Policy]\nAutoEnable=true\n' | sudo tee -a /etc/bluetooth/main.conf > /dev/null
fi
# Verify the setting
local autoenable_value
autoenable_value=$(sudo grep "^AutoEnable=" /etc/bluetooth/main.conf 2>/dev/null || echo "not found")
if [[ "$autoenable_value" == "AutoEnable=true" ]]; then
success "Bluetooth AutoEnable set to true"
else
warn "Could not verify Bluetooth AutoEnable setting"
fi
echo
log "Restarting Bluetooth service..."
sudo systemctl restart bluetooth || warn "Could not restart Bluetooth service"
success "Bluetooth service restarted"
echo
}
builtin_install_tlp() {
show_section_banner "TLP POWER MANAGEMENT INSTALLATION"
cat << EOF
${YELLOW}Description:${NC}
TLP is an advanced power management tool that optimizes battery life and
system performance. This installer will:
β’ Install TLP and TLP-RDW (radio device wizard for Wi-Fi/Bluetooth on boot)
β’ Remove conflicting power-profiles-daemon (if present)
β’ Enable TLP service at boot
β’ Apply Bluetooth optimization fixes
β’ Optionally install TLPUI (graphical interface)
${YELLOW}System:${NC} $DISTRO_ID
${YELLOW}Package Manager:${NC} $PACKAGE_MANAGER
EOF
read -p "Continue with TLP installation? [y/N]: " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
warn "Skipped TLP installation"
return
fi
log "Starting TLP installation process..."
echo
# Step 1: Remove conflicting package
log "Checking for conflicting power management packages..."
if check_package "power-profiles-daemon"; then
log "Found power-profiles-daemon - removing to avoid conflicts..."
case "$PACKAGE_MANAGER" in
pacman) sudo pacman -R --noconfirm power-profiles-daemon || warn "Failed to remove power-profiles-daemon" ;;
apt) sudo apt-get remove -y power-profiles-daemon || warn "Failed to remove power-profiles-daemon" ;;
dnf) sudo dnf remove -y power-profiles-daemon || warn "Failed to remove power-profiles-daemon" ;;
zypper) sudo zypper remove -y power-profiles-daemon || warn "Failed to remove power-profiles-daemon" ;;
*) warn "Cannot auto-remove power-profiles-daemon for $PACKAGE_MANAGER β please remove it manually" ;;
esac
success "Removed power-profiles-daemon"
else
debug "power-profiles-daemon not installed"
fi
# Stop and mask the service to prevent conflicts
if systemctl is-active --quiet power-profiles-daemon.service 2>/dev/null; then
log "Stopping power-profiles-daemon.service..."
sudo systemctl stop power-profiles-daemon.service || warn "Failed to stop power-profiles-daemon.service"
success "power-profiles-daemon.service stopped"
fi
if systemctl list-unit-files 2>/dev/null | grep -q "power-profiles-daemon.service"; then
log "Masking power-profiles-daemon.service to prevent interference..."
sudo systemctl mask power-profiles-daemon.service || warn "Failed to mask power-profiles-daemon.service"
success "power-profiles-daemon.service masked"
fi
echo
# Step 2: Install TLP and related tools
log "Installing TLP packages (tlp, tlp-rdw)..."
case "$PACKAGE_MANAGER" in
pacman)
sudo pacman -S --noconfirm tlp tlp-rdw || fatal "Failed to install TLP packages"
;;
apt)
sudo apt-get install -y tlp tlp-rdw || fatal "Failed to install TLP packages"
;;
dnf)
sudo dnf install -y tlp tlp-rdw || fatal "Failed to install TLP packages"
;;
*)
error "Automatic TLP installation not supported for $PACKAGE_MANAGER"
error "Please install TLP manually and run this script again"
press_any_key
return
;;
esac
success "TLP packages installed"
echo
# Step 3: Enable TLP service
log "Enabling TLP service..."
sudo systemctl enable --now tlp.service || fatal "Failed to enable TLP service"
success "TLP service enabled and started"
echo
# Step 4: Verify installation
log "Verifying TLP installation and configuration..."
if tlp-stat -s &>/dev/null; then
success "TLP installation verified"
echo
log "TLP Status:"
tlp-stat -s | sed 's/^/ /'
else
warn "TLP verification failed - service may need restart"
fi
echo
# Step 4a: Configure USB blacklist for Bluetooth and other devices
read -p "Configure USB device blacklist (keep Bluetooth/devices alive)? [y/N]: " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
configure_tlp_usb_blacklist
fi
echo
# Step 4b: Configure Bluetooth auto-enable
read -p "Configure Bluetooth auto-enable on boot? [y/N]: " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
configure_bluetooth_autoenable
fi
echo
# Step 6: Optional TLPUI installation
read -p "Install TLPUI (graphical interface) for TLP? [y/N]: " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
log "Installing TLPUI..."
case "$PACKAGE_MANAGER" in
pacman)
# Check if AUR helper exists
if command -v yay &>/dev/null; then
log "Using yay for AUR access..."
yay -S --noconfirm tlp-ui 2>/dev/null && success "TLPUI installed via yay" || \
warn "Failed to install TLPUI via yay"
elif command -v paru &>/dev/null; then
log "Using paru for AUR access..."
paru -S --noconfirm tlp-ui 2>/dev/null && success "TLPUI installed via paru" || \
warn "Failed to install TLPUI via paru"
else
warn "No AUR helper (yay/paru) found β cannot install TLPUI"
echo " Install an AUR helper first, then retry:"
echo " git clone https://aur.archlinux.org/yay.git && cd yay && makepkg -si"
echo " Then run: yay -S tlp-ui"
fi
;;
*)
warn "TLPUI installation requires AUR/source access - not fully supported for $DISTRO_ID"
warn "Try installing manually if available for your distro"
;;
esac
fi
success "TLP installation and optimization complete!"
log "Next steps:"
echo " 1. Review TLP configuration: sudo nano /etc/tlp.conf"
echo " 2. Check status: tlp-stat -s"
echo " 3. Monitor thermal data: watch -n 1 sensors"
echo " 4. For graphical control: tlpui (if installed)"
echo
press_any_key
}
builtin_mullvad_headless_setup() {
show_section_banner "MULLVAD VPN INSTALLATION & HEADLESS SETUP"
cat << EOF
${YELLOW}Description:${NC}
Installs Mullvad VPN and configures it for headless operation with auto-connect.
${YELLOW}This installer will:${NC}
β’ Install Mullvad VPN for your distro (if not already installed)
β’ Start the Mullvad daemon
β’ Prompt you to log in with your account number
β’ Enable auto-connect on boot
β’ Establish and verify the VPN connection
${YELLOW}Requirements:${NC}
β’ Valid Mullvad account number (format: XXXX-XXXX-XXXX-XXXX)
β’ Internet connection
EOF
read -p "Continue with Mullvad installation & headless setup? [y/N]: " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
warn "Skipped Mullvad setup"
return
fi
log "Starting Mullvad VPN installation and headless setup..."
echo
# ββ Step 1: Install Mullvad if not present ββββββββββββββββββββββββββββββ
if command -v mullvad &>/dev/null; then
success "Mullvad already installed: $(mullvad --version 2>/dev/null || echo 'version unknown')"
else
log "Installing Mullvad VPN for $DISTRO_ID ($PACKAGE_MANAGER)..."
echo
case "$PACKAGE_MANAGER" in
pacman)
# mullvad-vpn is in the official Arch extra repository
sudo pacman -S --noconfirm mullvad-vpn \
|| fatal "Failed to install mullvad-vpn"
;;
apt)
log "Adding Mullvad official apt repository..."
# Key stored as plain .asc β no gpg --dearmor needed
sudo curl -fsSLo /usr/share/keyrings/mullvad-keyring.asc \
https://repository.mullvad.net/deb/mullvad-keyring.asc \
|| fatal "Failed to download Mullvad signing key"
echo "deb [signed-by=/usr/share/keyrings/mullvad-keyring.asc arch=$(dpkg --print-architecture)] \
https://repository.mullvad.net/deb/stable stable main" \
| sudo tee /etc/apt/sources.list.d/mullvad.list > /dev/null
sudo apt-get update -qq
sudo apt-get install -y mullvad-vpn || fatal "Failed to install mullvad-vpn"
;;
dnf)
log "Adding Mullvad official dnf repository..."
# addrepo with --from-repofile is the current correct syntax
sudo dnf config-manager addrepo \
--from-repofile=https://repository.mullvad.net/rpm/stable/mullvad.repo \
|| fatal "Failed to add Mullvad dnf repo"
sudo dnf install -y mullvad-vpn || fatal "Failed to install mullvad-vpn"
;;
zypper)
# No official zypper repo β download the RPM directly
log "Downloading Mullvad RPM for openSUSE..."
local mullvad_rpm
mullvad_rpm=$(mktemp --suffix=.rpm)
wget -q --trust-server-names \
"https://mullvad.net/download/app/rpm/latest" \
-O "$mullvad_rpm" \
|| { rm -f "$mullvad_rpm"; fatal "Failed to download Mullvad RPM"; }
sudo zypper --non-interactive install "$mullvad_rpm" \
|| { rm -f "$mullvad_rpm"; fatal "Failed to install Mullvad RPM"; }
rm -f "$mullvad_rpm"
;;
*)
error "Unsupported package manager: $PACKAGE_MANAGER"
warn "Install Mullvad manually from https://mullvad.net/download/vpn/linux then re-run this setup."
press_any_key
return
;;
esac
success "Mullvad VPN installed"
fi
echo
# ββ Step 2: Enable and start the Mullvad daemon βββββββββββββββββββββββββ
log "Enabling Mullvad daemon..."
sudo systemctl enable --now mullvad-daemon 2>/dev/null \
|| fatal "Failed to enable/start mullvad-daemon"
# Wait for daemon socket to be ready (up to 30s via systemd, then CLI check)
if ! systemctl is-active --quiet mullvad-daemon 2>/dev/null; then
log "Waiting for mullvad-daemon to become active..."
timeout 30 bash -c 'until systemctl is-active --quiet mullvad-daemon 2>/dev/null; do sleep 1; done' \
|| fatal "mullvad-daemon did not start in time"
fi
if ! mullvad status &>/dev/null; then
fatal "Mullvad daemon is not responding"
fi
success "Mullvad daemon running"
echo
local MULLVAD_BIN
MULLVAD_BIN=$(command -v mullvad)
# ββ Step 3: Account login ββββββββββββββββββββββββββββββββββββββββββββββββ
# Check positively for "Not logged in" β grep -qv would falsely pass on blank lines
if "$MULLVAD_BIN" account get 2>/dev/null | grep -q "Not logged in"; then
echo -e "${BOLD}${CYAN}Mullvad Account Login${NC}"
echo
echo " Your account number can be found at https://mullvad.net/account"
echo " Format: XXXX-XXXX-XXXX-XXXX"
echo
local login_success=false
local attempts=0
while [[ $login_success == false && $attempts -lt 3 ]]; do
read -p " Enter account number (or 'q' to skip): " -r account_number
echo
if [[ "$account_number" == "q" || -z "$account_number" ]]; then
warn "Skipped account login β headless setup cannot continue without login"
press_any_key
return
fi
if [[ ! "$account_number" =~ ^[0-9]{4}(-[0-9]{4}){3}$ ]]; then
warn "Invalid format β expected XXXX-XXXX-XXXX-XXXX"
attempts=$((attempts + 1))
continue
fi
log "Logging in..."
if "$MULLVAD_BIN" account login "$account_number" 2>&1; then
if ! "$MULLVAD_BIN" account get 2>/dev/null | grep -q "Not logged in"; then
login_success=true
success "Logged in to Mullvad account:"
"$MULLVAD_BIN" account get 2>/dev/null | sed 's/^/ /'
else
warn "Login command ran but account not verified β try again"
fi
else
warn "Login failed β check your account number and try again"
fi
# Use arithmetic assignment β ((attempts++)) exits 1 when attempts=0 under set -e
attempts=$((attempts + 1))
done
if [[ $login_success == false ]]; then
error "Could not log in after $attempts attempts"
press_any_key
return
fi
else
success "Already logged in to Mullvad account:"
"$MULLVAD_BIN" account get 2>/dev/null | sed 's/^/ /'
fi
echo
# ββ Step 4: Enable auto-connect βββββββββββββββββββββββββββββββββββββββββ
log "Enabling auto-connect on boot..."
"$MULLVAD_BIN" auto-connect set on >/dev/null 2>&1 || warn "Could not enable auto-connect"
success "Auto-connect enabled"
echo
# ββ Step 5: Connect ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
log "Establishing VPN connection..."
timeout 30 "$MULLVAD_BIN" connect --wait 2>/dev/null || true
local attempt=0
while [[ $attempt -lt 15 ]]; do
sleep 1
if "$MULLVAD_BIN" status 2>/dev/null | grep -iq "connected"; then
success "VPN connected successfully"
echo
log "Current status:"
"$MULLVAD_BIN" status | head -5 | sed 's/^/ /'
break
fi
attempt=$((attempt + 1))
done
if [[ $attempt -eq 15 ]]; then
warn "VPN connection may not be established yet β verify with: mullvad status"
fi
echo
success "Mullvad headless setup complete!"
log "Useful commands:"
echo " β’ Check status: mullvad status"
echo " β’ View account info: mullvad account get"
echo " β’ Disconnect: mullvad disconnect"
echo " β’ Reconnect: mullvad connect"
echo
press_any_key
}
builtin_install_omz_zsh_setup() {
show_section_banner "OH MY ZSH + FZF-TAB + FASTFETCH SETUP"
cat << EOF
${YELLOW}Description:${NC}
Full Zsh environment powered by Oh My Zsh, with fzf fuzzy tab completions,
fastfetch system info on launch, auto-suggestions, and syntax highlighting.
${YELLOW}This installer will:${NC}
β’ Install Zsh, fzf, git, and fastfetch
β’ Set Zsh as your default shell (chsh)
β’ Install Oh My Zsh (unattended β no interactive shell spawn)
β’ Clone fzf-tab, zsh-autosuggestions, and zsh-syntax-highlighting as OMZ plugins
β’ Configure ~/.zshrc with the correct plugin load order
β’ Optionally add fastfetch to terminal startup
${YELLOW}Plugin load order in .zshrc (enforced by OMZ):${NC}
1. compinit (handled automatically by Oh My Zsh)
2. fzf-tab (after compinit, before widget-wrapping plugins)
3. zsh-autosuggestions
4. zsh-syntax-highlighting
${YELLOW}Key bindings:${NC}
β’ Tab β fzf-tab fuzzy completion menu
β’ Ctrl+R β fzf fuzzy history search
β’ Ctrl+T β fzf fuzzy file search
${YELLOW}Requirements:${NC}
β’ curl and git for OMZ installation
β’ sudo access for package installation
β’ Log out / back in after chsh to activate default shell
EOF
read -p "Continue with Oh My Zsh + fzf-tab + fastfetch setup? [y/N]: " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
warn "Skipped Oh My Zsh setup"
return
fi
log "Starting Oh My Zsh + fzf-tab + fastfetch setup..."
echo
# ββ Step 1: Install base packages ββββββββββββββββββββββββββββββββββββββ
log "Installing zsh, fzf, git, and fastfetch..."
case "$PACKAGE_MANAGER" in
pacman)
sudo pacman -S --needed --noconfirm zsh fzf git fastfetch || \
fatal "Failed to install required packages"
;;
apt)
# fastfetch is not in repos before Debian 13 / Ubuntu 24.04
sudo apt-get install -y zsh fzf git || \
fatal "Failed to install required packages"
if apt-cache show fastfetch &>/dev/null 2>&1; then
sudo apt-get install -y fastfetch || warn "Failed to install fastfetch"
else
warn "fastfetch not available in your apt repos (requires Debian 13+ / Ubuntu 24.04+)"
warn "You can install it manually later from https://github.com/fastfetch-cli/fastfetch"
fi
;;
dnf)
sudo dnf install -y zsh fzf git fastfetch || \
fatal "Failed to install required packages"
;;
zypper)
sudo zypper install -y zsh fzf git fastfetch || \
fatal "Failed to install required packages"
;;
*)
error "Automatic installation not supported for $PACKAGE_MANAGER"
warn "Please install zsh, fzf, git, and fastfetch manually, then re-run"
press_any_key
return
;;
esac
success "Base packages installed"
echo
# ββ Step 2: Set Zsh as default shell ββββββββββββββββββββββββββββββββββ
local zsh_path
zsh_path="$(command -v zsh 2>/dev/null)" || fatal "zsh binary not found after install"
if [[ "$SHELL" == "$zsh_path" ]]; then
success "Zsh is already the default shell ($SHELL)"
else
log "Setting Zsh as default shell for $USER..."
chsh -s "$zsh_path" || warn "chsh failed β set manually: chsh -s $zsh_path"
success "Default shell set to $zsh_path"
warn "Log out and back in (or reboot) for the shell change to take effect"
fi
echo
# ββ Step 3: Install Oh My Zsh (headless) ββββββββββββββββββββββββββββββ
local omz_dir="$HOME/.oh-my-zsh"
if [[ -d "$omz_dir/.git" ]]; then