forked from darrenhinde/OpenAgentsControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·1493 lines (1275 loc) · 50.8 KB
/
install.sh
File metadata and controls
executable file
·1493 lines (1275 loc) · 50.8 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
#############################################################################
# OpenAgents Control Installer
# Interactive installer for OpenCode agents, commands, tools, and plugins
#
# Compatible with:
# - macOS (bash 3.2+)
# - Linux (bash 3.2+)
# - Windows (Git Bash, WSL)
#############################################################################
set -e
# Detect platform
PLATFORM="$(uname -s)"
case "$PLATFORM" in
Linux*) PLATFORM="Linux";;
Darwin*) PLATFORM="macOS";;
CYGWIN*|MINGW*|MSYS*) PLATFORM="Windows";;
*) PLATFORM="Unknown";;
esac
# Colors for output (disable on Windows if not supported)
if [ "$PLATFORM" = "Windows" ] && [ -z "$WT_SESSION" ] && [ -z "$ConEmuPID" ]; then
# Basic Windows terminal without color support
RED=''
GREEN=''
YELLOW=''
BLUE=''
MAGENTA=''
CYAN=''
BOLD=''
NC=''
else
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m' # No Color
fi
# Configuration
REPO_URL="https://github.com/darrenhinde/OpenAgentsControl"
BRANCH="${OPENCODE_BRANCH:-main}" # Allow override via environment variable
RAW_URL="https://raw.githubusercontent.com/darrenhinde/OpenAgentsControl/${BRANCH}"
# Registry URL - supports local fallback for development
# Priority: 1) REGISTRY_URL env var, 2) Local registry.json, 3) Remote GitHub
if [ -n "$REGISTRY_URL" ]; then
# Use explicitly set REGISTRY_URL (for testing)
:
elif [ -f "./registry.json" ]; then
# Use local registry.json if it exists (for development)
REGISTRY_URL="file://$(pwd)/registry.json"
else
# Default to remote GitHub registry
REGISTRY_URL="${RAW_URL}/registry.json"
fi
INSTALL_DIR="${OPENCODE_INSTALL_DIR:-.opencode}" # Allow override via environment variable
TEMP_DIR="/tmp/opencode-installer-$$"
# Cleanup temp directory on exit (success or failure)
trap 'rm -rf "$TEMP_DIR" 2>/dev/null || true' EXIT INT TERM
# Global variables
SELECTED_COMPONENTS=()
INSTALL_MODE=""
PROFILE=""
NON_INTERACTIVE=false
CUSTOM_INSTALL_DIR="" # Set via --install-dir argument
#############################################################################
# Utility Functions
#############################################################################
jq_exec() {
local output
output=$(jq -r "$@")
local ret=$?
printf "%s\n" "$output" | tr -d '\r'
return $ret
}
print_header() {
echo -e "${CYAN}${BOLD}"
echo "╔════════════════════════════════════════════════════════════════╗"
echo "║ ║"
echo "║ OpenAgents Control Installer v1.0.0 ║"
echo "║ ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo -e "${NC}"
}
print_success() {
echo -e "${GREEN}✓${NC} $1"
}
print_error() {
echo -e "${RED}✗${NC} $1"
}
print_info() {
echo -e "${BLUE}ℹ${NC} $1"
}
print_warning() {
echo -e "${YELLOW}⚠${NC} $1"
}
print_step() {
echo -e "\n${MAGENTA}${BOLD}▶${NC} $1\n"
}
#############################################################################
# Path Handling (Cross-Platform)
#############################################################################
normalize_and_validate_path() {
local input_path="$1"
local normalized_path
# Handle empty path
if [ -z "$input_path" ]; then
echo ""
return 1
fi
# Expand tilde to $HOME (works on Linux, macOS, Windows Git Bash)
if [[ $input_path == ~* ]]; then
normalized_path="${HOME}${input_path:1}"
else
normalized_path="$input_path"
fi
# Convert backslashes to forward slashes (Windows compatibility)
normalized_path="${normalized_path//\\//}"
# Remove trailing slashes
normalized_path="${normalized_path%/}"
# If path is relative, make it absolute based on current directory
if [[ ! "$normalized_path" = /* ]] && [[ ! "$normalized_path" =~ ^[A-Za-z]: ]]; then
normalized_path="$(pwd)/${normalized_path}"
fi
echo "$normalized_path"
return 0
}
validate_install_path() {
local path="$1"
local parent_dir
# Get parent directory
parent_dir="$(dirname "$path")"
# Check if parent directory exists
if [ ! -d "$parent_dir" ]; then
print_error "Parent directory does not exist: $parent_dir"
return 1
fi
# Check if parent directory is writable
if [ ! -w "$parent_dir" ]; then
print_error "No write permission for directory: $parent_dir"
return 1
fi
# If target directory exists, check if it's writable
if [ -d "$path" ] && [ ! -w "$path" ]; then
print_error "No write permission for directory: $path"
return 1
fi
return 0
}
get_global_install_path() {
# Return platform-appropriate global installation path
case "$PLATFORM" in
macOS)
# macOS: Use XDG standard (consistent with Linux)
echo "${HOME}/.config/opencode"
;;
Linux)
echo "${HOME}/.config/opencode"
;;
Windows)
# Windows Git Bash/WSL: Use same as Linux
echo "${HOME}/.config/opencode"
;;
*)
echo "${HOME}/.config/opencode"
;;
esac
}
#############################################################################
# Dependency Checks
#############################################################################
check_bash_version() {
# Check bash version (need 3.2+)
local bash_version="${BASH_VERSION%%.*}"
if [ "$bash_version" -lt 3 ]; then
echo "Error: This script requires Bash 3.2 or higher"
echo "Current version: $BASH_VERSION"
echo ""
echo "Please upgrade bash or use a different shell:"
echo " macOS: brew install bash"
echo " Linux: Use your package manager to update bash"
echo " Windows: Use Git Bash or WSL"
exit 1
fi
}
check_dependencies() {
print_step "Checking dependencies..."
local missing_deps=()
if ! command -v curl &> /dev/null; then
missing_deps+=("curl")
fi
if ! command -v jq &> /dev/null; then
missing_deps+=("jq")
fi
if [ ${#missing_deps[@]} -ne 0 ]; then
print_error "Missing required dependencies: ${missing_deps[*]}"
echo ""
echo "Please install them:"
case "$PLATFORM" in
macOS)
echo " brew install ${missing_deps[*]}"
;;
Linux)
echo " Ubuntu/Debian: sudo apt-get install ${missing_deps[*]}"
echo " Fedora/RHEL: sudo dnf install ${missing_deps[*]}"
echo " Arch: sudo pacman -S ${missing_deps[*]}"
;;
Windows)
echo " Git Bash: Install via https://git-scm.com/"
echo " WSL: sudo apt-get install ${missing_deps[*]}"
echo " Scoop: scoop install ${missing_deps[*]}"
;;
*)
echo " Use your package manager to install: ${missing_deps[*]}"
;;
esac
exit 1
fi
print_success "All dependencies found"
}
#############################################################################
# Registry Functions
#############################################################################
fetch_registry() {
print_step "Fetching component registry..."
mkdir -p "$TEMP_DIR"
# Handle local file:// URLs
if [[ "$REGISTRY_URL" == file://* ]]; then
local local_path="${REGISTRY_URL#file://}"
if [ -f "$local_path" ]; then
cp "$local_path" "$TEMP_DIR/registry.json"
print_success "Using local registry: $local_path"
else
print_error "Local registry not found: $local_path"
exit 1
fi
else
# Fetch from remote URL
if ! curl -fsSL "$REGISTRY_URL" -o "$TEMP_DIR/registry.json"; then
print_error "Failed to fetch registry from $REGISTRY_URL"
exit 1
fi
print_success "Registry fetched successfully"
fi
}
get_profile_components() {
local profile=$1
jq_exec ".profiles.${profile}.components[]?" "$TEMP_DIR/registry.json"
}
get_component_info() {
local component_id=$1
local component_type=$2
if [ "$component_type" = "context" ] && [[ "$component_id" == */* ]]; then
jq_exec "first(.components.contexts[]? | select(.path == \".opencode/context/${component_id}.md\"))" "$TEMP_DIR/registry.json"
return
fi
jq_exec ".components.${component_type}[]? | select(.id == \"${component_id}\" or (.aliases // [] | index(\"${component_id}\")))" "$TEMP_DIR/registry.json"
}
resolve_component_path() {
local component_type=$1
local component_id=$2
local registry_key
registry_key=$(get_registry_key "$component_type")
if [ "$component_type" = "context" ] && [[ "$component_id" == */* ]]; then
jq_exec "first(.components.contexts[]? | select(.path == \".opencode/context/${component_id}.md\") | .path)" "$TEMP_DIR/registry.json"
return
fi
jq_exec ".components.${registry_key}[]? | select(.id == \"${component_id}\" or (.aliases // [] | index(\"${component_id}\"))) | .path" "$TEMP_DIR/registry.json"
}
# Helper function to get the correct registry key for a component type
get_registry_key() {
local type=$1
# Most types are pluralized, but 'config' stays singular
case "$type" in
config) echo "config" ;;
*) echo "${type}s" ;;
esac
}
# Helper function to convert registry path to installation path
# Registry paths are like ".opencode/agent/foo.md"
# We need to replace ".opencode" with the actual INSTALL_DIR
get_install_path() {
local registry_path=$1
# Strip leading .opencode/ if present
local relative_path="${registry_path#.opencode/}"
# Return INSTALL_DIR + relative path
echo "${INSTALL_DIR}/${relative_path}"
}
expand_context_wildcard() {
local pattern=$1
local prefix="${pattern%%\**}"
prefix="${prefix%/}"
if [ -n "$prefix" ]; then
prefix="${prefix}/"
fi
jq_exec ".components.contexts[]? | select(.path | startswith(\".opencode/context/${prefix}\")) | .path | sub(\"^\\\\.opencode/context/\"; \"\") | sub(\"\\\\.md$\"; \"\")" "$TEMP_DIR/registry.json"
}
expand_selected_components() {
local expanded=()
for comp in "${SELECTED_COMPONENTS[@]}"; do
local type="${comp%%:*}"
local id="${comp##*:}"
if [[ "$id" == *"*"* ]]; then
if [ "$type" != "context" ]; then
print_warning "Wildcard only supported for context components: ${comp}"
continue
fi
local matches
matches=$(expand_context_wildcard "$id")
if [ -z "$matches" ]; then
print_warning "No contexts matched: ${comp}"
continue
fi
while IFS= read -r match; do
[ -n "$match" ] && expanded+=("context:${match}")
done <<< "$matches"
continue
fi
expanded+=("$comp")
done
local deduped=()
for comp in "${expanded[@]}"; do
local found=0
for existing in "${deduped[@]}"; do
if [ "$existing" = "$comp" ]; then
found=1
break
fi
done
if [ "$found" -eq 0 ]; then
deduped+=("$comp")
fi
done
SELECTED_COMPONENTS=("${deduped[@]}")
}
resolve_dependencies() {
local component=$1
local type="${component%%:*}"
local id="${component##*:}"
# Get the correct registry key (handles singular/plural)
local registry_key
registry_key=$(get_registry_key "$type")
# Get dependencies for this component
local deps
deps=$(jq_exec ".components.${registry_key}[] | select(.id == \"${id}\" or (.aliases // [] | index(\"${id}\"))) | .dependencies[]?" "$TEMP_DIR/registry.json" 2>/dev/null || echo "")
if [ -n "$deps" ]; then
for dep in $deps; do
if [[ "$dep" == *"*"* ]]; then
local dep_type="${dep%%:*}"
local dep_id="${dep##*:}"
if [ "$dep_type" = "context" ]; then
local matched
matched=$(expand_context_wildcard "$dep_id")
if [ -z "$matched" ]; then
print_warning "No contexts matched dependency: ${dep}"
continue
fi
while IFS= read -r match; do
local expanded_dep="context:${match}"
local found=0
for existing in "${SELECTED_COMPONENTS[@]}"; do
if [ "$existing" = "$expanded_dep" ]; then
found=1
break
fi
done
if [ "$found" -eq 0 ]; then
SELECTED_COMPONENTS+=("$expanded_dep")
resolve_dependencies "$expanded_dep"
fi
done <<< "$matched"
continue
fi
fi
# Add dependency if not already in list
local found=0
for existing in "${SELECTED_COMPONENTS[@]}"; do
if [ "$existing" = "$dep" ]; then
found=1
break
fi
done
if [ "$found" -eq 0 ]; then
SELECTED_COMPONENTS+=("$dep")
# Recursively resolve dependencies
resolve_dependencies "$dep"
fi
done
fi
}
#############################################################################
# Installation Mode Selection
#############################################################################
check_interactive_mode() {
# Check if stdin is a terminal (not piped from curl)
if [ ! -t 0 ]; then
print_header
print_error "Interactive mode requires a terminal"
echo ""
echo "You're running this script in a pipe (e.g., curl | bash)"
echo "For interactive mode, download the script first:"
echo ""
echo -e "${CYAN}# Download the script${NC}"
echo "curl -fsSL https://raw.githubusercontent.com/darrenhinde/OpenAgentsControl/main/install.sh -o install.sh"
echo ""
echo -e "${CYAN}# Run interactively${NC}"
echo "bash install.sh"
echo ""
echo "Or use a profile directly:"
echo ""
echo -e "${CYAN}# Quick install with profile${NC}"
echo "curl -fsSL https://raw.githubusercontent.com/darrenhinde/OpenAgentsControl/main/install.sh | bash -s essential"
echo ""
echo "Available profiles: essential, developer, business, full, advanced"
echo ""
cleanup_and_exit 1
fi
}
show_install_location_menu() {
check_interactive_mode
clear
print_header
local global_path
global_path=$(get_global_install_path)
echo -e "${BOLD}Choose installation location:${NC}\n"
echo -e " ${GREEN}1) Local${NC} - Install to ${CYAN}.opencode/${NC} in current directory"
echo " (Best for project-specific agents)"
echo ""
echo -e " ${BLUE}2) Global${NC} - Install to ${CYAN}${global_path}${NC}"
echo " (Best for user-wide agents available everywhere)"
echo ""
echo -e " ${MAGENTA}3) Custom${NC} - Enter exact path"
echo " Examples:"
case "$PLATFORM" in
Windows)
echo -e " ${CYAN}C:/Users/username/my-agents${NC} or ${CYAN}~/my-agents${NC}"
;;
*)
echo -e " ${CYAN}/home/username/my-agents${NC} or ${CYAN}~/my-agents${NC}"
;;
esac
echo ""
echo " 4) Back / Exit"
echo ""
read -r -p "Enter your choice [1-4]: " location_choice
case $location_choice in
1)
INSTALL_DIR=".opencode"
print_success "Installing to local directory: .opencode/"
sleep 1
;;
2)
INSTALL_DIR="$global_path"
print_success "Installing to global directory: $global_path"
sleep 1
;;
3)
echo ""
read -r -p "Enter installation path: " custom_path
if [ -z "$custom_path" ]; then
print_error "No path entered"
sleep 2
show_install_location_menu
return
fi
local normalized_path
normalized_path=$(normalize_and_validate_path "$custom_path")
if ! normalize_and_validate_path "$custom_path" > /dev/null; then
print_error "Invalid path"
sleep 2
show_install_location_menu
return
fi
if ! validate_install_path "$normalized_path"; then
echo ""
read -r -p "Continue anyway? [y/N]: " continue_choice
if [[ ! $continue_choice =~ ^[Yy] ]]; then
show_install_location_menu
return
fi
fi
INSTALL_DIR="$normalized_path"
print_success "Installing to custom directory: $INSTALL_DIR"
sleep 1
;;
4)
cleanup_and_exit 0
;;
*)
print_error "Invalid choice"
sleep 2
show_install_location_menu
return
;;
esac
}
show_main_menu() {
check_interactive_mode
clear
print_header
echo -e "${BOLD}Choose installation mode:${NC}\n"
echo " 1) Quick Install (Choose a profile)"
echo " 2) Custom Install (Pick individual components)"
echo " 3) List Available Components"
echo " 4) Exit"
echo ""
read -r -p "Enter your choice [1-4]: " choice
case $choice in
1) INSTALL_MODE="profile" ;;
2) INSTALL_MODE="custom" ;;
3) list_components; read -r -p "Press Enter to continue..."; show_main_menu ;;
4) cleanup_and_exit 0 ;;
*) print_error "Invalid choice"; sleep 2; show_main_menu ;;
esac
}
#############################################################################
# Profile Installation
#############################################################################
show_profile_menu() {
clear
print_header
echo -e "${BOLD}Available Installation Profiles:${NC}\n"
# Essential profile
local essential_name
essential_name=$(jq_exec '.profiles.essential.name' "$TEMP_DIR/registry.json")
local essential_desc
essential_desc=$(jq_exec '.profiles.essential.description' "$TEMP_DIR/registry.json")
local essential_count
essential_count=$(jq_exec '.profiles.essential.components | length' "$TEMP_DIR/registry.json")
echo -e " ${GREEN}1) ${essential_name}${NC}"
echo -e " ${essential_desc}"
echo -e " Components: ${essential_count}\n"
# Developer profile
local dev_desc
dev_desc=$(jq_exec '.profiles.developer.description' "$TEMP_DIR/registry.json")
local dev_count
dev_count=$(jq_exec '.profiles.developer.components | length' "$TEMP_DIR/registry.json")
local dev_badge
dev_badge=$(jq_exec '.profiles.developer.badge // ""' "$TEMP_DIR/registry.json")
if [ -n "$dev_badge" ]; then
echo -e " ${BLUE}2) Developer ${GREEN}[${dev_badge}]${NC}"
else
echo -e " ${BLUE}2) Developer${NC}"
fi
echo -e " ${dev_desc}"
echo -e " Components: ${dev_count}\n"
# Business profile
local business_name
business_name=$(jq_exec '.profiles.business.name' "$TEMP_DIR/registry.json")
local business_desc
business_desc=$(jq_exec '.profiles.business.description' "$TEMP_DIR/registry.json")
local business_count
business_count=$(jq_exec '.profiles.business.components | length' "$TEMP_DIR/registry.json")
echo -e " ${CYAN}3) ${business_name}${NC}"
echo -e " ${business_desc}"
echo -e " Components: ${business_count}\n"
# Full profile
local full_name
full_name=$(jq_exec '.profiles.full.name' "$TEMP_DIR/registry.json")
local full_desc
full_desc=$(jq_exec '.profiles.full.description' "$TEMP_DIR/registry.json")
local full_count
full_count=$(jq_exec '.profiles.full.components | length' "$TEMP_DIR/registry.json")
echo -e " ${MAGENTA}4) ${full_name}${NC}"
echo -e " ${full_desc}"
echo -e " Components: ${full_count}\n"
# Advanced profile
local adv_name
adv_name=$(jq_exec '.profiles.advanced.name' "$TEMP_DIR/registry.json")
local adv_desc
adv_desc=$(jq_exec '.profiles.advanced.description' "$TEMP_DIR/registry.json")
local adv_count
adv_count=$(jq_exec '.profiles.advanced.components | length' "$TEMP_DIR/registry.json")
echo -e " ${YELLOW}5) ${adv_name}${NC}"
echo -e " ${adv_desc}"
echo -e " Components: ${adv_count}\n"
echo " 6) Back to main menu"
echo ""
read -r -p "Enter your choice [1-6]: " choice
case $choice in
1) PROFILE="essential" ;;
2) PROFILE="developer" ;;
3) PROFILE="business" ;;
4) PROFILE="full" ;;
5) PROFILE="advanced" ;;
6) show_main_menu; return ;;
*) print_error "Invalid choice"; sleep 2; show_profile_menu; return ;;
esac
# Load profile components (compatible with bash 3.2+)
SELECTED_COMPONENTS=()
local temp_file="$TEMP_DIR/components.tmp"
get_profile_components "$PROFILE" > "$temp_file"
while IFS= read -r component; do
[ -n "$component" ] && SELECTED_COMPONENTS+=("$component")
done < "$temp_file"
expand_selected_components
# Resolve dependencies for profile installs
print_step "Resolving dependencies..."
local original_count=${#SELECTED_COMPONENTS[@]}
for comp in "${SELECTED_COMPONENTS[@]}"; do
resolve_dependencies "$comp"
done
local new_count=${#SELECTED_COMPONENTS[@]}
if [ "$new_count" -gt "$original_count" ]; then
local added=$((new_count - original_count))
print_info "Added $added dependencies"
fi
show_installation_preview
}
#############################################################################
# Custom Component Selection
#############################################################################
show_custom_menu() {
clear
print_header
echo -e "${BOLD}Select component categories to install:${NC}\n"
echo "Use space to toggle, Enter to continue"
echo ""
local categories=("agents" "subagents" "commands" "tools" "plugins" "skills" "contexts" "config")
local selected_categories=()
# Simple selection (for now, we'll make it interactive later)
echo "Available categories:"
for i in "${!categories[@]}"; do
local cat="${categories[$i]}"
local count
count=$(jq_exec ".components.${cat} | length" "$TEMP_DIR/registry.json")
local cat_display
cat_display=$(echo "$cat" | awk '{print toupper(substr($0,1,1)) tolower(substr($0,2))}')
echo " $((i+1))) ${cat_display} (${count} available)"
done
echo " $((${#categories[@]}+1))) Select All"
echo " $((${#categories[@]}+2))) Continue to component selection"
echo " $((${#categories[@]}+3))) Back to main menu"
echo ""
read -r -p "Enter category numbers (space-separated) or option: " -a selections
for sel in "${selections[@]}"; do
if [ "$sel" -eq $((${#categories[@]}+1)) ]; then
selected_categories=("${categories[@]}")
break
elif [ "$sel" -eq $((${#categories[@]}+2)) ]; then
break
elif [ "$sel" -eq $((${#categories[@]}+3)) ]; then
show_main_menu
return
elif [ "$sel" -ge 1 ] && [ "$sel" -le ${#categories[@]} ]; then
selected_categories+=("${categories[$((sel-1))]}")
fi
done
if [ ${#selected_categories[@]} -eq 0 ]; then
print_warning "No categories selected"
sleep 2
show_custom_menu
return
fi
show_component_selection "${selected_categories[@]}"
}
show_component_selection() {
local categories=("$@")
clear
print_header
echo -e "${BOLD}Select components to install:${NC}\n"
local all_components=()
local component_details=()
for category in "${categories[@]}"; do
local cat_display
cat_display=$(echo "$category" | awk '{print toupper(substr($0,1,1)) tolower(substr($0,2))}')
echo -e "${CYAN}${BOLD}${cat_display}:${NC}"
local components
components=$(jq_exec ".components.${category}[]? | .id" "$TEMP_DIR/registry.json")
local idx=1
while IFS= read -r comp_id; do
local comp_name
comp_name=$(jq_exec ".components.${category}[]? | select(.id == \"${comp_id}\") | .name" "$TEMP_DIR/registry.json")
local comp_desc
comp_desc=$(jq_exec ".components.${category}[]? | select(.id == \"${comp_id}\") | .description" "$TEMP_DIR/registry.json")
echo " ${idx}) ${comp_name}"
echo " ${comp_desc}"
all_components+=("${category}:${comp_id}")
component_details+=("${comp_name}|${comp_desc}")
idx=$((idx+1))
done <<< "$components"
echo ""
done
echo "Enter component numbers (space-separated), 'all' for all, or 'done' to continue:"
read -r -a selections
for sel in "${selections[@]}"; do
if [ "$sel" = "all" ]; then
SELECTED_COMPONENTS=("${all_components[@]}")
break
elif [ "$sel" = "done" ]; then
break
elif [ "$sel" -ge 1 ] && [ "$sel" -le ${#all_components[@]} ]; then
SELECTED_COMPONENTS+=("${all_components[$((sel-1))]}")
fi
done
if [ ${#SELECTED_COMPONENTS[@]} -eq 0 ]; then
print_warning "No components selected"
sleep 2
show_custom_menu
return
fi
# Resolve dependencies
print_step "Resolving dependencies..."
local original_count=${#SELECTED_COMPONENTS[@]}
for comp in "${SELECTED_COMPONENTS[@]}"; do
resolve_dependencies "$comp"
done
if [ ${#SELECTED_COMPONENTS[@]} -gt "$original_count" ]; then
print_info "Added $((${#SELECTED_COMPONENTS[@]} - original_count)) dependencies"
fi
show_installation_preview
}
#############################################################################
# Installation Preview & Confirmation
#############################################################################
show_installation_preview() {
# Only clear screen in interactive mode
if [ "$NON_INTERACTIVE" != true ]; then
clear
fi
print_header
echo -e "${BOLD}Installation Preview${NC}\n"
if [ -n "$PROFILE" ]; then
echo -e "Profile: ${GREEN}${PROFILE}${NC}"
else
echo -e "Mode: ${GREEN}Custom${NC}"
fi
echo -e "Installation directory: ${CYAN}${INSTALL_DIR}${NC}"
echo -e "\nComponents to install (${#SELECTED_COMPONENTS[@]} total):\n"
# Group by type
local agents=()
local subagents=()
local commands=()
local tools=()
local plugins=()
local skills=()
local contexts=()
local configs=()
for comp in "${SELECTED_COMPONENTS[@]}"; do
local type="${comp%%:*}"
case $type in
agent) agents+=("$comp") ;;
subagent) subagents+=("$comp") ;;
command) commands+=("$comp") ;;
tool) tools+=("$comp") ;;
plugin) plugins+=("$comp") ;;
skill) skills+=("$comp") ;;
context) contexts+=("$comp") ;;
config) configs+=("$comp") ;;
esac
done
[ ${#agents[@]} -gt 0 ] && echo -e "${CYAN}Agents (${#agents[@]}):${NC} ${agents[*]##*:}"
[ ${#subagents[@]} -gt 0 ] && echo -e "${CYAN}Subagents (${#subagents[@]}):${NC} ${subagents[*]##*:}"
[ ${#commands[@]} -gt 0 ] && echo -e "${CYAN}Commands (${#commands[@]}):${NC} ${commands[*]##*:}"
[ ${#tools[@]} -gt 0 ] && echo -e "${CYAN}Tools (${#tools[@]}):${NC} ${tools[*]##*:}"
[ ${#plugins[@]} -gt 0 ] && echo -e "${CYAN}Plugins (${#plugins[@]}):${NC} ${plugins[*]##*:}"
[ ${#skills[@]} -gt 0 ] && echo -e "${CYAN}Skills (${#skills[@]}):${NC} ${skills[*]##*:}"
[ ${#contexts[@]} -gt 0 ] && echo -e "${CYAN}Contexts (${#contexts[@]}):${NC} ${contexts[*]##*:}"
[ ${#configs[@]} -gt 0 ] && echo -e "${CYAN}Config (${#configs[@]}):${NC} ${configs[*]##*:}"
echo ""
# Skip confirmation if profile was provided via command line
if [ "$NON_INTERACTIVE" = true ]; then
print_info "Installing automatically (profile specified)..."
perform_installation
else
read -r -p "Proceed with installation? [Y/n]: " confirm
if [[ $confirm =~ ^[Nn] ]]; then
print_info "Installation cancelled"
cleanup_and_exit 0
fi
perform_installation
fi
}
#############################################################################
# Collision Detection
#############################################################################
show_collision_report() {
local collision_count=$1
shift
local collisions=("$@")
echo ""
print_warning "Found ${collision_count} file collision(s):"
echo ""
# Group by type
local agents=()
local subagents=()
local commands=()
local tools=()
local plugins=()
local skills=()
local contexts=()
local configs=()
for file in "${collisions[@]}"; do
# Skip empty entries
[ -z "$file" ] && continue
if [[ $file == *"/agent/subagents/"* ]]; then
subagents+=("$file")
elif [[ $file == *"/agent/"* ]]; then
agents+=("$file")
elif [[ $file == *"/command/"* ]]; then
commands+=("$file")
elif [[ $file == *"/tool/"* ]]; then
tools+=("$file")
elif [[ $file == *"/plugin/"* ]]; then
plugins+=("$file")
elif [[ $file == *"/skills/"* ]]; then
skills+=("$file")
elif [[ $file == *"/context/"* ]]; then
contexts+=("$file")
else
configs+=("$file")
fi
done
# Display grouped collisions
[ ${#agents[@]} -gt 0 ] && echo -e "${YELLOW} Agents (${#agents[@]}):${NC}" && printf ' %s\n' "${agents[@]}"
[ ${#subagents[@]} -gt 0 ] && echo -e "${YELLOW} Subagents (${#subagents[@]}):${NC}" && printf ' %s\n' "${subagents[@]}"
[ ${#commands[@]} -gt 0 ] && echo -e "${YELLOW} Commands (${#commands[@]}):${NC}" && printf ' %s\n' "${commands[@]}"
[ ${#tools[@]} -gt 0 ] && echo -e "${YELLOW} Tools (${#tools[@]}):${NC}" && printf ' %s\n' "${tools[@]}"
[ ${#plugins[@]} -gt 0 ] && echo -e "${YELLOW} Plugins (${#plugins[@]}):${NC}" && printf ' %s\n' "${plugins[@]}"
[ ${#skills[@]} -gt 0 ] && echo -e "${YELLOW} Skills (${#skills[@]}):${NC}" && printf ' %s\n' "${skills[@]}"
[ ${#contexts[@]} -gt 0 ] && echo -e "${YELLOW} Context (${#contexts[@]}):${NC}" && printf ' %s\n' "${contexts[@]}"
[ ${#configs[@]} -gt 0 ] && echo -e "${YELLOW} Config (${#configs[@]}):${NC}" && printf ' %s\n' "${configs[@]}"
echo ""
}
get_install_strategy() {
echo -e "${BOLD}How would you like to proceed?${NC}\n" >&2
echo " 1) ${GREEN}Skip existing${NC} - Only install new files, keep all existing files unchanged" >&2
echo " 2) ${YELLOW}Overwrite all${NC} - Replace existing files with new versions (your changes will be lost)" >&2
echo " 3) ${CYAN}Backup & overwrite${NC} - Backup existing files, then install new versions" >&2
echo " 4) ${RED}Cancel${NC} - Exit without making changes" >&2
echo "" >&2
read -r -p "Enter your choice [1-4]: " strategy_choice
case $strategy_choice in
1) echo "skip" ;;
2)
echo "" >&2
print_warning "This will overwrite existing files. Your changes will be lost!"
read -r -p "Are you sure? Type 'yes' to confirm: " confirm
if [ "$confirm" = "yes" ]; then
echo "overwrite"
else
echo "cancel"
fi
;;
3) echo "backup" ;;
4) echo "cancel" ;;
*) echo "cancel" ;;