-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinstall.sh
More file actions
1427 lines (1215 loc) · 44.3 KB
/
install.sh
File metadata and controls
1427 lines (1215 loc) · 44.3 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 — Install opencode-multi-auth plugin for OpenCode Config Suites
# Supports 3 auth paths: gh CLI → GITHUB_TOKEN env → interactive prompt
set -euo pipefail
# Recover HOME when missing (can happen in some pipe-to-bash shells).
if [[ -z "${HOME:-}" ]]; then
HOME="$(getent passwd "$(id -u)" | cut -d: -f6 2>/dev/null || true)"
if [[ -z "${HOME:-}" ]]; then
HOME="$(cd ~ 2>/dev/null && pwd || true)"
fi
if [[ -z "${HOME:-}" ]]; then
HOME="/tmp"
fi
export HOME
fi
resolve_target_home_early() {
if [[ -n "${OCS_TARGET_HOME:-}" ]]; then
printf '%s\n' "${OCS_TARGET_HOME}"
return 0
fi
if [[ "${EUID:-$(id -u)}" -eq 0 && -n "${SUDO_USER:-}" && "${SUDO_USER}" != "root" ]]; then
local sudo_home=""
sudo_home="$(getent passwd "${SUDO_USER}" | cut -d: -f6 2>/dev/null || true)"
if [[ -z "${sudo_home}" ]]; then
sudo_home="$(eval printf '%s' "~${SUDO_USER}" 2>/dev/null || true)"
fi
if [[ -n "${sudo_home}" ]]; then
printf '%s\n' "${sudo_home}"
return 0
fi
fi
printf '%s\n' "${HOME:-/tmp}"
}
TARGET_HOME="$(resolve_target_home_early)"
if [[ -n "${TARGET_HOME}" && "${TARGET_HOME}" != "${HOME}" ]]; then
HOME="${TARGET_HOME}"
export HOME
fi
# ─── Config ──────────────────────────────────────────────────────────────────
GITHUB_SOURCE_REPO="andyvandaric/andyvand-opencode-config"
GITHUB_SOURCE_BRANCH="${OCS_RELEASE_BRANCH:-beta}"
DEFAULT_RELEASE_BRANCH="beta"
INSTALLER_DEFAULT_PROFILE="codex-5.3-token-saver"
INSTALLER_DEFAULT_MODE="performance"
WHATSAPP_ORDER_URL="https://wa.me/6281289731212?text=Mau%20order%20OCS%20nya%2C%20mohon%20infonya%20ya"
PLUGIN_DIR="${HOME}/.config/opencode/plugins/opencode-multi-auth"
TOKEN_FILE="${HOME}/.opencode-suites/.token"
TMP_DIR="$(mktemp -d /tmp/ocs-install-XXXXXX)"
REQUESTED_VERSION="${OCS_VERSION:-}"
RESOLVED_SOURCE_BRANCH="${GITHUB_SOURCE_BRANCH}"
# ─── Cleanup on exit ─────────────────────────────────────────────────────────
trap 'rm -rf "${TMP_DIR}"' EXIT
# ─── Helpers ─────────────────────────────────────────────────────────────────
info() { echo " $*"; }
success() { echo "✅ $*"; }
error() { echo "❌ $*" >&2; exit 1; }
warn() { echo "⚠️ $*" >&2; }
is_root_user() {
[[ "${EUID:-$(id -u)}" -eq 0 ]]
}
run_with_privilege() {
if is_root_user; then
"$@"
return $?
fi
if command -v sudo >/dev/null 2>&1; then
if sudo -n true >/dev/null 2>&1; then
sudo -n "$@"
return $?
fi
if [[ -t 0 && -t 1 ]]; then
sudo "$@"
return $?
fi
return 1
fi
if command -v su >/dev/null 2>&1; then
if [[ -t 0 && -t 1 ]]; then
su -c "$(printf '%q ' "$@")"
return $?
fi
return 1
fi
return 127
}
run_with_retries() {
local attempts="$1"
shift
local try=1
while (( try <= attempts )); do
if "$@"; then
return 0
fi
if (( try == attempts )); then
return 1
fi
sleep 2
try=$((try + 1))
done
return 1
}
resolve_absolute_path_safe() {
local candidate="$1"
if [[ -z "$candidate" ]]; then
return 1
fi
if command -v realpath >/dev/null 2>&1; then
realpath "$candidate"
return $?
fi
if command -v readlink >/dev/null 2>&1; then
local linked
linked="$(readlink -f "$candidate" 2>/dev/null || true)"
if [[ -n "$linked" ]]; then
printf '%s\n' "$linked"
return 0
fi
fi
if [[ "$candidate" = /* ]]; then
printf '%s\n' "$candidate"
return 0
fi
printf '%s/%s\n' "$(pwd)" "$candidate"
}
show_usage() {
cat <<'EOF'
Usage: install.sh [--version <x.y.z>] [--branch <name>] [--help]
Options:
--version, -v Install specific bundle version (example: 2.0.15)
--branch Override source branch (default: feat/buyer-setup-smoke)
--help, -h Show this help
Env alternatives:
OCS_VERSION Same as --version
OCS_RELEASE_BRANCH Same as --branch
EOF
}
parse_cli_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
--version|-v)
[[ $# -ge 2 ]] || error "Missing value for $1"
REQUESTED_VERSION="${2#v}"
shift 2
;;
--branch)
[[ $# -ge 2 ]] || error "Missing value for --branch"
GITHUB_SOURCE_BRANCH="$2"
shift 2
;;
--help|-h)
show_usage
exit 0
;;
*)
error "Unknown option: $1 (use --help for usage)"
;;
esac
done
}
detect_package_manager() {
if command -v apt-get >/dev/null 2>&1; then echo "apt"; return; fi
if command -v dnf >/dev/null 2>&1; then echo "dnf"; return; fi
if command -v yum >/dev/null 2>&1; then echo "yum"; return; fi
if command -v pacman >/dev/null 2>&1; then echo "pacman"; return; fi
if command -v zypper >/dev/null 2>&1; then echo "zypper"; return; fi
if command -v apk >/dev/null 2>&1; then echo "apk"; return; fi
if command -v brew >/dev/null 2>&1; then echo "brew"; return; fi
echo ""
}
install_packages_auto() {
local pm="$1"
shift
local pkgs=("$@")
local dep_retries="${OCS_DEP_INSTALL_RETRIES:-2}"
case "$pm" in
apt)
run_with_retries "$dep_retries" run_with_privilege env DEBIAN_FRONTEND=noninteractive apt-get -o Acquire::Retries=3 update && run_with_retries "$dep_retries" run_with_privilege env DEBIAN_FRONTEND=noninteractive apt-get -o Acquire::Retries=3 install -y "${pkgs[@]}"
;;
dnf)
run_with_retries "$dep_retries" run_with_privilege dnf install -y "${pkgs[@]}"
;;
yum)
run_with_retries "$dep_retries" run_with_privilege yum install -y "${pkgs[@]}"
;;
pacman)
run_with_retries "$dep_retries" run_with_privilege pacman -Sy --noconfirm --needed "${pkgs[@]}"
;;
zypper)
run_with_retries "$dep_retries" run_with_privilege zypper --non-interactive install --no-recommends "${pkgs[@]}"
;;
apk)
run_with_retries "$dep_retries" run_with_privilege apk add --no-cache "${pkgs[@]}"
;;
brew)
run_with_retries "$dep_retries" brew install "${pkgs[@]}"
;;
*)
return 2
;;
esac
}
ensure_shell_dependencies() {
local required=(curl git tar)
local missing=()
local dep
local total
local idx=0
if ! command -v bun >/dev/null 2>&1; then
required+=(unzip)
fi
total=${#required[@]}
info "Checking required dependencies..."
for dep in "${required[@]}"; do
idx=$((idx + 1))
printf " [%d/%d] %-5s ... " "$idx" "$total" "$dep"
if ! command -v "$dep" >/dev/null 2>&1; then
printf "MISSING\n"
missing+=("$dep")
else
printf "OK\n"
fi
done
if [[ ${#missing[@]} -eq 0 ]]; then
success "All required dependencies already available."
return 0
fi
warn "Missing dependencies: ${missing[*]}"
local pm
pm="$(detect_package_manager)"
if [[ -z "$pm" ]]; then
error "Cannot auto-install dependencies (${missing[*]}): no supported package manager detected"
fi
info "Attempting to auto-install dependencies via ${pm}..."
info "Installing: ${missing[*]}"
if ! install_packages_auto "$pm" "${missing[@]}"; then
error "Auto-install failed for dependencies (${missing[*]}). Please install them manually and rerun."
fi
local verify_total
local verify_idx=0
verify_total=${#missing[@]}
info "Verifying installed dependencies..."
for dep in "${missing[@]}"; do
verify_idx=$((verify_idx + 1))
printf " [%d/%d] %-5s ... " "$verify_idx" "$verify_total" "$dep"
if ! command -v "$dep" >/dev/null 2>&1; then
printf "MISSING\n"
error "Dependency '${dep}' still missing after auto-install"
fi
printf "OK\n"
done
success "Dependencies installed: ${missing[*]}"
}
ocs_works() {
if ! command -v ocs >/dev/null 2>&1; then
return 1
fi
ocs --version >/dev/null 2>&1 || return 1
ocs --help >/dev/null 2>&1 || return 1
return 0
}
resolve_primary_shell_profile() {
local shell_name="${SHELL##*/}"
local candidate
case "${shell_name}" in
zsh)
for candidate in "${HOME}/.zprofile" "${HOME}/.zshrc"; do
if [[ -f "${candidate}" ]]; then
printf '%s\n' "${candidate}"
return 0
fi
done
printf '%s\n' "${HOME}/.zprofile"
;;
bash)
for candidate in "${HOME}/.bash_profile" "${HOME}/.profile" "${HOME}/.bashrc"; do
if [[ -f "${candidate}" ]]; then
printf '%s\n' "${candidate}"
return 0
fi
done
printf '%s\n' "${HOME}/.profile"
;;
fish)
printf '%s\n' ""
;;
*)
for candidate in "${HOME}/.profile" "${HOME}/.bash_profile" "${HOME}/.zprofile"; do
if [[ -f "${candidate}" ]]; then
printf '%s\n' "${candidate}"
return 0
fi
done
printf '%s\n' "${HOME}/.profile"
;;
esac
}
ensure_text_file_exists_if_writable() {
local file_path="$1"
local parent_dir
parent_dir="$(dirname "${file_path}")"
mkdir -p "${parent_dir}" 2>/dev/null || true
if [[ -f "${file_path}" ]]; then
[[ -w "${file_path}" ]]
return $?
fi
if [[ -w "${parent_dir}" ]]; then
: > "${file_path}"
return 0
fi
return 1
}
ensure_antigravity_oauth_integrity() {
local setup_script="$1"
local config_dir="${HOME}/.config/opencode"
local runtime_opencode="${config_dir}/opencode.json"
local runtime_antigravity="${config_dir}/antigravity.json"
local template_antigravity="${PLUGIN_DIR}/backups/antigravity.json.template"
local plugin_manifest="${PLUGIN_DIR}/package.json"
local plugin_setup_script="${PLUGIN_DIR}/scripts/setup.js"
local plugin_dist_dir="${PLUGIN_DIR}/dist"
local plugin_entry_primary="${PLUGIN_DIR}/dist/src/plugin.js"
local plugin_entry_fallback="${PLUGIN_DIR}/dist/index.js"
local runtime_references_plugin=0
local needs_repair=0
mkdir -p "${config_dir}" 2>/dev/null || true
if [[ ! -f "${runtime_antigravity}" && -f "${template_antigravity}" ]]; then
cp "${template_antigravity}" "${runtime_antigravity}"
needs_repair=1
fi
if [[ -f "${runtime_opencode}" ]]; then
if grep -Eq 'file:///.*dist/index\.js|plugins/.*/dist/index\.js' "${runtime_opencode}"; then
needs_repair=1
fi
if grep -Fq "opencode-multi-auth" "${runtime_opencode}"; then
runtime_references_plugin=1
if [[ ! -f "${plugin_manifest}" || ! -f "${plugin_setup_script}" || ! -d "${plugin_dist_dir}" || ( ! -f "${plugin_entry_primary}" && ! -f "${plugin_entry_fallback}" ) ]]; then
needs_repair=1
fi
fi
fi
if (( needs_repair )); then
info "Repairing final Antigravity OAuth visibility before installer exit..."
if [[ -f "${plugin_manifest}" ]]; then
info "Rebuilding plugin artifacts to restore OAuth methods..."
(
cd "${PLUGIN_DIR}" || exit 1
install_dependencies_with_retry "${PLUGIN_DIR}"
if [[ ! -f "${plugin_entry_primary}" && ! -f "${plugin_entry_fallback}" ]]; then
bun run build >/dev/null 2>&1 || npm run build >/dev/null 2>&1 || true
fi
) >/dev/null 2>&1 || true
fi
if [[ -f "${plugin_setup_script}" ]]; then
export OCS_SETUP_INSTALLER_MODE=1
bun "${setup_script}" --headless --profile "${INSTALLER_DEFAULT_PROFILE}" --mode "${INSTALLER_DEFAULT_MODE}" >/dev/null 2>&1 || true
unset OCS_SETUP_INSTALLER_MODE
fi
if [[ ! -f "${runtime_antigravity}" && -f "${template_antigravity}" ]]; then
cp "${template_antigravity}" "${runtime_antigravity}"
fi
fi
[[ -f "${runtime_antigravity}" ]] || error "Final Antigravity OAuth integrity check failed: antigravity.json is missing."
if [[ -f "${runtime_opencode}" ]] && grep -Eq 'file:///.*dist/index\.js|plugins/.*/dist/index\.js' "${runtime_opencode}"; then
error "Final Antigravity OAuth integrity check failed: runtime config still references a raw dist/index.js plugin path."
fi
if (( runtime_references_plugin )); then
[[ -f "${plugin_manifest}" ]] || error "Final Antigravity OAuth integrity check failed: plugin package is missing at ${plugin_manifest}."
[[ -f "${plugin_setup_script}" ]] || error "Final Antigravity OAuth integrity check failed: plugin setup script is missing at ${plugin_setup_script}."
[[ -d "${plugin_dist_dir}" ]] || error "Final Antigravity OAuth integrity check failed: plugin build directory is missing at ${plugin_dist_dir}."
if [[ ! -f "${plugin_entry_primary}" && ! -f "${plugin_entry_fallback}" ]]; then
error "Final Antigravity OAuth integrity check failed: plugin OAuth entry file is missing under ${plugin_dist_dir}."
fi
fi
success "Antigravity OAuth integrity check passed."
}
opencode_works() {
command -v opencode >/dev/null 2>&1 || return 1
if command -v timeout >/dev/null 2>&1; then
timeout 8 opencode --version >/dev/null 2>&1 || timeout 8 opencode --help >/dev/null 2>&1 || return 1
return 0
fi
if command -v python3 >/dev/null 2>&1; then
python3 - <<'PY' >/dev/null 2>&1
import subprocess
import sys
for args in (["opencode", "--version"], ["opencode", "--help"]):
try:
result = subprocess.run(args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=8)
except Exception:
continue
if result.returncode == 0:
sys.exit(0)
sys.exit(1)
PY
return $?
fi
opencode --version >/dev/null 2>&1 || opencode --help >/dev/null 2>&1
}
install_opencode_shim() {
local bun_bin="${HOME}/.bun/bin"
local local_bin="${HOME}/.local/bin"
local bunx_exec="${HOME}/.bun/bin/bunx"
mkdir -p "$bun_bin" "$local_bin"
cat > "${bun_bin}/opencode" <<EOF
#!/usr/bin/env bash
if [[ -x "$bunx_exec" ]]; then
exec "$bunx_exec" --bun opencode-ai "\$@"
fi
exec bunx --bun opencode-ai "\$@"
EOF
chmod +x "${bun_bin}/opencode"
cat > "${local_bin}/opencode" <<EOF
#!/usr/bin/env bash
if [[ -x "$bunx_exec" ]]; then
exec "$bunx_exec" --bun opencode-ai "\$@"
fi
exec bunx --bun opencode-ai "\$@"
EOF
chmod +x "${local_bin}/opencode"
export PATH="${local_bin}:${bun_bin}:${PATH}"
hash -r 2>/dev/null || true
if opencode_works; then
return 0
fi
rm -f "${bun_bin}/opencode" "${local_bin}/opencode"
hash -r 2>/dev/null || true
info "Generated opencode bunx shim failed health check and was removed."
return 1
}
install_opencode_official() {
command -v curl >/dev/null 2>&1 || return 1
info "Installing opencode via official installer..."
if ! curl -fsSL https://opencode.ai/install | bash >/tmp/ocs-opencode-official.log 2>&1; then
warn "$(cat /tmp/ocs-opencode-official.log 2>/dev/null || true)"
return 1
fi
export PATH="${HOME}/.local/bin:${HOME}/.bun/bin:${PATH}"
hash -r 2>/dev/null || true
opencode_works
}
install_opencode_bun_global() {
command -v bun >/dev/null 2>&1 || return 1
info "Installing opencode-ai via bun global package..."
if ! bun add -g opencode-ai@latest >/tmp/ocs-opencode-bun-global.log 2>&1; then
warn "$(cat /tmp/ocs-opencode-bun-global.log 2>/dev/null || true)"
return 1
fi
export PATH="${HOME}/.bun/bin:${HOME}/.local/bin:${PATH}"
hash -r 2>/dev/null || true
opencode_works
}
ensure_opencode_command() {
if opencode_works; then
return 0
fi
warn "opencode command not healthy. Trying official installer..."
if install_opencode_official && opencode_works; then
return 0
fi
warn "official installer did not recover opencode. Trying bun global install..."
if install_opencode_bun_global && opencode_works; then
return 0
fi
warn "opencode command not healthy. Installing bunx shim..."
if install_opencode_shim && opencode_works; then
return 0
fi
if [[ "${OCS_ENABLE_NODE_AUTO_INSTALL:-0}" == "1" ]]; then
warn "bunx shim did not recover opencode. Trying Node.js + npm global install..."
if ensure_nodejs_runtime && install_opencode_npm_global && opencode_works; then
return 0
fi
else
warn "Skipping Node.js auto-install fallback (set OCS_ENABLE_NODE_AUTO_INSTALL=1 to enable)."
fi
return 1
}
ensure_nodejs_runtime() {
if command -v node >/dev/null 2>&1 && command -v npm >/dev/null 2>&1; then
return 0
fi
local pm
pm="$(detect_package_manager)"
[[ -n "$pm" ]] || return 1
info "Attempting to auto-install Node.js runtime via ${pm}..."
case "$pm" in
apt)
install_packages_auto "$pm" nodejs npm || return 1
;;
dnf|yum|zypper|apk)
install_packages_auto "$pm" nodejs npm || return 1
;;
pacman)
install_packages_auto "$pm" nodejs npm || return 1
;;
brew)
install_packages_auto "$pm" node || return 1
;;
*)
return 1
;;
esac
command -v node >/dev/null 2>&1 && command -v npm >/dev/null 2>&1
}
install_opencode_npm_global() {
command -v npm >/dev/null 2>&1 || return 1
info "Installing opencode-ai globally via npm..."
if ! npm install -g opencode-ai@latest >/tmp/ocs-opencode-npm.err 2>&1; then
warn "$(cat /tmp/ocs-opencode-npm.err 2>/dev/null || true)"
return 1
fi
local npm_prefix
npm_prefix="$(npm config get prefix 2>/dev/null || true)"
if [[ -n "$npm_prefix" && -d "$npm_prefix/bin" ]]; then
export PATH="$npm_prefix/bin:${PATH}"
fi
hash -r 2>/dev/null || true
opencode_works
}
install_bun_global_with_retry() {
local source_path="$1"
local attempts=5
local i
for ((i=1; i<=attempts; i++)); do
if bun install -g "$source_path" >/tmp/ocs-bun-global.err 2>&1; then
return 0
fi
local err
err="$(cat /tmp/ocs-bun-global.err 2>/dev/null || true)"
if (( i < attempts )); then
warn "bun global install failed (attempt ${i}/${attempts}), retrying..."
if is_lock_error "$err"; then
stop_windows_lock_holders
fi
sleep "$i"
continue
fi
warn "$err"
return 1
done
return 1
}
install_ocs_from_path() {
local source_path="$1"
[[ -n "$source_path" && -d "$source_path" ]] || return 1
info "Attempting ocs install from local path..."
if install_bun_global_with_retry "$source_path"; then
if [[ -d "${HOME}/.bun/bin" ]]; then
export PATH="${HOME}/.bun/bin:${PATH}"
fi
ocs_works && return 0
fi
if command -v npm >/dev/null 2>&1; then
warn "bun global install failed, trying npm global install..."
if npm install -g "$source_path" >/tmp/ocs-npm-global.err 2>&1; then
if [[ -d "${HOME}/.bun/bin" ]]; then
export PATH="${HOME}/.bun/bin:${PATH}"
fi
ocs_works && return 0
else
warn "$(cat /tmp/ocs-npm-global.err 2>/dev/null || true)"
fi
fi
if command -v pnpm >/dev/null 2>&1; then
warn "npm fallback unavailable/failed, trying pnpm global install..."
if pnpm add -g "$source_path" >/tmp/ocs-pnpm-global.err 2>&1; then
if [[ -d "${HOME}/.bun/bin" ]]; then
export PATH="${HOME}/.bun/bin:${PATH}"
fi
ocs_works && return 0
else
warn "$(cat /tmp/ocs-pnpm-global.err 2>/dev/null || true)"
fi
fi
return 1
}
install_ocs_from_private_repo() {
local token="$1"
[[ -n "$token" ]] || return 1
command -v git >/dev/null 2>&1 || return 1
local suite_tmp="${TMP_DIR}/opencode-config-suites"
rm -rf "$suite_tmp"
info "Attempting ocs install from private repository source..."
git clone --branch "${GITHUB_SOURCE_BRANCH}" --single-branch "https://x-access-token:${token}@github.com/${GITHUB_SOURCE_REPO}.git" "$suite_tmp" >/dev/null 2>&1 || return 1
install_ocs_from_path "$suite_tmp"
}
open_purchase_page() {
echo " Open purchase chat: ${WHATSAPP_ORDER_URL}"
if command -v open >/dev/null 2>&1; then
open "${WHATSAPP_ORDER_URL}" >/dev/null 2>&1 || true
return
fi
if command -v xdg-open >/dev/null 2>&1; then
xdg-open "${WHATSAPP_ORDER_URL}" >/dev/null 2>&1 || true
return
fi
}
install_ocs_shim_from_bundle() {
local plugin_path="$1"
local ocs_js="${plugin_path}/bin/ocs.cjs"
if [[ ! -f "$ocs_js" ]]; then
ocs_js="${plugin_path}/bin/ocs.js"
fi
[[ -f "$ocs_js" ]] || return 1
local bun_bin="${HOME}/.bun/bin"
local local_bin="${HOME}/.local/bin"
local bun_exec="${HOME}/.bun/bin/bun"
mkdir -p "$bun_bin" "$local_bin"
cat > "${bun_bin}/ocs" <<EOF
#!/usr/bin/env bash
if [[ -x "$bun_exec" ]]; then
exec "$bun_exec" "$ocs_js" "\$@"
fi
exec bun "$ocs_js" "\$@"
EOF
chmod +x "${bun_bin}/ocs"
cat > "${local_bin}/ocs" <<EOF
#!/usr/bin/env bash
if [[ -x "$bun_exec" ]]; then
exec "$bun_exec" "$ocs_js" "\$@"
fi
exec bun "$ocs_js" "\$@"
EOF
chmod +x "${local_bin}/ocs"
export PATH="${bun_bin}:${local_bin}:${PATH}"
hash -r 2>/dev/null || true
ocs_works
}
install_ocs_shim_from_opencode() {
local shim_cmd='bunx --bun opencode-ai "\$@"'
local bunx_exec="${HOME}/.bun/bin/bunx"
if [[ -x "$bunx_exec" ]]; then
shim_cmd="\"$bunx_exec\" --bun opencode-ai \"\$@\""
fi
if command -v opencode >/dev/null 2>&1; then
shim_cmd='opencode "\$@"'
fi
local bun_bin="${HOME}/.bun/bin"
local local_bin="${HOME}/.local/bin"
mkdir -p "$bun_bin" "$local_bin"
cat > "${bun_bin}/ocs" <<EOF
#!/usr/bin/env bash
${shim_cmd}
EOF
chmod +x "${bun_bin}/ocs"
cat > "${local_bin}/ocs" <<EOF
#!/usr/bin/env bash
${shim_cmd}
EOF
chmod +x "${local_bin}/ocs"
export PATH="${bun_bin}:${local_bin}:${PATH}"
hash -r 2>/dev/null || true
ocs_works
}
ensure_ocs_command() {
local token="$1"
local root_dir="$2"
local is_local_source="$3"
local plugin_dir="$4"
if [[ -d "${HOME}/.bun/bin" ]]; then
export PATH="${HOME}/.bun/bin:${PATH}"
fi
if ocs_works; then
info "ocs verification passed."
return 0
fi
if install_ocs_shim_from_bundle "$plugin_dir"; then
success "ocs shim install and verification passed."
return 0
fi
if [[ "$is_local_source" == "true" ]]; then
if install_ocs_from_path "$root_dir"; then
success "ocs auto-install and verification passed."
return 0
fi
fi
if install_ocs_from_private_repo "$token"; then
success "ocs auto-install and verification passed."
return 0
fi
return 1
}
ensure_shell_path_priority() {
local export_line='export PATH="$HOME/.opencode/bin:$HOME/.local/bin:$HOME/.bun/bin:$PATH"'
local source_line='[ -f "$HOME/.config/opencode/shell/ocs-path.sh" ] && . "$HOME/.config/opencode/shell/ocs-path.sh"'
local snippet_dir="${HOME}/.config/opencode/shell"
local snippet_path="${snippet_dir}/ocs-path.sh"
local profile
local shell_name="${SHELL##*/}"
export PATH="${HOME}/.opencode/bin:${HOME}/.local/bin:${HOME}/.bun/bin:${PATH}"
hash -r 2>/dev/null || true
mkdir -p "${snippet_dir}" 2>/dev/null || true
if ! ensure_text_file_exists_if_writable "${snippet_path}"; then
warn "Cannot persist shell PATH snippet at ${snippet_path}. Keep using current-session PATH export only."
else
printf '# OCS installer path\n%s\n' "${export_line}" > "${snippet_path}"
fi
profile="$(resolve_primary_shell_profile)"
if [[ -n "${profile}" ]]; then
if ensure_text_file_exists_if_writable "${profile}"; then
if ! grep -Fq "${source_line}" "${profile}"; then
printf '\n# OCS installer path\n%s\n' "${source_line}" >> "${profile}"
fi
else
warn "Cannot write to shell profile ${profile}. Current-session PATH is active, but persistence was skipped."
fi
fi
local fish_cfg="${HOME}/.config/fish/config.fish"
local fish_line='fish_add_path -m $HOME/.opencode/bin $HOME/.local/bin $HOME/.bun/bin'
if [[ "${shell_name}" == "fish" || -f "$fish_cfg" ]]; then
mkdir -p "$(dirname "$fish_cfg")" 2>/dev/null || true
if ensure_text_file_exists_if_writable "$fish_cfg"; then
if ! grep -Fq "$fish_line" "$fish_cfg"; then
printf '\n# OCS installer path\n%s\n' "$fish_line" >> "$fish_cfg"
fi
else
warn "Cannot write to fish config at ${fish_cfg}."
fi
fi
}
ensure_system_command_links() {
local target_dir="/usr/local/bin"
local cmd source_path target_path current_target
for cmd in ocs opencode; do
source_path=""
if [[ -x "${HOME}/.local/bin/${cmd}" ]]; then
source_path="${HOME}/.local/bin/${cmd}"
elif [[ -x "${HOME}/.bun/bin/${cmd}" ]]; then
source_path="${HOME}/.bun/bin/${cmd}"
fi
[[ -n "${source_path}" ]] || continue
target_path="${target_dir}/${cmd}"
if [[ -e "${target_path}" && ! -L "${target_path}" ]]; then
continue
fi
if [[ -L "${target_path}" ]]; then
current_target="$(readlink "${target_path}" 2>/dev/null || true)"
if [[ "${current_target}" == "${source_path}" ]]; then
continue
fi
fi
if [[ -w "${target_dir}" ]]; then
ln -sfn "${source_path}" "${target_path}" || true
elif run_with_privilege mkdir -p "${target_dir}" && run_with_privilege ln -sfn "${source_path}" "${target_path}"; then
:
else
warn "Cannot create ${target_path}. Keep using shell profile PATH entries."
fi
done
hash -r 2>/dev/null || true
}
is_lock_error() {
local msg="$1"
[[ "$msg" =~ EBUSY|EFAULT|EPERM|ENOENT|resource\ busy|being\ used\ by\ another\ process|Access\ is\ denied ]]
}
stop_windows_lock_holders() {
if ! command -v powershell >/dev/null 2>&1; then
return 0
fi
powershell -NoProfile -ExecutionPolicy Bypass -Command "Get-Process bun,node,opencode,biome -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue" >/dev/null 2>&1 || true
}
install_dependencies_with_retry() {
local install_dir="$1"
local attempts=5
local i
for ((i=1; i<=attempts; i++)); do
if bun install --frozen-lockfile >/dev/null 2>&1; then
return 0
fi
if bun install >/tmp/ocs-bun-install.err 2>&1; then
return 0
fi
local err
err="$(cat /tmp/ocs-bun-install.err 2>/dev/null || true)"
if (( i < attempts )); then
warn "bun install failed (attempt ${i}/${attempts}), retrying..."
if is_lock_error "$err"; then
stop_windows_lock_holders "$install_dir"
fi
sleep "$i"
continue
fi
warn "$err"
return 1
done
return 1
}
has_interactive_tty() {
[[ -r /dev/tty && -w /dev/tty ]]
}
ensure_gh_cli_for_oauth() {
if command -v gh >/dev/null 2>&1; then
return 0
fi
if ! has_interactive_tty; then
return 1
fi
local pm
pm="$(detect_package_manager)"
if [[ -z "$pm" ]]; then
warn "Cannot auto-install gh: no supported package manager detected."
return 1
fi
info "GitHub CLI (gh) not found. Attempting auto-install via ${pm} for OAuth login..."
if install_packages_auto "$pm" gh; then
success "GitHub CLI installed."
return 0
fi
warn "Failed to auto-install gh."
return 1
}
print_gh_auth_terminal_guide() {
warn "Run this command in terminal, then rerun installer:"
warn "gh auth login"
warn "Then choose: GitHub.com -> HTTPS -> Yes -> Login with a web browser"
warn "If browser auto-open fails (WSL), open shown URL manually and finish login"
warn "Optional hardening: gh auth refresh -h github.com -s repo"
if ! command -v gh >/dev/null 2>&1; then
warn "Install GitHub CLI first: https://cli.github.com/"
fi
}
refresh_gh_repo_scope() {
has_interactive_tty || return 1
if gh auth login >/dev/null 2>&1; then
gh config set -h github.com git_protocol https >/dev/null 2>&1 || true
gh auth refresh -h github.com -s repo >/dev/null 2>&1 || true
return 0
fi
if gh auth refresh -h github.com -s repo >/dev/null 2>&1; then
gh config set -h github.com git_protocol https >/dev/null 2>&1 || true
return 0
fi
gh auth login >/dev/null 2>&1 || return 1
gh config set -h github.com git_protocol https >/dev/null 2>&1 || true
gh auth refresh -h github.com -s repo >/dev/null 2>&1 || true
return 0
}
# ─── Auth: resolve GitHub token ───────────────────────────────────────────────
resolve_token() {
# Path 1: GITHUB_TOKEN env var
if [[ -n "${GITHUB_TOKEN:-}" ]]; then