-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellSetup.sh
More file actions
executable file
·2580 lines (2284 loc) · 104 KB
/
Copy pathshellSetup.sh
File metadata and controls
executable file
·2580 lines (2284 loc) · 104 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
#!/bin/bash
# Cross-Platform Linux Setup Script (Stow, Zsh, OMZ, OMP, Tmux)
main() {
# --- CLI ARGS ---
# No arguments -> the interactive whiptail menu, exactly as before.
# --run ITEM1,ITEM2 runs those menu items' functions directly and skips the
# menu (non-interactive path, used by migrations). It also sets
# LPX_NO_MIGRATE so a migration that shells back into --run can't recurse
# into the migration runner. See README "--run flag".
local RUN_ITEMS=""
while [ $# -gt 0 ]; do
case "$1" in
--run)
if [ $# -lt 2 ] || [ -z "$2" ]; then
echo "--run needs a comma-separated item list, e.g. --run CLITOOLS,STOW" >&2
exit 2
fi
RUN_ITEMS="$2"; shift 2 ;;
--run=*)
RUN_ITEMS="${1#--run=}"
[ -z "$RUN_ITEMS" ] && { echo "--run needs a comma-separated item list" >&2; exit 2; }
shift ;;
-h|--help)
echo "Usage: $(basename "${BASH_SOURCE[0]:-shellSetup.sh}") [--run ITEM1,ITEM2,...]"
echo " (no args) interactive whiptail menu"
echo " --run run the named menu items directly, non-interactively"
exit 0 ;;
*)
echo "Unknown argument: $1 (try --help)" >&2; exit 2 ;;
esac
done
if [ -n "$RUN_ITEMS" ]; then
export LPX_NO_MIGRATE=1
fi
# 1. RECLAIM TTY: Prevent curl | bash from breaking interactive prompts.
# Skipped under --run: that path is non-interactive and /dev/tty may be absent.
if [ -z "$RUN_ITEMS" ]; then
exec < /dev/tty
fi
# --- COLORS & FORMATTING ---
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
msg_info() { echo -e "${BLUE}[*] $1${NC}"; }
msg_success() { echo -e "${GREEN}[+] $1${NC}"; }
msg_warn() { echo -e "${YELLOW}[!] $1${NC}"; }
msg_error() { echo -e "${RED}[x] $1${NC}"; }
msg_header() { echo -e "\n${CYAN}=== $1 ===${NC}"; }
# --- ASCII ART HEADER ---
clear
echo -e "${CYAN}"
cat << "EOF"
__ _ _ _ _
/ / (_)_ __ _ ___ | |_ ___(_) |_
/ / | | '_ \| | | \ \/ / '_ \ | __|
/ /___ | | | | | |_| |> <| |_) | | |_
\____/ |_|_| |_|\__,_/_/\_\ .__/|_|\__|
|_|
Created by Alex Ivantsov @Exploitacious
EOF
echo -e "${NC}"
if [ "$EUID" -eq 0 ]; then
msg_warn "Running as root."
echo
echo " This works on single-user / root-only systems (VMs, containers,"
echo " pen-testing rigs) but is NOT recommended on machines with regular"
echo " user accounts. As root, every package postinstall (pnpm/npm/curl|bash)"
echo " runs with full system privileges, and configs deploy to /root only."
echo
echo " If your machine has a standard user, exit and re-run as that user."
echo
msg_info "Continuing in 10s. Press 'q' or 'n' (then Enter) to abort, any other key to continue now."
local secs=10 reply=""
while [ "$secs" -gt 0 ]; do
printf "\r [%2ds] continuing as root... " "$secs"
if read -r -t 1 -n 1 reply 2>/dev/null; then
echo
case "$reply" in
q|Q|n|N) msg_error "Aborted by user."; exit 1 ;;
*) break ;;
esac
fi
secs=$((secs - 1))
done
echo
msg_success "Proceeding as root."
fi
# 2. CONTEXT AWARENESS & BOOTSTRAPPING
# If BASH_SOURCE is empty (piped) or doesn't have a .git folder, we are remote.
REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$PWD}")" &> /dev/null && pwd)"
if [ ! -d "$REPO_DIR/.git" ]; then
msg_info "Remote execution detected. Bootstrapping environment..."
sudo -v
# Validate/Install Git
if ! command -v git &> /dev/null; then
msg_warn "Git missing. Installing..."
if [ -f /etc/debian_version ]; then
sudo apt-get update && sudo apt-get install -y git
elif [ -f /etc/arch-release ]; then
sudo pacman -S --noconfirm --needed git
elif [ -f /etc/redhat-release ]; then
sudo dnf install -y git
else
msg_error "Unsupported OS for automatic Git installation. Install Git manually."
exit 1
fi
fi
# Configure Global Git Identity
EXISTING_GIT_NAME=$(git config --global user.name 2>/dev/null)
EXISTING_GIT_EMAIL=$(git config --global user.email 2>/dev/null)
if [ -n "$EXISTING_GIT_NAME" ] && [ -n "$EXISTING_GIT_EMAIL" ]; then
msg_info "Git identity already configured: $EXISTING_GIT_NAME <$EXISTING_GIT_EMAIL>"
else
msg_header "Configuring Git Global Identity"
read -p "Enter Git User Name: " GIT_NAME
read -p "Enter Git Email: " GIT_EMAIL
if [ -z "$GIT_NAME" ] || [ -z "$GIT_EMAIL" ]; then
msg_warn "Git identity not set — you'll need to configure it before committing."
else
git config --global user.name "$GIT_NAME"
git config --global user.email "$GIT_EMAIL"
msg_success "Git identity set to $GIT_NAME <$GIT_EMAIL>"
fi
fi
# Clone Repository
TARGET_DIR="$HOME/linuxploitacious"
if [ ! -d "$TARGET_DIR" ]; then
msg_info "Cloning repository to $TARGET_DIR..."
git clone https://github.com/Exploitacious/linuxploitacious.git "$TARGET_DIR"
else
msg_warn "Directory $TARGET_DIR already exists. Pulling latest..."
cd "$TARGET_DIR" && git pull
fi
# 3. THE HANDOFF: Execute the local script and kill the remote stream
msg_info "Handoff to local repository execution..."
cd "$TARGET_DIR" || exit 1
chmod +x shellSetup.sh
exec ./shellSetup.sh
fi
# --- LOCAL EXECUTION CONTINUES HERE ---
sudo -v
# --- AUTO-SYNC WITH UPSTREAM ---
# Re-runs from inside an existing clone never hit the bootstrap pull above,
# so they used to drift behind origin. Sync now if the tree is clean.
if [ -d "$REPO_DIR/.git" ]; then
cd "$REPO_DIR" || exit 1
# Mark the repo safe even when invoked under an unexpected uid
git config --global --add safe.directory "$REPO_DIR" 2>/dev/null || true
if [ -n "$(git status --porcelain 2>/dev/null)" ]; then
msg_warn "Working tree has uncommitted changes — skipping auto-pull."
msg_warn "Commit/stash and re-run to sync with upstream."
else
msg_info "Syncing with upstream (git pull --ff-only)..."
if git pull --ff-only 2>&1; then
msg_success "Repository up to date."
else
msg_warn "Auto-pull failed (network, diverged history, or missing upstream). Continuing with local copy."
fi
fi
fi
# --- RUN-ONCE MIGRATIONS ---
# After syncing with upstream, apply any pending fleet migrations so an
# already-provisioned box picks up changes that aren't plain config edits.
# LPX_NO_MIGRATE=1 skips this — the --run path sets it so a migration that
# re-invokes this script can't recurse back into the runner. The stowed
# lpx-migrate wins if present; on a first run (before STOW) fall back to the
# copy in the freshly-pulled checkout. See migrations/README.md.
if [ "${LPX_NO_MIGRATE:-0}" != "1" ]; then
local _lpx_migrate=""
if command -v lpx-migrate >/dev/null 2>&1; then
_lpx_migrate="lpx-migrate"
elif [ -x "$REPO_DIR/scripts/.local/bin/lpx-migrate" ]; then
_lpx_migrate="$REPO_DIR/scripts/.local/bin/lpx-migrate"
fi
if [ -n "$_lpx_migrate" ]; then
"$_lpx_migrate" || msg_warn "Migrations pending or failed (see output above)."
fi
fi
# --- OS DETECTION ---
if [ -f /etc/os-release ]; then
. /etc/os-release
OS_ID=$ID
OS_LIKE=$ID_LIKE
else
msg_error "Cannot detect Operating System."
exit 1
fi
# Verify whiptail
if [[ "$OS_ID" == "debian" || "$OS_ID" == "ubuntu" || "$OS_ID" == "kali" || "$OS_LIKE" == *"debian"* ]]; then
if ! command -v whiptail &> /dev/null; then sudo apt-get update && sudo apt-get install -y whiptail; fi
elif [[ "$OS_ID" == "arch" || "$OS_LIKE" == *"arch"* ]]; then
if ! command -v whiptail &> /dev/null; then sudo pacman -S --noconfirm --needed libnewt; fi
elif [[ "$OS_ID" == "fedora" || "$OS_LIKE" == *"fedora"* ]]; then
if ! command -v whiptail &> /dev/null; then sudo dnf install -y newt; fi
fi
# --- STOW PACKAGES (single source of truth; deploy_stow + setup_root_profile both reference this) ---
# systemd/ lands user unit templates in ~/.config/systemd/user (currently the
# herdr@.service headless-server template); stow folds into the existing dir.
readonly STOW_PACKAGES=("fastfetch" "omp" "rustscan" "scripts" "tmux" "zsh" "bash" "btop" "superfile" "bat" "systemd" "herdr")
# --- PACKAGE MANAGERS ---
ensure_utf8_locale() {
# Nerd Font glyphs (and most modern TUIs) require a UTF-8 locale.
# Fresh containers / minimal images often default to LANG=C, which
# renders icons as boxes or '?'. Make sure en_US.UTF-8 is generated
# and set as the system default.
msg_header "Ensuring UTF-8 locale (en_US.UTF-8)"
local CURRENT_LANG="${LANG:-C}"
if [[ "$CURRENT_LANG" == *"UTF-8"* || "$CURRENT_LANG" == *"utf8"* ]] \
&& locale -a 2>/dev/null | grep -qiE '^en_US\.utf-?8$'; then
msg_info "UTF-8 locale already active ($CURRENT_LANG). Skipping."
return 0
fi
if [[ "$OS_ID" == "debian" || "$OS_ID" == "ubuntu" || "$OS_ID" == "kali" || "$OS_LIKE" == *"debian"* ]]; then
sudo apt-get install -y locales >/dev/null 2>&1 || msg_warn "Could not install 'locales' package."
if [ -f /etc/locale.gen ]; then
sudo sed -i 's/^# *en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen
fi
sudo locale-gen en_US.UTF-8 >/dev/null 2>&1
sudo update-locale LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8
elif [[ "$OS_ID" == "arch" || "$OS_LIKE" == *"arch"* ]]; then
if [ -f /etc/locale.gen ]; then
sudo sed -i 's/^# *en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen
fi
sudo locale-gen >/dev/null 2>&1
echo "LANG=en_US.UTF-8" | sudo tee /etc/locale.conf >/dev/null
elif [[ "$OS_ID" == "fedora" || "$OS_LIKE" == *"fedora"* ]]; then
sudo dnf install -y glibc-langpack-en >/dev/null 2>&1 || msg_warn "Could not install 'glibc-langpack-en'."
if command -v localectl &> /dev/null; then
sudo localectl set-locale LANG=en_US.UTF-8 2>/dev/null || \
echo "LANG=en_US.UTF-8" | sudo tee /etc/locale.conf >/dev/null
else
echo "LANG=en_US.UTF-8" | sudo tee /etc/locale.conf >/dev/null
fi
else
msg_warn "Unknown OS family; skipping locale setup."
return 0
fi
# Apply to the current shell so the remainder of this script (and any
# fc-cache / font rendering checks) sees the UTF-8 locale immediately.
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
msg_success "UTF-8 locale configured. Re-login for it to take effect in new shells."
}
install_nerd_fonts() {
local FONT_DIR="/usr/local/share/fonts/NerdFonts"
if [ -d "$FONT_DIR" ] && fc-list | grep -qi "Nerd Font"; then
msg_info "Nerd Fonts already installed. Skipping."
return 0
fi
msg_header "Installing Nerd Fonts (MesloLG - recommended for Oh My Posh)"
sudo mkdir -p "$FONT_DIR"
local MESLO_URL
MESLO_URL=$(curl -s https://api.github.com/repos/ryanoasis/nerd-fonts/releases/latest | jq -r '.assets[] | select(.name == "Meslo.zip") | .browser_download_url')
if [[ -z "$MESLO_URL" || "$MESLO_URL" == "null" ]]; then
msg_error "Could not find Meslo Nerd Font download URL."
return 1
fi
msg_info "Downloading MesloLG Nerd Font..."
if wget -q "$MESLO_URL" -O /tmp/meslo-nf.zip; then
sudo unzip -o /tmp/meslo-nf.zip -d "$FONT_DIR/" > /dev/null 2>&1
rm /tmp/meslo-nf.zip
sudo fc-cache -f
msg_success "MesloLG Nerd Font installed. Set your terminal font to 'MesloLGM Nerd Font' for full symbol support."
else
msg_error "Failed to download Meslo Nerd Font."
return 1
fi
}
install_debian_base() {
msg_header "Configuring Debian/Kali base"
ensure_utf8_locale
sudo apt-get update
# Ensure software-properties-common is installed first
sudo apt-get install -y software-properties-common
msg_info "Upgrading system..."
sudo apt-get update && sudo apt-get full-upgrade -y
# Install base packages (stow, jq, etc.) first to ensure they exist for later steps
# chafa renders images as terminal blocks — lets a headless box preview
# screenshots/diagrams over SSH (grabit --queue uses it, and degrades to a
# plain listing when absent, so this is a nicety rather than a dependency).
local DEBIAN_PKGS=(zsh stow git curl unzip tmux fzf gnupg2 xclip ffmpeg nmap build-essential wget jq btop tree chafa)
if [[ "$OS_ID" == "kali" ]]; then DEBIAN_PKGS+=(kali-win-kex); fi
msg_info "Installing packages: ${DEBIAN_PKGS[*]}"
sudo apt-get install -y "${DEBIAN_PKGS[@]}"
# Attempt Fastfetch installation separately to avoid blocking other packages
if ! command -v fastfetch &> /dev/null; then
msg_info "Attempting to install Fastfetch..."
# 1. Try PPA for Ubuntu
if [[ "$OS_ID" == "ubuntu" ]]; then
msg_info "Trying Fastfetch PPA..."
if ! grep -rq "zhangsongcui3336/fastfetch" /etc/apt/sources.list.d/ 2>/dev/null; then
sudo add-apt-repository -y ppa:zhangsongcui3336/fastfetch 2>/dev/null && \
sudo apt-get update && sudo apt-get install -y fastfetch
else
sudo apt-get install -y fastfetch
fi
# 2. Try direct install (available in newer Debian/Kali)
else
sudo apt-get install -y fastfetch 2>/dev/null
fi
# 3. Manual Fallback using jq (which we just installed)
if ! command -v fastfetch &> /dev/null; then
msg_warn "Fastfetch not found in repos. Attempting robust manual install..."
local FASTFETCH_URL
FASTFETCH_URL=$(curl -s https://api.github.com/repos/fastfetch-cli/fastfetch/releases/latest | jq -r '.assets[] | select(.name | test("linux-amd64.deb")) | .browser_download_url' | head -n 1)
if [[ -n "$FASTFETCH_URL" && "$FASTFETCH_URL" != "null" ]]; then
msg_info "Downloading Fastfetch from: $FASTFETCH_URL"
if wget -q "$FASTFETCH_URL" -O /tmp/fastfetch.deb; then
sudo apt-get install -y /tmp/fastfetch.deb
rm /tmp/fastfetch.deb
msg_success "Fastfetch installed manually."
else
msg_error "Failed to download Fastfetch .deb"
fi
else
msg_error "Could not find Fastfetch download URL."
fi
fi
fi
# Install Nerd Fonts for Oh My Posh / terminal symbol support
install_nerd_fonts
msg_success "Base packages installed."
}
install_arch_base() {
msg_header "Configuring Arch base"
ensure_utf8_locale
sudo pacman -Syu --noconfirm
local ARCH_PKGS=(zsh stow git curl wget unzip tmux fzf fastfetch gnupg2 xclip ffmpeg nmap base-devel jq btop tree)
msg_info "Installing packages: ${ARCH_PKGS[*]}"
sudo pacman -S --noconfirm --needed "${ARCH_PKGS[@]}"
if ! command -v yay &> /dev/null; then
msg_info "Installing yay..."
git clone https://aur.archlinux.org/yay.git /tmp/yay
cd /tmp/yay && makepkg -si --noconfirm
cd "$REPO_DIR" || exit 1
fi
# Install Nerd Fonts for Oh My Posh / terminal symbol support
install_nerd_fonts
msg_success "Base packages installed."
}
install_fedora_base() {
msg_header "Configuring Fedora/RHEL base"
ensure_utf8_locale
sudo dnf upgrade -y
local FEDORA_PKGS=(zsh stow git curl unzip tmux fzf gnupg2 xclip nmap wget jq btop tree)
msg_info "Installing packages: ${FEDORA_PKGS[*]}"
sudo dnf install -y "${FEDORA_PKGS[@]}"
# Install Development Tools group (equivalent of build-essential)
msg_info "Installing Development Tools group..."
sudo dnf group install -y "Development Tools"
# FFmpeg: try ffmpeg-free (Fedora default repos) then ffmpeg (RPM Fusion)
if ! command -v ffmpeg &> /dev/null; then
sudo dnf install -y ffmpeg-free 2>/dev/null || sudo dnf install -y ffmpeg 2>/dev/null || \
msg_warn "FFmpeg unavailable in current repos. Enable RPM Fusion for full codec support."
fi
# Attempt Fastfetch installation separately
if ! command -v fastfetch &> /dev/null; then
msg_info "Attempting to install Fastfetch..."
sudo dnf install -y fastfetch 2>/dev/null
# Manual Fallback using jq (which we just installed)
if ! command -v fastfetch &> /dev/null; then
msg_warn "Fastfetch not found in repos. Attempting manual install..."
local FASTFETCH_URL
FASTFETCH_URL=$(curl -s https://api.github.com/repos/fastfetch-cli/fastfetch/releases/latest | jq -r '.assets[] | select(.name | test("linux-amd64\\.rpm$")) | .browser_download_url' | head -n 1)
if [[ -n "$FASTFETCH_URL" && "$FASTFETCH_URL" != "null" ]]; then
msg_info "Downloading Fastfetch from: $FASTFETCH_URL"
if wget -q "$FASTFETCH_URL" -O /tmp/fastfetch.rpm; then
sudo dnf install -y /tmp/fastfetch.rpm
rm /tmp/fastfetch.rpm
msg_success "Fastfetch installed manually."
else
msg_error "Failed to download Fastfetch .rpm"
fi
else
msg_error "Could not find Fastfetch download URL."
fi
fi
fi
# Install Nerd Fonts for Oh My Posh / terminal symbol support
install_nerd_fonts
msg_success "Base packages installed."
}
install_cloudflared() {
msg_header "Installing cloudflared (Cloudflare Tunnel + Access)"
if command -v cloudflared >/dev/null 2>&1; then
msg_info "cloudflared already installed: $(cloudflared --version 2>&1 | head -1)"
return 0
fi
local CF_ARCH="amd64"
case "$(uname -m)" in
aarch64|arm64) CF_ARCH="arm64" ;;
x86_64) CF_ARCH="amd64" ;;
esac
local CF_TMP
CF_TMP=$(mktemp -d)
msg_info "Downloading cloudflared-linux-${CF_ARCH} from GitHub releases..."
if ! curl -fsSL -o "$CF_TMP/cloudflared" \
"https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-${CF_ARCH}"; then
msg_error "cloudflared download failed."
rm -rf "$CF_TMP"
return 1
fi
sudo install -m 0755 "$CF_TMP/cloudflared" /usr/local/bin/cloudflared
rm -rf "$CF_TMP"
msg_success "cloudflared installed: $(cloudflared --version 2>&1 | head -1)"
}
install_superfile() {
msg_header "Installing superfile (default terminal file manager)"
if command -v spf >/dev/null 2>&1; then
msg_info "superfile already installed: $(spf --version 2>&1 | head -1)"
return 0
fi
local SPF_ARCH="amd64"
case "$(uname -m)" in
aarch64|arm64) SPF_ARCH="arm64" ;;
x86_64) SPF_ARCH="amd64" ;;
esac
local SPF_TMP SPF_TAG
SPF_TMP=$(mktemp -d)
# Asset names embed the tag (superfile-linux-v1.6.0-amd64.tar.gz), so resolve it first
SPF_TAG=$(curl -s https://api.github.com/repos/yorukot/superfile/releases/latest | jq -r '.tag_name')
if [ -z "$SPF_TAG" ] || [ "$SPF_TAG" = "null" ]; then
msg_error "Could not resolve latest superfile release tag."
rm -rf "$SPF_TMP"
return 1
fi
msg_info "Downloading superfile-linux-${SPF_TAG}-${SPF_ARCH} from GitHub releases..."
if ! curl -fsSL -o "$SPF_TMP/spf.tar.gz" \
"https://github.com/yorukot/superfile/releases/latest/download/superfile-linux-${SPF_TAG}-${SPF_ARCH}.tar.gz"; then
msg_error "superfile download failed."
rm -rf "$SPF_TMP"
return 1
fi
tar -xzf "$SPF_TMP/spf.tar.gz" -C "$SPF_TMP"
# find the binary rather than hardcoding the archive's internal layout
local SPF_BIN
SPF_BIN=$(find "$SPF_TMP" -type f -name spf | head -n 1)
if [ -z "$SPF_BIN" ]; then
msg_error "spf binary not found inside superfile release archive."
rm -rf "$SPF_TMP"
return 1
fi
sudo install -m 0755 "$SPF_BIN" /usr/local/bin/spf
rm -rf "$SPF_TMP"
msg_success "superfile installed: $(spf --version 2>&1 | head -1)"
}
install_herdr() {
# Pin the version deliberately: herdr ships breaking changes roughly weekly,
# so tracking "latest" would drift the fleet unpredictably. Bump this by a
# considered migration (edit here, add a migrations/*.sh that re-runs the
# installer), never automatically.
local HERDR_VERSION="0.8.2"
# Release-asset checksums, pinned alongside the version (recompute on bump:
# curl -fsSL <asset-url> | sha256sum).
local HERDR_SHA256_x86_64="976150a14d490c94b243ea2e1a7eb2dfb67f12e36b182db90936f6728e6aecf4"
local HERDR_SHA256_aarch64="f55610658e1c2e0d2aaef730b4b2ab885f7f8ba00285ab372bfb14f2e3d5b40d"
msg_header "Installing herdr ${HERDR_VERSION} (multiplexer for AI coding agents)"
mkdir -p "$HOME/.local/bin"
# Arch-aware release asset (herdr-linux-x86_64 / herdr-linux-aarch64).
local HERDR_ARCH
case "$(uname -m)" in
x86_64) HERDR_ARCH="x86_64" ;;
aarch64|arm64) HERDR_ARCH="aarch64" ;;
*) msg_error "Unsupported arch '$(uname -m)' for herdr — skipping."; return 1 ;;
esac
# User-level install (no sudo): herdr is a per-user tool and its per-session
# server runs under the login user, so it belongs in ~/.local/bin.
local HERDR_BIN="$HOME/.local/bin/herdr" need_install=1 cur=""
if [ -x "$HERDR_BIN" ]; then
cur="$("$HERDR_BIN" --version 2>/dev/null | awk '{print $2}')"
if [ "$cur" = "$HERDR_VERSION" ]; then
msg_info "herdr ${HERDR_VERSION} already installed. Skipping binary."
need_install=0
else
msg_info "herdr ${cur:-unknown} present — upgrading (${cur:-unknown} -> ${HERDR_VERSION})."
fi
fi
if [ "$need_install" -eq 1 ]; then
local HERDR_TMP url
HERDR_TMP="$(mktemp -d)"
url="https://github.com/herdrdev/herdr/releases/download/v${HERDR_VERSION}/herdr-linux-${HERDR_ARCH}"
msg_info "Downloading herdr-linux-${HERDR_ARCH} v${HERDR_VERSION}..."
if ! curl -fsSL -o "$HERDR_TMP/herdr" "$url"; then
msg_error "herdr download failed: $url"
rm -rf "$HERDR_TMP"
return 1
fi
local want got
want="$(eval echo "\$HERDR_SHA256_${HERDR_ARCH}")"
got="$(sha256sum "$HERDR_TMP/herdr" | awk '{print $1}')"
if [ "$got" != "$want" ]; then
msg_error "herdr checksum mismatch (got ${got:0:12}…, want ${want:0:12}…) — refusing to install."
rm -rf "$HERDR_TMP"
return 1
fi
install -m 0755 "$HERDR_TMP/herdr" "$HERDR_BIN"
rm -rf "$HERDR_TMP"
if "$HERDR_BIN" --version >/dev/null 2>&1; then
msg_success "herdr installed: $("$HERDR_BIN" --version 2>&1 | head -1)"
else
msg_warn "herdr installed to $HERDR_BIN but --version check failed."
fi
fi
# zsh completion. No other tool here ships a generated-completions dir
# (zoxide/pyenv/nvm init at runtime; extras come from the zsh-completions OMZ
# plugin), so herdr generates a file into a standard fpath dir. .zshrc adds
# that dir to fpath (guarded, before compinit). Runs on both the fresh and
# idempotent-skip paths so a box provisioned before this step picks it up.
local comp_dir="$HOME/.local/share/zsh/completions"
mkdir -p "$comp_dir"
if "$HERDR_BIN" completion zsh > "$comp_dir/_herdr" 2>/dev/null && [ -s "$comp_dir/_herdr" ]; then
msg_success "herdr zsh completion written to $comp_dir/_herdr"
else
rm -f "$comp_dir/_herdr"
msg_warn "herdr completion generation failed — skipping."
fi
# NOTE: herdr is intentionally NOT in the default/auto-selected menu set and
# has NO fleet migration — activating it across the fleet is a pending
# operator decision, so it stays opt-in (menu item OFF) until that call.
}
install_cli_tools() {
msg_header "Installing Modern CLI Tools"
mkdir -p "$HOME/.local/bin"
# Two arch spellings: dpkg (amd64/arm64) for .debs, rust triples
# (x86_64/aarch64) for the rust-built release tarballs, and the
# lazygit/lazydocker "x86_64/arm64" spelling.
local uname_m deb_arch rust_arch lg_arch
uname_m="$(uname -m)"
case "$uname_m" in
x86_64) deb_arch="amd64"; rust_arch="x86_64"; lg_arch="x86_64" ;;
aarch64|arm64) deb_arch="arm64"; rust_arch="aarch64"; lg_arch="arm64" ;;
*) msg_warn "Unrecognized arch '$uname_m' — release-binary tools will be skipped."; deb_arch=""; rust_arch=""; lg_arch="" ;;
esac
local is_debian=0
if [[ "$OS_ID" == "debian" || "$OS_ID" == "ubuntu" || "$OS_ID" == "kali" || "$OS_LIKE" == *"debian"* ]]; then
is_debian=1
fi
local is_arch=0
if [[ "$OS_ID" == "arch" || "$OS_LIKE" == *"arch"* ]]; then
is_arch=1
fi
# Is a package actually installable from the configured apt repos? Lets us
# queue only what exists here (eza/git-delta are absent on older Debian).
_apt_has() {
local c; c="$(apt-cache policy "$1" 2>/dev/null | awk '/Candidate:/{print $2}')"
[ -n "$c" ] && [ "$c" != "(none)" ]
}
_cli_latest_tag() {
curl -fsSL "https://api.github.com/repos/$1/releases/latest" 2>/dev/null | jq -r '.tag_name // empty'
}
# Fetch a release tarball, install the named binary, verify it runs.
_cli_install_tarball() {
local repo="$1" tag="$2" asset="$3" bin="$4" dest="$5" tmp found
tmp="$(mktemp -d)"
msg_info "Downloading $asset ..."
if ! curl -fsSL -o "$tmp/dl.tar.gz" "https://github.com/$repo/releases/download/$tag/$asset"; then
msg_warn "Download failed: $repo $asset"; rm -rf "$tmp"; return 1
fi
if ! tar -xzf "$tmp/dl.tar.gz" -C "$tmp" 2>/dev/null; then
msg_error "Extract failed: $asset"; rm -rf "$tmp"; return 1
fi
found="$(find "$tmp" -type f -name "$bin" | head -n1)"
if [ -z "$found" ]; then
msg_error "$bin not found inside $asset"; rm -rf "$tmp"; return 1
fi
sudo install -m 0755 "$found" "$dest/$bin"
rm -rf "$tmp"
if "$dest/$bin" --version >/dev/null 2>&1; then
msg_success "$bin installed: $("$dest/$bin" --version 2>&1 | head -1)"
else
msg_warn "$bin installed to $dest but --version check failed."
fi
}
# --- apt-provided tools (Debian family) ---
if [ "$is_debian" -eq 1 ]; then
msg_info "Refreshing apt index..."
sudo apt-get update -qq || msg_warn "apt-get update failed — continuing with the current index."
# command already present? skip. else queue its package if the repo has it.
local -a apt_pkgs=()
command -v batcat >/dev/null 2>&1 || command -v bat >/dev/null 2>&1 || { _apt_has bat && apt_pkgs+=(bat); }
command -v fdfind >/dev/null 2>&1 || command -v fd >/dev/null 2>&1 || { _apt_has fd-find && apt_pkgs+=(fd-find); }
command -v rg >/dev/null 2>&1 || { _apt_has ripgrep && apt_pkgs+=(ripgrep); }
# jq: the GitHub-release installs below parse the API with it. On a fresh
# box the CLITOOLS migration can run before BASE installs jq — own the dep.
command -v jq >/dev/null 2>&1 || { _apt_has jq && apt_pkgs+=(jq); }
command -v zoxide >/dev/null 2>&1 || { _apt_has zoxide && apt_pkgs+=(zoxide); }
command -v tldr >/dev/null 2>&1 || command -v tealdeer >/dev/null 2>&1 || { _apt_has tealdeer && apt_pkgs+=(tealdeer); }
command -v eza >/dev/null 2>&1 || { _apt_has eza && apt_pkgs+=(eza); }
command -v inotifywait >/dev/null 2>&1 || { _apt_has inotify-tools && apt_pkgs+=(inotify-tools); }
if [ "${#apt_pkgs[@]}" -gt 0 ]; then
msg_info "Installing via apt: ${apt_pkgs[*]}"
if ! sudo apt-get install -y "${apt_pkgs[@]}"; then
# Fail loudly: a half-installed set (or no sudo under --run) must not
# look like success — the migration's fail-closed path depends on it.
msg_error "apt install failed for: ${apt_pkgs[*]}"
return 1
fi
else
msg_info "All apt-provided CLI tools already present or unavailable."
fi
command -v eza >/dev/null 2>&1 || msg_warn "eza unavailable via apt on this release — tree aliases (lt/lta) stay inactive until it is installed."
# --- pacman-provided tools (Arch family) ---
# Arch ships all of these in the official repos, including the lazygit/
# git-delta/dust/jq that the Debian path pulls from GitHub releases below.
# So pacman is the single install source here; the release-binary sections
# downstream find delta/dust/lazygit already present (command -v checks) and
# skip. lazydocker is AUR-only, so it goes through yay.
elif [ "$is_arch" -eq 1 ]; then
msg_info "Installing via pacman: bat eza fd zoxide ripgrep tealdeer inotify-tools lazygit git-delta dust jq"
# --needed makes this idempotent (already-present packages are skipped).
# Fail loudly, same contract as the apt path: a half-installed set (or no
# sudo under --run) must not look like success — the CLITOOLS migration's
# fail-closed path depends on a non-zero exit here.
if ! sudo pacman -S --noconfirm --needed bat eza fd zoxide ripgrep tealdeer inotify-tools lazygit git-delta dust jq; then
msg_error "pacman install failed for the CLI toolset."
return 1
fi
# lazydocker is not in the official repos — build it from the AUR via yay.
# yay is optional on a box, so its absence is a skip-with-note, never a
# hard fail. yay runs unprivileged (makepkg refuses root) and calls sudo
# for the pacman step itself.
if command -v yay >/dev/null 2>&1; then
yay -S --noconfirm --needed lazydocker || msg_warn "lazydocker (AUR) build failed — skipping."
else
msg_info "yay not found — skipping lazydocker (AUR). Install yay, then re-run CLITOOLS to add it."
fi
else
msg_warn "Non-Debian system: skipping apt tools (bat/fd/ripgrep/zoxide/tealdeer/eza/inotify-tools). Install them with your package manager; the release-binary tools below still run."
fi
# --- Debian binary-name reconciliation: batcat -> bat, fdfind -> fd ---
if command -v batcat >/dev/null 2>&1 && ! command -v bat >/dev/null 2>&1; then
ln -sf "$(command -v batcat)" "$HOME/.local/bin/bat"
msg_success "Linked bat -> $(command -v batcat)"
fi
if command -v fdfind >/dev/null 2>&1 && ! command -v fd >/dev/null 2>&1; then
ln -sf "$(command -v fdfind)" "$HOME/.local/bin/fd"
msg_success "Linked fd -> $(command -v fdfind)"
fi
# --- delta (git-delta): apt if available, else latest GitHub .deb ---
if command -v delta >/dev/null 2>&1; then
msg_info "delta already installed: $(delta --version 2>&1 | head -1)"
elif [ "$is_debian" -eq 1 ] && _apt_has git-delta; then
sudo apt-get install -y git-delta && msg_success "delta installed via apt."
elif [ -n "$deb_arch" ] && command -v jq >/dev/null 2>&1; then
local dtag dtmp
dtag="$(_cli_latest_tag dandavison/delta)"
if [ -n "$dtag" ]; then
dtmp="$(mktemp -d)"
if curl -fsSL -o "$dtmp/delta.deb" "https://github.com/dandavison/delta/releases/download/${dtag}/git-delta_${dtag}_${deb_arch}.deb"; then
sudo apt-get install -y "$dtmp/delta.deb" && msg_success "delta installed: $(delta --version 2>&1 | head -1)"
else
msg_warn "delta .deb download failed — skipping."
fi
rm -rf "$dtmp"
else
msg_warn "Could not resolve latest delta release — skipping."
fi
else
msg_warn "delta skipped (needs jq + a known arch, or apt git-delta)."
fi
# --- dust: GitHub release binary, arch-aware; fall back to apt ncdu ---
if command -v dust >/dev/null 2>&1; then
msg_info "dust already installed: $(dust --version 2>&1 | head -1)"
elif [ -n "$rust_arch" ] && command -v jq >/dev/null 2>&1; then
local utag
utag="$(_cli_latest_tag bootandy/dust)"
if [ -z "$utag" ] || ! _cli_install_tarball bootandy/dust "$utag" "dust-${utag}-${rust_arch}-unknown-linux-gnu.tar.gz" dust /usr/local/bin; then
msg_warn "dust install failed — falling back to apt ncdu."
[ "$is_debian" -eq 1 ] && { sudo apt-get install -y ncdu || msg_warn "ncdu fallback also failed."; }
fi
else
msg_warn "dust skipped (needs jq + a known arch); trying apt ncdu instead."
[ "$is_debian" -eq 1 ] && { sudo apt-get install -y ncdu || true; }
fi
# --- lazygit + lazydocker: GitHub release tarballs -> /usr/local/bin ---
# (Follows install_superfile: sudo install -m 0755 into /usr/local/bin.)
if [ -n "$lg_arch" ] && command -v jq >/dev/null 2>&1; then
if command -v lazygit >/dev/null 2>&1; then
msg_info "lazygit already installed: $(lazygit --version 2>&1 | head -1)"
else
local lgtag lgver
lgtag="$(_cli_latest_tag jesseduffield/lazygit)"; lgver="${lgtag#v}"
[ -n "$lgtag" ] && _cli_install_tarball jesseduffield/lazygit "$lgtag" "lazygit_${lgver}_linux_${lg_arch}.tar.gz" lazygit /usr/local/bin \
|| msg_warn "lazygit install skipped/failed."
fi
if command -v lazydocker >/dev/null 2>&1; then
msg_info "lazydocker already installed: $(lazydocker --version 2>&1 | head -1)"
else
local ldtag ldver
ldtag="$(_cli_latest_tag jesseduffield/lazydocker)"; ldver="${ldtag#v}"
[ -n "$ldtag" ] && _cli_install_tarball jesseduffield/lazydocker "$ldtag" "lazydocker_${ldver}_Linux_${lg_arch}.tar.gz" lazydocker /usr/local/bin \
|| msg_warn "lazydocker install skipped/failed."
fi
else
msg_warn "lazygit/lazydocker skipped (needs jq + a known arch)."
fi
# --- git + delta wiring (idempotent; only when delta is present) ---
if command -v delta >/dev/null 2>&1; then
msg_info "Wiring git to use delta..."
git config --global core.pager delta
git config --global interactive.diffFilter "delta --color-only"
git config --global delta.navigate true
git config --global delta.line-numbers true
# zdiff3 shows the common ancestor in conflicts — easier merges with delta.
git config --global merge.conflictStyle zdiff3
msg_success "git delta integration configured."
fi
# --- bat theme cache ---
# bat only registers a custom theme after `bat cache --build`. Run it once
# the Catppuccin Mocha .tmTheme is stowed (bat/ package). On a CLITOOLS-only
# run before STOW the theme isn't there yet; it activates on the next STOW.
local bat_bin=""
command -v bat >/dev/null 2>&1 && bat_bin="bat"
[ -z "$bat_bin" ] && command -v batcat >/dev/null 2>&1 && bat_bin="batcat"
if [ -n "$bat_bin" ]; then
local bat_theme_dir="${XDG_CONFIG_HOME:-$HOME/.config}/bat/themes" t found_theme=0
for t in "$bat_theme_dir"/*.tmTheme; do [ -e "$t" ] && { found_theme=1; break; }; done
if [ "$found_theme" -eq 1 ]; then
msg_info "Building bat theme cache..."
"$bat_bin" cache --build >/dev/null 2>&1 && msg_success "bat theme cache built (Catppuccin Mocha)." || msg_warn "bat cache --build failed."
else
msg_info "bat theme not stowed yet — run STOW, then re-run CLITOOLS to activate Catppuccin Mocha."
fi
fi
msg_success "Modern CLI tools step complete."
}
# The `vpn` helper stows to ~/.local/bin on every box, but it is only a
# wrapper — without the vendor CLI behind it, `vpn` dies with exit 127.
# This installs the binary so the stowed helper and the vpnoff/vpnstatus
# aliases actually work on a fresh machine.
install_nordvpn() {
msg_header "Installing NordVPN CLI (backs the stowed 'vpn' helper)"
if command -v nordvpn >/dev/null 2>&1; then
msg_info "NordVPN already installed: $(nordvpn --version 2>&1 | head -1)"
return 0
fi
local NORD_TMP
NORD_TMP=$(mktemp -d)
msg_info "Downloading official NordVPN installer..."
if ! curl -fsSL -o "$NORD_TMP/install.sh" \
"https://downloads.nordcdn.com/apps/linux/install.sh"; then
msg_error "NordVPN installer download failed."
rm -rf "$NORD_TMP"
return 1
fi
# -n is the vendor script's non-interactive flag (it passes -y to
# apt/yum/dnf and -n to zypper). It exposes no recommends switch, and its
# internal apt-get calls run under its own sudo, so APT_CONFIG from here
# would not reach them: on Debian hosts the nordvpn package's
# "Recommends: nordvpn-gui" is pulled unless the box sets
# APT::Install-Recommends "0" globally. Not worth fighting the vendor
# script — headless nodes can drop the GUI afterwards. See README "VPN".
if ! sh "$NORD_TMP/install.sh" -n; then
msg_error "NordVPN install failed. Retry: curl -fsSL https://downloads.nordcdn.com/apps/linux/install.sh | sh -s -- -n"
rm -rf "$NORD_TMP"
return 1
fi
rm -rf "$NORD_TMP"
# The installer adds the invoking user itself; this is the idempotent
# belt-and-braces for the SUDO_USER case and for re-runs. Group membership
# is what grants access to the nordvpnd socket.
if [ "$EUID" -ne 0 ]; then
local TARGET_USER="${SUDO_USER:-$USER}"
if ! id -nG "$TARGET_USER" | tr ' ' '\n' | grep -qx nordvpn; then
sudo usermod -aG nordvpn "$TARGET_USER"
msg_warn "Added $TARGET_USER to 'nordvpn' group. Log out + back in (or 'newgrp nordvpn') for it to take effect."
else
msg_info "$TARGET_USER already in nordvpn group."
fi
fi
msg_success "NordVPN CLI installed: $(nordvpn --version 2>&1 | head -1)"
# Login is deliberately never scripted — it is interactive (browser + MFA)
# or needs a per-operator access token. One manual step per box.
msg_info "Run 'nordvpn login' to finish (headless: 'nordvpn login --token <TOKEN>')."
}
install_brave() {
msg_header "Installing Brave Browser"
if [[ "$OS_ID" == "debian" || "$OS_ID" == "ubuntu" || "$OS_ID" == "kali" || "$OS_LIKE" == *"debian"* ]]; then
sudo apt-get install -y curl
sudo curl -fsSLo /usr/share/keyrings/brave-browser-archive-keyring.gpg https://brave-browser-apt-release.s3.brave.com/brave-browser-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/brave-browser-archive-keyring.gpg] https://brave-browser-apt-release.s3.brave.com/ stable main" | sudo tee /etc/apt/sources.list.d/brave-browser-release.list > /dev/null
sudo apt-get update && sudo apt-get install -y brave-browser
elif [[ "$OS_ID" == "arch" || "$OS_LIKE" == *"arch"* ]]; then
if ! command -v yay &> /dev/null; then
msg_error "yay not found. Please install yay first or run BASE option."
return 1
fi
yay -S --noconfirm brave-bin
elif [[ "$OS_ID" == "fedora" || "$OS_LIKE" == *"fedora"* ]]; then
sudo rpm --import https://brave-browser-rpm-release.s3.brave.com/brave-core.asc
cat << 'BRAVEREPO' | sudo tee /etc/yum.repos.d/brave-browser.repo > /dev/null
[brave-browser]
name=Brave Browser
baseurl=https://brave-browser-rpm-release.s3.brave.com/$basearch
enabled=1
gpgcheck=1
gpgkey=https://brave-browser-rpm-release.s3.brave.com/brave-core.asc
BRAVEREPO
sudo dnf install -y brave-browser
fi
msg_success "Brave Browser installed."
}
setup_root_profile() {
msg_header "Setting up Root Profile"
if [ "$EUID" -eq 0 ]; then
msg_error "This option must be run as a regular user, not as root."
return 1
fi
msg_info "Ensuring system packages are installed..."
if [[ "$OS_ID" == "debian" || "$OS_ID" == "ubuntu" || "$OS_ID" == "kali" || "$OS_LIKE" == *"debian"* ]]; then
sudo apt-get install -y btop tmux fzf zsh
elif [[ "$OS_ID" == "arch" || "$OS_LIKE" == *"arch"* ]]; then
sudo pacman -S --noconfirm --needed btop tmux fzf zsh
elif [[ "$OS_ID" == "fedora" || "$OS_LIKE" == *"fedora"* ]]; then
sudo dnf install -y btop tmux fzf zsh
fi
# zsh path differs across distros: /usr/bin/zsh (Debian/Ubuntu),
# /bin/zsh (Arch), /usr/local/bin/zsh (compiled). Autodetect.
local ZSH_BIN
ZSH_BIN="$(command -v zsh)"
if [[ -z "$ZSH_BIN" ]]; then
msg_warn "zsh not on PATH after install — skipping root chsh"
else
if ! grep -qxF "$ZSH_BIN" /etc/shells; then
echo "$ZSH_BIN" | sudo tee -a /etc/shells >/dev/null
fi
sudo chsh -s "$ZSH_BIN" root
msg_success "Root shell set to $ZSH_BIN"
fi
# Use `sudo test` — /root isn't readable as the primary user, so a bare
# `[ -d /root/... ]` always returns false and we end up trying to clone
# into a populated dir, which fails non-idempotently.
if ! sudo test -d "/root/.oh-my-zsh"; then
msg_info "Installing Oh My Zsh for root..."
sudo git clone https://github.com/ohmyzsh/ohmyzsh.git /root/.oh-my-zsh
fi
local ROOT_ZSH_CUSTOM="/root/.oh-my-zsh/custom"
declare -A ROOT_OMZ_PLUGINS=(
[zsh-autosuggestions]="https://github.com/zsh-users/zsh-autosuggestions"
[zsh-syntax-highlighting]="https://github.com/zsh-users/zsh-syntax-highlighting"
[zsh-completions]="https://github.com/zsh-users/zsh-completions"
[fzf-tab]="https://github.com/Aloxaf/fzf-tab"
)
for plugin in "${!ROOT_OMZ_PLUGINS[@]}"; do
if ! sudo test -d "$ROOT_ZSH_CUSTOM/plugins/$plugin"; then
msg_info "Installing OMZ plugin for root: $plugin"
sudo git clone --depth 1 "${ROOT_OMZ_PLUGINS[$plugin]}" "$ROOT_ZSH_CUSTOM/plugins/$plugin" -q
fi
done
if ! sudo test -f "/root/.local/bin/oh-my-posh"; then
msg_info "Installing Oh My Posh for root..."
sudo mkdir -p /root/.local/bin
sudo curl -s https://ohmyposh.dev/install.sh | sudo bash -s -- -d /root/.local/bin
fi
if ! sudo test -d "/root/.tmux/plugins/tpm"; then
msg_info "Installing Tmux Plugin Manager for root..."
sudo git clone https://github.com/tmux-plugins/tpm /root/.tmux/plugins/tpm
fi
# Ensure Nerd Fonts are available for root's OMP themes
install_nerd_fonts
msg_info "Deploying configs to /root via stow..."
cd "$REPO_DIR" || exit 1
# NOTE: claude/ is excluded — ROOT shares ~/.claude via symlink (AI_DIRS block below)
local PACKAGES=("${STOW_PACKAGES[@]}")
local TS_ROOT
TS_ROOT="$(date +%Y%m%d_%H%M%S)"
for pkg in "${PACKAGES[@]}"; do
if [ -d "$pkg" ]; then
# Phase 1: drop any prior stow links
sudo stow -D -t /root "$pkg" 2>/dev/null || true
# Phase 2/3: back up real files in /root that would conflict with stow.
# /root/.bashrc ships as a real file from skel; without this, stow aborts.
while IFS= read -r src_file; do
local rel_path="${src_file#$pkg/}"
local dest_file="/root/$rel_path"
if sudo test -e "$dest_file" && ! sudo test -L "$dest_file"; then
local backup_file="${dest_file}.backup_${TS_ROOT}"
msg_warn "Backing up /root real file: $dest_file -> $backup_file"
sudo mv "$dest_file" "$backup_file"
fi
done < <(find "$pkg" -type f)
# Phase 4: ensure parent dirs exist (skip pkg-root)
while IFS= read -r src_dir; do
local rel_path="${src_dir#$pkg/}"
[ -n "$rel_path" ] && sudo mkdir -p "/root/$rel_path"
done < <(find "$pkg" -mindepth 1 -type d)
if sudo stow -v -t /root "$pkg" 2>&1; then
msg_success "Stowed to /root: $pkg"
else
msg_error "Failed to stow to /root: $pkg"
fi