-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·1353 lines (1208 loc) · 40.2 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·1353 lines (1208 loc) · 40.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#
# install.sh - Process Triage (pt) Installer
#
# One-line install (with cache buster):
# curl -fsSL "https://raw.githubusercontent.com/Dicklesworthstone/process_triage/main/install.sh?$(date +%s)" | bash
#
# One-line install (stable URL):
# curl -fsSL https://raw.githubusercontent.com/Dicklesworthstone/process_triage/main/install.sh | bash
#
# Common options:
# --version 2.0.5 Install a specific release
# --dest ~/.local/bin Install into a custom directory
# --system Install into /usr/local/bin
# --easy-mode Update shell PATH files automatically
# --verify Enforce checksum + signature verification
# --from-source Build pt-core from source
# --offline FILE Install from a local pt-core tarball
# Version inferred from bundle name or local VERSION file
# --quiet Suppress non-error output
# --no-gum Disable gum formatting
# --force Reinstall even if target version is already present
# --no-configure Skip agent auto-configuration and skill install
# --emit-skill-payload Print the canonical skill SKILL.md to stdout and exit
#
set -euo pipefail
umask 022
shopt -s lastpipe 2>/dev/null || true
OWNER="${OWNER:-Dicklesworthstone}"
REPO="${REPO:-process_triage}"
GITHUB_REPO="${OWNER}/${REPO}"
RAW_URL="https://raw.githubusercontent.com/${GITHUB_REPO}/main"
RELEASES_URL="https://github.com/${GITHUB_REPO}/releases"
API_URL="https://api.github.com/repos/${GITHUB_REPO}"
DEST_DEFAULT="$HOME/.local/bin"
DEST="${DEST:-$DEST_DEFAULT}"
VERSION="${PT_VERSION:-}"
CORE_VERSION="${PT_CORE_VERSION:-}"
SYSTEM=0
EASY_MODE=0
QUIET=0
NO_GUM=0
FORCE_INSTALL=0
FROM_SOURCE=0
VERIFY=0
NO_CONFIGURE=0
NO_VERIFY=0
VERIFY_SELF=0
EMIT_SKILL_PAYLOAD=0
OFFLINE_BUNDLE=""
LOCK_ROOT="/tmp/pt-install.lock.d"
LOCK_HELD=0
TEMP_DIR=""
HAS_GUM=0
OS=""
ARCH=""
PLATFORM=""
PROXY_ARGS=()
DOWNLOADED_ARTIFACT=""
INSTALLED_WRAPPER_VERSION=""
INSTALLED_CORE_VERSION=""
AGENT_LINES=()
BACKUP_LINES=()
CLAUDE_STATUS="skipped"
CODEX_STATUS="skipped"
COPILOT_STATUS="skipped"
CURSOR_STATUS="skipped"
WINDSURF_STATUS="skipped"
GEMINI_STATUS="skipped"
CLAUDE_SKILL_STATUS="skipped"
CODEX_SKILL_STATUS="skipped"
DEFAULT_RELEASE_PUBLIC_KEY_FINGERPRINT=""
strip_ansi() {
sed $'s/\033\\[[0-9;]*m//g'
}
detect_gum() {
if command -v gum >/dev/null 2>&1 && [[ -t 1 ]] && [[ "$NO_GUM" -eq 0 ]]; then
HAS_GUM=1
fi
}
info() {
[[ "$QUIET" -eq 1 ]] && return 0
if [[ "$HAS_GUM" -eq 1 ]]; then
gum style --foreground 39 "→ $*"
else
printf '\033[0;34m→\033[0m %s\n' "$*"
fi
}
ok() {
[[ "$QUIET" -eq 1 ]] && return 0
if [[ "$HAS_GUM" -eq 1 ]]; then
gum style --foreground 42 "✓ $*"
else
printf '\033[0;32m✓\033[0m %s\n' "$*"
fi
}
warn() {
[[ "$QUIET" -eq 1 ]] && return 0
if [[ "$HAS_GUM" -eq 1 ]]; then
gum style --foreground 214 "⚠ $*"
else
printf '\033[1;33m⚠\033[0m %s\n' "$*"
fi
}
err() {
if [[ "$HAS_GUM" -eq 1 ]]; then
gum style --foreground 196 "✗ $*" >&2
else
printf '\033[0;31m✗\033[0m %s\n' "$*" >&2
fi
}
run_with_spinner() {
local title="$1"
shift
if [[ "$HAS_GUM" -eq 1 && "$QUIET" -eq 0 ]]; then
gum spin --spinner dot --title "$title" -- "$@"
else
info "$title"
"$@"
fi
}
draw_box() {
local color="$1"
shift
local lines=("$@")
local max_width=0
local line stripped len pad
for line in "${lines[@]}"; do
stripped=$(printf '%b' "$line" | strip_ansi)
len=${#stripped}
[[ "$len" -gt "$max_width" ]] && max_width="$len"
done
local border=""
local inner_width=$((max_width + 4))
local i
for ((i = 0; i < inner_width; i++)); do
border+="═"
done
printf "\033[%sm╔%s╗\033[0m\n" "$color" "$border"
for line in "${lines[@]}"; do
stripped=$(printf '%b' "$line" | strip_ansi)
len=${#stripped}
pad=$((max_width - len))
printf "\033[%sm║\033[0m %b%*s \033[%sm║\033[0m\n" "$color" "$line" "$pad" "" "$color"
done
printf "\033[%sm╚%s╝\033[0m\n" "$color" "$border"
}
status_path() {
local path="$1"
if [[ -n "${HOME:-}" && "$path" == "$HOME"/* ]]; then
# shellcheck disable=SC2088 # This is display text, not an executable shell path.
printf '~/%s\n' "${path#"$HOME"/}"
else
printf '%s\n' "$path"
fi
}
show_header() {
[[ "$QUIET" -eq 1 ]] && return 0
if [[ "$HAS_GUM" -eq 1 ]]; then
gum style \
--border normal \
--border-foreground 39 \
--padding "0 1" \
--margin "1 0" \
"$(gum style --foreground 42 --bold 'pt installer')" \
"$(gum style --foreground 245 'Safety-first process triage with signed releases and agent integration')"
else
echo ""
draw_box "0;36" \
"\033[1;32mpt installer\033[0m" \
"Safety-first process triage with signed releases and agent integration"
echo ""
fi
}
usage() {
cat <<'EOF'
Process Triage installer
Usage:
bash install.sh [options]
Options:
--version <ver> Install a specific version
--dest <dir> Install directory (default: ~/.local/bin)
--system Install to /usr/local/bin
--easy-mode Update shell PATH files automatically
--verify Require checksum + signature verification
--verify-self Run post-install diagnostics
--from-source Build pt-core from source
--offline <file> Install pt-core from a local tarball
Version inferred from bundle name or local VERSION file
--force Reinstall even if already at target version
--quiet Suppress non-error output
--no-gum Disable gum formatting
--no-configure Skip agent init + skill install
--no-verify Disable checksum/signature verification
--emit-skill-payload Print the canonical skill SKILL.md to stdout and exit
-h, --help Show help
Environment:
PT_VERSION Version override
PT_CORE_VERSION pt-core version override
PT_SYSTEM=1 System install shortcut
PT_NO_PATH=1 Skip PATH updates
VERIFY=1 Require verification
PT_RELEASE_PUBLIC_KEY_FILE PEM file for release verification
PT_RELEASE_PUBLIC_KEY_PEM PEM contents for release verification
PT_RELEASE_PUBLIC_KEY_FINGERPRINT Expected SHA-256 fingerprint
PT_RELEASE_PUBLIC_KEY_FINGERPRINT_FILE File containing expected fingerprint
HTTP_PROXY / HTTPS_PROXY / NO_PROXY Proxy support
EOF
}
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
--version)
[[ -n "${2:-}" ]] || { err "--version requires a value"; exit 1; }
VERSION="$2"
shift 2
;;
--dest)
[[ -n "${2:-}" ]] || { err "--dest requires a value"; exit 1; }
DEST="$2"
shift 2
;;
--system)
SYSTEM=1
shift
;;
--easy-mode)
EASY_MODE=1
shift
;;
--verify)
VERIFY=1
shift
;;
--verify-self)
VERIFY_SELF=1
shift
;;
--from-source)
FROM_SOURCE=1
shift
;;
--offline)
[[ -n "${2:-}" ]] || { err "--offline requires a file path"; exit 1; }
OFFLINE_BUNDLE="$2"
shift 2
;;
--force)
FORCE_INSTALL=1
shift
;;
--quiet)
QUIET=1
shift
;;
--no-gum)
NO_GUM=1
shift
;;
--no-configure)
NO_CONFIGURE=1
shift
;;
--no-verify)
NO_VERIFY=1
shift
;;
--emit-skill-payload)
EMIT_SKILL_PAYLOAD=1
shift
;;
-h|--help)
usage
exit 0
;;
*)
err "Unknown option: $1"
usage
exit 1
;;
esac
done
if [[ "${PT_SYSTEM:-0}" == "1" ]]; then
SYSTEM=1
fi
if [[ "${VERIFY:-0}" == "1" ]]; then
VERIFY=1
fi
if [[ "$NO_VERIFY" -eq 1 ]]; then
VERIFY=0
fi
}
setup_proxy() {
PROXY_ARGS=()
if [[ -n "${HTTPS_PROXY:-}" ]]; then
PROXY_ARGS=(--proxy "$HTTPS_PROXY")
elif [[ -n "${HTTP_PROXY:-}" ]]; then
PROXY_ARGS=(--proxy "$HTTP_PROXY")
fi
}
fetch_stdout() {
local url="$1"
if command -v curl >/dev/null 2>&1; then
curl -fsSL "${PROXY_ARGS[@]}" --connect-timeout 10 --max-time 120 "$url"
elif command -v wget >/dev/null 2>&1; then
wget -q -O - "$url"
else
err "Neither curl nor wget is available"
return 1
fi
}
download() {
local url="$1"
local output="$2"
if command -v curl >/dev/null 2>&1; then
curl -fsSL "${PROXY_ARGS[@]}" --connect-timeout 10 --max-time 120 "$url" -o "$output"
elif command -v wget >/dev/null 2>&1; then
wget -q -O "$output" "$url"
else
err "Neither curl nor wget is available"
return 1
fi
}
append_cache_buster() {
local url="$1"
local ts
ts=$(date +%s)
if [[ "$url" == *"?"* ]]; then
printf '%s&cb=%s\n' "$url" "$ts"
else
printf '%s?cb=%s\n' "$url" "$ts"
fi
}
infer_version_from_text() {
local text="$1"
if [[ "$text" =~ ([0-9]+\.[0-9]+\.[0-9]+) ]]; then
printf '%s\n' "${BASH_REMATCH[1]}"
return 0
fi
return 1
}
infer_version_from_offline_bundle() {
local bundle_path="$1"
infer_version_from_text "$(basename "$bundle_path")"
}
resolve_local_version_file() {
local candidate=""
if [[ -f "./VERSION" ]]; then
candidate="$(tr -d '[:space:]' < ./VERSION)"
infer_version_from_text "$candidate" && return 0
fi
if [[ -f "$(dirname "$0")/VERSION" ]]; then
candidate="$(tr -d '[:space:]' < "$(dirname "$0")/VERSION")"
infer_version_from_text "$candidate" && return 0
fi
return 1
}
copy_local_verification_file() {
local output="$1"
shift
local candidate
for candidate in "$@"; do
[[ -n "$candidate" && -f "$candidate" ]] || continue
cp "$candidate" "$output"
return 0
done
return 1
}
prepare_offline_release_public_key() {
local output="$1"
local script_dir
script_dir="$(dirname "$0")"
if [[ -n "${PT_RELEASE_PUBLIC_KEY_FILE:-}" ]]; then
cp "${PT_RELEASE_PUBLIC_KEY_FILE}" "$output"
elif [[ -n "${PT_RELEASE_PUBLIC_KEY_PEM:-}" ]]; then
printf '%s\n' "${PT_RELEASE_PUBLIC_KEY_PEM}" > "$output"
elif ! copy_local_verification_file \
"$output" \
"./release-signing-public.pem" \
"${script_dir}/release-signing-public.pem" \
"$(dirname "$OFFLINE_BUNDLE")/release-signing-public.pem"; then
err "Offline verification requires a local release-signing-public.pem"
err "Provide PT_RELEASE_PUBLIC_KEY_FILE/PT_RELEASE_PUBLIC_KEY_PEM or place the PEM beside install.sh, the current directory, or the offline bundle"
return 1
fi
openssl pkey -pubin -in "$output" -noout >/dev/null 2>&1 || {
err "Invalid release public key PEM"
return 1
}
local expected actual
expected="$(resolve_expected_key_fingerprint || true)"
if [[ -n "$expected" ]]; then
actual="$(openssl pkey -pubin -in "$output" -outform der 2>/dev/null | sha256_stdin)"
actual="$(normalize_fingerprint "$actual")"
if [[ "$actual" != "$expected" ]]; then
err "Release public key fingerprint mismatch"
return 1
fi
ok "Release public key fingerprint verified: ${actual:0:16}..."
else
warn "No release public key fingerprint pin configured"
fi
}
prepare_offline_checksums() {
local output="$1"
local script_dir
script_dir="$(dirname "$0")"
if ! copy_local_verification_file \
"$output" \
"./checksums.sha256" \
"${script_dir}/checksums.sha256" \
"$(dirname "$OFFLINE_BUNDLE")/checksums.sha256"; then
err "Offline verification requires a local checksums.sha256"
err "Place checksums.sha256 beside install.sh, the current directory, or the offline bundle"
return 1
fi
}
verify_file_signature_offline() {
local file_path="$1"
local artifact_name="$2"
local pubkey_file="$3"
local sig_output="$4"
shift 4
copy_local_verification_file "$sig_output" "$@" || {
err "Offline verification requires a local signature for ${artifact_name}"
return 1
}
if openssl dgst -sha256 -verify "$pubkey_file" -signature "$sig_output" "$file_path" >/dev/null 2>&1; then
ok "${artifact_name} signature verified"
return 0
fi
err "Signature verification failed for ${artifact_name}"
return 1
}
cleanup() {
[[ -n "$TEMP_DIR" && -d "$TEMP_DIR" ]] && rm -rf "$TEMP_DIR"
if [[ "$LOCK_HELD" -eq 1 && -d "$LOCK_ROOT" ]]; then
rm -rf "$LOCK_ROOT"
fi
}
create_temp_dir() {
TEMP_DIR="$(mktemp -d 2>/dev/null || mktemp -d -t pt.XXXXXXXX 2>/dev/null)"
if [[ -z "$TEMP_DIR" || ! -d "$TEMP_DIR" ]]; then
err "Failed to create temporary directory"
exit 1
fi
trap cleanup EXIT
}
maybe_self_refresh() {
if [[ -p /dev/stdin && -z "${PT_REFRESHED:-}" && -z "$OFFLINE_BUNDLE" ]]; then
export PT_REFRESHED=1
exec bash <(fetch_stdout "$(append_cache_buster "${RAW_URL}/install.sh")") "$@"
fi
}
detect_platform() {
OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m)"
case "$ARCH" in
x86_64|amd64) ARCH="x86_64" ;;
arm64|aarch64) ARCH="aarch64" ;;
*)
err "Unsupported architecture: $ARCH"
exit 1
;;
esac
if [[ "$OS" == "linux" && -f /proc/version ]] && grep -qi microsoft /proc/version 2>/dev/null; then
warn "WSL detected; continuing with Linux install"
fi
case "$OS" in
linux)
if [[ -f /etc/alpine-release ]] || (command -v ldd >/dev/null 2>&1 && ldd --version 2>&1 | grep -qi musl); then
PLATFORM="linux-${ARCH}-musl"
else
PLATFORM="linux-${ARCH}"
fi
;;
darwin)
PLATFORM="macos-${ARCH}"
;;
*)
err "Unsupported operating system: $OS"
exit 1
;;
esac
case "$PLATFORM" in
linux-x86_64|linux-aarch64|linux-x86_64-musl|linux-aarch64-musl|macos-x86_64|macos-aarch64) ;;
*)
warn "No prebuilt for ${PLATFORM}; will fall back to source"
FROM_SOURCE=1
;;
esac
}
resolve_version() {
if [[ -n "$VERSION" ]]; then
return 0
fi
if [[ -n "$OFFLINE_BUNDLE" ]]; then
VERSION="$(infer_version_from_offline_bundle "$OFFLINE_BUNDLE" || true)"
if [[ -z "$VERSION" ]]; then
VERSION="$(resolve_local_version_file || true)"
fi
if [[ -z "$VERSION" ]]; then
err "Offline mode requires --version or a versioned bundle filename"
err "Example: pt-core-linux-x86_64-2.0.5.tar.gz"
exit 1
fi
return 0
fi
local latest=""
latest="$(fetch_stdout "${API_URL}/releases/latest" 2>/dev/null | jq -r '.tag_name // empty' 2>/dev/null || true)"
latest="${latest#v}"
if [[ -z "$latest" ]]; then
latest="$(
curl -fsSL "${PROXY_ARGS[@]}" --connect-timeout 10 --max-time 20 -o /dev/null -w '%{url_effective}' \
"${RELEASES_URL}/latest" 2>/dev/null \
| sed -E 's|.*/tag/v?([0-9]+\.[0-9]+\.[0-9]+)$|\1|' \
|| true
)"
fi
if [[ -z "$latest" ]]; then
latest="$(fetch_stdout "$(append_cache_buster "${RAW_URL}/VERSION")" 2>/dev/null | tr -d '[:space:]' || true)"
fi
if [[ -z "$latest" ]]; then
err "Could not resolve latest version"
exit 1
fi
VERSION="$latest"
}
resolve_core_version() {
if [[ -z "$CORE_VERSION" ]]; then
CORE_VERSION="$VERSION"
fi
}
artifact_name_for_platform() {
local version="$1"
local platform="$2"
printf 'pt-core-%s-%s.tar.gz\n' "$platform" "$version"
}
wrapper_urls() {
printf '%s\n' \
"${RELEASES_URL}/download/v${VERSION}/pt" \
"https://raw.githubusercontent.com/${GITHUB_REPO}/v${VERSION}/pt" \
"${RAW_URL}/pt"
}
core_urls() {
local artifact
artifact="$(artifact_name_for_platform "$CORE_VERSION" "$PLATFORM")"
printf '%s\n' \
"${RELEASES_URL}/download/v${CORE_VERSION}/${artifact}"
if [[ "$PLATFORM" == linux-x86_64 ]]; then
printf '%s\n' "${RELEASES_URL}/download/v${CORE_VERSION}/pt-core-linux-x86_64-musl-${CORE_VERSION}.tar.gz"
elif [[ "$PLATFORM" == linux-aarch64 ]]; then
printf '%s\n' "${RELEASES_URL}/download/v${CORE_VERSION}/pt-core-linux-aarch64-musl-${CORE_VERSION}.tar.gz"
fi
}
download_first_available() {
local output="$1"
shift
local url
for url in "$@"; do
[[ -z "$url" ]] && continue
if download "$url" "$output" 2>/dev/null; then
DOWNLOADED_ARTIFACT="$(basename "$url")"
return 0
fi
done
return 1
}
check_disk_space() {
local install_parent
install_parent="$(dirname "$DEST")"
mkdir -p "$install_parent"
local available
available=$(df -Pk "$install_parent" | awk 'NR==2 {print $4}')
if [[ -n "$available" && "$available" -lt 10240 ]]; then
err "At least 10MB of free space is required in $install_parent"
exit 1
fi
}
check_write_permissions() {
local install_parent
install_parent="$(dirname "$DEST")"
mkdir -p "$install_parent"
if [[ ! -w "$install_parent" ]] && [[ "$SYSTEM" -eq 0 ]]; then
err "Install directory is not writable: $install_parent"
exit 1
fi
}
check_network() {
if [[ -n "$OFFLINE_BUNDLE" || "$FROM_SOURCE" -eq 1 ]]; then
info "Skipping network preflight"
return 0
fi
if ! command -v curl >/dev/null 2>&1; then
warn "curl not found; skipping network preflight"
return 0
fi
local url
url="${RELEASES_URL}/download/v${VERSION}/pt"
if ! curl -fsSL "${PROXY_ARGS[@]}" --connect-timeout 3 --max-time 5 -o /dev/null "$url"; then
warn "Network preflight failed for $url"
fi
}
get_installed_version() {
local bin="$1"
[[ -x "$bin" ]] || return 0
"$bin" --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || true
}
check_existing_install() {
INSTALLED_WRAPPER_VERSION="$(get_installed_version "$DEST/pt")"
INSTALLED_CORE_VERSION="$(get_installed_version "$DEST/pt-core")"
if [[ -n "$INSTALLED_WRAPPER_VERSION" ]]; then
info "Existing pt version: ${INSTALLED_WRAPPER_VERSION}"
fi
if [[ -n "$INSTALLED_CORE_VERSION" ]]; then
info "Existing pt-core version: ${INSTALLED_CORE_VERSION}"
fi
}
preflight_checks() {
info "Running preflight checks"
check_disk_space
check_write_permissions
check_existing_install
check_network
}
acquire_lock() {
if mkdir "$LOCK_ROOT" 2>/dev/null; then
LOCK_HELD=1
printf '%s\n' "$$" > "${LOCK_ROOT}/pid"
return 0
fi
if [[ -f "${LOCK_ROOT}/pid" ]]; then
local existing_pid
existing_pid="$(cat "${LOCK_ROOT}/pid" 2>/dev/null || true)"
if [[ -n "$existing_pid" ]] && ! kill -0 "$existing_pid" 2>/dev/null; then
rm -rf "$LOCK_ROOT"
if mkdir "$LOCK_ROOT" 2>/dev/null; then
LOCK_HELD=1
printf '%s\n' "$$" > "${LOCK_ROOT}/pid"
return 0
fi
# Another process grabbed the lock between rm and mkdir
fi
fi
err "Another pt installer appears to be running (${LOCK_ROOT})"
exit 1
}
sha256_file() {
local file="$1"
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$file" | cut -d' ' -f1
elif command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$file" | cut -d' ' -f1
elif command -v openssl >/dev/null 2>&1; then
openssl dgst -sha256 "$file" | awk '{print $NF}'
else
return 1
fi
}
sha256_stdin() {
if command -v sha256sum >/dev/null 2>&1; then
sha256sum | cut -d' ' -f1
elif command -v shasum >/dev/null 2>&1; then
shasum -a 256 | cut -d' ' -f1
elif command -v openssl >/dev/null 2>&1; then
openssl dgst -sha256 | awk '{print $NF}'
else
return 1
fi
}
normalize_fingerprint() {
local value="$1"
value="${value//[[:space:]]/}"
value="$(printf '%s' "$value" | tr '[:upper:]' '[:lower:]')"
[[ "$value" =~ ^[a-f0-9]{64}$ ]] || return 1
printf '%s\n' "$value"
}
resolve_expected_key_fingerprint() {
local expected=""
if [[ -n "${PT_RELEASE_PUBLIC_KEY_FINGERPRINT:-}" ]]; then
expected="$PT_RELEASE_PUBLIC_KEY_FINGERPRINT"
elif [[ -n "${PT_RELEASE_PUBLIC_KEY_FINGERPRINT_FILE:-}" && -f "${PT_RELEASE_PUBLIC_KEY_FINGERPRINT_FILE}" ]]; then
expected="$(head -n1 "${PT_RELEASE_PUBLIC_KEY_FINGERPRINT_FILE}" | awk '{print $1}')"
elif [[ -n "$DEFAULT_RELEASE_PUBLIC_KEY_FINGERPRINT" ]]; then
expected="$DEFAULT_RELEASE_PUBLIC_KEY_FINGERPRINT"
fi
[[ -z "$expected" ]] && return 0
normalize_fingerprint "$expected"
}
resolve_release_public_key() {
local version="$1"
local output="$2"
if [[ -n "${PT_RELEASE_PUBLIC_KEY_FILE:-}" ]]; then
cp "${PT_RELEASE_PUBLIC_KEY_FILE}" "$output"
elif [[ -n "${PT_RELEASE_PUBLIC_KEY_PEM:-}" ]]; then
printf '%s\n' "${PT_RELEASE_PUBLIC_KEY_PEM}" > "$output"
elif ! download "${RELEASES_URL}/download/v${version}/release-signing-public.pem" "$output" 2>/dev/null; then
err "Release v${version} does not publish release-signing-public.pem"
err "Use PT_RELEASE_PUBLIC_KEY_FILE/PT_RELEASE_PUBLIC_KEY_PEM or install without --verify"
return 1
fi
openssl pkey -pubin -in "$output" -noout >/dev/null 2>&1 || {
err "Invalid release public key PEM"
return 1
}
local expected actual
expected="$(resolve_expected_key_fingerprint || true)"
if [[ -n "$expected" ]]; then
actual="$(openssl pkey -pubin -in "$output" -outform der 2>/dev/null | sha256_stdin)"
actual="$(normalize_fingerprint "$actual")"
if [[ "$actual" != "$expected" ]]; then
err "Release public key fingerprint mismatch"
return 1
fi
ok "Release public key fingerprint verified: ${actual:0:16}..."
else
warn "No release public key fingerprint pin configured"
fi
}
download_checksums() {
local version="$1"
local output="$2"
if ! download "${RELEASES_URL}/download/v${version}/checksums.sha256" "$output" 2>/dev/null; then
err "Release v${version} does not publish checksums.sha256"
err "Install without --verify or provide local verification materials in offline mode"
return 1
fi
}
lookup_checksum() {
local checksums_file="$1"
local filename="$2"
grep -E "^[a-f0-9]{64} ${filename}$" "$checksums_file" | awk '{print $1}'
}
verify_file_checksum() {
local file="$1"
local filename="$2"
local checksums_file="$3"
local expected actual
expected="$(lookup_checksum "$checksums_file" "$filename" || true)"
[[ -n "$expected" ]] || {
err "No checksum found for ${filename}"
return 1
}
actual="$(sha256_file "$file")" || {
err "Could not compute checksum"
return 1
}
if [[ "$actual" != "$expected" ]]; then
err "Checksum mismatch for ${filename}"
err "Expected: $expected"
err "Actual: $actual"
return 1
fi
ok "${filename} checksum verified: ${actual:0:16}..."
}
verify_file_signature() {
local file_path="$1"
local artifact_name="$2"
local version="$3"
local pubkey_file="$4"
local sig_output="$5"
if ! download "${RELEASES_URL}/download/v${version}/${artifact_name}.sig" "$sig_output" 2>/dev/null; then
err "Release v${version} does not publish ${artifact_name}.sig"
err "Install without --verify or publish the matching signature sidecar"
return 1
fi
if openssl dgst -sha256 -verify "$pubkey_file" -signature "$sig_output" "$file_path" >/dev/null 2>&1; then
ok "${artifact_name} signature verified"
return 0
fi
err "Signature verification failed for ${artifact_name}"
return 1
}
best_effort_sigstore_verify() {
local file_path="$1"
local artifact_name="$2"
if ! command -v cosign >/dev/null 2>&1; then
warn "cosign not found; skipping Sigstore verification for ${artifact_name}"
return 0
fi
warn "No Sigstore bundle published for ${artifact_name}; skipping cosign verification"
}
install_binary() {
local source_file="$1"
local dest_dir="$2"
local binary_name="$3"
local backup_file=""
local dest_file="${dest_dir}/${binary_name}"
local temp_dest="${dest_file}.new"
mkdir -p "$dest_dir"
if [[ -f "$dest_file" ]]; then
backup_file="${dest_file}.bak.$(date +%Y%m%d%H%M%S)"
cp "$dest_file" "$backup_file"
BACKUP_LINES+=("${binary_name} backup: ${backup_file}")
fi
install -m 0755 "$source_file" "$temp_dest"
mv "$temp_dest" "$dest_file"
ok "Installed ${dest_file}"
}
extract_core_archive() {
local archive="$1"
local extract_dir="$2"
mkdir -p "$extract_dir"
tar -xzf "$archive" -C "$extract_dir" pt-core 2>/dev/null || tar -xzf "$archive" -C "$extract_dir"
}
path_line_for_shell() {
local install_dir="$1"
local shell_name="$2"
case "$shell_name" in
fish)
printf "if not contains \"%s\" \$PATH\n set -gx PATH \"%s\" \$PATH\nend\n" "$install_dir" "$install_dir"
;;
*)
printf "case \":\$PATH:\" in\n *:\"%s\":*) ;;\n *) export PATH=\"%s:\$PATH\" ;;\nesac\n" "$install_dir" "$install_dir"
;;
esac
}
append_path_snippet() {
local file="$1"
local shell_name="$2"
local install_dir="$3"
local marker="# process_triage installer PATH"
[[ -f "$file" ]] || : > "$file"
if grep -Fq "$marker" "$file" 2>/dev/null; then
return 0
fi
{
printf '\n%s\n' "$marker"
path_line_for_shell "$install_dir" "$shell_name"
} >> "$file"
}
maybe_add_path() {
local install_dir="$1"
if [[ "${PT_NO_PATH:-0}" == "1" ]]; then
info "Skipping PATH changes (PT_NO_PATH=1)"
return 0
fi
if [[ ":$PATH:" == *":${install_dir}:"* ]]; then
info "${install_dir} already in PATH"
return 0
fi
if [[ "$EASY_MODE" -eq 1 ]]; then
append_path_snippet "$HOME/.zshenv" "zsh" "$install_dir"
append_path_snippet "$HOME/.profile" "sh" "$install_dir"
append_path_snippet "$HOME/.bashrc" "bash" "$install_dir"
ok "Added ${install_dir} to shell PATH config"
else
warn "Add ${install_dir} to PATH to use pt"
warn "Hint: run with --easy-mode to update your shell rc files"
fi
}
install_completions_for_shell() {
local shell_name="$1"
local target=""
case "$shell_name" in
bash)
target="${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion/completions/pt"
;;
zsh)
target="${XDG_DATA_HOME:-$HOME/.local/share}/zsh/site-functions/_pt"
;;
fish)
target="${XDG_CONFIG_HOME:-$HOME/.config}/fish/completions/pt.fish"
;;
*)
return 0
;;
esac
mkdir -p "$(dirname "$target")"
"$DEST/pt" completions "$shell_name" > "$target"
ok "Installed ${shell_name} completions"
}
maybe_install_completions() {
[[ -x "$DEST/pt" ]] || return 0
install_completions_for_shell "bash" || true
install_completions_for_shell "zsh" || true
install_completions_for_shell "fish" || true
}
agent_present() {
local agent="$1"
case "$agent" in
claude)
[[ -d "$HOME/.claude" ]] || command -v claude >/dev/null 2>&1
;;
codex)
[[ -d "$HOME/.codex" ]] || command -v codex >/dev/null 2>&1
;;
copilot)
command -v copilot >/dev/null 2>&1
;;
cursor)
[[ -d "$HOME/.cursor" ]] || [[ -d "$HOME/.config/Cursor" ]] || [[ -d "$HOME/Library/Application Support/Cursor" ]]
;;
windsurf)
[[ -d "$HOME/.codeium/windsurf" ]] || [[ -d "$HOME/.config/Windsurf" ]]
;;
gemini)
[[ -d "$HOME/.gemini" ]] || command -v gemini >/dev/null 2>&1
;;
*)
return 1