-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellfish.sh
More file actions
executable file
·923 lines (831 loc) · 29.6 KB
/
shellfish.sh
File metadata and controls
executable file
·923 lines (831 loc) · 29.6 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
#!/usr/bin/env bash
# UI facelift changelog:
# - Added colorized logging with minimal TUI helpers and spinner support.
# - Wrapped the startup banner in a framed header via ui_header().
# - Introduced boxed callouts for GitHub helper, SSH shortcuts, and next steps.
# - Hooked spinners around apt updates, package installs, and font setup.
# - Preserved existing prompts and side effects while refining terminal output.
# - Spinner prefers gum when available and falls back to an ANSI implementation.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TIMESTAMP="$(date +%Y%m%d-%H%M%S)"
SHELLFISH_VERSION="1.1.0"
SHELLFISH_ASCII="$(cat <<'EOF'
__
><((__o shellfish
) terminal toolkit by Tero Civill 2025
((
EOF
)"
STATE_DIR="$HOME/.local/share/shellfish"
MANIFEST_FILE="$STATE_DIR/managed_files.txt"
SHELLFISH_BASHRC="$STATE_DIR/bashrc"
SHELLFISH_SNIPPET_START="# >>> shellfish >>>"
SHELLFISH_SNIPPET_END="# <<< shellfish <<<"
declare -a MANAGED_PATHS=()
DEFAULT_PATHS=(
"$HOME/.config/fish/config.fish"
"$HOME/.config/fish/functions/gitget.fish"
"$HOME/.config/fish/functions/ssh_shortcuts.fish"
"$HOME/.config/fish/functions/scr.fish"
"$HOME/.config/fish/functions/screens.fish"
"$HOME/.config/fish/conf.d/fnm.fish"
"$HOME/.config/fish/conf.d/rustup.fish"
"$HOME/.config/fish/conf.d/99-shellfish-paths.fish"
"$HOME/.config/fish/completions/bun.fish"
"$HOME/.bashrc"
"$SHELLFISH_BASHRC"
"$HOME/.ssh/config"
)
# --- UI helpers ------------------------------------------------------------
_have() { command -v "$1" >/dev/null 2>&1; }
UI_RESET=$'\033[0m'
UI_BOLD=$'\033[1m'
UI_TTY=0
[[ -t 1 ]] && UI_TTY=1
_ui_fg() { printf '\033[38;5;%sm' "$1"; }
readonly UI_COLOR_ACCENT=75
readonly UI_COLOR_INFO=81
readonly UI_COLOR_WARN=214
readonly UI_COLOR_ERR=203
readonly UI_COLOR_OK=70
readonly UI_COLOR_FRAME=240
_ui_repeat() {
local char="$1" count="$2" out=""
while (( count-- > 0 )); do out+="$char"; done
printf '%s' "$out"
}
_ui_tag() {
local label="$1" color="$2"
printf "%s[%-4s]%s" "$(_ui_fg "$color")$UI_BOLD" "$label" "$UI_RESET"
}
_ui_status() {
local label="$1" color="$2"; shift 2
local message="$*"
printf "%s %s\n" "$(_ui_tag "$label" "$color")" "$message"
}
info() { _ui_status "info" "$UI_COLOR_INFO" "$@"; }
warn() { _ui_status "warn" "$UI_COLOR_WARN" "$@"; }
error() { _ui_status "err" "$UI_COLOR_ERR" "$@"; }
ui_box() {
local title="$1"
shift || true
local width=74
if [[ $# -ge 1 && -n "${1:-}" ]]; then
width="$1"
shift || true
fi
local lines=()
local line
while IFS= read -r line; do
lines+=("$line")
done
if [[ ${#lines[@]} -eq 0 ]]; then
lines=("")
fi
local max_len=0
for line in "${lines[@]}"; do
(( ${#line} > max_len )) && max_len=${#line}
done
local inner=$((width - 2))
if (( inner < max_len )); then
inner=$max_len
width=$((inner + 2))
fi
local frame="${UI_BOLD}$(_ui_fg "$UI_COLOR_FRAME")"
local border="$(_ui_repeat "─" "$inner")"
if [[ -n "$title" ]]; then
local header="─ ${title} "
if (( ${#header} > inner )); then
header="─ ${title:0:$((inner - 2))}"
fi
local remaining=$((inner - ${#header}))
(( remaining < 0 )) && remaining=0
printf "%s┌%s%s┐%s\n" "$frame" "$header" "$(_ui_repeat "─" "$remaining")" "$UI_RESET"
else
printf "%s┌%s┐%s\n" "$frame" "$border" "$UI_RESET"
fi
for line in "${lines[@]}"; do
printf "%s│%-*s│%s\n" "$frame" "$inner" "$line" "$UI_RESET"
done
printf "%s└%s┘%s\n" "$frame" "$border" "$UI_RESET"
}
ui_header() {
local title="$1"
local subtitle="$2"
local art="$3"
{
printf '%s\n' "$art"
printf '\n%s\n' "$subtitle"
} | ui_box "$title"
}
_ui_clear_line() {
(( UI_TTY == 1 )) || return 0
if tput el >/dev/null 2>&1; then
tput el
else
printf '\033[K'
fi
}
ui_spin() {
local message="$1"
shift
if [[ $# -eq 0 ]]; then
return 0
fi
if (( UI_TTY == 1 )) && _have gum; then
gum spin --title "$message" -- "$@"
local status=$?
if (( status == 0 )); then
_ui_status "ok" "$UI_COLOR_OK" "$message"
else
_ui_status "err" "$UI_COLOR_ERR" "$message"
fi
return $status
fi
if (( UI_TTY == 0 )); then
"$@"
local status=$?
if (( status == 0 )); then
_ui_status "ok" "$UI_COLOR_OK" "$message"
else
_ui_status "err" "$UI_COLOR_ERR" "$message"
fi
return $status
fi
local tmp
tmp="$(mktemp)"
"$@" >"$tmp" 2>&1 &
local pid=$!
local frames=(⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏)
local frame_index=0
while kill -0 "$pid" >/dev/null 2>&1; do
printf '\r%s %s' "$(_ui_tag "${frames[frame_index]}" "$UI_COLOR_ACCENT")" "$message"
sleep 0.1
frame_index=$(( (frame_index + 1) % ${#frames[@]} ))
done
wait "$pid"
local status=$?
printf '\r'
_ui_clear_line
if (( status == 0 )); then
_ui_status "ok" "$UI_COLOR_OK" "$message"
else
_ui_status "err" "$UI_COLOR_ERR" "$message"
fi
if [[ -s "$tmp" ]]; then
cat "$tmp"
fi
rm -f "$tmp"
return $status
}
normalize_answer() {
local ans="${1:-}"
ans="${ans,,}"
if [[ -z "$ans" || "$ans" == "y" || "$ans" == "yes" ]]; then printf "y"; else printf "n"; fi
}
contains_path() {
local candidate="$1"
local path
for path in "${MANAGED_PATHS[@]}"; do
[[ "$path" == "$candidate" ]] && return 0
done
return 1
}
add_known_path() {
local candidate="$1"
contains_path "$candidate" || MANAGED_PATHS+=("$candidate")
}
record_managed_path() {
local candidate="$1"
add_known_path "$candidate"
mkdir -p "$STATE_DIR"
if [[ -f "$MANIFEST_FILE" ]]; then
grep -Fxq "$candidate" "$MANIFEST_FILE" || echo "$candidate" >> "$MANIFEST_FILE"
else
echo "$candidate" > "$MANIFEST_FILE"
fi
}
ensure_state() {
MANAGED_PATHS=()
mkdir -p "$STATE_DIR"
if [[ -f "$MANIFEST_FILE" ]]; then
while IFS= read -r line; do [[ -n "$line" ]] && add_known_path "$line"; done < "$MANIFEST_FILE"
fi
local path
for path in "${DEFAULT_PATHS[@]}"; do add_known_path "$path"; done
}
has_shellfish_backups() {
shopt -s nullglob
local file backups
for file in "${MANAGED_PATHS[@]}"; do
backups=( "${file}".*.bak )
if ((${#backups[@]})); then shopt -u nullglob; return 0; fi
done
shopt -u nullglob
return 1
}
restore_shellfish() {
local file latest restored=0
shopt -s nullglob
for file in "${MANAGED_PATHS[@]}"; do
local backups=( "${file}".*.bak )
if ((${#backups[@]})); then
latest="$(printf '%s\n' "${backups[@]}" | sort -r | head -n1)"
mkdir -p "$(dirname "$file")"
cp -f "$latest" "$file"
info "Restored $file from $(basename "$latest")"
restored=1
else
info "No backup for $file; leaving as-is."
fi
done
shopt -u nullglob
if (( restored )); then
rm -f "$MANIFEST_FILE"
info "Restoration complete. Backups remain on disk."
else
warn "No Shellfish changes found to restore."
fi
return $restored
}
install_nerd_font() {
local fonts_root="$HOME/.local/share/fonts"
local fonts_dir="$fonts_root/JetBrainsMonoNerd"
local url="https://github.com/ryanoasis/nerd-fonts/releases/latest/download/JetBrainsMono.zip"
local tmp_dir archive
tmp_dir=$(mktemp -d) || return 1
archive="$tmp_dir/JetBrainsMono.zip"
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$url" -o "$archive" || { warn "Downloading Nerd Font archive failed."; rm -rf "$tmp_dir"; return 1; }
elif command -v wget >/dev/null 2>&1; then
wget -q "$url" -O "$archive" || { warn "Downloading Nerd Font archive failed."; rm -rf "$tmp_dir"; return 1; }
else
warn "Neither curl nor wget available; cannot download Nerd Font."; rm -rf "$tmp_dir"; return 1
fi
mkdir -p "$fonts_dir"
unzip -o "$archive" -d "$fonts_dir" >/dev/null || { warn "Unzipping Nerd Font archive failed."; rm -rf "$tmp_dir"; return 1; }
rm -rf "$tmp_dir"
if command -v fc-cache >/dev/null 2>&1; then
fc-cache -f "$fonts_root" >/dev/null 2>&1 || warn "fc-cache refresh failed; run 'fc-cache -fv ~/.local/share/fonts' manually."
else
warn "fc-cache not found; run 'fc-cache -fv ~/.local/share/fonts' to refresh fonts."
fi
info "JetBrainsMono Nerd Font installed under $fonts_dir. Set it to be used in your terminal."
record_managed_path "$fonts_dir"
return 0
}
copy_file() {
local src="$1"
local dest="$2"
local mode="${3:-0644}"
[[ -f "$src" ]] || { error "Missing source file: $src"; exit 1; }
local dest_dir rp
dest_dir="$(dirname "$dest")"
mkdir -p "$dest_dir"
if [[ -e "$dest" && ! -L "$dest" && ! -d "$dest" ]]; then
local backup="${dest}.${TIMESTAMP}.bak"
cp -f "$dest" "$backup"
if rp="$(readlink -f "$dest" 2>/dev/null)"; then :; else rp="$dest"; fi
info "Backed up existing $rp → $backup"
fi
install -m "$mode" "$src" "$dest"
info "Installed $(basename "$src") → $dest"
record_managed_path "$dest"
}
ensure_package_apt() {
local pkg="$1"
if ! dpkg -s "$pkg" >/dev/null 2>&1; then
sudo apt-get install -y "$pkg"
fi
}
ensure_gh_on_apt() {
if dpkg -s gh >/dev/null 2>&1; then return 0; fi
info "Installing GitHub CLI repo…"
sudo apt-get install -y ca-certificates curl >/dev/null 2>&1 || true
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | \
sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg >/dev/null 2>&1 || true
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" \
| sudo tee /etc/apt/sources.list.d/github-cli.list >/dev/null
sudo apt-get update
sudo apt-get install -y gh
}
ensure_packages() {
local packages=("$@")
if command -v apt-get >/dev/null 2>&1; then
ui_spin "Updating package index" sudo apt-get update
local pkg
for pkg in "${packages[@]}"; do
if [[ "$pkg" == "gh" ]]; then
if ! ui_spin "Installing $pkg" ensure_gh_on_apt; then
warn "Could not install gh automatically."
fi
else
if ! ui_spin "Installing $pkg" ensure_package_apt "$pkg"; then
warn "Could not install $pkg automatically."
fi
fi
done
else
warn "Unsupported package manager. Install manually: ${packages[*]}"
fi
}
start_ssh_agent_if_needed() {
if ! ssh-add -l >/dev/null 2>&1; then
eval "$(ssh-agent -s)" >/dev/null
fi
}
fish_set_universal() {
# $1 = var name, $2 = value
command -v fish >/dev/null 2>&1 || return 0
# Pass value as argv[1] to avoid quoting pitfalls
fish -c 'set -Ux -- $argv[1] $argv[2]' -- "$1" "$2" || return 1
}
ensure_bashrc_snippet() {
local target="$HOME/.bashrc"
local snippet_file="$SHELLFISH_BASHRC"
local resolved
if [[ -f "$target" ]]; then
if grep -Fq "$SHELLFISH_SNIPPET_START" "$target"; then
info "Shellfish snippet already present in $target"
else
local backup="${target}.${TIMESTAMP}.bak"
cp -f "$target" "$backup"
if resolved="$(readlink -f "$target" 2>/dev/null)"; then :; else resolved="$target"; fi
info "Backed up existing $resolved → $backup"
{
printf '\n%s\n' "$SHELLFISH_SNIPPET_START"
printf '%s\n' "# Added by shellfish to load its Bash helpers."
printf '%s\n' "if [ -f \"$snippet_file\" ]; then"
printf '%s\n' " . \"$snippet_file\""
printf '%s\n\n' "$SHELLFISH_SNIPPET_END"
} >> "$target"
info "Appended Shellfish snippet to $target"
fi
else
cat <<EOF > "$target"
# ~/.bashrc
# Created by shellfish on $TIMESTAMP
$SHELLFISH_SNIPPET_START
# Added by shellfish to load its Bash helpers.
if [ -f "$snippet_file" ]; then
. "$snippet_file"
fi
$SHELLFISH_SNIPPET_END
EOF
chmod 0644 "$target"
info "Created $target with Shellfish snippet."
fi
record_managed_path "$target"
}
main() {
local dry_run="n"
if [[ "${1:-}" == "--dry-run" ]]; then
dry_run="y"
info "Dry run mode. No changes will be made."
fi
ui_header "shellfish v${SHELLFISH_VERSION}" "terminal toolkit by Tero Civill" "$SHELLFISH_ASCII"
ensure_state
local previous_state="n"
if has_shellfish_backups || [[ -f "$MANIFEST_FILE" ]]; then previous_state="y"; fi
if [[ "$previous_state" == "y" ]]; then
echo
echo "Shellfish state detected."
if has_shellfish_backups; then
read -rp "Revert Shellfish changes using available backups? [y/N] " revert_ans
if [[ "$(normalize_answer "$revert_ans")" == "y" ]]; then
if [[ "$dry_run" == "n" ]]; then
if restore_shellfish; then
ensure_state
info "Shellfish settings reverted."
fi
else
info "Dry run: would restore backups."
fi
read -rp "Run Shellfish setup again with fresh settings? [Y/n] " rerun_after_restore
if [[ "$(normalize_answer "$rerun_after_restore")" != "y" ]]; then
info "Exiting."
printf "\n%s\n" "$SHELLFISH_ASCII"
return 0
fi
else
read -rp "Proceed with installation without reverting? [Y/n] " proceed_ans
if [[ "$(normalize_answer "$proceed_ans")" != "y" ]]; then
info "Exiting."
printf "\n%s\n" "$SHELLFISH_ASCII"
return 0
fi
fi
else
read -rp "Shellfish state found. Run setup again with new settings? [Y/n] " rerun_only
if [[ "$(normalize_answer "$rerun_only")" != "y" ]]; then
info "Exiting."
printf "\n%s\n" "$SHELLFISH_ASCII"
return 0
fi
fi
fi
echo
read -rp "Do you use GitHub for development? [Y/n] " answer
local use_github; use_github="$(normalize_answer "$answer")"
local ssh_config="$HOME/.ssh/config"
local key_path="$HOME/.ssh/id_ed25519_github"
if [[ "$use_github" == "y" ]]; then
ui_box "GitHub helper setup" <<'EOF'
Set a default GitHub username, optionally stage a GitHub-specific SSH config, and generate an ed25519 key. Each step can be skipped with the 'abort' keyword.
EOF
echo
echo "GitHub helper setup (type 'abort' at any prompt to skip the remaining steps)."
local github_abort=0
local abort_token="abort"
local github_user_input=""
read -rp "GitHub username (leave blank to detect via 'gh api'): " github_user_input
if [[ "${github_user_input,,}" == "$abort_token" ]]; then
github_abort=1
info "Skipping GitHub configuration."
else
local github_user="${github_user_input//[[:space:]]/}"
if [[ -n "$github_user" ]]; then
if [[ "$dry_run" == "n" ]]; then
if command -v fish >/dev/null 2>&1; then
if fish -c 'set -Ux GITGET_GITHUB_USER $argv[1]' -- "$github_user"; then
info "Stored GitHub username '$github_user' for gitget."
else
warn "Could not persist GITGET_GITHUB_USER via fish. Set it manually later."
fi
else
warn "fish executable not found; set GITGET_GITHUB_USER manually later."
fi
else
info "Dry run: would run 'fish -c \"set -Ux GITGET_GITHUB_USER $github_user\"'."
fi
else
info "Leaving GitHub username unset; gitget will query GitHub CLI when needed."
fi
fi
if (( github_abort == 0 )); then
echo
echo "GitHub requires SSH access for repo cloning."
read -rp "Prepare GitHub SSH setup now? [Y/n] " github_ssh_prepare
if [[ "${github_ssh_prepare,,}" == "$abort_token" ]]; then
github_abort=1
info "Skipping remaining GitHub configuration."
elif [[ "$(normalize_answer "$github_ssh_prepare")" == "y" ]]; then
if [[ -f "$SCRIPT_DIR/ssh/config" ]]; then
if [[ "$dry_run" == "n" ]]; then
mkdir -p "$HOME/.ssh"; chmod 700 "$HOME/.ssh"
copy_file "$SCRIPT_DIR/ssh/config" "$ssh_config" 0600
else
info "Dry run: would install sample ~/.ssh/config tuned for GitHub."
fi
fi
else
info "Skipped GitHub SSH helper configuration."
fi
fi
if (( github_abort == 0 )); then
if [[ ! -f "$key_path" ]]; then
echo
read -rp "Generate GitHub SSH key at $key_path? [y/N] " generate_key
if [[ "${generate_key,,}" == "$abort_token" ]]; then
github_abort=1
info "Skipping remaining GitHub configuration."
elif [[ "$(normalize_answer "$generate_key")" == "y" ]]; then
read -rp "SSH key comment [$USER@github]: " key_comment
if [[ "${key_comment,,}" == "$abort_token" ]]; then
github_abort=1
info "Skipping remaining GitHub configuration."
else
key_comment="${key_comment:-$USER@github}"
if [[ "$dry_run" == "n" ]]; then
mkdir -p "$HOME/.ssh"; chmod 700 "$HOME/.ssh"
ssh-keygen -t ed25519 -f "$key_path" -C "$key_comment" -N ""
info "Generated SSH key. Add the public key to GitHub."
else
info "Dry run: would generate $key_path with comment '$key_comment'."
fi
fi
else
info "Skipped SSH key generation."
fi
else
info "Existing $key_path key found."
fi
fi
if (( github_abort == 0 )) && [[ -f "$key_path" ]]; then
if [[ "$dry_run" == "n" ]]; then
start_ssh_agent_if_needed
ssh-add "$key_path" || warn "Could not add SSH key to agent."
else
info "Dry run: would add $key_path to ssh-agent."
fi
fi
else
info "Skipping GitHub configuration."
fi
echo
local install_irc="n"
read -rp "Install irssi (IRC client)? [Y/n] " answer
install_irc="$(normalize_answer "$answer")"
local irc_network="ircnet"
if [[ "$install_irc" == "y" ]]; then
echo
echo "Choose default irssi network (for 'irssi -c <network>')."
echo " 1) ircnet"
echo " 2) libera"
echo " 3) oftc"
echo " 4) efnet"
echo " 5) Custom"
read -rp "Selection [1]: " choice
case "${choice:-1}" in
2) irc_network="libera" ;;
3) irc_network="oftc" ;;
4) irc_network="efnet" ;;
5) read -rp "Enter custom irssi network name: " custom_net; [[ -n "${custom_net:-}" ]] && irc_network="$custom_net" ;;
*) irc_network="ircnet" ;;
esac
fi
local packages=(
fish git curl wget python3 python3-pip python3-venv
eza tree zoxide fzf neofetch unzip screen
)
if [[ "$use_github" == "y" ]]; then packages+=(gh); fi
if [[ "$install_irc" == "y" ]]; then packages+=(irssi); fi
if [[ "$dry_run" == "n" ]]; then
ensure_packages "${packages[@]}"
else
info "Dry run: would install packages: ${packages[*]}"
fi
if [[ "$dry_run" == "n" ]]; then
if ! ui_spin "Installing JetBrainsMono Nerd Font" install_nerd_font; then
warn "JetBrainsMono Nerd Font installation encountered issues."
fi
else
info "Dry run: would install JetBrainsMono Nerd Font to ~/.local/share/fonts."
fi
if [[ "$dry_run" == "n" ]]; then
mkdir -p "$HOME/.config/fish"/{functions,conf.d,completions}
copy_file "$SCRIPT_DIR/fish/config.fish" "$HOME/.config/fish/config.fish"
copy_file "$SCRIPT_DIR/fish/functions/gitget.fish" "$HOME/.config/fish/functions/gitget.fish"
local old_shortcuts="$HOME/.config/fish/functions/gameserver.fish"
local new_shortcuts="$HOME/.config/fish/functions/ssh_shortcuts.fish"
if [[ -f "$old_shortcuts" && ! -f "$new_shortcuts" ]]; then
mv "$old_shortcuts" "$new_shortcuts"
info "Migrated existing gameserver shortcuts → ssh_shortcuts.fish"
record_managed_path "$new_shortcuts"
if [[ -f "$MANIFEST_FILE" ]]; then
sed -i "\|$old_shortcuts|d" "$MANIFEST_FILE" || true
fi
else
copy_file "$SCRIPT_DIR/fish/functions/ssh_shortcuts.fish" "$new_shortcuts"
fi
copy_file "$SCRIPT_DIR/fish/functions/scr.fish" "$HOME/.config/fish/functions/scr.fish"
copy_file "$SCRIPT_DIR/fish/functions/screens.fish" "$HOME/.config/fish/functions/screens.fish"
copy_file "$SCRIPT_DIR/fish/conf.d/fnm.fish" "$HOME/.config/fish/conf.d/fnm.fish"
copy_file "$SCRIPT_DIR/fish/conf.d/rustup.fish" "$HOME/.config/fish/conf.d/rustup.fish"
copy_file "$SCRIPT_DIR/fish/conf.d/99-shellfish-paths.fish" "$HOME/.config/fish/conf.d/99-shellfish-paths.fish"
copy_file "$SCRIPT_DIR/fish/completions/bun.fish" "$HOME/.config/fish/completions/bun.fish"
else
info "Dry run: would copy Fish configuration files."
fi
if [[ "$dry_run" == "n" ]]; then
if command -v fish >/dev/null 2>&1; then
if fish_set_universal "SHELLFISH_IRC_NETWORK" "$irc_network"; then
info "Set SHELLFISH_IRC_NETWORK='$irc_network'"
else
warn "Could not persist SHELLFISH_IRC_NETWORK; run: fish -c 'set -Ux SHELLFISH_IRC_NETWORK $irc_network'"
fi
fi
else
info "Dry run: would set SHELLFISH_IRC_NETWORK to '$irc_network'"
fi
if [[ "$dry_run" == "n" ]]; then
copy_file "$SCRIPT_DIR/bash/.bashrc" "$SHELLFISH_BASHRC" 0644
ensure_bashrc_snippet
else
info "Dry run: would update Shellfish Bash helpers and ensure ~/.bashrc sources them."
fi
if [[ "$dry_run" == "n" ]]; then
mkdir -p "$HOME/.ssh"; chmod 700 "$HOME/.ssh"
fi
if [[ "$dry_run" == "n" ]]; then
[[ -f "$ssh_config" ]] || { touch "$ssh_config"; chmod 600 "$ssh_config"; }
fi
local default_identity="$key_path"
if [[ ! -f "$default_identity" ]]; then
if [[ -f "$HOME/.ssh/id_ed25519" ]]; then default_identity="$HOME/.ssh/id_ed25519"
elif [[ -f "$HOME/.ssh/id_rsa" ]]; then default_identity="$HOME/.ssh/id_rsa"
fi
fi
echo
ui_box "SSH Shortcuts" <<'EOF'
Adding an SSH shortcut records the host in ~/.ssh/config and creates matching Fish functions (lowercase and uppercase) for quick access.
EOF
echo "SSH shortcuts let you launch saved SSH connections; Shellfish updates ~/.ssh/config and adds Fish helpers for you."
read -rp "Create SSH shortcuts now? [Y/n] " add_hosts_answer
if [[ "$(normalize_answer "$add_hosts_answer")" == "y" ]]; then
local ssh_backup_done=0
local abort_token="abort"
local ssh_shortcuts_file="$HOME/.config/fish/functions/ssh_shortcuts.fish"
declare -a new_shortcuts=()
declare -A seen_aliases=()
local abort_shortcuts=0
while true; do
echo
echo "Enter shortcut details (type '$abort_token' at any prompt to cancel)."
read -rp "Shortcut alias (letters/numbers) (leave blank to finish): " host_alias
if [[ -z "$host_alias" ]]; then
break
fi
if [[ "${host_alias,,}" == "$abort_token" ]]; then
abort_shortcuts=1
break
fi
if [[ ! "$host_alias" =~ ^[A-Za-z0-9_]+$ ]]; then
warn "Alias must use only letters, numbers, or underscores."
continue
fi
local alias_key="${host_alias,,}"
if [[ -n "${seen_aliases[$alias_key]:-}" ]]; then
warn "Alias '$host_alias' already entered in this session."
continue
fi
if [[ "$dry_run" == "n" && -f "$ssh_config" ]]; then
if awk -v a="$host_alias" 'tolower($1)=="host" && tolower($2)==tolower(a){exit 0} END{exit 1}' "$ssh_config"; then
warn "Host '$host_alias' already exists in $ssh_config. Skipping."
continue
fi
fi
read -rp "Server hostname or IP: " host_name
if [[ "${host_name,,}" == "$abort_token" ]]; then
abort_shortcuts=1
break
fi
if [[ -z "$host_name" ]]; then
warn "Hostname cannot be empty."
continue
fi
read -rp "SSH username [$USER]: " host_user
if [[ "${host_user,,}" == "$abort_token" ]]; then
abort_shortcuts=1
break
fi
host_user="${host_user:-$USER}"
read -rp "Port [22]: " host_port
if [[ "${host_port,,}" == "$abort_token" ]]; then
abort_shortcuts=1
break
fi
host_port="${host_port:-22}"
if [[ ! "$host_port" =~ ^[0-9]+$ ]]; then
warn "Port must be numeric."
continue
fi
read -rp "Identity file [$default_identity]: " host_identity
if [[ "${host_identity,,}" == "$abort_token" ]]; then
abort_shortcuts=1
break
fi
host_identity="${host_identity:-$default_identity}"
seen_aliases[$alias_key]=1
new_shortcuts+=( "$host_alias|$host_name|$host_user|$host_port|$host_identity" )
if [[ "$dry_run" == "n" ]]; then
if [[ $ssh_backup_done -eq 0 && -f "$ssh_config" ]]; then
local backup="${ssh_config}.${TIMESTAMP}.bak"
cp -f "$ssh_config" "$backup"
info "Backed up existing $ssh_config → $backup"
record_managed_path "$ssh_config"
ssh_backup_done=1
elif [[ $ssh_backup_done -eq 0 ]]; then
record_managed_path "$ssh_config"
ssh_backup_done=1
fi
{
echo ""
echo "# Added by shellfish $(date '+%Y-%m-%d %H:%M:%S')"
echo "Host $host_alias"
echo " HostName $host_name"
echo " User $host_user"
echo " Port $host_port"
echo " IdentityFile $host_identity"
echo " IdentitiesOnly yes"
} >> "$ssh_config"
info "Added host '$host_alias' to $ssh_config"
else
info "Dry run: would append host '$host_alias' to $ssh_config."
fi
read -rp "Add another shortcut? [y/N]: " add_another_answer
if [[ "${add_another_answer,,}" == "$abort_token" ]]; then
abort_shortcuts=1
break
fi
[[ "$(normalize_answer "$add_another_answer")" == "y" ]] || break
done
if (( abort_shortcuts )); then
if ((${#new_shortcuts[@]} == 0)); then
info "Aborted SSH shortcut setup. No changes made."
else
info "Stopped collecting additional shortcuts."
fi
fi
if ((${#new_shortcuts[@]} > 0)); then
if [[ "$dry_run" == "n" ]]; then
local tmp_ssh_shortcuts
tmp_ssh_shortcuts="$(mktemp)"
if [[ -f "$ssh_shortcuts_file" ]]; then
if ! grep -Eq "Customize (gameserver|ssh_shortcuts).fish" "$ssh_shortcuts_file"; then
cat "$ssh_shortcuts_file" > "$tmp_ssh_shortcuts"
if [[ $(tail -c1 "$ssh_shortcuts_file" 2>/dev/null) != $'\n' ]]; then
printf "\n" >> "$tmp_ssh_shortcuts"
fi
printf "\n" >> "$tmp_ssh_shortcuts"
fi
fi
printf "# Added by shellfish on %s\n\n" "$(date '+%Y-%m-%d %H:%M:%S')" >> "$tmp_ssh_shortcuts"
for entry in "${new_shortcuts[@]}"; do
IFS='|' read -r alias name user port identity <<< "$entry"
local upper_alias="${alias^^}"
printf "function %s --wraps \"ssh %s\" --description 'SSH to %s as %s'\n" "$alias" "$alias" "$name" "$user" >> "$tmp_ssh_shortcuts"
printf " ssh %s \$argv\n" "$alias" >> "$tmp_ssh_shortcuts"
printf "end\n\n" >> "$tmp_ssh_shortcuts"
if [[ "$upper_alias" != "$alias" ]]; then
printf "function %s --wraps %s --description 'SSH to %s as %s'\n" "$upper_alias" "$alias" "$name" "$user" >> "$tmp_ssh_shortcuts"
printf " %s \$argv\n" "$alias" >> "$tmp_ssh_shortcuts"
printf "end\n\n" >> "$tmp_ssh_shortcuts"
fi
done
install -m 0644 "$tmp_ssh_shortcuts" "$ssh_shortcuts_file"
rm -f "$tmp_ssh_shortcuts"
info "Updated $ssh_shortcuts_file with SSH helpers."
record_managed_path "$ssh_shortcuts_file"
else
info "Dry run: would generate $ssh_shortcuts_file with functions:"
for entry in "${new_shortcuts[@]}"; do
IFS='|' read -r alias name user port identity <<< "$entry"
info " • function $alias → ssh $alias"
local upper_alias="${alias^^}"
if [[ "$upper_alias" != "$alias" ]]; then
info " • function $upper_alias → $alias"
fi
done
fi
fi
fi
if [[ "$use_github" == "y" ]]; then
if [[ "$dry_run" == "n" ]]; then
if ! gh auth status >/dev/null 2>&1; then
warn "GitHub CLI not logged in. Run: gh auth login --hostname github.com --git-protocol ssh --web"
else
info "GitHub CLI already authenticated."
fi
else
info "Dry run: would check gh auth status."
fi
fi
if command -v fish >/dev/null 2>&1; then
local current_shell
current_shell="$(basename "${SHELL:-}")"
if [[ "$current_shell" != "fish" ]]; then
if [[ "$dry_run" == "n" ]]; then
info "Setting fish as the default shell (was $current_shell)."
if ! grep -Fxq "$(command -v fish)" /etc/shells 2>/dev/null; then
command -v fish | sudo tee -a /etc/shells >/dev/null || true
fi
chsh -s "$(command -v fish)" || warn "Could not change default shell automatically; run 'chsh -s $(command -v fish)' manually."
else
info "Dry run: would run chsh -s $(command -v fish)."
fi
fi
else
warn "fish executable not found; cannot change default shell."
fi
echo
ui_box "Next steps" <<'EOF'
Finish GitHub authentication, start a fresh Fish session, and explore repositories with gitget --list.
EOF
echo "Next steps:"
if [[ "$use_github" == "y" ]]; then
echo " • Copy ~/.ssh/id_ed25519_github.pub → GitHub → Settings → SSH and GPG keys."
echo " • Run 'gh auth login --hostname github.com --git-protocol ssh --web' to finish CLI auth."
else
echo " • (Optional) Set a gitget default user anytime: fish -c 'set -Ux GITGET_GITHUB_USER <username>'"
fi
echo " • Open a new Fish session and explore gitget --list or gitget --pick when you're ready."
echo
echo "Enjoy your Shellfish ><((°>"
if [[ "$dry_run" == "n" ]]; then
read -rp "Remove the installer directory $(pwd) now? [y/N] " cleanup
if [[ "$(normalize_answer "$cleanup")" == "y" ]]; then
local install_dir="$(pwd)"
local parent_dir="$(dirname "$install_dir")"
cd "$HOME"
if rm -rf "$install_dir"; then
info "Removed $install_dir"
cd "$parent_dir" 2>/dev/null || true
else
warn "Could not remove $install_dir automatically; delete it manually if desired."
fi
fi
fi
}
main "$@"