-
Notifications
You must be signed in to change notification settings - Fork 357
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·2061 lines (1920 loc) · 84.1 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·2061 lines (1920 loc) · 84.1 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
set -e
# APM CLI Installer Script
# Usage: curl -sSL https://aka.ms/apm-unix | sh
# Specific version: curl -sSL https://aka.ms/apm-unix | sh -s -- @v1.2.3 (or VERSION=v1.2.3)
# Prefix install: curl -sSL https://aka.ms/apm-unix | sh -s -- --prefix "$HOME/.local"
# Custom install dir: curl -sSL https://aka.ms/apm-unix | APM_INSTALL_DIR=$HOME/tools/bin sh
# Custom repository: APM_REPO=ghe-org/apm sh install.sh
# GitHub Enterprise: GITHUB_URL=https://gh.corp.com sh install.sh
# Enterprise mirror: APM_RELEASE_BASE_URL=https://mirror.example/apm VERSION=v1.2.3 sh install.sh
# PyPI mirror fallback: APM_PYPI_INDEX_URL=https://mirror.example/pypi/simple sh install.sh
# Fail closed: APM_NO_DIRECT_FALLBACK=1 sh install.sh
# For private repositories, use with authentication:
# curl -sSL -H "Authorization: token $GITHUB_APM_PAT" \
# https://raw.githubusercontent.com/microsoft/apm/main/install.sh | \
# GITHUB_APM_PAT=$GITHUB_APM_PAT sh
# Colors for output
RED=$(printf '\033[0;31m')
GREEN=$(printf '\033[0;32m')
BLUE=$(printf '\033[0;34m')
YELLOW=$(printf '\033[1;33m')
NC=$(printf '\033[0m') # No Color
apm_echo() {
printf '%s\n' "$*"
}
# Configuration (all overridable via environment variables)
APM_REPO="${APM_REPO:-microsoft/apm}"
_APM_INSTALL_DIR_SET="${APM_INSTALL_DIR:+1}"
_APM_LIB_DIR_SET="${APM_LIB_DIR:+1}"
_APM_PREFIX_SET=""
APM_INSTALL_PREFIX=""
APM_INSTALL_DIR="${APM_INSTALL_DIR:-$HOME/.local/bin}"
APM_LIB_DIR="${APM_LIB_DIR:-$(dirname "$APM_INSTALL_DIR")/lib/apm}"
BINARY_NAME="apm"
GITHUB_URL="${GITHUB_URL:-https://github.com}"
APM_RELEASE_BASE_URL="${APM_RELEASE_BASE_URL:-}"
APM_RELEASE_METADATA_URL="${APM_RELEASE_METADATA_URL:-}"
APM_INSTALLER_BASE_URL="${APM_INSTALLER_BASE_URL:-}"
APM_PYPI_INDEX_URL="${APM_PYPI_INDEX_URL:-}"
APM_NO_DIRECT_FALLBACK="${APM_NO_DIRECT_FALLBACK:-}"
APM_NO_MODIFY_PATH="${APM_NO_MODIFY_PATH:-}"
_APM_MODIFY_PATH_REQUEST="inherit"
_APM_PREVIOUS_SHELL_RECEIPT_VALID=""
_APM_PREVIOUS_SHELL_SELECTED_BIN=""
_APM_PREVIOUS_SHELL_HOOK_DIR=""
_APM_PREVIOUS_SHELL_HOOK_KIND=""
_APM_PREVIOUS_SHELL_HOOK_DIGEST=""
_APM_PREVIOUS_SHELL_MODIFY_PATH=""
_APM_NEW_SHELL_HOOK_DIR=""
_APM_NEW_SHELL_HOOK_KIND="none"
_APM_NEW_SHELL_HOOK_DIGEST="none"
_APM_NATIVE_RECEIPT_WRITTEN=""
# INSTALL_OWNERSHIP_BEGIN
# Resolve symlink targets portably, including on macOS without readlink -f.
apm_real_path() (
_path="$1"
case "$_path" in /*) ;; *) _path="$PWD/$_path" ;; esac
_links=0
while [ -L "$_path" ]; do
_links=$((_links + 1))
[ "$_links" -le 40 ] || return 1
_link="$(readlink "$_path")" || return 1
case "$_link" in
/*) _path="$_link" ;;
*) _path="$(dirname "$_path")/$_link" ;;
esac
done
_parent="$(dirname "$_path")"
_leaf="${_path##*/}"
while [ ! -d "$_parent" ]; do
[ "$_parent" != "/" ] || return 1
_leaf="${_parent##*/}/$_leaf"
_parent="${_parent%/*}"
[ -n "$_parent" ] || _parent="/"
done
_parent="$(cd -P "$_parent" && pwd)" || return 1
printf '%s/%s\n' "${_parent%/}" "$_leaf"
)
apm_install_error() {
printf '%s\n' "[x] $*" >&2
exit 1
}
apm_print_usage() {
printf '%s\n' \
"Usage: sh install.sh [--prefix PATH] [@vVERSION]" \
"" \
"Options:" \
" --prefix PATH Install launcher at PATH/bin and bundle at PATH/lib/apm." \
" --prefix=PATH Same as --prefix PATH." \
" -h, --help Show this help." \
"" \
"Environment:" \
" APM_INSTALL_DIR Explicit launcher directory when --prefix is absent." \
" APM_LIB_DIR Explicit Unix bundle directory when --prefix is absent." \
" APM_NO_MODIFY_PATH Set 1 to skip native shell PATH setup, 0 to re-enable." \
" VERSION Release tag to install, equivalent to @vVERSION." \
"" \
"The installer never runs sudo. Run the shell with the privileges needed for" \
"the selected destination."
}
apm_set_cli_version() {
[ -n "$1" ] || apm_install_error "Version argument is empty. Use @v1.2.3 or VERSION=v1.2.3."
if [ -n "$VERSION" ] && [ "$VERSION" != "$1" ]; then
apm_install_error "VERSION is already set to $VERSION. Remove the positional version argument or make it match."
fi
VERSION="$1"
}
apm_parse_installer_args() {
while [ "$#" -gt 0 ]; do
case "$1" in
-h|--help)
apm_print_usage
exit 0
;;
--prefix=*)
[ -z "$_APM_PREFIX_SET" ] ||
apm_install_error "--prefix may be provided only once."
APM_INSTALL_PREFIX="${1#--prefix=}"
[ -n "$APM_INSTALL_PREFIX" ] ||
apm_install_error "Missing value for --prefix. Use --prefix PATH or --prefix=PATH."
_APM_PREFIX_SET=1
;;
--prefix)
[ -z "$_APM_PREFIX_SET" ] ||
apm_install_error "--prefix may be provided only once."
shift
[ "$#" -gt 0 ] ||
apm_install_error "Missing value for --prefix. Use --prefix PATH or --prefix=PATH."
case "$1" in
-*) apm_install_error "Missing value for --prefix. Use --prefix PATH or --prefix=PATH." ;;
esac
APM_INSTALL_PREFIX="$1"
[ -n "$APM_INSTALL_PREFIX" ] ||
apm_install_error "Missing value for --prefix. Use --prefix PATH or --prefix=PATH."
_APM_PREFIX_SET=1
;;
--*)
apm_install_error "Unknown option: $1. Run install.sh --help for usage."
;;
@v[0-9]*|@[0-9]*)
apm_set_cli_version "${1#@}"
;;
@*)
apm_install_error "Invalid version argument: $1. Use @v1.2.3."
;;
v[0-9]*|[0-9]*)
apm_set_cli_version "$1"
;;
*)
apm_install_error "Unknown argument: $1. Use @v1.2.3 for a version or --prefix PATH for destinations."
;;
esac
shift
done
}
apm_trim_trailing_slashes() (
_path="$1"
while [ "$_path" != "/" ] && [ "${_path%/}" != "$_path" ]; do
_path="${_path%/}"
done
printf '%s\n' "$_path"
)
# Produce one POSIX shell word without adding a Python dependency.
apm_shell_quote() (
_rest="$1"
printf "'"
while :; do
case "$_rest" in
*"'"*)
printf "%s'\\\\''" "${_rest%%"'"*}"
_rest="${_rest#*"'"}"
;;
*)
printf "%s'" "$_rest"
return
;;
esac
done
)
apm_print_path_guidance() {
_apm_run_hint="apm"
_apm_guidance_shell="$(apm_detect_current_shell)"
case "$1" in
*:*|*[[:cntrl:]]*)
if [ "$_apm_guidance_shell" = "fish" ]; then
_apm_run_hint="$(apm_fish_quote "$1/apm")"
else
_apm_run_hint="$(apm_shell_quote "$1/apm")"
fi
echo "Run APM using its absolute path:"
printf ' %s --version\n' "$_apm_run_hint"
echo "For PATH discovery, reinstall through its owner into a directory without ':' or control characters."
;;
*)
echo "For this shell, run the following for the current terminal:"
if [ "$_apm_guidance_shell" = "fish" ]; then
printf ' set -gx PATH %s $PATH\n' "$(apm_fish_quote "$1")"
echo "To persist it manually, add that line to your Fish config."
else
printf ' export PATH=%s:"$PATH"\n' "$(apm_shell_quote "$1")"
echo "To persist it manually, add that line to your shell profile."
fi
;;
esac
if [ "${_APM_PROFILE_CHANGE_STATUS:-}" = "partial" ]; then
echo "Some shell profile changes may remain. Inspect blocks marked 'apm shell setup' before retrying."
else
echo "No shell profiles were changed."
fi
}
apm_parse_modify_path_env() {
[ -n "$APM_NO_MODIFY_PATH" ] || {
_APM_MODIFY_PATH_REQUEST="inherit"
return 0
}
_apm_value="$(printf '%s' "$APM_NO_MODIFY_PATH" | tr '[:upper:]' '[:lower:]')"
case "$_apm_value" in
1|true|yes|on)
_APM_MODIFY_PATH_REQUEST="disable"
;;
0|false|no|off)
_APM_MODIFY_PATH_REQUEST="enable"
;;
*)
apm_install_error "Invalid APM_NO_MODIFY_PATH value: $APM_NO_MODIFY_PATH. Use 1 to opt out, 0 to re-enable, or leave it unset."
;;
esac
}
apm_path_is_safe_for_path() {
case "$1" in
*:*|*[[:cntrl:]]*) return 1 ;;
*) return 0 ;;
esac
}
apm_fish_quote() (
_rest="$1"
printf "'"
while [ -n "$_rest" ]; do
_char="${_rest%"${_rest#?}"}"
_rest="${_rest#?}"
case "$_char" in
"'") printf "%s" "\\'" ;;
"\\") printf "%s" "\\\\" ;;
*) printf "%s" "$_char" ;;
esac
done
printf "'"
)
apm_bool_env_active() {
_apm_flag_value="$1"
[ -n "$_apm_flag_value" ] || return 1
_apm_flag_value="$(printf '%s' "$_apm_flag_value" | tr '[:upper:]' '[:lower:]')"
case "$_apm_flag_value" in
0|false|no|off) return 1 ;;
*) return 0 ;;
esac
}
apm_has_controlling_tty() {
( : < /dev/tty ) >/dev/null 2>&1
}
apm_file_digest() {
[ -f "$1" ] || return 1
set -- $(cksum < "$1") || return 1
printf '%s:%s' "$1" "$2"
}
apm_reset_previous_shell_receipt() {
_APM_PREVIOUS_SHELL_RECEIPT_VALID=""
_APM_PREVIOUS_SHELL_RECEIPT_PRESENT=""
_APM_PREVIOUS_SHELL_SELECTED_BIN=""
_APM_PREVIOUS_SHELL_HOOK_DIR=""
_APM_PREVIOUS_SHELL_HOOK_KIND=""
_APM_PREVIOUS_SHELL_HOOK_DIGEST=""
_APM_PREVIOUS_SHELL_MODIFY_PATH=""
}
apm_read_shell_receipt() {
apm_reset_previous_shell_receipt
_apm_receipt="$1"
[ -e "$_apm_receipt" ] || [ -L "$_apm_receipt" ] || return 0
_APM_PREVIOUS_SHELL_RECEIPT_PRESENT=1
[ -f "$_apm_receipt" ] || return 0
[ ! -L "$_apm_receipt" ] || return 0
[ ! -x "$_apm_receipt" ] || return 0
_apm_seen_version=""
_apm_seen_owner=""
_apm_seen_selected_bin=""
_apm_seen_hook_dir=""
_apm_seen_hook_kind=""
_apm_seen_hook_digest=""
_apm_seen_modify_path=""
_apm_version=""
_apm_owner=""
_apm_selected_bin=""
_apm_hook_dir=""
_apm_hook_kind=""
_apm_hook_digest=""
_apm_modify_path=""
while IFS= read -r _apm_line || [ -n "$_apm_line" ]; do
case "$_apm_line" in
"") return 0 ;;
version=*)
[ -z "$_apm_seen_version" ] || return 0
_apm_seen_version=1
_apm_version="${_apm_line#version=}"
;;
owner=*)
[ -z "$_apm_seen_owner" ] || return 0
_apm_seen_owner=1
_apm_owner="${_apm_line#owner=}"
;;
selected_bin=*)
[ -z "$_apm_seen_selected_bin" ] || return 0
_apm_seen_selected_bin=1
_apm_selected_bin="${_apm_line#selected_bin=}"
;;
hook_dir=*)
[ -z "$_apm_seen_hook_dir" ] || return 0
_apm_seen_hook_dir=1
_apm_hook_dir="${_apm_line#hook_dir=}"
;;
hook_kind=*)
[ -z "$_apm_seen_hook_kind" ] || return 0
_apm_seen_hook_kind=1
_apm_hook_kind="${_apm_line#hook_kind=}"
;;
hook_digest=*)
[ -z "$_apm_seen_hook_digest" ] || return 0
_apm_seen_hook_digest=1
_apm_hook_digest="${_apm_line#hook_digest=}"
;;
modify_path=*)
[ -z "$_apm_seen_modify_path" ] || return 0
_apm_seen_modify_path=1
_apm_modify_path="${_apm_line#modify_path=}"
;;
*) return 0 ;;
esac
done < "$_apm_receipt"
[ "$_apm_version" = "1" ] || return 0
[ "$_apm_owner" = "native" ] || return 0
[ "$_apm_selected_bin" = "$APM_INSTALL_DIR" ] || return 0
case "$_apm_hook_kind" in posix|fish|none) ;; *) return 0 ;; esac
case "$_apm_modify_path" in managed|disabled) ;; *) return 0 ;; esac
[ -n "$_apm_seen_version$_apm_seen_owner$_apm_seen_selected_bin$_apm_seen_hook_dir$_apm_seen_hook_kind$_apm_seen_hook_digest$_apm_seen_modify_path" ] || return 0
[ -n "$_apm_seen_version" ] && [ -n "$_apm_seen_owner" ] &&
[ -n "$_apm_seen_selected_bin" ] && [ -n "$_apm_seen_hook_dir" ] &&
[ -n "$_apm_seen_hook_kind" ] && [ -n "$_apm_seen_hook_digest" ] &&
[ -n "$_apm_seen_modify_path" ] || return 0
if [ "$_apm_hook_kind" = "none" ]; then
[ "$_apm_hook_dir" = "none" ] && [ "$_apm_hook_digest" = "none" ] || return 0
else
case "$_apm_hook_dir" in /*) ;; *) return 0 ;; esac
[ "$_apm_hook_digest" != "none" ] || return 0
fi
_APM_PREVIOUS_SHELL_SELECTED_BIN="$_apm_selected_bin"
_APM_PREVIOUS_SHELL_HOOK_DIR="$_apm_hook_dir"
_APM_PREVIOUS_SHELL_HOOK_KIND="$_apm_hook_kind"
_APM_PREVIOUS_SHELL_HOOK_DIGEST="$_apm_hook_digest"
_APM_PREVIOUS_SHELL_MODIFY_PATH="$_apm_modify_path"
_APM_PREVIOUS_SHELL_RECEIPT_VALID=1
}
apm_mktemp_in_dir() {
_apm_dir="$1"
_apm_name="$2"
mktemp "$_apm_dir/.$_apm_name.XXXXXX"
}
apm_write_shell_receipt() {
_apm_modify_path="$1"
_apm_hook_dir="$2"
_apm_hook_kind="$3"
_apm_hook_digest="$4"
apm_path_is_safe_for_path "$APM_INSTALL_DIR" || return 0
_apm_receipt="$APM_LIB_DIR/.apm-shell-setup"
_apm_tmp="$(apm_mktemp_in_dir "$APM_LIB_DIR" "apm-shell-setup")" || return 1
{
printf 'version=1\n'
printf 'owner=native\n'
printf 'selected_bin=%s\n' "$APM_INSTALL_DIR"
printf 'hook_dir=%s\n' "$_apm_hook_dir"
printf 'hook_kind=%s\n' "$_apm_hook_kind"
printf 'hook_digest=%s\n' "$_apm_hook_digest"
printf 'modify_path=%s\n' "$_apm_modify_path"
} > "$_apm_tmp" || { rm -f "$_apm_tmp"; return 1; }
chmod 600 "$_apm_tmp" 2>/dev/null || true
mv "$_apm_tmp" "$_apm_receipt" || return 1
_APM_NATIVE_RECEIPT_WRITTEN=1
}
apm_preserve_previous_shell_receipt_to() {
_apm_target_dir="$1"
_apm_previous_receipt="$APM_LIB_DIR/.apm-shell-setup"
[ -f "$_apm_previous_receipt" ] || return 0
[ ! -L "$_apm_previous_receipt" ] || return 0
cp -p "$_apm_previous_receipt" "$_apm_target_dir/.apm-shell-setup" || return 1
}
apm_detect_current_shell() {
_apm_comm=""
if command -v ps >/dev/null 2>&1; then
_apm_comm="$(ps -p "$PPID" -o comm= 2>/dev/null | sed 's/^ *//;s/ *$//' || true)"
fi
_apm_base="${_apm_comm##*/}"
_apm_base="${_apm_base#-}"
case "$_apm_base" in
bash|zsh|fish)
printf '%s\n' "$_apm_base"
return 0
;;
esac
_apm_base="${SHELL##*/}"
_apm_base="${_apm_base#-}"
case "$_apm_base" in
bash|zsh|fish)
printf '%s\n' "$_apm_base"
return 0
;;
esac
printf 'unknown\n'
}
apm_detect_profile_shell() {
_apm_login_base="${SHELL##*/}"
_apm_login_base="${_apm_login_base#-}"
case "$_apm_login_base" in
bash|zsh|fish)
printf '%s\n' "$_apm_login_base"
return 0
;;
esac
apm_detect_current_shell
}
apm_is_desktop_shell_setup_eligible() {
[ -n "${APM_SELF_UPDATE_SOURCE:-}" ] && {
_APM_SHELL_SETUP_SKIP_REASON="self-update never edits shell profiles"
return 1
}
[ "$(id -u)" -eq 0 ] && {
_APM_SHELL_SETUP_SKIP_REASON="administrator installs never edit shell profiles"
return 1
}
if apm_bool_env_active "${CI:-}" || apm_bool_env_active "${GITHUB_ACTIONS:-}" ||
apm_bool_env_active "${TF_BUILD:-}" || apm_bool_env_active "${BUILD_BUILDID:-}" ||
apm_bool_env_active "${BUILDKITE:-}" || apm_bool_env_active "${GITLAB_CI:-}"; then
_APM_SHELL_SETUP_SKIP_REASON="CI environment detected"
return 1
fi
if ! apm_has_controlling_tty; then
_APM_SHELL_SETUP_SKIP_REASON="no interactive terminal detected"
return 1
fi
case "$HOME" in
/*) ;;
*) _APM_SHELL_SETUP_SKIP_REASON="HOME is not an absolute path"; return 1 ;;
esac
[ -d "$HOME" ] && [ -w "$HOME" ] && [ -x "$HOME" ] || {
_APM_SHELL_SETUP_SKIP_REASON="HOME is not writable"
return 1
}
apm_path_is_safe_for_path "$APM_INSTALL_DIR" || {
_APM_SHELL_SETUP_SKIP_REASON="the install bin path cannot be represented safely in PATH"
return 1
}
_APM_DETECTED_SHELL="$(apm_detect_profile_shell)"
case "$_APM_DETECTED_SHELL" in
bash|zsh|fish) return 0 ;;
*) _APM_SHELL_SETUP_SKIP_REASON="unsupported or unknown shell"; return 1 ;;
esac
}
apm_posix_hook_content() {
_apm_bin_word="$(apm_shell_quote "$1")"
printf '%s\n' \
"# APM shell setup generated by install.sh; do not edit inside this file." \
"# selected_bin=$1" \
"_apm_bin=$_apm_bin_word" \
'_apm_old_path="${PATH-}"' \
'_apm_had_path="${PATH+x}"' \
'PATH="$_apm_bin"' \
'if [ "$_apm_had_path" = "x" ]; then' \
' _apm_remaining="$_apm_old_path"' \
' while :; do' \
' case "$_apm_remaining" in' \
' *:*) _apm_entry="${_apm_remaining%%:*}"; _apm_remaining="${_apm_remaining#*:}"; _apm_more=1 ;;' \
' *) _apm_entry="$_apm_remaining"; _apm_more=0 ;;' \
' esac' \
' if [ "$_apm_entry" != "$_apm_bin" ]; then' \
' PATH="$PATH:$_apm_entry"' \
' fi' \
' [ "$_apm_more" = 1 ] || break' \
'done' \
'fi' \
'export PATH' \
'unset _apm_bin _apm_old_path _apm_had_path _apm_remaining _apm_entry _apm_more'
}
apm_fish_hook_content() {
_apm_bin_word="$(apm_fish_quote "$1")"
printf '%s\n' \
"# APM shell setup generated by install.sh; do not edit inside this file." \
"# selected_bin=$1" \
"set -l apm_bin $_apm_bin_word" \
"set -l apm_path_entries \$apm_bin" \
"for apm_entry in \$PATH" \
" if test \"\$apm_entry\" != \"\$apm_bin\"" \
" set apm_path_entries \$apm_path_entries \$apm_entry" \
" end" \
"end" \
"set -gx PATH \$apm_path_entries"
}
apm_expected_profile_block() {
_apm_hook="$1"
_apm_shell="$2"
printf '%s\n' "# >>> apm shell setup >>>"
printf '%s\n' "# Generated by install.sh. Remove this block to stop loading APM."
if [ "$_apm_shell" = "fish" ]; then
printf 'test -f %s; and source %s\n' "$(apm_fish_quote "$_apm_hook")" "$(apm_fish_quote "$_apm_hook")"
else
printf '[ -f %s ] && . %s\n' "$(apm_shell_quote "$_apm_hook")" "$(apm_shell_quote "$_apm_hook")"
fi
printf '%s\n' "# <<< apm shell setup <<<"
}
apm_existing_path_owned_safe() {
_apm_path="$1"
[ ! -L "$_apm_path" ] || return 1
[ -O "$_apm_path" ] || return 1
if [ -d "$_apm_path" ]; then
[ -w "$_apm_path" ] && [ -x "$_apm_path" ] || return 1
else
[ -f "$_apm_path" ] && [ -r "$_apm_path" ] && [ -w "$_apm_path" ] || return 1
fi
}
apm_prepare_owned_directory() {
_apm_dir="$1"
_apm_base="$HOME"
case "$_apm_dir" in "$HOME"|"$HOME"/*) _apm_base="$HOME" ;; *) _apm_base="" ;; esac
if [ -z "$_apm_base" ] && [ -n "${ZDOTDIR:-}" ]; then
case "$_apm_dir" in "$ZDOTDIR"|"$ZDOTDIR"/*) _apm_base="$ZDOTDIR" ;; esac
fi
if [ -z "$_apm_base" ] && [ -n "${XDG_CONFIG_HOME:-}" ]; then
case "$_apm_dir" in "$XDG_CONFIG_HOME"|"$XDG_CONFIG_HOME"/*) _apm_base="$XDG_CONFIG_HOME" ;; esac
fi
[ -n "$_apm_base" ] || return 1
_apm_current="$_apm_base"
if [ -e "$_apm_current" ] || [ -L "$_apm_current" ]; then
apm_existing_path_owned_safe "$_apm_current" || return 1
else
_apm_base_parent="${_apm_current%/*}"
[ -n "$_apm_base_parent" ] && [ "$_apm_base_parent" != "$_apm_current" ] || return 1
apm_existing_path_owned_safe "$_apm_base_parent" || return 1
mkdir "$_apm_current" || return 1
chmod 700 "$_apm_current" 2>/dev/null || true
fi
_apm_rest="${_apm_dir#"$_apm_base"}"
_apm_rest="${_apm_rest#/}"
while [ -n "$_apm_rest" ]; do
_apm_part="${_apm_rest%%/*}"
if [ "$_apm_part" = "$_apm_rest" ]; then
_apm_rest=""
else
_apm_rest="${_apm_rest#*/}"
fi
[ -n "$_apm_part" ] || continue
_apm_current="$_apm_current/$_apm_part"
if [ -e "$_apm_current" ] || [ -L "$_apm_current" ]; then
apm_existing_path_owned_safe "$_apm_current" || return 1
else
mkdir "$_apm_current" || return 1
chmod 700 "$_apm_current" 2>/dev/null || true
fi
done
}
apm_profile_has_unreachable_exit() {
_apm_file="$1"
[ -f "$_apm_file" ] || return 1
grep -E '^[[:space:]]*(exit|return)([[:space:]]+[0-9]+)?[[:space:]]*(#.*)?$' "$_apm_file" >/dev/null 2>&1
}
apm_write_generated_hook() {
_apm_hook="$1"
_apm_kind="$2"
_apm_parent="${_apm_hook%/*}"
[ "$_apm_parent" != "$_apm_hook" ] || return 1
apm_prepare_owned_directory "$_apm_parent" || return 1
_apm_tmp="$(apm_mktemp_in_dir "$_apm_parent" "${_apm_hook##*/}")" || return 1
if [ "$_apm_kind" = "fish" ]; then
apm_fish_hook_content "$APM_INSTALL_DIR" > "$_apm_tmp" || { rm -f "$_apm_tmp"; return 1; }
else
apm_posix_hook_content "$APM_INSTALL_DIR" > "$_apm_tmp" || { rm -f "$_apm_tmp"; return 1; }
fi
chmod 600 "$_apm_tmp" 2>/dev/null || true
if [ -e "$_apm_hook" ] || [ -L "$_apm_hook" ]; then
apm_existing_path_owned_safe "$_apm_hook" || { rm -f "$_apm_tmp"; return 1; }
_apm_current_digest="$(apm_file_digest "$_apm_hook" || true)"
_apm_generated_digest="$(apm_file_digest "$_apm_tmp" || true)"
if [ -z "$_APM_PREVIOUS_SHELL_RECEIPT_VALID" ] ||
{
{ [ "$_APM_PREVIOUS_SHELL_HOOK_DIR/${_apm_hook##*/}" != "$_apm_hook" ] ||
[ "$_APM_PREVIOUS_SHELL_HOOK_KIND" != "$_apm_kind" ] ||
[ "$_APM_PREVIOUS_SHELL_HOOK_DIGEST" != "$_apm_current_digest" ]; } &&
[ "$_apm_current_digest" != "$_apm_generated_digest" ]
}; then
rm -f "$_apm_tmp"
return 1
fi
fi
mv "$_apm_tmp" "$_apm_hook" || return 1
_APM_NEW_SHELL_HOOK_DIGEST="$(apm_file_digest "$_apm_hook" || printf 'none')"
return 0
}
apm_profile_contains_exact_block() {
_apm_file="$1"
_apm_block="$2"
_apm_content="$(cat "$_apm_file" 2>/dev/null || true)"
case "$_apm_content" in
*"$_apm_block"*) return 0 ;;
*) return 1 ;;
esac
}
apm_update_profile_file() {
_apm_file="$1"
_apm_block="$2"
_apm_parent="${_apm_file%/*}"
[ "$_apm_parent" != "$_apm_file" ] || return 1
apm_prepare_owned_directory "$_apm_parent" || return 1
[ ! -L "$_apm_file" ] || return 1
if [ -e "$_apm_file" ]; then
apm_existing_path_owned_safe "$_apm_file" || return 1
apm_profile_has_unreachable_exit "$_apm_file" && return 1
_apm_start_count="$(grep -F -c "# >>> apm shell setup >>>" "$_apm_file" 2>/dev/null || true)"
_apm_end_count="$(grep -F -c "# <<< apm shell setup <<<" "$_apm_file" 2>/dev/null || true)"
case "$_apm_start_count:$_apm_end_count" in
0:0) ;;
1:1)
apm_profile_contains_exact_block "$_apm_file" "$_apm_block" || return 1
return 0
;;
*) return 1 ;;
esac
_apm_before_digest="$(apm_file_digest "$_apm_file")" || return 1
_apm_tmp="$(apm_mktemp_in_dir "$_apm_parent" "${_apm_file##*/}.apm")" || return 1
cp -p "$_apm_file" "$_apm_tmp" || return 1
[ -s "$_apm_tmp" ] && printf '\n' >> "$_apm_tmp"
printf '%s\n' "$_apm_block" >> "$_apm_tmp" || { rm -f "$_apm_tmp"; return 1; }
[ "$(apm_file_digest "$_apm_file" || true)" = "$_apm_before_digest" ] ||
{ rm -f "$_apm_tmp"; return 1; }
mv "$_apm_tmp" "$_apm_file" || return 1
else
_apm_tmp="$(apm_mktemp_in_dir "$_apm_parent" "${_apm_file##*/}.apm")" || return 1
printf '%s\n' "$_apm_block" > "$_apm_tmp" || return 1
chmod 600 "$_apm_tmp" 2>/dev/null || true
[ ! -e "$_apm_file" ] || { rm -f "$_apm_tmp"; return 1; }
mv "$_apm_tmp" "$_apm_file" || return 1
fi
}
apm_remove_profile_block() {
_apm_file="$1"
_apm_block="$2"
[ -f "$_apm_file" ] && [ ! -L "$_apm_file" ] || return 1
apm_profile_contains_exact_block "$_apm_file" "$_apm_block" || return 0
_apm_parent="${_apm_file%/*}"
_apm_tmp="$(apm_mktemp_in_dir "$_apm_parent" "${_apm_file##*/}.apm-remove")" || return 1
_apm_content="$(cat "$_apm_file")" || return 1
_apm_prefix="${_apm_content%%"$_apm_block"*}"
_apm_suffix="${_apm_content#*"$_apm_block"}"
printf '%s%s' "$_apm_prefix" "$_apm_suffix" > "$_apm_tmp" || return 1
mv "$_apm_tmp" "$_apm_file" || return 1
}
apm_restore_profile_after_partial_write() {
_apm_file="$1"
_apm_backup="$2"
_apm_existed="$3"
_apm_expected_digest="$4"
[ -n "$_apm_expected_digest" ] || return 1
[ -f "$_apm_file" ] && [ ! -L "$_apm_file" ] || return 1
[ "$(apm_file_digest "$_apm_file" || true)" = "$_apm_expected_digest" ] || return 1
if [ "$_apm_existed" = "1" ]; then
[ -n "$_apm_backup" ] && [ -f "$_apm_backup" ] || return 1
mv "$_apm_backup" "$_apm_file" || return 1
else
rm -f "$_apm_file" || return 1
fi
}
apm_bash_login_profile() {
for _apm_candidate in "$HOME/.bash_profile" "$HOME/.bash_login" "$HOME/.profile"; do
if [ -e "$_apm_candidate" ]; then
[ -r "$_apm_candidate" ] && {
printf '%s\n' "$_apm_candidate"
return 0
}
return 1
fi
done
printf '%s\n' "$HOME/.bash_profile"
}
apm_zsh_profile() {
if [ -n "${ZDOTDIR:-}" ]; then
case "$ZDOTDIR" in
/*) printf '%s\n' "$ZDOTDIR/.zshrc"; return 0 ;;
*) return 1 ;;
esac
fi
if [ -f "$HOME/.zshenv" ] &&
grep -E '^[[:space:]]*(export[[:space:]]+)?ZDOTDIR=' "$HOME/.zshenv" >/dev/null 2>&1; then
return 1
fi
printf '%s\n' "$HOME/.zshrc"
}
apm_fish_profile() {
if [ -n "${XDG_CONFIG_HOME:-}" ]; then
case "$XDG_CONFIG_HOME" in
/*) printf '%s\n' "$XDG_CONFIG_HOME/fish/conf.d/apm.fish"; return 0 ;;
*) return 1 ;;
esac
else
printf '%s\n' "$HOME/.config/fish/conf.d/apm.fish"
fi
}
apm_record_shell_setup_not_configured() {
if [ "$_APM_PREVIOUS_SHELL_RECEIPT_VALID" = "1" ]; then
apm_write_shell_receipt "$_APM_PREVIOUS_SHELL_MODIFY_PATH" "$_APM_PREVIOUS_SHELL_HOOK_DIR" "$_APM_PREVIOUS_SHELL_HOOK_KIND" "$_APM_PREVIOUS_SHELL_HOOK_DIGEST" ||
apm_echo "${YELLOW}[!] APM installed, but shell setup receipt could not be saved.${NC}"
elif [ -z "$_APM_PREVIOUS_SHELL_RECEIPT_PRESENT" ]; then
apm_write_shell_receipt "managed" "none" "none" "none" ||
apm_echo "${YELLOW}[!] APM installed, but shell setup receipt could not be saved.${NC}"
fi
}
apm_configure_native_shell_path() {
_apm_modify_path="managed"
if [ -n "${APM_SELF_UPDATE_SOURCE:-}" ]; then
echo "Self-update leaves existing shell PATH setup unchanged."
return 0
fi
if [ "$_APM_MODIFY_PATH_REQUEST" = "disable" ]; then
_apm_previous_hook_dir="none"
_apm_previous_hook_kind="none"
_apm_previous_hook_digest="none"
if [ "$_APM_PREVIOUS_SHELL_RECEIPT_VALID" = "1" ]; then
_apm_previous_hook_dir="$_APM_PREVIOUS_SHELL_HOOK_DIR"
_apm_previous_hook_kind="$_APM_PREVIOUS_SHELL_HOOK_KIND"
_apm_previous_hook_digest="$_APM_PREVIOUS_SHELL_HOOK_DIGEST"
fi
apm_write_shell_receipt "disabled" "$_apm_previous_hook_dir" "$_apm_previous_hook_kind" "$_apm_previous_hook_digest" ||
apm_echo "${YELLOW}[!] APM installed, but shell setup preference could not be saved.${NC}"
echo "APM_NO_MODIFY_PATH is set; no shell profiles were changed."
echo "To remove existing APM shell setup, delete only blocks marked 'apm shell setup' from your shell profiles."
apm_print_path_guidance "$APM_INSTALL_DIR"
return 0
fi
if [ "$_APM_MODIFY_PATH_REQUEST" = "inherit" ] &&
[ "$_APM_PREVIOUS_SHELL_RECEIPT_VALID" = "1" ] &&
[ "$_APM_PREVIOUS_SHELL_MODIFY_PATH" = "disabled" ]; then
apm_write_shell_receipt "disabled" "$_APM_PREVIOUS_SHELL_HOOK_DIR" "$_APM_PREVIOUS_SHELL_HOOK_KIND" "$_APM_PREVIOUS_SHELL_HOOK_DIGEST" ||
apm_echo "${YELLOW}[!] APM installed, but shell setup preference could not be saved.${NC}"
echo "Shell PATH setup remains disabled by the previous APM installer preference."
echo "To re-enable on a normal desktop shell, rerun with APM_NO_MODIFY_PATH=0."
apm_print_path_guidance "$APM_INSTALL_DIR"
return 0
fi
if ! apm_is_desktop_shell_setup_eligible; then
apm_record_shell_setup_not_configured
apm_echo "${YELLOW}[!] APM installed, but PATH was not configured automatically: $_APM_SHELL_SETUP_SKIP_REASON.${NC}"
apm_print_path_guidance "$APM_INSTALL_DIR"
return 0
fi
_apm_hook_dir="$HOME/.apm/shell"
case "$_APM_DETECTED_SHELL" in
fish)
_apm_hook="$_apm_hook_dir/fish.fish"
_apm_profile="$(apm_fish_profile)" || {
apm_record_shell_setup_not_configured
apm_echo "${YELLOW}[!] APM installed, but PATH was not configured automatically: fish config location is ambiguous.${NC}"
apm_print_path_guidance "$APM_INSTALL_DIR"
return 0
}
_apm_kind="fish"
;;
zsh)
_apm_hook="$_apm_hook_dir/env"
_apm_profile="$(apm_zsh_profile)" || {
apm_record_shell_setup_not_configured
apm_echo "${YELLOW}[!] APM installed, but PATH was not configured automatically: zsh config location is ambiguous.${NC}"
apm_print_path_guidance "$APM_INSTALL_DIR"
return 0
}
_apm_kind="posix"
;;
*)
_apm_hook="$_apm_hook_dir/env"
_apm_profile="$(apm_bash_login_profile)" || {
apm_record_shell_setup_not_configured
apm_echo "${YELLOW}[!] APM installed, but PATH was not configured automatically: bash login profile is not readable.${NC}"
apm_print_path_guidance "$APM_INSTALL_DIR"
return 0
}
_apm_kind="posix"
;;
esac
if ! apm_write_generated_hook "$_apm_hook" "$_apm_kind"; then
apm_record_shell_setup_not_configured
apm_echo "${YELLOW}[!] APM installed, but PATH was not configured automatically: existing hook is not owned by this installer.${NC}"
apm_print_path_guidance "$APM_INSTALL_DIR"
return 0
fi
_apm_block="$(apm_expected_profile_block "$_apm_hook" "$_APM_DETECTED_SHELL")"
_apm_profile_failed=""
_apm_primary_had_block=""
_apm_primary_existed=""
_apm_primary_backup=""
_apm_primary_written_digest=""
_apm_configured_profiles="$_apm_profile"
_APM_PROFILE_CHANGE_STATUS=""
if [ -f "$_apm_profile" ] && apm_profile_contains_exact_block "$_apm_profile" "$_apm_block"; then
_apm_primary_had_block=1
fi
if [ -e "$_apm_profile" ] && [ ! -L "$_apm_profile" ]; then
_apm_primary_existed=1
_apm_primary_backup="$(apm_mktemp_in_dir "${_apm_profile%/*}" "${_apm_profile##*/}.apm-backup" || true)"
if [ -n "$_apm_primary_backup" ]; then
cp -p "$_apm_profile" "$_apm_primary_backup" || _apm_primary_backup=""
fi
fi
if ! apm_update_profile_file "$_apm_profile" "$_apm_block"; then
_apm_profile_failed="$_apm_profile"
[ -z "$_apm_primary_backup" ] || rm -f "$_apm_primary_backup"
else
_apm_primary_written_digest="$(apm_file_digest "$_apm_profile" || true)"
fi
if [ -z "$_apm_profile_failed" ] && [ "$_APM_DETECTED_SHELL" = "bash" ]; then
if ! apm_update_profile_file "$HOME/.bashrc" "$_apm_block"; then
_apm_profile_failed="$HOME/.bashrc"
[ -n "$_apm_primary_had_block" ] ||
apm_restore_profile_after_partial_write "$_apm_profile" "$_apm_primary_backup" "$_apm_primary_existed" "$_apm_primary_written_digest" ||
_APM_PROFILE_CHANGE_STATUS="partial"
else
_apm_configured_profiles="$_apm_configured_profiles and $HOME/.bashrc"
fi
fi
[ -z "$_apm_primary_backup" ] || rm -f "$_apm_primary_backup"
if [ -n "$_apm_profile_failed" ]; then
apm_echo "${YELLOW}[!] APM installed, but PATH was not configured automatically: cannot safely update $_apm_profile_failed.${NC}"
apm_write_shell_receipt "managed" "$_apm_hook_dir" "$_apm_kind" "$_APM_NEW_SHELL_HOOK_DIGEST" ||
apm_echo "${YELLOW}[!] APM installed, but shell setup receipt could not be saved.${NC}"
apm_print_path_guidance "$APM_INSTALL_DIR"
return 0
fi
_APM_NEW_SHELL_HOOK_DIR="$_apm_hook_dir"
_APM_NEW_SHELL_HOOK_KIND="$_apm_kind"
apm_write_shell_receipt "managed" "$_APM_NEW_SHELL_HOOK_DIR" "$_APM_NEW_SHELL_HOOK_KIND" "$_APM_NEW_SHELL_HOOK_DIGEST" ||
apm_echo "${YELLOW}[!] APM installed, but shell setup receipt could not be saved.${NC}"
apm_echo "${GREEN}[+] Shell PATH configured for $_APM_DETECTED_SHELL: $_apm_configured_profiles.${NC}"
_apm_current_shell="$(apm_detect_current_shell)"
if [ "$_apm_current_shell" = "$_APM_DETECTED_SHELL" ]; then
echo "Open a new terminal, or run this for the current shell:"
else
echo "Open a new $_APM_DETECTED_SHELL terminal, or run this for the current shell:"
fi
if [ "$_apm_current_shell" = "$_APM_DETECTED_SHELL" ] && [ "$_APM_DETECTED_SHELL" = "fish" ]; then
printf ' source %s\n' "$(apm_fish_quote "$_apm_hook")"
elif [ "$_apm_current_shell" = "$_APM_DETECTED_SHELL" ]; then
printf ' . %s\n' "$(apm_shell_quote "$_apm_hook")"
elif [ "$_apm_current_shell" = "fish" ]; then
printf ' set -gx PATH %s $PATH\n' "$(apm_fish_quote "$APM_INSTALL_DIR")"
else
printf ' export PATH=%s:"$PATH"\n' "$(apm_shell_quote "$APM_INSTALL_DIR")"
fi
}
apm_is_recognized_bundle() {
[ -f "$1/apm" ] && [ ! -L "$1/apm" ] && {
{ [ -f "$1/.apm-installed" ] && [ ! -L "$1/.apm-installed" ]; } ||
{ [ -f "$1/VERSION" ] && [ ! -L "$1/VERSION" ] &&
[ -d "$1/_internal" ] && [ ! -L "$1/_internal" ]; }
}
}
apm_probe_installation() {
case "$1" in
*/*) _probe_parent="${1%/*}"
[ -n "$_probe_parent" ] || _probe_parent="/" ;;
*) _probe_parent="." ;;
esac
_probe_original_parent="$_probe_parent"
while [ "$_probe_parent" != "/" ] && [ "$_probe_parent" != "." ]; do
if [ -d "$_probe_parent" ] && [ ! -x "$_probe_parent" ]; then
apm_install_error "Cannot inspect existing APM through $_probe_parent: directory is not searchable. Ask its owner to repair permissions before retrying."
fi
case "$_probe_parent" in
*/*) _probe_parent="${_probe_parent%/*}"
[ -n "$_probe_parent" ] || _probe_parent="/" ;;
*) _probe_parent="." ;;
esac
done
[ -e "$1" ] || [ -L "$1" ] || return 0
_candidate="$(apm_real_path "$1")" ||
apm_install_error "Cannot resolve existing APM at $1. Repair this path before reinstalling."
_candidate_lib="${_candidate%/*}"
[ -n "$_candidate_lib" ] || _candidate_lib="/"
if [ ! -f "$_candidate" ] || [ "${_candidate##*/}" != "apm" ]; then
apm_install_error "Invalid existing APM launcher at $1. Repair it with its original installer before retrying."
fi
case "$_candidate" in
*/Cellar/*|*/Caskroom/*|*/.linuxbrew/*)
apm_install_error "Package-manager installation at $1. Update or uninstall it with its package manager; the installer will not replace it." ;;
esac
if ! apm_is_recognized_bundle "$_candidate_lib"; then
apm_install_error "Unrecognized or package-manager installation at $1. Update or uninstall it with its original installer; refusing a second installation."
fi
if [ -n "$_apm_existing_binary" ] && [ "$_candidate" != "$_apm_existing_binary" ]; then
apm_install_error "Conflicting APM installations at $_apm_existing_binary and $1. Remove the unwanted installation with its owner before retrying."
fi
_apm_existing_binary="$_candidate"
_apm_existing_lib="$_candidate_lib"
if [ "$(apm_real_path "$_probe_original_parent")" != "$(apm_real_path "$_candidate_lib")" ]; then
_apm_existing_bin="$_probe_original_parent"
fi
}
# One destination/ownership authority for bootstrap and self-update.
# Arguments are historical system entry points, also checked when absent from PATH.
apm_resolve_install_paths() {
_apm_existing_binary=""
_apm_existing_lib=""
_apm_existing_bin=""
APM_INSTALL_DIR="$(apm_trim_trailing_slashes "$APM_INSTALL_DIR")"
APM_LIB_DIR="$(apm_trim_trailing_slashes "$APM_LIB_DIR")"
if [ -n "$_APM_PREFIX_SET" ]; then
APM_INSTALL_PREFIX="$(apm_trim_trailing_slashes "$APM_INSTALL_PREFIX")"
case "$APM_INSTALL_PREFIX" in
/*) ;;
*) apm_install_error "--prefix must be an absolute path. Use --prefix /path/to/root." ;;
esac
case "/$APM_INSTALL_PREFIX/" in
*/../*|*/./*) apm_install_error "--prefix must not contain dot segments. Supply a normalized absolute path." ;;
esac
if [ "$APM_INSTALL_PREFIX" = "/" ]; then
_apm_prefix_install_dir="/bin"
_apm_prefix_lib_dir="/lib/apm"
else
_apm_prefix_install_dir="$APM_INSTALL_PREFIX/bin"
_apm_prefix_lib_dir="$APM_INSTALL_PREFIX/lib/apm"
fi
if [ -n "$_APM_INSTALL_DIR_SET" ] &&
[ "$APM_INSTALL_DIR" != "$_apm_prefix_install_dir" ]; then
apm_install_error "--prefix derives APM_INSTALL_DIR=$_apm_prefix_install_dir, but APM_INSTALL_DIR=$APM_INSTALL_DIR was also provided. Use one destination selector."
fi
if [ -n "$_APM_LIB_DIR_SET" ] &&
[ "$APM_LIB_DIR" != "$_apm_prefix_lib_dir" ]; then
apm_install_error "--prefix derives APM_LIB_DIR=$_apm_prefix_lib_dir, but APM_LIB_DIR=$APM_LIB_DIR was also provided. Use one destination selector."
fi
APM_INSTALL_DIR="$_apm_prefix_install_dir"
APM_LIB_DIR="$_apm_prefix_lib_dir"
_APM_INSTALL_DIR_SET=1
_APM_LIB_DIR_SET=1
fi
for _dir in "$APM_INSTALL_DIR" "$APM_LIB_DIR"; do
case "$_dir" in
/*) ;;
*) apm_install_error "Install destinations must be absolute paths. Set APM_INSTALL_DIR and APM_LIB_DIR to absolute paths." ;;
esac
case "/$_dir/" in
*/../*|*/./*) apm_install_error "Install destinations must not contain dot segments. Supply normalized absolute paths." ;;
esac
done
if [ "$(id -u)" -eq 0 ] &&
{ [ -z "$_APM_INSTALL_DIR_SET" ] || [ -z "$_APM_LIB_DIR_SET" ]; }; then