install.sh 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. #!/usr/bin/env bash
  2. #############################################################################
  3. # OpenAgents Control Installer
  4. # Interactive installer for OpenCode agents, commands, tools, and plugins
  5. #
  6. # Compatible with:
  7. # - macOS (bash 3.2+)
  8. # - Linux (bash 3.2+)
  9. # - Windows (Git Bash, WSL)
  10. #############################################################################
  11. set -e
  12. # Detect platform
  13. PLATFORM="$(uname -s)"
  14. case "$PLATFORM" in
  15. Linux*) PLATFORM="Linux";;
  16. Darwin*) PLATFORM="macOS";;
  17. CYGWIN*|MINGW*|MSYS*) PLATFORM="Windows";;
  18. *) PLATFORM="Unknown";;
  19. esac
  20. # Colors for output (disable on Windows if not supported)
  21. if [ "$PLATFORM" = "Windows" ] && [ -z "$WT_SESSION" ] && [ -z "$ConEmuPID" ]; then
  22. # Basic Windows terminal without color support
  23. RED=''
  24. GREEN=''
  25. YELLOW=''
  26. BLUE=''
  27. MAGENTA=''
  28. CYAN=''
  29. BOLD=''
  30. NC=''
  31. else
  32. RED='\033[0;31m'
  33. GREEN='\033[0;32m'
  34. YELLOW='\033[1;33m'
  35. BLUE='\033[0;34m'
  36. MAGENTA='\033[0;35m'
  37. CYAN='\033[0;36m'
  38. BOLD='\033[1m'
  39. NC='\033[0m' # No Color
  40. fi
  41. # Configuration
  42. REPO_URL="https://github.com/darrenhinde/OpenAgentsControl"
  43. BRANCH="${OPENCODE_BRANCH:-main}" # Allow override via environment variable
  44. RAW_URL="https://raw.githubusercontent.com/darrenhinde/OpenAgentsControl/${BRANCH}"
  45. # Registry URL - supports local fallback for development
  46. # Priority: 1) REGISTRY_URL env var, 2) Local registry.json, 3) Remote GitHub
  47. if [ -n "$REGISTRY_URL" ]; then
  48. # Use explicitly set REGISTRY_URL (for testing)
  49. :
  50. elif [ -f "./registry.json" ]; then
  51. # Use local registry.json if it exists (for development)
  52. REGISTRY_URL="file://$(pwd)/registry.json"
  53. else
  54. # Default to remote GitHub registry
  55. REGISTRY_URL="${RAW_URL}/registry.json"
  56. fi
  57. INSTALL_DIR="${OPENCODE_INSTALL_DIR:-.opencode}" # Allow override via environment variable
  58. TEMP_DIR="/tmp/opencode-installer-$$"
  59. # Cleanup temp directory on exit (success or failure)
  60. trap 'rm -rf "$TEMP_DIR" 2>/dev/null || true' EXIT INT TERM
  61. # Global variables
  62. SELECTED_COMPONENTS=()
  63. INSTALL_MODE=""
  64. PROFILE=""
  65. NON_INTERACTIVE=false
  66. CUSTOM_INSTALL_DIR="" # Set via --install-dir argument
  67. #############################################################################
  68. # Utility Functions
  69. #############################################################################
  70. jq_exec() {
  71. local output
  72. output=$(jq -r "$@")
  73. local ret=$?
  74. printf "%s\n" "$output" | tr -d '\r'
  75. return $ret
  76. }
  77. print_header() {
  78. echo -e "${CYAN}${BOLD}"
  79. echo "╔════════════════════════════════════════════════════════════════╗"
  80. echo "║ ║"
  81. echo "║ OpenAgents Control Installer v1.0.0 ║"
  82. echo "║ ║"
  83. echo "╚════════════════════════════════════════════════════════════════╝"
  84. echo -e "${NC}"
  85. }
  86. print_success() {
  87. echo -e "${GREEN}✓${NC} $1"
  88. }
  89. print_error() {
  90. echo -e "${RED}✗${NC} $1"
  91. }
  92. print_info() {
  93. echo -e "${BLUE}ℹ${NC} $1"
  94. }
  95. print_warning() {
  96. echo -e "${YELLOW}⚠${NC} $1"
  97. }
  98. print_step() {
  99. echo -e "\n${MAGENTA}${BOLD}▶${NC} $1\n"
  100. }
  101. #############################################################################
  102. # Path Handling (Cross-Platform)
  103. #############################################################################
  104. normalize_and_validate_path() {
  105. local input_path="$1"
  106. local normalized_path
  107. # Handle empty path
  108. if [ -z "$input_path" ]; then
  109. echo ""
  110. return 1
  111. fi
  112. # Expand tilde to $HOME (works on Linux, macOS, Windows Git Bash)
  113. if [[ $input_path == ~* ]]; then
  114. normalized_path="${HOME}${input_path:1}"
  115. else
  116. normalized_path="$input_path"
  117. fi
  118. # Convert backslashes to forward slashes (Windows compatibility)
  119. normalized_path="${normalized_path//\\//}"
  120. # Remove trailing slashes
  121. normalized_path="${normalized_path%/}"
  122. # If path is relative, make it absolute based on current directory
  123. if [[ ! "$normalized_path" = /* ]] && [[ ! "$normalized_path" =~ ^[A-Za-z]: ]]; then
  124. normalized_path="$(pwd)/${normalized_path}"
  125. fi
  126. echo "$normalized_path"
  127. return 0
  128. }
  129. validate_install_path() {
  130. local path="$1"
  131. local parent_dir
  132. # Get parent directory
  133. parent_dir="$(dirname "$path")"
  134. # Check if parent directory exists
  135. if [ ! -d "$parent_dir" ]; then
  136. print_error "Parent directory does not exist: $parent_dir"
  137. return 1
  138. fi
  139. # Check if parent directory is writable
  140. if [ ! -w "$parent_dir" ]; then
  141. print_error "No write permission for directory: $parent_dir"
  142. return 1
  143. fi
  144. # If target directory exists, check if it's writable
  145. if [ -d "$path" ] && [ ! -w "$path" ]; then
  146. print_error "No write permission for directory: $path"
  147. return 1
  148. fi
  149. return 0
  150. }
  151. get_global_install_path() {
  152. # Return platform-appropriate global installation path
  153. case "$PLATFORM" in
  154. macOS)
  155. # macOS: Use XDG standard (consistent with Linux)
  156. echo "${HOME}/.config/opencode"
  157. ;;
  158. Linux)
  159. echo "${HOME}/.config/opencode"
  160. ;;
  161. Windows)
  162. # Windows Git Bash/WSL: Use same as Linux
  163. echo "${HOME}/.config/opencode"
  164. ;;
  165. *)
  166. echo "${HOME}/.config/opencode"
  167. ;;
  168. esac
  169. }
  170. #############################################################################
  171. # Dependency Checks
  172. #############################################################################
  173. check_bash_version() {
  174. # Check bash version (need 3.2+)
  175. local bash_version="${BASH_VERSION%%.*}"
  176. if [ "$bash_version" -lt 3 ]; then
  177. echo "Error: This script requires Bash 3.2 or higher"
  178. echo "Current version: $BASH_VERSION"
  179. echo ""
  180. echo "Please upgrade bash or use a different shell:"
  181. echo " macOS: brew install bash"
  182. echo " Linux: Use your package manager to update bash"
  183. echo " Windows: Use Git Bash or WSL"
  184. exit 1
  185. fi
  186. }
  187. check_dependencies() {
  188. print_step "Checking dependencies..."
  189. local missing_deps=()
  190. if ! command -v curl &> /dev/null; then
  191. missing_deps+=("curl")
  192. fi
  193. if ! command -v jq &> /dev/null; then
  194. missing_deps+=("jq")
  195. fi
  196. if [ ${#missing_deps[@]} -ne 0 ]; then
  197. print_error "Missing required dependencies: ${missing_deps[*]}"
  198. echo ""
  199. echo "Please install them:"
  200. case "$PLATFORM" in
  201. macOS)
  202. echo " brew install ${missing_deps[*]}"
  203. ;;
  204. Linux)
  205. echo " Ubuntu/Debian: sudo apt-get install ${missing_deps[*]}"
  206. echo " Fedora/RHEL: sudo dnf install ${missing_deps[*]}"
  207. echo " Arch: sudo pacman -S ${missing_deps[*]}"
  208. ;;
  209. Windows)
  210. echo " Git Bash: Install via https://git-scm.com/"
  211. echo " WSL: sudo apt-get install ${missing_deps[*]}"
  212. echo " Scoop: scoop install ${missing_deps[*]}"
  213. ;;
  214. *)
  215. echo " Use your package manager to install: ${missing_deps[*]}"
  216. ;;
  217. esac
  218. exit 1
  219. fi
  220. print_success "All dependencies found"
  221. }
  222. #############################################################################
  223. # Registry Functions
  224. #############################################################################
  225. fetch_registry() {
  226. print_step "Fetching component registry..."
  227. mkdir -p "$TEMP_DIR"
  228. # Handle local file:// URLs
  229. if [[ "$REGISTRY_URL" == file://* ]]; then
  230. local local_path="${REGISTRY_URL#file://}"
  231. if [ -f "$local_path" ]; then
  232. cp "$local_path" "$TEMP_DIR/registry.json"
  233. print_success "Using local registry: $local_path"
  234. else
  235. print_error "Local registry not found: $local_path"
  236. exit 1
  237. fi
  238. else
  239. # Fetch from remote URL
  240. if ! curl -fsSL "$REGISTRY_URL" -o "$TEMP_DIR/registry.json"; then
  241. print_error "Failed to fetch registry from $REGISTRY_URL"
  242. exit 1
  243. fi
  244. print_success "Registry fetched successfully"
  245. fi
  246. }
  247. get_profile_components() {
  248. local profile=$1
  249. jq_exec ".profiles.${profile}.components[]?" "$TEMP_DIR/registry.json"
  250. }
  251. get_component_info() {
  252. local component_id=$1
  253. local component_type=$2
  254. if [ "$component_type" = "context" ] && [[ "$component_id" == */* ]]; then
  255. jq_exec "first(.components.contexts[]? | select(.path == \".opencode/context/${component_id}.md\"))" "$TEMP_DIR/registry.json"
  256. return
  257. fi
  258. jq_exec ".components.${component_type}[]? | select(.id == \"${component_id}\" or (.aliases // [] | index(\"${component_id}\")))" "$TEMP_DIR/registry.json"
  259. }
  260. resolve_component_path() {
  261. local component_type=$1
  262. local component_id=$2
  263. local registry_key
  264. registry_key=$(get_registry_key "$component_type")
  265. if [ "$component_type" = "context" ] && [[ "$component_id" == */* ]]; then
  266. jq_exec "first(.components.contexts[]? | select(.path == \".opencode/context/${component_id}.md\") | .path)" "$TEMP_DIR/registry.json"
  267. return
  268. fi
  269. jq_exec ".components.${registry_key}[]? | select(.id == \"${component_id}\" or (.aliases // [] | index(\"${component_id}\"))) | .path" "$TEMP_DIR/registry.json"
  270. }
  271. # Helper function to get the correct registry key for a component type
  272. get_registry_key() {
  273. local type=$1
  274. # Most types are pluralized, but 'config' stays singular
  275. case "$type" in
  276. config) echo "config" ;;
  277. *) echo "${type}s" ;;
  278. esac
  279. }
  280. # Helper function to convert registry path to installation path
  281. # Registry paths are like ".opencode/agent/foo.md"
  282. # We need to replace ".opencode" with the actual INSTALL_DIR
  283. get_install_path() {
  284. local registry_path=$1
  285. # Strip leading .opencode/ if present
  286. local relative_path="${registry_path#.opencode/}"
  287. # Return INSTALL_DIR + relative path
  288. echo "${INSTALL_DIR}/${relative_path}"
  289. }
  290. expand_context_wildcard() {
  291. local pattern=$1
  292. local prefix="${pattern%%\**}"
  293. prefix="${prefix%/}"
  294. if [ -n "$prefix" ]; then
  295. prefix="${prefix}/"
  296. fi
  297. jq_exec ".components.contexts[]? | select(.path | startswith(\".opencode/context/${prefix}\")) | .path | sub(\"^\\\\.opencode/context/\"; \"\") | sub(\"\\\\.md$\"; \"\")" "$TEMP_DIR/registry.json"
  298. }
  299. expand_selected_components() {
  300. local expanded=()
  301. for comp in "${SELECTED_COMPONENTS[@]}"; do
  302. local type="${comp%%:*}"
  303. local id="${comp##*:}"
  304. if [[ "$id" == *"*"* ]]; then
  305. if [ "$type" != "context" ]; then
  306. print_warning "Wildcard only supported for context components: ${comp}"
  307. continue
  308. fi
  309. local matches
  310. matches=$(expand_context_wildcard "$id")
  311. if [ -z "$matches" ]; then
  312. print_warning "No contexts matched: ${comp}"
  313. continue
  314. fi
  315. while IFS= read -r match; do
  316. [ -n "$match" ] && expanded+=("context:${match}")
  317. done <<< "$matches"
  318. continue
  319. fi
  320. expanded+=("$comp")
  321. done
  322. local deduped=()
  323. for comp in "${expanded[@]}"; do
  324. local found=0
  325. for existing in "${deduped[@]}"; do
  326. if [ "$existing" = "$comp" ]; then
  327. found=1
  328. break
  329. fi
  330. done
  331. if [ "$found" -eq 0 ]; then
  332. deduped+=("$comp")
  333. fi
  334. done
  335. SELECTED_COMPONENTS=("${deduped[@]}")
  336. }
  337. resolve_dependencies() {
  338. local component=$1
  339. local type="${component%%:*}"
  340. local id="${component##*:}"
  341. # Get the correct registry key (handles singular/plural)
  342. local registry_key
  343. registry_key=$(get_registry_key "$type")
  344. # Get dependencies for this component
  345. local deps
  346. deps=$(jq_exec ".components.${registry_key}[] | select(.id == \"${id}\" or (.aliases // [] | index(\"${id}\"))) | .dependencies[]?" "$TEMP_DIR/registry.json" 2>/dev/null || echo "")
  347. if [ -n "$deps" ]; then
  348. for dep in $deps; do
  349. if [[ "$dep" == *"*"* ]]; then
  350. local dep_type="${dep%%:*}"
  351. local dep_id="${dep##*:}"
  352. if [ "$dep_type" = "context" ]; then
  353. local matched
  354. matched=$(expand_context_wildcard "$dep_id")
  355. if [ -z "$matched" ]; then
  356. print_warning "No contexts matched dependency: ${dep}"
  357. continue
  358. fi
  359. while IFS= read -r match; do
  360. local expanded_dep="context:${match}"
  361. local found=0
  362. for existing in "${SELECTED_COMPONENTS[@]}"; do
  363. if [ "$existing" = "$expanded_dep" ]; then
  364. found=1
  365. break
  366. fi
  367. done
  368. if [ "$found" -eq 0 ]; then
  369. SELECTED_COMPONENTS+=("$expanded_dep")
  370. resolve_dependencies "$expanded_dep"
  371. fi
  372. done <<< "$matched"
  373. continue
  374. fi
  375. fi
  376. # Add dependency if not already in list
  377. local found=0
  378. for existing in "${SELECTED_COMPONENTS[@]}"; do
  379. if [ "$existing" = "$dep" ]; then
  380. found=1
  381. break
  382. fi
  383. done
  384. if [ "$found" -eq 0 ]; then
  385. SELECTED_COMPONENTS+=("$dep")
  386. # Recursively resolve dependencies
  387. resolve_dependencies "$dep"
  388. fi
  389. done
  390. fi
  391. }
  392. #############################################################################
  393. # Installation Mode Selection
  394. #############################################################################
  395. check_interactive_mode() {
  396. # Check if stdin is a terminal (not piped from curl)
  397. if [ ! -t 0 ]; then
  398. print_header
  399. print_error "Interactive mode requires a terminal"
  400. echo ""
  401. echo "You're running this script in a pipe (e.g., curl | bash)"
  402. echo "For interactive mode, download the script first:"
  403. echo ""
  404. echo -e "${CYAN}# Download the script${NC}"
  405. echo "curl -fsSL https://raw.githubusercontent.com/darrenhinde/OpenAgentsControl/main/install.sh -o install.sh"
  406. echo ""
  407. echo -e "${CYAN}# Run interactively${NC}"
  408. echo "bash install.sh"
  409. echo ""
  410. echo "Or use a profile directly:"
  411. echo ""
  412. echo -e "${CYAN}# Quick install with profile${NC}"
  413. echo "curl -fsSL https://raw.githubusercontent.com/darrenhinde/OpenAgentsControl/main/install.sh | bash -s essential"
  414. echo ""
  415. echo "Available profiles: essential, developer, business, full, advanced"
  416. echo ""
  417. cleanup_and_exit 1
  418. fi
  419. }
  420. show_install_location_menu() {
  421. check_interactive_mode
  422. clear
  423. print_header
  424. local global_path
  425. global_path=$(get_global_install_path)
  426. echo -e "${BOLD}Choose installation location:${NC}\n"
  427. echo -e " ${GREEN}1) Local${NC} - Install to ${CYAN}.opencode/${NC} in current directory"
  428. echo " (Best for project-specific agents)"
  429. echo ""
  430. echo -e " ${BLUE}2) Global${NC} - Install to ${CYAN}${global_path}${NC}"
  431. echo " (Best for user-wide agents available everywhere)"
  432. echo ""
  433. echo -e " ${MAGENTA}3) Custom${NC} - Enter exact path"
  434. echo " Examples:"
  435. case "$PLATFORM" in
  436. Windows)
  437. echo -e " ${CYAN}C:/Users/username/my-agents${NC} or ${CYAN}~/my-agents${NC}"
  438. ;;
  439. *)
  440. echo -e " ${CYAN}/home/username/my-agents${NC} or ${CYAN}~/my-agents${NC}"
  441. ;;
  442. esac
  443. echo ""
  444. echo " 4) Back / Exit"
  445. echo ""
  446. read -r -p "Enter your choice [1-4]: " location_choice
  447. case $location_choice in
  448. 1)
  449. INSTALL_DIR=".opencode"
  450. print_success "Installing to local directory: .opencode/"
  451. sleep 1
  452. ;;
  453. 2)
  454. INSTALL_DIR="$global_path"
  455. print_success "Installing to global directory: $global_path"
  456. sleep 1
  457. ;;
  458. 3)
  459. echo ""
  460. read -r -p "Enter installation path: " custom_path
  461. if [ -z "$custom_path" ]; then
  462. print_error "No path entered"
  463. sleep 2
  464. show_install_location_menu
  465. return
  466. fi
  467. local normalized_path
  468. normalized_path=$(normalize_and_validate_path "$custom_path")
  469. if ! normalize_and_validate_path "$custom_path" > /dev/null; then
  470. print_error "Invalid path"
  471. sleep 2
  472. show_install_location_menu
  473. return
  474. fi
  475. if ! validate_install_path "$normalized_path"; then
  476. echo ""
  477. read -r -p "Continue anyway? [y/N]: " continue_choice
  478. if [[ ! $continue_choice =~ ^[Yy] ]]; then
  479. show_install_location_menu
  480. return
  481. fi
  482. fi
  483. INSTALL_DIR="$normalized_path"
  484. print_success "Installing to custom directory: $INSTALL_DIR"
  485. sleep 1
  486. ;;
  487. 4)
  488. cleanup_and_exit 0
  489. ;;
  490. *)
  491. print_error "Invalid choice"
  492. sleep 2
  493. show_install_location_menu
  494. return
  495. ;;
  496. esac
  497. }
  498. show_main_menu() {
  499. check_interactive_mode
  500. clear
  501. print_header
  502. echo -e "${BOLD}Choose installation mode:${NC}\n"
  503. echo " 1) Quick Install (Choose a profile)"
  504. echo " 2) Custom Install (Pick individual components)"
  505. echo " 3) List Available Components"
  506. echo " 4) Exit"
  507. echo ""
  508. read -r -p "Enter your choice [1-4]: " choice
  509. case $choice in
  510. 1) INSTALL_MODE="profile" ;;
  511. 2) INSTALL_MODE="custom" ;;
  512. 3) list_components; read -r -p "Press Enter to continue..."; show_main_menu ;;
  513. 4) cleanup_and_exit 0 ;;
  514. *) print_error "Invalid choice"; sleep 2; show_main_menu ;;
  515. esac
  516. }
  517. #############################################################################
  518. # Profile Installation
  519. #############################################################################
  520. show_profile_menu() {
  521. clear
  522. print_header
  523. echo -e "${BOLD}Available Installation Profiles:${NC}\n"
  524. # Essential profile
  525. local essential_name
  526. essential_name=$(jq_exec '.profiles.essential.name' "$TEMP_DIR/registry.json")
  527. local essential_desc
  528. essential_desc=$(jq_exec '.profiles.essential.description' "$TEMP_DIR/registry.json")
  529. local essential_count
  530. essential_count=$(jq_exec '.profiles.essential.components | length' "$TEMP_DIR/registry.json")
  531. echo -e " ${GREEN}1) ${essential_name}${NC}"
  532. echo -e " ${essential_desc}"
  533. echo -e " Components: ${essential_count}\n"
  534. # Developer profile
  535. local dev_desc
  536. dev_desc=$(jq_exec '.profiles.developer.description' "$TEMP_DIR/registry.json")
  537. local dev_count
  538. dev_count=$(jq_exec '.profiles.developer.components | length' "$TEMP_DIR/registry.json")
  539. local dev_badge
  540. dev_badge=$(jq_exec '.profiles.developer.badge // ""' "$TEMP_DIR/registry.json")
  541. if [ -n "$dev_badge" ]; then
  542. echo -e " ${BLUE}2) Developer ${GREEN}[${dev_badge}]${NC}"
  543. else
  544. echo -e " ${BLUE}2) Developer${NC}"
  545. fi
  546. echo -e " ${dev_desc}"
  547. echo -e " Components: ${dev_count}\n"
  548. # Business profile
  549. local business_name
  550. business_name=$(jq_exec '.profiles.business.name' "$TEMP_DIR/registry.json")
  551. local business_desc
  552. business_desc=$(jq_exec '.profiles.business.description' "$TEMP_DIR/registry.json")
  553. local business_count
  554. business_count=$(jq_exec '.profiles.business.components | length' "$TEMP_DIR/registry.json")
  555. echo -e " ${CYAN}3) ${business_name}${NC}"
  556. echo -e " ${business_desc}"
  557. echo -e " Components: ${business_count}\n"
  558. # Full profile
  559. local full_name
  560. full_name=$(jq_exec '.profiles.full.name' "$TEMP_DIR/registry.json")
  561. local full_desc
  562. full_desc=$(jq_exec '.profiles.full.description' "$TEMP_DIR/registry.json")
  563. local full_count
  564. full_count=$(jq_exec '.profiles.full.components | length' "$TEMP_DIR/registry.json")
  565. echo -e " ${MAGENTA}4) ${full_name}${NC}"
  566. echo -e " ${full_desc}"
  567. echo -e " Components: ${full_count}\n"
  568. # Advanced profile
  569. local adv_name
  570. adv_name=$(jq_exec '.profiles.advanced.name' "$TEMP_DIR/registry.json")
  571. local adv_desc
  572. adv_desc=$(jq_exec '.profiles.advanced.description' "$TEMP_DIR/registry.json")
  573. local adv_count
  574. adv_count=$(jq_exec '.profiles.advanced.components | length' "$TEMP_DIR/registry.json")
  575. echo -e " ${YELLOW}5) ${adv_name}${NC}"
  576. echo -e " ${adv_desc}"
  577. echo -e " Components: ${adv_count}\n"
  578. echo " 6) Back to main menu"
  579. echo ""
  580. read -r -p "Enter your choice [1-6]: " choice
  581. case $choice in
  582. 1) PROFILE="essential" ;;
  583. 2) PROFILE="developer" ;;
  584. 3) PROFILE="business" ;;
  585. 4) PROFILE="full" ;;
  586. 5) PROFILE="advanced" ;;
  587. 6) show_main_menu; return ;;
  588. *) print_error "Invalid choice"; sleep 2; show_profile_menu; return ;;
  589. esac
  590. # Load profile components (compatible with bash 3.2+)
  591. SELECTED_COMPONENTS=()
  592. local temp_file="$TEMP_DIR/components.tmp"
  593. get_profile_components "$PROFILE" > "$temp_file"
  594. while IFS= read -r component; do
  595. [ -n "$component" ] && SELECTED_COMPONENTS+=("$component")
  596. done < "$temp_file"
  597. expand_selected_components
  598. # Resolve dependencies for profile installs
  599. print_step "Resolving dependencies..."
  600. local original_count=${#SELECTED_COMPONENTS[@]}
  601. for comp in "${SELECTED_COMPONENTS[@]}"; do
  602. resolve_dependencies "$comp"
  603. done
  604. local new_count=${#SELECTED_COMPONENTS[@]}
  605. if [ "$new_count" -gt "$original_count" ]; then
  606. local added=$((new_count - original_count))
  607. print_info "Added $added dependencies"
  608. fi
  609. show_installation_preview
  610. }
  611. #############################################################################
  612. # Custom Component Selection
  613. #############################################################################
  614. show_custom_menu() {
  615. clear
  616. print_header
  617. echo -e "${BOLD}Select component categories to install:${NC}\n"
  618. echo "Use space to toggle, Enter to continue"
  619. echo ""
  620. local categories=("agents" "subagents" "commands" "tools" "plugins" "skills" "contexts" "config")
  621. local selected_categories=()
  622. # Simple selection (for now, we'll make it interactive later)
  623. echo "Available categories:"
  624. for i in "${!categories[@]}"; do
  625. local cat="${categories[$i]}"
  626. local count
  627. count=$(jq_exec ".components.${cat} | length" "$TEMP_DIR/registry.json")
  628. local cat_display
  629. cat_display=$(echo "$cat" | awk '{print toupper(substr($0,1,1)) tolower(substr($0,2))}')
  630. echo " $((i+1))) ${cat_display} (${count} available)"
  631. done
  632. echo " $((${#categories[@]}+1))) Select All"
  633. echo " $((${#categories[@]}+2))) Continue to component selection"
  634. echo " $((${#categories[@]}+3))) Back to main menu"
  635. echo ""
  636. read -r -p "Enter category numbers (space-separated) or option: " -a selections
  637. for sel in "${selections[@]}"; do
  638. if [ "$sel" -eq $((${#categories[@]}+1)) ]; then
  639. selected_categories=("${categories[@]}")
  640. break
  641. elif [ "$sel" -eq $((${#categories[@]}+2)) ]; then
  642. break
  643. elif [ "$sel" -eq $((${#categories[@]}+3)) ]; then
  644. show_main_menu
  645. return
  646. elif [ "$sel" -ge 1 ] && [ "$sel" -le ${#categories[@]} ]; then
  647. selected_categories+=("${categories[$((sel-1))]}")
  648. fi
  649. done
  650. if [ ${#selected_categories[@]} -eq 0 ]; then
  651. print_warning "No categories selected"
  652. sleep 2
  653. show_custom_menu
  654. return
  655. fi
  656. show_component_selection "${selected_categories[@]}"
  657. }
  658. show_component_selection() {
  659. local categories=("$@")
  660. clear
  661. print_header
  662. echo -e "${BOLD}Select components to install:${NC}\n"
  663. local all_components=()
  664. local component_details=()
  665. for category in "${categories[@]}"; do
  666. local cat_display
  667. cat_display=$(echo "$category" | awk '{print toupper(substr($0,1,1)) tolower(substr($0,2))}')
  668. echo -e "${CYAN}${BOLD}${cat_display}:${NC}"
  669. local components
  670. components=$(jq_exec ".components.${category}[]? | .id" "$TEMP_DIR/registry.json")
  671. local idx=1
  672. while IFS= read -r comp_id; do
  673. local comp_name
  674. comp_name=$(jq_exec ".components.${category}[]? | select(.id == \"${comp_id}\") | .name" "$TEMP_DIR/registry.json")
  675. local comp_desc
  676. comp_desc=$(jq_exec ".components.${category}[]? | select(.id == \"${comp_id}\") | .description" "$TEMP_DIR/registry.json")
  677. echo " ${idx}) ${comp_name}"
  678. echo " ${comp_desc}"
  679. all_components+=("${category}:${comp_id}")
  680. component_details+=("${comp_name}|${comp_desc}")
  681. idx=$((idx+1))
  682. done <<< "$components"
  683. echo ""
  684. done
  685. echo "Enter component numbers (space-separated), 'all' for all, or 'done' to continue:"
  686. read -r -a selections
  687. for sel in "${selections[@]}"; do
  688. if [ "$sel" = "all" ]; then
  689. SELECTED_COMPONENTS=("${all_components[@]}")
  690. break
  691. elif [ "$sel" = "done" ]; then
  692. break
  693. elif [ "$sel" -ge 1 ] && [ "$sel" -le ${#all_components[@]} ]; then
  694. SELECTED_COMPONENTS+=("${all_components[$((sel-1))]}")
  695. fi
  696. done
  697. if [ ${#SELECTED_COMPONENTS[@]} -eq 0 ]; then
  698. print_warning "No components selected"
  699. sleep 2
  700. show_custom_menu
  701. return
  702. fi
  703. # Resolve dependencies
  704. print_step "Resolving dependencies..."
  705. local original_count=${#SELECTED_COMPONENTS[@]}
  706. for comp in "${SELECTED_COMPONENTS[@]}"; do
  707. resolve_dependencies "$comp"
  708. done
  709. if [ ${#SELECTED_COMPONENTS[@]} -gt "$original_count" ]; then
  710. print_info "Added $((${#SELECTED_COMPONENTS[@]} - original_count)) dependencies"
  711. fi
  712. show_installation_preview
  713. }
  714. #############################################################################
  715. # Installation Preview & Confirmation
  716. #############################################################################
  717. show_installation_preview() {
  718. # Only clear screen in interactive mode
  719. if [ "$NON_INTERACTIVE" != true ]; then
  720. clear
  721. fi
  722. print_header
  723. echo -e "${BOLD}Installation Preview${NC}\n"
  724. if [ -n "$PROFILE" ]; then
  725. echo -e "Profile: ${GREEN}${PROFILE}${NC}"
  726. else
  727. echo -e "Mode: ${GREEN}Custom${NC}"
  728. fi
  729. echo -e "Installation directory: ${CYAN}${INSTALL_DIR}${NC}"
  730. echo -e "\nComponents to install (${#SELECTED_COMPONENTS[@]} total):\n"
  731. # Group by type
  732. local agents=()
  733. local subagents=()
  734. local commands=()
  735. local tools=()
  736. local plugins=()
  737. local skills=()
  738. local contexts=()
  739. local configs=()
  740. for comp in "${SELECTED_COMPONENTS[@]}"; do
  741. local type="${comp%%:*}"
  742. case $type in
  743. agent) agents+=("$comp") ;;
  744. subagent) subagents+=("$comp") ;;
  745. command) commands+=("$comp") ;;
  746. tool) tools+=("$comp") ;;
  747. plugin) plugins+=("$comp") ;;
  748. skill) skills+=("$comp") ;;
  749. context) contexts+=("$comp") ;;
  750. config) configs+=("$comp") ;;
  751. esac
  752. done
  753. [ ${#agents[@]} -gt 0 ] && echo -e "${CYAN}Agents (${#agents[@]}):${NC} ${agents[*]##*:}"
  754. [ ${#subagents[@]} -gt 0 ] && echo -e "${CYAN}Subagents (${#subagents[@]}):${NC} ${subagents[*]##*:}"
  755. [ ${#commands[@]} -gt 0 ] && echo -e "${CYAN}Commands (${#commands[@]}):${NC} ${commands[*]##*:}"
  756. [ ${#tools[@]} -gt 0 ] && echo -e "${CYAN}Tools (${#tools[@]}):${NC} ${tools[*]##*:}"
  757. [ ${#plugins[@]} -gt 0 ] && echo -e "${CYAN}Plugins (${#plugins[@]}):${NC} ${plugins[*]##*:}"
  758. [ ${#skills[@]} -gt 0 ] && echo -e "${CYAN}Skills (${#skills[@]}):${NC} ${skills[*]##*:}"
  759. [ ${#contexts[@]} -gt 0 ] && echo -e "${CYAN}Contexts (${#contexts[@]}):${NC} ${contexts[*]##*:}"
  760. [ ${#configs[@]} -gt 0 ] && echo -e "${CYAN}Config (${#configs[@]}):${NC} ${configs[*]##*:}"
  761. echo ""
  762. # Skip confirmation if profile was provided via command line
  763. if [ "$NON_INTERACTIVE" = true ]; then
  764. print_info "Installing automatically (profile specified)..."
  765. perform_installation
  766. else
  767. read -r -p "Proceed with installation? [Y/n]: " confirm
  768. if [[ $confirm =~ ^[Nn] ]]; then
  769. print_info "Installation cancelled"
  770. cleanup_and_exit 0
  771. fi
  772. perform_installation
  773. fi
  774. }
  775. #############################################################################
  776. # Collision Detection
  777. #############################################################################
  778. show_collision_report() {
  779. local collision_count=$1
  780. shift
  781. local collisions=("$@")
  782. echo ""
  783. print_warning "Found ${collision_count} file collision(s):"
  784. echo ""
  785. # Group by type
  786. local agents=()
  787. local subagents=()
  788. local commands=()
  789. local tools=()
  790. local plugins=()
  791. local skills=()
  792. local contexts=()
  793. local configs=()
  794. for file in "${collisions[@]}"; do
  795. # Skip empty entries
  796. [ -z "$file" ] && continue
  797. if [[ $file == *"/agent/subagents/"* ]]; then
  798. subagents+=("$file")
  799. elif [[ $file == *"/agent/"* ]]; then
  800. agents+=("$file")
  801. elif [[ $file == *"/command/"* ]]; then
  802. commands+=("$file")
  803. elif [[ $file == *"/tool/"* ]]; then
  804. tools+=("$file")
  805. elif [[ $file == *"/plugin/"* ]]; then
  806. plugins+=("$file")
  807. elif [[ $file == *"/skills/"* ]]; then
  808. skills+=("$file")
  809. elif [[ $file == *"/context/"* ]]; then
  810. contexts+=("$file")
  811. else
  812. configs+=("$file")
  813. fi
  814. done
  815. # Display grouped collisions
  816. [ ${#agents[@]} -gt 0 ] && echo -e "${YELLOW} Agents (${#agents[@]}):${NC}" && printf ' %s\n' "${agents[@]}"
  817. [ ${#subagents[@]} -gt 0 ] && echo -e "${YELLOW} Subagents (${#subagents[@]}):${NC}" && printf ' %s\n' "${subagents[@]}"
  818. [ ${#commands[@]} -gt 0 ] && echo -e "${YELLOW} Commands (${#commands[@]}):${NC}" && printf ' %s\n' "${commands[@]}"
  819. [ ${#tools[@]} -gt 0 ] && echo -e "${YELLOW} Tools (${#tools[@]}):${NC}" && printf ' %s\n' "${tools[@]}"
  820. [ ${#plugins[@]} -gt 0 ] && echo -e "${YELLOW} Plugins (${#plugins[@]}):${NC}" && printf ' %s\n' "${plugins[@]}"
  821. [ ${#skills[@]} -gt 0 ] && echo -e "${YELLOW} Skills (${#skills[@]}):${NC}" && printf ' %s\n' "${skills[@]}"
  822. [ ${#contexts[@]} -gt 0 ] && echo -e "${YELLOW} Context (${#contexts[@]}):${NC}" && printf ' %s\n' "${contexts[@]}"
  823. [ ${#configs[@]} -gt 0 ] && echo -e "${YELLOW} Config (${#configs[@]}):${NC}" && printf ' %s\n' "${configs[@]}"
  824. echo ""
  825. }
  826. get_install_strategy() {
  827. echo -e "${BOLD}How would you like to proceed?${NC}\n" >&2
  828. echo " 1) ${GREEN}Skip existing${NC} - Only install new files, keep all existing files unchanged" >&2
  829. echo " 2) ${YELLOW}Overwrite all${NC} - Replace existing files with new versions (your changes will be lost)" >&2
  830. echo " 3) ${CYAN}Backup & overwrite${NC} - Backup existing files, then install new versions" >&2
  831. echo " 4) ${RED}Cancel${NC} - Exit without making changes" >&2
  832. echo "" >&2
  833. read -r -p "Enter your choice [1-4]: " strategy_choice
  834. case $strategy_choice in
  835. 1) echo "skip" ;;
  836. 2)
  837. echo "" >&2
  838. print_warning "This will overwrite existing files. Your changes will be lost!"
  839. read -r -p "Are you sure? Type 'yes' to confirm: " confirm
  840. if [ "$confirm" = "yes" ]; then
  841. echo "overwrite"
  842. else
  843. echo "cancel"
  844. fi
  845. ;;
  846. 3) echo "backup" ;;
  847. 4) echo "cancel" ;;
  848. *) echo "cancel" ;;
  849. esac
  850. }
  851. #############################################################################
  852. # Installation
  853. #############################################################################
  854. perform_installation() {
  855. print_step "Preparing installation..."
  856. # Create base directory only - subdirectories created on-demand when files are installed
  857. mkdir -p "$INSTALL_DIR"
  858. # Check for collisions
  859. local collisions=()
  860. for comp in "${SELECTED_COMPONENTS[@]}"; do
  861. local type="${comp%%:*}"
  862. local id="${comp##*:}"
  863. local registry_key
  864. registry_key=$(get_registry_key "$type")
  865. local path
  866. path=$(resolve_component_path "$type" "$id")
  867. if [ -n "$path" ] && [ "$path" != "null" ]; then
  868. local install_path
  869. install_path=$(get_install_path "$path")
  870. if [ -f "$install_path" ]; then
  871. collisions+=("$install_path")
  872. fi
  873. fi
  874. done
  875. # Determine installation strategy
  876. local install_strategy="fresh"
  877. if [ ${#collisions[@]} -gt 0 ]; then
  878. # In non-interactive mode, use default strategy (skip existing files)
  879. if [ "$NON_INTERACTIVE" = true ]; then
  880. print_info "Found ${#collisions[@]} existing file(s) - using 'skip' strategy (non-interactive mode)"
  881. print_info "To overwrite, download script and run interactively, or delete existing files first"
  882. install_strategy="skip"
  883. else
  884. show_collision_report ${#collisions[@]} "${collisions[@]}"
  885. install_strategy=$(get_install_strategy)
  886. if [ "$install_strategy" = "cancel" ]; then
  887. print_info "Installation cancelled by user"
  888. cleanup_and_exit 0
  889. fi
  890. fi
  891. # Handle backup strategy
  892. if [ "$install_strategy" = "backup" ]; then
  893. local backup_dir
  894. backup_dir="${INSTALL_DIR}.backup.$(date +%Y%m%d-%H%M%S)"
  895. print_step "Creating backup..."
  896. # Only backup files that will be overwritten
  897. local backup_count=0
  898. for file in "${collisions[@]}"; do
  899. if [ -f "$file" ]; then
  900. local backup_file="${backup_dir}/${file}"
  901. mkdir -p "$(dirname "$backup_file")"
  902. if cp "$file" "$backup_file" 2>/dev/null; then
  903. backup_count=$((backup_count + 1))
  904. else
  905. print_warning "Failed to backup: $file"
  906. fi
  907. fi
  908. done
  909. if [ $backup_count -gt 0 ]; then
  910. print_success "Backed up ${backup_count} file(s) to $backup_dir"
  911. install_strategy="overwrite" # Now we can overwrite
  912. else
  913. print_error "Backup failed. Installation cancelled."
  914. cleanup_and_exit 1
  915. fi
  916. fi
  917. fi
  918. # Perform installation
  919. print_step "Installing components..."
  920. local installed=0
  921. local skipped=0
  922. local failed=0
  923. for comp in "${SELECTED_COMPONENTS[@]}"; do
  924. local type="${comp%%:*}"
  925. local id="${comp##*:}"
  926. # Get the correct registry key (handles singular/plural)
  927. local registry_key
  928. registry_key=$(get_registry_key "$type")
  929. # Get component path
  930. local path
  931. path=$(resolve_component_path "$type" "$id")
  932. if [ -z "$path" ] || [ "$path" = "null" ]; then
  933. print_warning "Could not find path for ${comp}"
  934. failed=$((failed + 1))
  935. continue
  936. fi
  937. # Check if component has additional files (for skills)
  938. local files_array
  939. files_array=$(jq_exec ".components.${registry_key}[]? | select(.id == \"${id}\") | .files[]?" "$TEMP_DIR/registry.json")
  940. if [ -n "$files_array" ]; then
  941. # Component has multiple files - download all of them
  942. local component_installed=0
  943. local component_failed=0
  944. while IFS= read -r file_path; do
  945. [ -z "$file_path" ] && continue
  946. local dest
  947. dest=$(get_install_path "$file_path")
  948. # Check if file exists and we're in skip mode
  949. if [ -f "$dest" ] && [ "$install_strategy" = "skip" ]; then
  950. continue
  951. fi
  952. # Download file
  953. local url="${RAW_URL}/${file_path}"
  954. mkdir -p "$(dirname "$dest")"
  955. if curl -fsSL "$url" -o "$dest"; then
  956. # Transform paths for global installation
  957. if [[ "$INSTALL_DIR" != ".opencode" ]] && [[ "$INSTALL_DIR" != *"/.opencode" ]]; then
  958. local expanded_path="${INSTALL_DIR/#\~/$HOME}"
  959. sed -i.bak -e "s|@\.opencode/context/|@${expanded_path}/context/|g" \
  960. -e "s|\.opencode/context|${expanded_path}/context|g" "$dest" 2>/dev/null || true
  961. rm -f "${dest}.bak" 2>/dev/null || true
  962. fi
  963. component_installed=$((component_installed + 1))
  964. else
  965. component_failed=$((component_failed + 1))
  966. fi
  967. done <<< "$files_array"
  968. if [ $component_failed -eq 0 ]; then
  969. print_success "Installed ${type}: ${id} (${component_installed} files)"
  970. installed=$((installed + 1))
  971. else
  972. print_error "Failed to install ${type}: ${id} (${component_failed} files failed)"
  973. failed=$((failed + 1))
  974. fi
  975. else
  976. # Single file component - original logic
  977. local dest
  978. dest=$(get_install_path "$path")
  979. # Check if file exists before we install (for proper messaging)
  980. local file_existed=false
  981. if [ -f "$dest" ]; then
  982. file_existed=true
  983. fi
  984. # Check if file exists and we're in skip mode
  985. if [ "$file_existed" = true ] && [ "$install_strategy" = "skip" ]; then
  986. print_info "Skipped existing: ${type}:${id}"
  987. skipped=$((skipped + 1))
  988. continue
  989. fi
  990. # Download component
  991. local url="${RAW_URL}/${path}"
  992. # Create parent directory if needed
  993. mkdir -p "$(dirname "$dest")"
  994. if curl -fsSL "$url" -o "$dest"; then
  995. # Transform paths for global installation (any non-local path)
  996. # Local paths: .opencode or */.opencode
  997. if [[ "$INSTALL_DIR" != ".opencode" ]] && [[ "$INSTALL_DIR" != *"/.opencode" ]]; then
  998. # Expand tilde and get absolute path for transformation
  999. local expanded_path="${INSTALL_DIR/#\~/$HOME}"
  1000. # Transform @.opencode/context/ references to actual install path
  1001. sed -i.bak -e "s|@\.opencode/context/|@${expanded_path}/context/|g" \
  1002. -e "s|\.opencode/context|${expanded_path}/context|g" "$dest" 2>/dev/null || true
  1003. rm -f "${dest}.bak" 2>/dev/null || true
  1004. fi
  1005. # Show appropriate message based on whether file existed before
  1006. if [ "$file_existed" = true ]; then
  1007. print_success "Updated ${type}: ${id}"
  1008. else
  1009. print_success "Installed ${type}: ${id}"
  1010. fi
  1011. installed=$((installed + 1))
  1012. else
  1013. print_error "Failed to install ${type}: ${id}"
  1014. failed=$((failed + 1))
  1015. fi
  1016. fi
  1017. done
  1018. # Handle additional paths for advanced profile
  1019. if [ "$PROFILE" = "advanced" ]; then
  1020. local additional_paths
  1021. additional_paths=$(jq_exec '.profiles.advanced.additionalPaths[]?' "$TEMP_DIR/registry.json")
  1022. if [ -n "$additional_paths" ]; then
  1023. print_step "Installing additional paths..."
  1024. while IFS= read -r path; do
  1025. # For directories, we'd need to recursively download
  1026. # For now, just note them
  1027. print_info "Additional path: $path (manual download required)"
  1028. done <<< "$additional_paths"
  1029. fi
  1030. fi
  1031. echo ""
  1032. print_success "Installation complete!"
  1033. echo -e " Installed: ${GREEN}${installed}${NC}"
  1034. [ $skipped -gt 0 ] && echo -e " Skipped: ${CYAN}${skipped}${NC}"
  1035. [ $failed -gt 0 ] && echo -e " Failed: ${RED}${failed}${NC}"
  1036. show_post_install
  1037. }
  1038. #############################################################################
  1039. # Post-Installation
  1040. #############################################################################
  1041. show_post_install() {
  1042. echo ""
  1043. print_step "Next Steps"
  1044. echo "1. Review the installed components in ${CYAN}${INSTALL_DIR}/${NC}"
  1045. # Check if env.example was installed
  1046. if [ -f "${INSTALL_DIR}/env.example" ] || [ -f "env.example" ]; then
  1047. echo "2. Copy env.example to .env and configure:"
  1048. echo -e " ${CYAN}cp env.example .env${NC}"
  1049. echo "3. Start using OpenCode agents:"
  1050. else
  1051. echo "2. Start using OpenCode agents:"
  1052. fi
  1053. echo -e " ${CYAN}opencode${NC}"
  1054. echo ""
  1055. # Show installation location info
  1056. print_info "Installation directory: ${CYAN}${INSTALL_DIR}${NC}"
  1057. # Check for backup directories
  1058. local has_backup=0
  1059. local backup_dir
  1060. local backup_dirs=()
  1061. shopt -s nullglob
  1062. backup_dirs=("${INSTALL_DIR}.backup."*)
  1063. shopt -u nullglob
  1064. for backup_dir in "${backup_dirs[@]}"; do
  1065. if [ -d "$backup_dir" ]; then
  1066. has_backup=1
  1067. break
  1068. fi
  1069. done
  1070. if [ "$has_backup" -eq 1 ]; then
  1071. print_info "Backup created - you can restore files from ${INSTALL_DIR}.backup.* if needed"
  1072. fi
  1073. print_info "Documentation: ${REPO_URL}"
  1074. echo ""
  1075. cleanup_and_exit 0
  1076. }
  1077. #############################################################################
  1078. # Component Listing
  1079. #############################################################################
  1080. list_components() {
  1081. clear || true
  1082. print_header
  1083. echo -e "${BOLD}Available Components${NC}\n"
  1084. local categories=("agents" "subagents" "commands" "tools" "plugins" "skills" "contexts")
  1085. for category in "${categories[@]}"; do
  1086. local cat_display
  1087. cat_display=$(echo "$category" | awk '{print toupper(substr($0,1,1)) tolower(substr($0,2))}')
  1088. echo -e "${CYAN}${BOLD}${cat_display}:${NC}"
  1089. local components
  1090. components=$(jq_exec ".components.${category}[]? | \"\(.id)|\(.name)|\(.description)\"" "$TEMP_DIR/registry.json")
  1091. while IFS='|' read -r id name desc; do
  1092. echo -e " ${GREEN}${name}${NC} (${id})"
  1093. echo -e " ${desc}"
  1094. done <<< "$components"
  1095. echo ""
  1096. done
  1097. }
  1098. #############################################################################
  1099. # Cleanup
  1100. #############################################################################
  1101. cleanup_and_exit() {
  1102. rm -rf "$TEMP_DIR"
  1103. exit "$1"
  1104. }
  1105. trap 'cleanup_and_exit 1' INT TERM
  1106. #############################################################################
  1107. # Main
  1108. #############################################################################
  1109. main() {
  1110. # Parse command line arguments
  1111. while [ $# -gt 0 ]; do
  1112. case "$1" in
  1113. --install-dir=*)
  1114. CUSTOM_INSTALL_DIR="${1#*=}"
  1115. # Basic validation - check not empty
  1116. if [ -z "$CUSTOM_INSTALL_DIR" ]; then
  1117. echo "Error: --install-dir requires a non-empty path"
  1118. exit 1
  1119. fi
  1120. shift
  1121. ;;
  1122. --install-dir)
  1123. if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
  1124. CUSTOM_INSTALL_DIR="$2"
  1125. shift 2
  1126. else
  1127. echo "Error: --install-dir requires a path argument"
  1128. exit 1
  1129. fi
  1130. ;;
  1131. essential|--essential)
  1132. INSTALL_MODE="profile"
  1133. PROFILE="essential"
  1134. NON_INTERACTIVE=true
  1135. shift
  1136. ;;
  1137. developer|--developer)
  1138. INSTALL_MODE="profile"
  1139. PROFILE="developer"
  1140. NON_INTERACTIVE=true
  1141. shift
  1142. ;;
  1143. business|--business)
  1144. INSTALL_MODE="profile"
  1145. PROFILE="business"
  1146. NON_INTERACTIVE=true
  1147. shift
  1148. ;;
  1149. full|--full)
  1150. INSTALL_MODE="profile"
  1151. PROFILE="full"
  1152. NON_INTERACTIVE=true
  1153. shift
  1154. ;;
  1155. advanced|--advanced)
  1156. INSTALL_MODE="profile"
  1157. PROFILE="advanced"
  1158. NON_INTERACTIVE=true
  1159. shift
  1160. ;;
  1161. list|--list)
  1162. check_dependencies
  1163. fetch_registry
  1164. list_components
  1165. cleanup_and_exit 0
  1166. ;;
  1167. --help|-h|help)
  1168. print_header
  1169. echo "Usage: $0 [PROFILE] [OPTIONS]"
  1170. echo ""
  1171. echo -e "${BOLD}Profiles:${NC}"
  1172. echo " essential, --essential Minimal setup with core agents"
  1173. echo " developer, --developer Code-focused development tools"
  1174. echo " business, --business Content and business-focused tools"
  1175. echo " full, --full Everything except system-builder"
  1176. echo " advanced, --advanced Complete system with all components"
  1177. echo ""
  1178. echo -e "${BOLD}Options:${NC}"
  1179. echo " --install-dir PATH Custom installation directory"
  1180. echo " (default: .opencode)"
  1181. echo " list, --list List all available components"
  1182. echo " help, --help, -h Show this help message"
  1183. echo ""
  1184. echo -e "${BOLD}Environment Variables:${NC}"
  1185. echo " OPENCODE_INSTALL_DIR Installation directory"
  1186. echo " OPENCODE_BRANCH Git branch to install from (default: main)"
  1187. echo ""
  1188. echo -e "${BOLD}Examples:${NC}"
  1189. echo ""
  1190. echo -e " ${CYAN}# Interactive mode (choose location and components)${NC}"
  1191. echo " $0"
  1192. echo ""
  1193. echo -e " ${CYAN}# Quick install with default location (.opencode/)${NC}"
  1194. echo " $0 developer"
  1195. echo ""
  1196. echo -e " ${CYAN}# Install to global location (Linux/macOS)${NC}"
  1197. echo " $0 developer --install-dir ~/.config/opencode"
  1198. echo ""
  1199. echo -e " ${CYAN}# Install to global location (Windows Git Bash)${NC}"
  1200. echo " $0 developer --install-dir ~/.config/opencode"
  1201. echo ""
  1202. echo -e " ${CYAN}# Install to custom location${NC}"
  1203. echo " $0 essential --install-dir ~/my-agents"
  1204. echo ""
  1205. echo -e " ${CYAN}# Using environment variable${NC}"
  1206. echo " export OPENCODE_INSTALL_DIR=~/.config/opencode"
  1207. echo " $0 developer"
  1208. echo ""
  1209. echo -e " ${CYAN}# Install from URL (non-interactive)${NC}"
  1210. echo " curl -fsSL https://raw.githubusercontent.com/darrenhinde/OpenAgentsControl/main/install.sh | bash -s developer"
  1211. echo ""
  1212. echo -e "${BOLD}Platform Support:${NC}"
  1213. echo " ✓ Linux (bash 3.2+)"
  1214. echo " ✓ macOS (bash 3.2+)"
  1215. echo " ✓ Windows (Git Bash, WSL)"
  1216. echo ""
  1217. exit 0
  1218. ;;
  1219. *)
  1220. echo "Unknown option: $1"
  1221. echo "Run '$0 --help' for usage information"
  1222. exit 1
  1223. ;;
  1224. esac
  1225. done
  1226. # Apply custom install directory if specified (CLI arg overrides env var)
  1227. if [ -n "$CUSTOM_INSTALL_DIR" ]; then
  1228. local normalized_path
  1229. if normalize_and_validate_path "$CUSTOM_INSTALL_DIR" > /dev/null; then
  1230. normalized_path=$(normalize_and_validate_path "$CUSTOM_INSTALL_DIR")
  1231. INSTALL_DIR="$normalized_path"
  1232. if ! validate_install_path "$INSTALL_DIR"; then
  1233. print_warning "Installation path may have issues, but continuing..."
  1234. fi
  1235. else
  1236. print_error "Invalid installation directory: $CUSTOM_INSTALL_DIR"
  1237. exit 1
  1238. fi
  1239. fi
  1240. check_bash_version
  1241. check_dependencies
  1242. fetch_registry
  1243. if [ -n "$PROFILE" ]; then
  1244. # Non-interactive mode (compatible with bash 3.2+)
  1245. SELECTED_COMPONENTS=()
  1246. local temp_file="$TEMP_DIR/components.tmp"
  1247. get_profile_components "$PROFILE" > "$temp_file"
  1248. while IFS= read -r component; do
  1249. [ -n "$component" ] && SELECTED_COMPONENTS+=("$component")
  1250. done < "$temp_file"
  1251. expand_selected_components
  1252. # Resolve dependencies for profile installs
  1253. print_step "Resolving dependencies..."
  1254. local original_count=${#SELECTED_COMPONENTS[@]}
  1255. for comp in "${SELECTED_COMPONENTS[@]}"; do
  1256. resolve_dependencies "$comp"
  1257. done
  1258. local new_count=${#SELECTED_COMPONENTS[@]}
  1259. if [ "$new_count" -gt "$original_count" ]; then
  1260. local added=$((new_count - original_count))
  1261. print_info "Added $added dependencies"
  1262. fi
  1263. show_installation_preview
  1264. else
  1265. # Interactive mode - show location menu first
  1266. show_install_location_menu
  1267. show_main_menu
  1268. if [ "$INSTALL_MODE" = "profile" ]; then
  1269. show_profile_menu
  1270. elif [ "$INSTALL_MODE" = "custom" ]; then
  1271. show_custom_menu
  1272. fi
  1273. fi
  1274. }
  1275. main "$@"