Repository navigation
Expand file tree
/
Copy pathinstall
More file actions
executable file
·1353 lines (1230 loc) · 57.9 KB
/
Copy pathinstall
File metadata and controls
executable file
·1353 lines (1230 loc) · 57.9 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/sh
# Bootstrap spell: main wizardry installer
set -eu
# After installation, wizardry will be available in new terminal windows.
# To use it immediately in the current terminal, run the command shown at the end.
case "${1-}" in
--help|--usage|-h)
cat <<'USAGE'
Usage: install
Install Wizardry into the configured install directory.
USAGE
exit 0
;;
esac
# Abort if unexpected arguments are supplied so we do not accidentally discard
# them when repurposing the positional parameters to track missing directories.
if [ "$#" -ne 0 ]; then
printf '%s\n' "Usage: install" >&2
exit 1
fi
# Disable CDPATH so that "cd" behaves predictably even if the user has a
# custom search path configured in their environment. This keeps the script
# portable across login shells that set CDPATH to multiple directories.
unset CDPATH
# NOTE: Directories are added to PATH automatically when invoke-wizardry is
# sourced from the rc file at shell startup.
# Determine the default target directory shown in the install prompt. The
# installer accepts three environment variables to steer non-interactive
# installs:
# * WIZARDRY_INSTALL_DIR – fully overrides the prompt and is used directly.
# * WIZARDRY_ROOT – legacy alias used as an override if set.
# * WIZARDRY_REMOTE_DIR – retained for backwards compatibility; becomes the
# prompt default when present.
if [ -n "${WIZARDRY_INSTALL_DIR-}" ]; then
PROMPT_DEFAULT=$WIZARDRY_INSTALL_DIR
elif [ -n "${WIZARDRY_ROOT-}" ]; then
PROMPT_DEFAULT=$WIZARDRY_ROOT
elif [ -n "${WIZARDRY_REMOTE_DIR-}" ]; then
PROMPT_DEFAULT=$WIZARDRY_REMOTE_DIR
elif [ -n "${HOME-}" ]; then
PROMPT_DEFAULT=$HOME/.wizardry
else
PROMPT_DEFAULT=.wizardry
fi
# Validate and normalize PROMPT_DEFAULT to prevent path construction issues.
# Environment variables should provide absolute paths, but we handle relative
# paths defensively. If a relative path looks like it's missing a leading slash
# (e.g., starts with "home/"), we treat it as absolute.
# Note: Using character classes for case-insensitive matching (e.g., [Hh]ome)
# is POSIX-compliant and handles common variations without external tools.
case $PROMPT_DEFAULT in
/*)
# Already absolute path, no normalization needed
:
;;
[Hh]ome/*|[Uu]sers/*|[Uu]sr/*|[Oo]pt/*|[Vv]ar/*|[Tt]mp/*|[Mm]nt/*|[Rr]oot/*)
# Looks like an absolute path missing the leading slash
# This handles cases where environment variables are set incorrectly
# Using case-insensitive patterns to catch common variations
PROMPT_DEFAULT="/$PROMPT_DEFAULT"
;;
*)
# Other relative paths: resolve relative to HOME if possible
if [ -n "${HOME-}" ]; then
# Convert relative to absolute by prepending HOME
PROMPT_DEFAULT=$HOME/$PROMPT_DEFAULT
else
# No HOME set and relative path given - use current directory
cwd=$(pwd -P)
PROMPT_DEFAULT=$cwd/$PROMPT_DEFAULT
fi
;;
esac
if [ -n "${WIZARDRY_INSTALL_PLATFORM-}" ]; then
LEARN_SPELLBOOK_PLATFORM=$WIZARDRY_INSTALL_PLATFORM
DETECT_RC_FILE_PLATFORM=$WIZARDRY_INSTALL_PLATFORM
export LEARN_SPELLBOOK_PLATFORM DETECT_RC_FILE_PLATFORM
fi
# Track whether we are running out of an existing checkout so local installs
# can simply copy the files instead of downloading a fresh archive. Piped
# installs (wget|sh) expose the script through /bin/sh so we only find a
# checkout when this file is run directly from disk.
LOCAL_SOURCE=""
case $0 in
*/*)
script_dir=${0%/*}
if [ -z "$script_dir" ]; then
script_dir=.
fi
if script_dir_abs=$(cd "$script_dir" 2>/dev/null && pwd -P); then
if [ -d "$script_dir_abs/spells" ]; then
LOCAL_SOURCE=$script_dir_abs
fi
fi
;;
*)
if [ -n "${PWD-}" ] && [ -f "$PWD/$0" ]; then
if script_dir_abs=$(cd "$PWD" 2>/dev/null && pwd -P); then
if [ -d "$script_dir_abs/spells" ]; then
LOCAL_SOURCE=$script_dir_abs
fi
fi
fi
;;
esac
cleanup_bootstrap() {
# Clean up any temporary directory created during the download process.
if [ -n "${BOOTSTRAP_TMP_DIR-}" ] && [ -d "$BOOTSTRAP_TMP_DIR" ]; then
rm -rf "$BOOTSTRAP_TMP_DIR"
fi
# If installation was not completed and wizardry was downloaded by this installer
# (not pre-existing), remove it. This cleanup happens when user cancels or interrupts.
# Safety check: only delete if it looks like a wizardry directory (has spells/ subdirectory)
if [ "${INSTALL_COMPLETED-}" != "1" ] && [ "${WIZARDRY_EXISTED_BEFORE-}" = "0" ] && [ -n "${INSTALL_DIR-}" ] && [ -d "$INSTALL_DIR/spells" ]; then
rm -rf "$INSTALL_DIR"
fi
}
# Handle interrupt signals (Ctrl-C, HUP, TERM) by cleaning up and exiting.
# This ensures the script terminates immediately when the user presses Ctrl-C.
handle_interrupt() {
cleanup_bootstrap
printf '\n%s\n' "Installation interrupted." >&2
exit 130
}
# Remote installs create a throwaway working directory; trap its cleanup so
# shells on every platform remove the temporary files even if the install
# process exits early.
trap 'cleanup_bootstrap' EXIT
trap 'handle_interrupt' HUP INT TERM
download_file() {
# Fetch a remote archive using curl or wget, whichever is available on the
# host platform. Some of our supported OS images only ship one of the two
# tools by default, so we try both before giving up.
dest=$1
url=$2
if command -v curl >/dev/null 2>&1; then
if curl -fsSL "$url" -o "$dest"; then
return 0
fi
fi
if command -v wget >/dev/null 2>&1; then
if wget -qO "$dest" "$url"; then
return 0
fi
fi
printf '%s\n' "Error: Neither curl nor wget is available to download wizardry." >&2
return 1
}
download_via_git() {
# Download wizardry using git clone with a shallow clone to minimize download size.
# This sets up a proper git repository with remote configured, enabling update-wizardry
# to work via git pull.
#
# The shallow clone (--depth=1) and single-branch setup keeps download size minimal
# (~1MB) while still allowing git pull to fetch new commits on the main branch.
#
# For developers who need full history or other branches:
# - Full history of main: git fetch --unshallow
# - All branches: git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*' && git fetch
#
# Args: dest_dir repo_url [branch]
dest=$1
repo_url=$2
branch=${3:-main}
# Use shallow clone of main branch to minimize download size (~1MB vs ~550KB tarball)
# --depth=1 creates a shallow clone (only latest commit)
# --branch=main ensures we clone main and set up tracking for it
# This enables git pull to work for updating wizardry
if git clone --depth=1 --branch="$branch" "$repo_url" "$dest" >/dev/null 2>&1; then
return 0
fi
return 1
}
INSTALL_INPUT=""
if [ -n "${WIZARDRY_INSTALL_DIR-}" ]; then
INSTALL_INPUT=$WIZARDRY_INSTALL_DIR
elif [ -n "${WIZARDRY_ROOT-}" ]; then
INSTALL_INPUT=$WIZARDRY_ROOT
else
message="Wizardry install directory? (press Enter for default: $PROMPT_DEFAULT) "
# Check for interactive input. When running via curl | sh, stdin is a pipe
# containing the script content, not user input. We must check if we can
# read from /dev/tty instead.
if [ -t 0 ]; then
# stdin is a terminal - read directly from stdin
printf '%s' "$message" >&2
IFS= read -r INSTALL_INPUT
elif [ -r /dev/tty ]; then
# stdin is not a terminal, but /dev/tty is available
# This handles curl | sh case where stdin has script content
printf '%s' "$message" >&2
IFS= read -r INSTALL_INPUT </dev/tty || INSTALL_INPUT=
else
# No interactive input available
printf '%s\n' "Info: No interactive input detected; using '$PROMPT_DEFAULT'." >&2
fi
fi
if [ -z "$INSTALL_INPUT" ]; then
INSTALL_INPUT=$PROMPT_DEFAULT
fi
case $INSTALL_INPUT in
"~")
if [ -z "${HOME-}" ]; then
printf '%s\n' "Error: HOME is not set; cannot expand '~'." >&2
exit 1
fi
INSTALL_INPUT=$HOME
;;
"~/"*)
if [ -z "${HOME-}" ]; then
printf '%s\n' "Error: HOME is not set; cannot expand '~'." >&2
exit 1
fi
INSTALL_INPUT=$HOME/${INSTALL_INPUT#\~/}
;;
esac
# Normalize INSTALL_INPUT to handle paths that look like absolute paths but are
# missing the leading slash (e.g., "home/username/.wizardry" -> "/home/username/.wizardry").
# This prevents accidental path doubling like "/home/username/home/username/.wizardry".
case $INSTALL_INPUT in
/*)
# Already absolute path, no normalization needed
:
;;
[Hh]ome/*|[Uu]sers/*|[Uu]sr/*|[Oo]pt/*|[Vv]ar/*|[Tt]mp/*|[Mm]nt/*|[Rr]oot/*)
# Looks like an absolute path missing the leading slash
INSTALL_INPUT="/$INSTALL_INPUT"
;;
esac
case $INSTALL_INPUT in
/*)
INSTALL_DIR=$INSTALL_INPUT
;;
*)
cwd=$(pwd -P)
INSTALL_DIR=$cwd/$INSTALL_INPUT
;;
esac
# Track whether wizardry directory existed before we started.
# This prevents showing "already installed" menu when we just downloaded it.
WIZARDRY_EXISTED_BEFORE=0
if [ -d "$INSTALL_DIR/spells" ]; then
WIZARDRY_EXISTED_BEFORE=1
ABS_DIR=$(cd "$INSTALL_DIR" && pwd -P)
else
if [ -e "$INSTALL_DIR" ]; then
printf '%s\n' "Error: '$INSTALL_DIR' already exists but does not look like wizardry." >&2
exit 1
fi
parent=${INSTALL_DIR%/*}
if [ "$parent" = "$INSTALL_DIR" ]; then
parent=.
fi
if ! mkdir -p "$parent"; then
printf '%s\n' "Error: Unable to create directory '$parent'." >&2
exit 1
fi
if [ -n "$LOCAL_SOURCE" ]; then
printf '%s\n' "Copying wizardry from '$LOCAL_SOURCE' to '$INSTALL_DIR'..."
if ! mkdir -p "$INSTALL_DIR"; then
printf '%s\n' "Error: Unable to create install directory '$INSTALL_DIR'." >&2
exit 1
fi
copy_failed=0
for entry in "$LOCAL_SOURCE"/.[!.]* "$LOCAL_SOURCE"/..?* "$LOCAL_SOURCE"/*; do
[ -e "$entry" ] || continue
entry_name=${entry##*/}
case $entry_name in
.|..|.git|.codex|.uninstall)
continue
;;
esac
if ! cp -R "$entry" "$INSTALL_DIR/$entry_name"; then
copy_failed=1
break
fi
done
if [ "$copy_failed" -ne 0 ]; then
printf '%s\n' "Error: Unable to copy wizardry from '$LOCAL_SOURCE'." >&2
exit 1
fi
else
# Bootstrap wizardry by downloading from remote.
# Prefer git clone (creates proper git repo for update-wizardry),
# fall back to tarball download if git is not available.
repo_url=${WIZARDRY_REMOTE_REPO-https://github.com/andersaamodt/wizardry.git}
archive_url=${WIZARDRY_REMOTE_ARCHIVE-}
if [ -z "$archive_url" ]; then
archive_base=https://github.com/andersaamodt/wizardry/archive
archive_url=$archive_base/refs/heads/main.tar.gz
fi
# Try git clone first if git is available
if command -v git >/dev/null 2>&1; then
printf '%s\n' "Downloading wizardry via git to '$INSTALL_DIR'..."
if download_via_git "$INSTALL_DIR" "$repo_url"; then
printf '%s\n' "Wizardry downloaded to '$INSTALL_DIR' (git repository)."
else
# Git clone failed, fall back to tarball
printf '%s\n' "Git clone failed, trying tarball download..." >&2
use_tarball=1
fi
else
# Git not available, use tarball
use_tarball=1
fi
# Tarball fallback
if [ "${use_tarball-}" = "1" ]; then
if ! command -v tar >/dev/null 2>&1; then
printf '%s\n' "Error: tar is required to unpack wizardry when git is not available." >&2
exit 1
fi
BOOTSTRAP_TMP_DIR=$(mktemp -d "${TMPDIR:-/tmp}/wizardry-install.XXXXXX") || exit 1
archive="$BOOTSTRAP_TMP_DIR/wizardry.tar.gz"
printf '%s\n' "Downloading wizardry to '$INSTALL_DIR'..."
if ! download_file "$archive" "$archive_url"; then
exit 1
fi
if ! tar -xzf "$archive" -C "$BOOTSTRAP_TMP_DIR"; then
printf '%s\n' "Error: Failed to unpack wizardry archive." >&2
exit 1
fi
extracted=""
for candidate in "$BOOTSTRAP_TMP_DIR"/*; do
if [ -d "$candidate/spells" ]; then
extracted=$candidate
break
fi
done
if [ -z "$extracted" ]; then
printf '%s\n' "Error: Downloaded archive did not contain wizardry spells." >&2
exit 1
fi
if ! mv "$extracted" "$INSTALL_DIR"; then
printf '%s\n' "Error: Unable to move wizardry into '$INSTALL_DIR'." >&2
exit 1
fi
printf '%s\n' "Wizardry downloaded to '$INSTALL_DIR'."
fi
fi
ABS_DIR=$(cd "$INSTALL_DIR" && pwd -P)
fi
# Now that the wizardry tree is available, point at the helper spells we rely
# on to set up shell integration and ask for confirmation.
INVOKE_WIZARDRY="$ABS_DIR/spells/.imps/sys/invoke-wizardry"
DETECT_RC_FILE="$ABS_DIR/spells/divination/detect-rc-file"
NIX_SHELL_ADD="$ABS_DIR/spells/.imps/sys/nix-shell-add"
NIX_SHELL_REMOVE="$ABS_DIR/spells/.imps/sys/nix-shell-remove"
CORE_INSTALLER=${WIZARDRY_CORE_INSTALLER:-$ABS_DIR/spells/.arcana/core/install-core}
ASK_YN="$ABS_DIR/spells/cantrips/ask-yn"
# MUD-related helpers (these are used in the optional MUD installation section)
INSTALL_CD="$ABS_DIR/spells/.arcana/mud/install-cd"
TOGGLE_CD="$ABS_DIR/spells/.arcana/mud/toggle-cd"
TOGGLE_ALL_MUD="$ABS_DIR/spells/.arcana/mud/toggle-all-mud"
# Fresh local-source installs may call copied helper spells before
# invoke-wizardry has ever prepared PATH. Seed the copied tree onto PATH so
# sourced helpers such as env-clear and die resolve from the installation being
# prepared instead of depending on the caller's ambient environment.
install_saved_path=$PATH
PATH="$ABS_DIR/spells"
if [ -d "$ABS_DIR/spells/.imps" ]; then
PATH="$PATH:$ABS_DIR/spells/.imps"
for impdir in "$ABS_DIR"/spells/.imps/*; do
[ -d "$impdir" ] || continue
PATH="$PATH:$impdir"
done
fi
for spell_dir in "$ABS_DIR"/spells/*; do
[ -d "$spell_dir" ] || continue
PATH="$PATH:$spell_dir"
done
PATH="$PATH:$install_saved_path"
export PATH
# NOTE: Spells no longer need to be pre-installed.
# Users can still use install() functions in spells for explicit customizations like aliases.
# Export environment variables for all helper spells so that they use the new
# versions being installed, not any old versions that may be in PATH.
# This prevents issues where a user has an old wizardry installation in PATH.
LEARN_SPELL="$ABS_DIR/spells/spellcraft/learn"
MEMORIZE_LEARN="$ABS_DIR/spells/spellcraft/learn"
MEMORIZE_SPELL_HELPER="$ABS_DIR/spells/cantrips/memorize"
export DETECT_RC_FILE LEARN_SPELL MEMORIZE_LEARN MEMORIZE_SPELL_HELPER NIX_SHELL_INIT
# Bootstrap color definitions (inline - cannot rely on wizardry spells during install)
# Create the escape character in a POSIX-compliant way.
ESC=$(printf '\033')
disable_colors() {
RESET=''
GREEN=''
CYAN=''
YELLOW=''
BLUE=''
BOLD=''
GREY=''
RED=''
}
# Set up colors inline since install script cannot assume spells are available
RESET="${ESC}[0m"
GREEN="${ESC}[32m"
CYAN="${ESC}[36m"
YELLOW="${ESC}[33m"
BLUE="${ESC}[34m"
BOLD="${ESC}[1m"
GREY="${ESC}[2m"
RED="${ESC}[31m"
# Disable colors if stdout is not a terminal or NO_COLOR is set
if [ ! -t 1 ]; then
disable_colors
elif [ -n "${NO_COLOR-}" ]; then
disable_colors
fi
# Helper functions for formatted messages
info_msg() {
printf '%s==>%s %s\n' "$BOLD$CYAN" "$RESET" "$*"
}
success_msg() {
printf '%s✓%s %s\n' "$BOLD$GREEN" "$RESET" "$*"
}
section_msg() {
printf '\n%s===== %s =====%s\n' "$BOLD$BLUE" "$*" "$RESET"
}
substep_msg() {
printf '%s ->%s %s\n' "$CYAN" "$RESET" "$*"
}
if [ ! -x "$INVOKE_WIZARDRY" ]; then
printf '%s\n' "Error: Expected helper '$INVOKE_WIZARDRY' is missing or not executable." >&2
exit 1
fi
if [ ! -x "$DETECT_RC_FILE" ]; then
printf '%s\n' "Error: Required helper '$DETECT_RC_FILE' is missing." >&2
exit 1
fi
if [ ! -x "$ASK_YN" ]; then
printf '%s\n' "Error: Required cantrip '$ASK_YN' is missing." >&2
exit 1
fi
# Note: Core prerequisite installation is deferred until after the install plan is shown
# This avoids prompting for sudo before the user understands what will happen
# Perform comprehensive installation detection across all RC files
# This checks multiple RC files to see if wizardry is installed anywhere
detect_installation_status() {
# Check common RC files for wizardry invoke-wizardry source or wizardry markers
rc_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $HOME/.zshrc $HOME/.zprofile $HOME/.config/home-manager/home.nix $HOME/.config/nixpkgs/home.nix /etc/nixos/configuration.nix"
found_in_any_rc=0
for rc_file in $rc_files; do
if [ -f "$rc_file" ]; then
# Look for invoke-wizardry source line or wizardry markers
if grep -qE "(invoke-wizardry|wizardry: invoke-wizardry|wizardry: wizardry-init)" "$rc_file" 2>/dev/null; then
found_in_any_rc=1
break
fi
# Also check for legacy PATH entries with wizardry dir
if grep -q "$ABS_DIR" "$rc_file" 2>/dev/null; then
found_in_any_rc=1
break
fi
fi
done
# Check if spells directory exists
spells_exist=0
if [ -d "$ABS_DIR/spells" ]; then
spells_exist=1
fi
# Return status: 0=not installed, 1=partially installed, 2=fully installed
if [ "$found_in_any_rc" -eq 0 ] && [ "$spells_exist" -eq 0 ]; then
return 0 # Not installed
elif [ "$found_in_any_rc" -eq 1 ] && [ "$spells_exist" -eq 1 ]; then
return 2 # Fully installed
else
return 1 # Partially installed (needs repair)
fi
}
# Detect installation status for better user messaging. Status 1 and 2 are
# meaningful states, not fatal errors, so capture them through an if statement
# where set -e will not abort the installer.
if detect_installation_status; then
install_status=0
else
install_status=$?
fi
case $install_status in
0)
install_mode="fresh"
;;
1)
install_mode="repair"
;;
2)
install_mode="detected"
;;
*)
install_mode="fresh"
;;
esac
# Check if invoke-wizardry source line is already in the RC file.
# This replaces the old PATH-based detection approach.
invoke_wizardry_installed=0
check_invoke_wizardry_status() {
if [ -z "${detect_rc_file_value-}" ]; then
return 1
fi
if [ ! -f "$detect_rc_file_value" ]; then
return 1
fi
# For nix format, check for wizardry shell init marker
case $detect_format_value in
nix)
if grep -qE "wizardry: (invoke-wizardry|wizardry-init)" "$detect_rc_file_value" 2>/dev/null; then
return 0
fi
;;
*)
# For shell format, check for invoke-wizardry source line or marker
if grep -qE "(invoke-wizardry|wizardry: invoke-wizardry|wizardry: wizardry-init)" "$detect_rc_file_value" 2>/dev/null; then
return 0
fi
;;
esac
return 1
}
# Detect the rc file and format early - needed for installation status check
detect_rc_file_value=""
detect_format_value=""
detect_platform_value=""
if detect_output="$($DETECT_RC_FILE 2>/dev/null)"; then
while IFS='=' read -r key value; do
case $key in
rc_file)
detect_rc_file_value=$value
;;
format)
detect_format_value=$value
;;
platform)
detect_platform_value=$value
;;
esac
done <<EOF
$detect_output
EOF
fi
# On NixOS, if no nix config was found (format is shell), prompt for the config path
# since NixOS systems always have a configuration.nix somewhere
if [ "$detect_platform_value" = "nixos" ] && [ "$detect_format_value" = "shell" ]; then
info_msg "NixOS detected but no configuration file found at standard paths"
substep_msg "Checked: /etc/nixos/configuration.nix"
substep_msg "Checked: ~/.config/home-manager/home.nix"
substep_msg "Checked: ~/.config/nixpkgs/home.nix"
printf '\n'
# Check if we have input available (either terminal or piped/redirected)
# Respect ASK_CANTRIP_INPUT=none to disable interactive prompts
if [ "${ASK_CANTRIP_INPUT-}" = "none" ]; then
printf '%s\n' "Error: No NixOS configuration file found and no interactive input available." >&2
printf '%s\n' "Set NIXOS_CONFIG environment variable to specify the configuration file path." >&2
exit 1
fi
printf '%s' "Enter the path to your configuration.nix: " >&2
IFS= read -r user_nix_config
if [ -n "$user_nix_config" ]; then
# Expand ~/ patterns. Note: ~user/ patterns are not supported;
# users should provide absolute paths or use ~/ for their home.
case $user_nix_config in
~/*)
user_nix_config=$HOME/${user_nix_config#\~/}
;;
esac
# Normalize paths that look like absolute paths but are missing
# the leading slash (e.g., "etc/nixos/configuration.nix").
case $user_nix_config in
/*)
# Already absolute path
:
;;
[Hh]ome/*|[Ee]tc/*|[Uu]sers/*|[Uu]sr/*|[Oo]pt/*|[Vv]ar/*|[Nn]ix/*)
# Looks like an absolute path missing the leading slash
user_nix_config="/$user_nix_config"
;;
esac
if [ -f "$user_nix_config" ]; then
detect_rc_file_value=$user_nix_config
detect_format_value=nix
# Export for any helper that needs it
NIXOS_CONFIG=$user_nix_config
export NIXOS_CONFIG
success_msg "Using configuration file: $user_nix_config"
else
printf '%s\n' "Error: File not found: $user_nix_config" >&2
exit 1
fi
else
printf '%s\n' "Error: NixOS requires a configuration file path." >&2
exit 1
fi
fi
# Now that we have detect_rc_file_value, we can check if invoke-wizardry is installed
if check_invoke_wizardry_status; then
invoke_wizardry_installed=1
else
invoke_wizardry_installed=0
fi
# Determine if installation is needed
needs_installation=0
if [ "$install_status" -eq 0 ]; then
# Not installed at all - needs full installation
needs_installation=1
elif [ "$install_status" -eq 1 ]; then
# Partially installed - needs repair
needs_installation=1
elif [ "$invoke_wizardry_installed" -eq 0 ]; then
# Fully installed files but no invoke-wizardry in rc (legacy or needs upgrade)
needs_installation=1
fi
# If wizardry is fully installed (including invoke-wizardry), show the options menu
# BUT only if the files existed before we started (not just downloaded)
if [ "$needs_installation" -eq 0 ] && [ "$WIZARDRY_EXISTED_BEFORE" -eq 1 ]; then
# Wizardry is already installed - offer options via simple text menu
printf '\n'
info_msg "Wizardry is already installed at: $ABS_DIR"
printf '\n'
printf '%sWhat would you like to do?%s\n' "$BOLD" "$RESET"
printf ' 1) %sRepair%s - Fix shell configuration if needed\n' "$CYAN" "$RESET"
printf ' 2) %sReinstall%s - Reinstall wizardry (keeps current location)\n' "$CYAN" "$RESET"
printf ' 3) %sUninstall%s - Remove wizardry from your system\n' "$CYAN" "$RESET"
printf ' 4) %sExit%s - Do nothing and exit\n' "$CYAN" "$RESET"
printf '\n'
choice=""
if [ "${WIZARDRY_INSTALL_ASSUME_YES-}" = "1" ]; then
choice="1"
printf '%s\n' "Non-interactive mode: defaulting to repair (option 1)"
else
# Read user choice
# Check if we can read input. When running via curl | sh, stdin is a pipe
# containing the script content, not user input. We must check if we can
# read from /dev/tty instead.
if [ -t 0 ]; then
# stdin is a terminal - read directly from stdin
printf '%sEnter your choice (1-4): %s' "$BOLD" "$RESET" >&2
IFS= read -r choice
elif [ -r /dev/tty ]; then
# stdin is not a terminal, but /dev/tty is available
# This handles curl | sh case where stdin has script content
printf '%sEnter your choice (1-4): %s' "$BOLD" "$RESET" >&2
IFS= read -r choice </dev/tty || choice=
fi
if [ -z "$choice" ]; then
printf '%s\n' "Wizardry is already installed. Run 'menu' or 'mud' to use it."
exit 0
fi
fi
case $choice in
1)
# Repair - continue with normal flow
info_msg "Repairing wizardry installation..."
needs_installation=1
;;
2)
# Reinstall - continue with normal installation flow
info_msg "Reinstalling wizardry..."
needs_installation=1
;;
3)
# Uninstall - use the uninstall script from the existing installation
uninstall_script="$ABS_DIR/.uninstall"
if [ -x "$uninstall_script" ]; then
exec "$uninstall_script"
else
printf '%s\n' "Error: uninstall script not found at '$uninstall_script'." >&2
printf '%s\n' "The uninstall script is created during installation." >&2
printf '%s\n' "You may need to manually remove '$ABS_DIR' and the invoke-wizardry source line from your shell rc file." >&2
exit 1
fi
;;
4|"")
# Exit (empty input also exits)
printf '%s\n' "No changes made. Run 'menu' or 'mud' to use wizardry."
exit 0
;;
*)
# Invalid choice - treat as exit
printf '%s\n' "Invalid choice. No changes made."
exit 0
;;
esac
fi
section_msg "Installation Plan"
# Show what will be installed (streamlined for novice users)
if [ -n "$detect_rc_file_value" ]; then
if [ "$detect_format_value" = "nix" ]; then
info_msg "Configuration file: $detect_rc_file_value"
else
info_msg "Shell configuration: $detect_rc_file_value"
fi
else
info_msg "Will add wizardry to your shell configuration"
substep_msg "${YELLOW}Warning: Could not auto-detect shell configuration file${RESET}"
fi
substep_msg "This sets up wizardry commands in your terminal"
# MUD features are now always enabled by default (no prompt)
# The config will default to enabled when unset, making MUD discoverable in main-menu
# Users can disable via mud-menu after installation if desired
install_mud=1
# Confirm with the user (or their automation) before editing their configuration.
# When stdin is not interactive we fall back to the non-blocking ASK_CANTRIP_INPUT=none
# pathway so wget|sh installs do not hang.
proceed=0
printf '\n'
if [ "${WIZARDRY_INSTALL_ASSUME_YES-}" = "1" ]; then
proceed=1
else
# Handle different input scenarios for curl|sh and other non-interactive cases
if [ ! -t 0 ]; then
# stdin is not a terminal (e.g., curl|sh, wget|sh, or piped input)
if [ -r /dev/tty ]; then
# /dev/tty is available - force ask-yn to use it instead of stdin
# This prevents ask-yn from trying to read from stdin (which contains the script)
if ASK_CANTRIP_INPUT=tty "$ASK_YN" "Proceed with installation?" yes >/dev/null; then
proceed=1
fi
elif [ -z "${ASK_CANTRIP_INPUT-}" ]; then
# No /dev/tty and no ASK_CANTRIP_INPUT set - use default
if ASK_CANTRIP_INPUT=none "$ASK_YN" "Proceed with installation?" yes >/dev/null; then
proceed=1
fi
else
# ASK_CANTRIP_INPUT is already set by the user - respect it
if "$ASK_YN" "Proceed with installation?" yes >/dev/null; then
proceed=1
fi
fi
else
# stdin is a terminal - normal interactive mode
if "$ASK_YN" "Proceed with installation?" yes >/dev/null; then
proceed=1
fi
fi
fi
if [ "$proceed" -ne 1 ]; then
printf '%s\n' "Installation cancelled."
exit 1
fi
# Install core prerequisites now that user has confirmed and seen the plan
# This is where we might ask for sudo on some platforms, so it comes after showing the plan
if [ -x "$CORE_INSTALLER" ]; then
section_msg "Checking Core Prerequisites"
# Show what we're checking with a sublist
info_msg "Verifying required tools:"
# Check and display status of each core dependency
# List all common prerequisites together
prereq_tar_ok=0
prereq_curl_wget_ok=0
if command -v tar >/dev/null 2>&1; then
prereq_tar_ok=1
substep_msg "tar: ${GREEN}✓ installed${RESET}"
else
substep_msg "tar: ${RED}✗ missing (required)${RESET}"
fi
if command -v curl >/dev/null 2>&1; then
prereq_curl_wget_ok=1
substep_msg "curl: ${GREEN}✓ installed${RESET}"
elif command -v wget >/dev/null 2>&1; then
prereq_curl_wget_ok=1
substep_msg "wget: ${GREEN}✓ installed${RESET}"
else
substep_msg "curl/wget: ${YELLOW}neither found (needed for remote downloads)${RESET}"
fi
if command -v git >/dev/null 2>&1; then
substep_msg "git: ${GREEN}✓ installed${RESET}"
else
substep_msg "git: ${YELLOW}not found (will attempt install)${RESET}"
fi
# Platform-specific checks
case $(uname -s 2>/dev/null) in
Darwin)
# macOS specific
if command -v stty >/dev/null 2>&1; then
substep_msg "stty: ${GREEN}✓ installed${RESET}"
else
substep_msg "stty: ${YELLOW}not found (will attempt install)${RESET}"
fi
if command -v tput >/dev/null 2>&1; then
substep_msg "tput: ${GREEN}✓ installed${RESET}"
else
substep_msg "tput: ${YELLOW}not found (will attempt install)${RESET}"
fi
;;
*)
# Linux
if command -v bwrap >/dev/null 2>&1; then
substep_msg "bubblewrap: ${GREEN}✓ installed${RESET}"
else
substep_msg "bubblewrap: ${YELLOW}not found (optional, for test sandboxing)${RESET}"
fi
if command -v stty >/dev/null 2>&1; then
substep_msg "stty: ${GREEN}✓ installed${RESET}"
else
substep_msg "stty: ${YELLOW}not found (will attempt install)${RESET}"
fi
if command -v tput >/dev/null 2>&1; then
substep_msg "tput: ${GREEN}✓ installed${RESET}"
else
substep_msg "tput: ${YELLOW}not found (will attempt install)${RESET}"
fi
;;
esac
# Run core installer - it returns success even if some optional deps fail
ASSUME_YES=1 "$CORE_INSTALLER" >/dev/null 2>&1 || true
success_msg "Core prerequisites verified"
fi
section_msg "Setting up Shell Integration"
shell_setup_made=0
rebuild_success=0
profile_file_modified=""
# Determine the destination description for messages
if [ -n "$detect_rc_file_value" ]; then
rc_destination=$(printf "'%s'" "$detect_rc_file_value")
else
rc_destination="your shell configuration file"
fi
# Add invoke-wizardry source line to the shell configuration file.
# This replaces the old approach of adding directories to PATH manually.
# invoke-wizardry dynamically sets up PATH at shell startup.
# Skip if detect-rc-file failed to find a configuration file
if [ -n "$detect_rc_file_value" ]; then
info_msg "Updating $rc_destination..."
# Generate the source line for invoke-wizardry
invoke_wizardry_source_line=". \"$INVOKE_WIZARDRY\""
case $detect_format_value in
nix)
# For NixOS, use nix-shell-add to add shell initialization code
substep_msg "Using nix-shell-add for NixOS integration"
if [ -x "$NIX_SHELL_ADD" ]; then
if printf '%s\n' "$invoke_wizardry_source_line" | "$NIX_SHELL_ADD" wizardry-init "$detect_rc_file_value" bash 2>/dev/null; then
shell_setup_made=1
substep_msg "Added to NixOS configuration: ${GREEN}success${RESET}"
else
printf '%s\n' "Error: Failed to add wizardry initialization to '$detect_rc_file_value'." >&2
exit 1
fi
else
printf '%s\n' "Error: nix-shell-add helper not found at '$NIX_SHELL_ADD'." >&2
exit 1
fi
;;
*)
# For standard shell rc files, add the source line directly with a wizardry marker
# Check if the source line already exists
substep_msg "Checking for existing wizardry initialization..."
if grep -qE "(invoke-wizardry|wizardry: wizardry-init)" "$detect_rc_file_value" 2>/dev/null; then
shell_setup_made=0
substep_msg "Already configured: ${GREEN}yes${RESET} (skipping)"
else
substep_msg "Already configured: no (adding now)"
# Create rc file if it doesn't exist
if [ ! -f "$detect_rc_file_value" ]; then
if touch "$detect_rc_file_value" 2>/dev/null; then
substep_msg "Created new file: ${GREEN}success${RESET}"
else
printf '%s\n' "Error: Failed to create '$detect_rc_file_value'." >&2
printf '%s\n' "Check that the directory exists and you have write permissions." >&2
exit 1
fi
fi
# Add the source line with inline marker
if printf '%s\n' "$invoke_wizardry_source_line # wizardry: wizardry-init" >> "$detect_rc_file_value" 2>/dev/null; then
shell_setup_made=1
substep_msg "Added source line: ${GREEN}success${RESET}"
else
printf '%s\n' "Error: Failed to write to '$detect_rc_file_value'." >&2
printf '%s\n' "Check that you have write permissions for this file." >&2
exit 1
fi
fi
;;
esac
if [ "$shell_setup_made" -eq 1 ]; then
success_msg "Shell configuration updated in $rc_destination"
# For bash users: if we modified .bashrc, ensure .bash_profile sources it
# This is needed because bash login shells (common on macOS) don't source .bashrc
# We do this after modifying .bashrc to ensure login shells pick up the changes
case "$detect_rc_file_value" in
*/.bashrc)
bash_profile="$HOME/.bash_profile"
if [ ! -f "$bash_profile" ]; then
# Create a .bash_profile that sources .bashrc
# This is the standard pattern for bash configuration
substep_msg "Creating .bash_profile to source .bashrc (for login shells)"
cat > "$bash_profile" <<'BASH_PROFILE_EOF'
# Source .bashrc if it exists (added by wizardry installer)
# This ensures bash login shells (like those opened by Terminal.app on macOS)
# get the same configuration as interactive shells
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
BASH_PROFILE_EOF
substep_msg "Created $bash_profile"
profile_file_modified="$bash_profile"
elif ! grep -q '\.bashrc' "$bash_profile" 2>/dev/null; then
# .bash_profile exists but doesn't source .bashrc
# Add the sourcing logic to ensure our changes are picked up
substep_msg "Updating .bash_profile to source .bashrc"
cat >> "$bash_profile" <<'BASH_PROFILE_APPEND'
# Source .bashrc if it exists (added by wizardry installer)
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
BASH_PROFILE_APPEND
substep_msg "Updated $bash_profile"
profile_file_modified="$bash_profile"
fi
;;
*/.zshrc)
# For zsh users: if we modified .zshrc, ensure .zprofile sources it
# This is needed because zsh login shells may not source .zshrc
zsh_profile="$HOME/.zprofile"
if [ ! -f "$zsh_profile" ]; then
# Create a .zprofile that sources .zshrc
substep_msg "Creating .zprofile to source .zshrc (for login shells)"
cat > "$zsh_profile" <<'ZSH_PROFILE_EOF'
# Source .zshrc if it exists (added by wizardry installer)
# This ensures zsh login shells get the same configuration as interactive shells