install.sh 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  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
  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. local registry_key
  255. registry_key=$(get_registry_key "$component_type")
  256. if [ "$component_type" = "context" ] && [[ "$component_id" == */* ]]; then
  257. local result
  258. result=$(jq_exec "first(.components.contexts[]? | select(.path == \".opencode/context/${component_id}.md\"))" "$TEMP_DIR/registry.json")
  259. if [ -z "$result" ] || [ "$result" = "null" ]; then
  260. result=$(jq_exec "first(.components.contexts[]? | select(.path == \".opencode/context/${component_id}\"))" "$TEMP_DIR/registry.json")
  261. fi
  262. echo "$result"
  263. return
  264. fi
  265. jq_exec ".components.${registry_key}[]? | select(.id == \"${component_id}\" or (.aliases // [] | index(\"${component_id}\")))" "$TEMP_DIR/registry.json"
  266. }
  267. resolve_component_path() {
  268. local component_type=$1
  269. local component_id=$2
  270. local registry_key
  271. registry_key=$(get_registry_key "$component_type")
  272. if [ "$component_type" = "context" ] && [[ "$component_id" == */* ]]; then
  273. # Try .md extension first (most context files), then fall back to the
  274. # path as-is for non-markdown files (e.g. paths.json). Fixes #251.
  275. local result
  276. result=$(jq_exec "first(.components.contexts[]? | select(.path == \".opencode/context/${component_id}.md\") | .path)" "$TEMP_DIR/registry.json")
  277. if [ -z "$result" ] || [ "$result" = "null" ]; then
  278. result=$(jq_exec "first(.components.contexts[]? | select(.path == \".opencode/context/${component_id}\") | .path)" "$TEMP_DIR/registry.json")
  279. fi
  280. echo "$result"
  281. return
  282. fi
  283. jq_exec ".components.${registry_key}[]? | select(.id == \"${component_id}\" or (.aliases // [] | index(\"${component_id}\"))) | .path" "$TEMP_DIR/registry.json"
  284. }
  285. # Helper function to get the correct registry key for a component type
  286. get_registry_key() {
  287. local type=$1
  288. # Most types are pluralized, but 'config' stays singular
  289. case "$type" in
  290. config) echo "config" ;;
  291. *) echo "${type}s" ;;
  292. esac
  293. }
  294. # Helper function to convert registry path to installation path
  295. # Registry paths are like ".opencode/agent/foo.md"
  296. # We need to replace ".opencode" with the actual INSTALL_DIR
  297. get_install_path() {
  298. local registry_path=$1
  299. # Strip leading .opencode/ if present
  300. local relative_path="${registry_path#.opencode/}"
  301. # If path didn't start with .opencode/, it's a root-relative path (e.g. env.example, README.md)
  302. if [ "$relative_path" = "$registry_path" ]; then
  303. echo "./${registry_path}"
  304. else
  305. echo "${INSTALL_DIR}/${relative_path}"
  306. fi
  307. }
  308. expand_context_wildcard() {
  309. local pattern=$1
  310. local prefix="${pattern%%\**}"
  311. prefix="${prefix%/}"
  312. if [ -n "$prefix" ]; then
  313. prefix="${prefix}/"
  314. fi
  315. jq_exec ".components.contexts[]? | select(.path | startswith(\".opencode/context/${prefix}\")) | .path | sub(\"^\\\\.opencode/context/\"; \"\") | sub(\"\\\\.md$\"; \"\")" "$TEMP_DIR/registry.json"
  316. }
  317. expand_selected_components() {
  318. local expanded=()
  319. for comp in "${SELECTED_COMPONENTS[@]}"; do
  320. local type="${comp%%:*}"
  321. local id="${comp##*:}"
  322. if [[ "$id" == *"*"* ]]; then
  323. if [ "$type" != "context" ]; then
  324. print_warning "Wildcard only supported for context components: ${comp}"
  325. continue
  326. fi
  327. local matches
  328. matches=$(expand_context_wildcard "$id")
  329. if [ -z "$matches" ]; then
  330. print_warning "No contexts matched: ${comp}"
  331. continue
  332. fi
  333. while IFS= read -r match; do
  334. [ -n "$match" ] && expanded+=("context:${match}")
  335. done <<< "$matches"
  336. continue
  337. fi
  338. expanded+=("$comp")
  339. done
  340. local deduped=()
  341. for comp in "${expanded[@]}"; do
  342. local found=0
  343. for existing in "${deduped[@]}"; do
  344. if [ "$existing" = "$comp" ]; then
  345. found=1
  346. break
  347. fi
  348. done
  349. if [ "$found" -eq 0 ]; then
  350. deduped+=("$comp")
  351. fi
  352. done
  353. SELECTED_COMPONENTS=("${deduped[@]}")
  354. }
  355. resolve_dependencies() {
  356. local component=$1
  357. local type="${component%%:*}"
  358. local id="${component##*:}"
  359. # Get the correct registry key (handles singular/plural)
  360. local registry_key
  361. registry_key=$(get_registry_key "$type")
  362. # Get dependencies for this component
  363. local deps
  364. deps=$(jq_exec ".components.${registry_key}[] | select(.id == \"${id}\" or (.aliases // [] | index(\"${id}\"))) | .dependencies[]?" "$TEMP_DIR/registry.json" 2>/dev/null || echo "")
  365. if [ -n "$deps" ]; then
  366. for dep in $deps; do
  367. if [[ "$dep" == *"*"* ]]; then
  368. local dep_type="${dep%%:*}"
  369. local dep_id="${dep##*:}"
  370. if [ "$dep_type" = "context" ]; then
  371. local matched
  372. matched=$(expand_context_wildcard "$dep_id")
  373. if [ -z "$matched" ]; then
  374. print_warning "No contexts matched dependency: ${dep}"
  375. continue
  376. fi
  377. while IFS= read -r match; do
  378. local expanded_dep="context:${match}"
  379. local found=0
  380. for existing in "${SELECTED_COMPONENTS[@]}"; do
  381. if [ "$existing" = "$expanded_dep" ]; then
  382. found=1
  383. break
  384. fi
  385. done
  386. if [ "$found" -eq 0 ]; then
  387. SELECTED_COMPONENTS+=("$expanded_dep")
  388. resolve_dependencies "$expanded_dep"
  389. fi
  390. done <<< "$matched"
  391. continue
  392. fi
  393. fi
  394. # Add dependency if not already in list
  395. local found=0
  396. for existing in "${SELECTED_COMPONENTS[@]}"; do
  397. if [ "$existing" = "$dep" ]; then
  398. found=1
  399. break
  400. fi
  401. done
  402. if [ "$found" -eq 0 ]; then
  403. SELECTED_COMPONENTS+=("$dep")
  404. # Recursively resolve dependencies
  405. resolve_dependencies "$dep"
  406. fi
  407. done
  408. fi
  409. }
  410. #############################################################################
  411. # Installation Mode Selection
  412. #############################################################################
  413. check_interactive_mode() {
  414. # Check if stdin is a terminal (not piped from curl)
  415. if [ ! -t 0 ]; then
  416. print_header
  417. print_error "Interactive mode requires a terminal"
  418. echo ""
  419. echo "You're running this script in a pipe (e.g., curl | bash)"
  420. echo "For interactive mode, download the script first:"
  421. echo ""
  422. echo -e "${CYAN}# Download the script${NC}"
  423. echo "curl -fsSL https://raw.githubusercontent.com/darrenhinde/OpenAgentsControl/main/install.sh -o install.sh"
  424. echo ""
  425. echo -e "${CYAN}# Run interactively${NC}"
  426. echo "bash install.sh"
  427. echo ""
  428. echo "Or use a profile directly:"
  429. echo ""
  430. echo -e "${CYAN}# Quick install with profile${NC}"
  431. echo "curl -fsSL https://raw.githubusercontent.com/darrenhinde/OpenAgentsControl/main/install.sh | bash -s essential"
  432. echo ""
  433. echo "Available profiles: essential, developer, business, full, advanced"
  434. echo ""
  435. cleanup_and_exit 1
  436. fi
  437. }
  438. show_install_location_menu() {
  439. check_interactive_mode
  440. clear
  441. print_header
  442. local global_path
  443. global_path=$(get_global_install_path)
  444. echo -e "${BOLD}Choose installation location:${NC}\n"
  445. echo -e " ${GREEN}1) Local${NC} - Install to ${CYAN}.opencode/${NC} in current directory"
  446. echo " (Best for project-specific agents)"
  447. echo ""
  448. echo -e " ${BLUE}2) Global${NC} - Install to ${CYAN}${global_path}${NC}"
  449. echo " (Best for user-wide agents available everywhere)"
  450. echo ""
  451. echo -e " ${MAGENTA}3) Custom${NC} - Enter exact path"
  452. echo " Examples:"
  453. case "$PLATFORM" in
  454. Windows)
  455. echo -e " ${CYAN}C:/Users/username/my-agents${NC} or ${CYAN}~/my-agents${NC}"
  456. ;;
  457. *)
  458. echo -e " ${CYAN}/home/username/my-agents${NC} or ${CYAN}~/my-agents${NC}"
  459. ;;
  460. esac
  461. echo ""
  462. echo " 4) Back / Exit"
  463. echo ""
  464. read -r -p "Enter your choice [1-4]: " location_choice
  465. case $location_choice in
  466. 1)
  467. INSTALL_DIR=".opencode"
  468. print_success "Installing to local directory: .opencode/"
  469. sleep 1
  470. ;;
  471. 2)
  472. INSTALL_DIR="$global_path"
  473. print_success "Installing to global directory: $global_path"
  474. sleep 1
  475. ;;
  476. 3)
  477. echo ""
  478. read -r -p "Enter installation path: " custom_path
  479. if [ -z "$custom_path" ]; then
  480. print_error "No path entered"
  481. sleep 2
  482. show_install_location_menu
  483. return
  484. fi
  485. local normalized_path
  486. if ! normalized_path=$(normalize_and_validate_path "$custom_path"); then
  487. print_error "Invalid path"
  488. sleep 2
  489. show_install_location_menu
  490. return
  491. fi
  492. if ! validate_install_path "$normalized_path"; then
  493. echo ""
  494. read -r -p "Continue anyway? [y/N]: " continue_choice
  495. if [[ ! $continue_choice =~ ^[Yy] ]]; then
  496. show_install_location_menu
  497. return
  498. fi
  499. fi
  500. INSTALL_DIR="$normalized_path"
  501. print_success "Installing to custom directory: $INSTALL_DIR"
  502. sleep 1
  503. ;;
  504. 4)
  505. cleanup_and_exit 0
  506. ;;
  507. *)
  508. print_error "Invalid choice"
  509. sleep 2
  510. show_install_location_menu
  511. return
  512. ;;
  513. esac
  514. }
  515. show_main_menu() {
  516. check_interactive_mode
  517. clear
  518. print_header
  519. echo -e "${BOLD}Choose installation mode:${NC}\n"
  520. echo " 1) Quick Install (Choose a profile)"
  521. echo " 2) Custom Install (Pick individual components)"
  522. echo " 3) List Available Components"
  523. echo " 4) Exit"
  524. echo ""
  525. read -r -p "Enter your choice [1-4]: " choice
  526. case $choice in
  527. 1) INSTALL_MODE="profile" ;;
  528. 2) INSTALL_MODE="custom" ;;
  529. 3) list_components; read -r -p "Press Enter to continue..."; show_main_menu ;;
  530. 4) cleanup_and_exit 0 ;;
  531. *) print_error "Invalid choice"; sleep 2; show_main_menu ;;
  532. esac
  533. }
  534. #############################################################################
  535. # Profile Installation
  536. #############################################################################
  537. show_profile_menu() {
  538. clear
  539. print_header
  540. echo -e "${BOLD}Available Installation Profiles:${NC}\n"
  541. # Essential profile
  542. local essential_name
  543. essential_name=$(jq_exec '.profiles.essential.name' "$TEMP_DIR/registry.json")
  544. local essential_desc
  545. essential_desc=$(jq_exec '.profiles.essential.description' "$TEMP_DIR/registry.json")
  546. local essential_count
  547. essential_count=$(jq_exec '.profiles.essential.components | length' "$TEMP_DIR/registry.json")
  548. echo -e " ${GREEN}1) ${essential_name}${NC}"
  549. echo -e " ${essential_desc}"
  550. echo -e " Components: ${essential_count}\n"
  551. # Developer profile
  552. local dev_desc
  553. dev_desc=$(jq_exec '.profiles.developer.description' "$TEMP_DIR/registry.json")
  554. local dev_count
  555. dev_count=$(jq_exec '.profiles.developer.components | length' "$TEMP_DIR/registry.json")
  556. local dev_badge
  557. dev_badge=$(jq_exec '.profiles.developer.badge // ""' "$TEMP_DIR/registry.json")
  558. if [ -n "$dev_badge" ]; then
  559. echo -e " ${BLUE}2) Developer ${GREEN}[${dev_badge}]${NC}"
  560. else
  561. echo -e " ${BLUE}2) Developer${NC}"
  562. fi
  563. echo -e " ${dev_desc}"
  564. echo -e " Components: ${dev_count}\n"
  565. # Business profile
  566. local business_name
  567. business_name=$(jq_exec '.profiles.business.name' "$TEMP_DIR/registry.json")
  568. local business_desc
  569. business_desc=$(jq_exec '.profiles.business.description' "$TEMP_DIR/registry.json")
  570. local business_count
  571. business_count=$(jq_exec '.profiles.business.components | length' "$TEMP_DIR/registry.json")
  572. echo -e " ${CYAN}3) ${business_name}${NC}"
  573. echo -e " ${business_desc}"
  574. echo -e " Components: ${business_count}\n"
  575. # Full profile
  576. local full_name
  577. full_name=$(jq_exec '.profiles.full.name' "$TEMP_DIR/registry.json")
  578. local full_desc
  579. full_desc=$(jq_exec '.profiles.full.description' "$TEMP_DIR/registry.json")
  580. local full_count
  581. full_count=$(jq_exec '.profiles.full.components | length' "$TEMP_DIR/registry.json")
  582. echo -e " ${MAGENTA}4) ${full_name}${NC}"
  583. echo -e " ${full_desc}"
  584. echo -e " Components: ${full_count}\n"
  585. # Advanced profile
  586. local adv_name
  587. adv_name=$(jq_exec '.profiles.advanced.name' "$TEMP_DIR/registry.json")
  588. local adv_desc
  589. adv_desc=$(jq_exec '.profiles.advanced.description' "$TEMP_DIR/registry.json")
  590. local adv_count
  591. adv_count=$(jq_exec '.profiles.advanced.components | length' "$TEMP_DIR/registry.json")
  592. echo -e " ${YELLOW}5) ${adv_name}${NC}"
  593. echo -e " ${adv_desc}"
  594. echo -e " Components: ${adv_count}\n"
  595. echo " 6) Back to main menu"
  596. echo ""
  597. read -r -p "Enter your choice [1-6]: " choice
  598. case $choice in
  599. 1) PROFILE="essential" ;;
  600. 2) PROFILE="developer" ;;
  601. 3) PROFILE="business" ;;
  602. 4) PROFILE="full" ;;
  603. 5) PROFILE="advanced" ;;
  604. 6) show_main_menu; return ;;
  605. *) print_error "Invalid choice"; sleep 2; show_profile_menu; return ;;
  606. esac
  607. # Load profile components (compatible with bash 3.2+)
  608. SELECTED_COMPONENTS=()
  609. local temp_file="$TEMP_DIR/components.tmp"
  610. get_profile_components "$PROFILE" > "$temp_file"
  611. while IFS= read -r component; do
  612. [ -n "$component" ] && SELECTED_COMPONENTS+=("$component")
  613. done < "$temp_file"
  614. expand_selected_components
  615. # Resolve dependencies for profile installs
  616. print_step "Resolving dependencies..."
  617. local original_count=${#SELECTED_COMPONENTS[@]}
  618. for comp in "${SELECTED_COMPONENTS[@]}"; do
  619. resolve_dependencies "$comp"
  620. done
  621. local new_count=${#SELECTED_COMPONENTS[@]}
  622. if [ "$new_count" -gt "$original_count" ]; then
  623. local added=$((new_count - original_count))
  624. print_info "Added $added dependencies"
  625. fi
  626. show_installation_preview
  627. }
  628. #############################################################################
  629. # Custom Component Selection
  630. #############################################################################
  631. show_custom_menu() {
  632. clear
  633. print_header
  634. echo -e "${BOLD}Select component categories to install:${NC}\n"
  635. echo "Use space to toggle, Enter to continue"
  636. echo ""
  637. local categories=("agents" "subagents" "commands" "tools" "plugins" "skills" "contexts" "config")
  638. local selected_categories=()
  639. # Simple selection (for now, we'll make it interactive later)
  640. echo "Available categories:"
  641. for i in "${!categories[@]}"; do
  642. local cat="${categories[$i]}"
  643. local count
  644. count=$(jq_exec ".components.${cat} | length" "$TEMP_DIR/registry.json")
  645. local cat_display
  646. cat_display=$(echo "$cat" | awk '{print toupper(substr($0,1,1)) tolower(substr($0,2))}')
  647. echo " $((i+1))) ${cat_display} (${count} available)"
  648. done
  649. echo " $((${#categories[@]}+1))) Select All"
  650. echo " $((${#categories[@]}+2))) Continue to component selection"
  651. echo " $((${#categories[@]}+3))) Back to main menu"
  652. echo ""
  653. read -r -p "Enter category numbers (space-separated) or option: " -a selections
  654. for sel in "${selections[@]}"; do
  655. if [[ ! "$sel" =~ ^[0-9]+$ ]]; then
  656. continue
  657. fi
  658. if [ "$sel" -eq $((${#categories[@]}+1)) ]; then
  659. selected_categories=("${categories[@]}")
  660. break
  661. elif [ "$sel" -eq $((${#categories[@]}+2)) ]; then
  662. break
  663. elif [ "$sel" -eq $((${#categories[@]}+3)) ]; then
  664. show_main_menu
  665. return
  666. elif [ "$sel" -ge 1 ] && [ "$sel" -le ${#categories[@]} ]; then
  667. selected_categories+=("${categories[$((sel-1))]}")
  668. fi
  669. done
  670. if [ ${#selected_categories[@]} -eq 0 ]; then
  671. print_warning "No categories selected"
  672. sleep 2
  673. show_custom_menu
  674. return
  675. fi
  676. show_component_selection "${selected_categories[@]}"
  677. }
  678. show_component_selection() {
  679. local categories=("$@")
  680. clear
  681. print_header
  682. echo -e "${BOLD}Select components to install:${NC}\n"
  683. local all_components=()
  684. local component_details=()
  685. local idx=1
  686. for category in "${categories[@]}"; do
  687. local cat_display
  688. cat_display=$(echo "$category" | awk '{print toupper(substr($0,1,1)) tolower(substr($0,2))}')
  689. echo -e "${CYAN}${BOLD}${cat_display}:${NC}"
  690. local components
  691. components=$(jq_exec ".components.${category}[]? | .id" "$TEMP_DIR/registry.json")
  692. while IFS= read -r comp_id; do
  693. local comp_name
  694. comp_name=$(jq_exec ".components.${category}[]? | select(.id == \"${comp_id}\") | .name" "$TEMP_DIR/registry.json")
  695. local comp_desc
  696. comp_desc=$(jq_exec ".components.${category}[]? | select(.id == \"${comp_id}\") | .description" "$TEMP_DIR/registry.json")
  697. echo " ${idx}) ${comp_name}"
  698. echo " ${comp_desc}"
  699. local singular_type
  700. case "$category" in
  701. agents) singular_type="agent" ;;
  702. subagents) singular_type="subagent" ;;
  703. commands) singular_type="command" ;;
  704. tools) singular_type="tool" ;;
  705. plugins) singular_type="plugin" ;;
  706. skills) singular_type="skill" ;;
  707. contexts) singular_type="context" ;;
  708. config) singular_type="config" ;;
  709. *) singular_type="${category%s}" ;;
  710. esac
  711. all_components+=("${singular_type}:${comp_id}")
  712. component_details+=("${comp_name}|${comp_desc}")
  713. idx=$((idx+1))
  714. done <<< "$components"
  715. echo ""
  716. done
  717. echo "Enter component numbers (space-separated), 'all' for all, or 'done' to continue:"
  718. read -r -a selections
  719. for sel in "${selections[@]}"; do
  720. if [ "$sel" = "all" ]; then
  721. SELECTED_COMPONENTS=("${all_components[@]}")
  722. break
  723. elif [ "$sel" = "done" ]; then
  724. break
  725. elif [[ "$sel" =~ ^[0-9]+$ ]] && [ "$sel" -ge 1 ] && [ "$sel" -le ${#all_components[@]} ]; then
  726. SELECTED_COMPONENTS+=("${all_components[$((sel-1))]}")
  727. fi
  728. done
  729. if [ ${#SELECTED_COMPONENTS[@]} -eq 0 ]; then
  730. print_warning "No components selected"
  731. sleep 2
  732. show_custom_menu
  733. return
  734. fi
  735. # Resolve dependencies
  736. print_step "Resolving dependencies..."
  737. local original_count=${#SELECTED_COMPONENTS[@]}
  738. for comp in "${SELECTED_COMPONENTS[@]}"; do
  739. resolve_dependencies "$comp"
  740. done
  741. if [ ${#SELECTED_COMPONENTS[@]} -gt "$original_count" ]; then
  742. print_info "Added $((${#SELECTED_COMPONENTS[@]} - original_count)) dependencies"
  743. fi
  744. show_installation_preview
  745. }
  746. #############################################################################
  747. # Installation Preview & Confirmation
  748. #############################################################################
  749. show_installation_preview() {
  750. # Only clear screen in interactive mode
  751. if [ "$NON_INTERACTIVE" != true ]; then
  752. clear
  753. fi
  754. print_header
  755. echo -e "${BOLD}Installation Preview${NC}\n"
  756. if [ -n "$PROFILE" ]; then
  757. echo -e "Profile: ${GREEN}${PROFILE}${NC}"
  758. else
  759. echo -e "Mode: ${GREEN}Custom${NC}"
  760. fi
  761. echo -e "Installation directory: ${CYAN}${INSTALL_DIR}${NC}"
  762. echo -e "\nComponents to install (${#SELECTED_COMPONENTS[@]} total):\n"
  763. # Group by type
  764. local agents=()
  765. local subagents=()
  766. local commands=()
  767. local tools=()
  768. local plugins=()
  769. local skills=()
  770. local contexts=()
  771. local configs=()
  772. for comp in "${SELECTED_COMPONENTS[@]}"; do
  773. local type="${comp%%:*}"
  774. case $type in
  775. agent) agents+=("$comp") ;;
  776. subagent) subagents+=("$comp") ;;
  777. command) commands+=("$comp") ;;
  778. tool) tools+=("$comp") ;;
  779. plugin) plugins+=("$comp") ;;
  780. skill) skills+=("$comp") ;;
  781. context) contexts+=("$comp") ;;
  782. config) configs+=("$comp") ;;
  783. esac
  784. done
  785. [ ${#agents[@]} -gt 0 ] && echo -e "${CYAN}Agents (${#agents[@]}):${NC} ${agents[*]##*:}"
  786. [ ${#subagents[@]} -gt 0 ] && echo -e "${CYAN}Subagents (${#subagents[@]}):${NC} ${subagents[*]##*:}"
  787. [ ${#commands[@]} -gt 0 ] && echo -e "${CYAN}Commands (${#commands[@]}):${NC} ${commands[*]##*:}"
  788. [ ${#tools[@]} -gt 0 ] && echo -e "${CYAN}Tools (${#tools[@]}):${NC} ${tools[*]##*:}"
  789. [ ${#plugins[@]} -gt 0 ] && echo -e "${CYAN}Plugins (${#plugins[@]}):${NC} ${plugins[*]##*:}"
  790. [ ${#skills[@]} -gt 0 ] && echo -e "${CYAN}Skills (${#skills[@]}):${NC} ${skills[*]##*:}"
  791. [ ${#contexts[@]} -gt 0 ] && echo -e "${CYAN}Contexts (${#contexts[@]}):${NC} ${contexts[*]##*:}"
  792. [ ${#configs[@]} -gt 0 ] && echo -e "${CYAN}Config (${#configs[@]}):${NC} ${configs[*]##*:}"
  793. echo ""
  794. # Skip confirmation if profile was provided via command line
  795. if [ "$NON_INTERACTIVE" = true ]; then
  796. print_info "Installing automatically (profile specified)..."
  797. perform_installation
  798. else
  799. read -r -p "Proceed with installation? [Y/n]: " confirm
  800. if [[ $confirm =~ ^[Nn] ]]; then
  801. print_info "Installation cancelled"
  802. cleanup_and_exit 0
  803. fi
  804. perform_installation
  805. fi
  806. }
  807. #############################################################################
  808. # Collision Detection
  809. #############################################################################
  810. show_collision_report() {
  811. local collision_count=$1
  812. shift
  813. local collisions=("$@")
  814. echo ""
  815. print_warning "Found ${collision_count} file collision(s):"
  816. echo ""
  817. # Group by type
  818. local agents=()
  819. local subagents=()
  820. local commands=()
  821. local tools=()
  822. local plugins=()
  823. local skills=()
  824. local contexts=()
  825. local configs=()
  826. for file in "${collisions[@]}"; do
  827. # Skip empty entries
  828. [ -z "$file" ] && continue
  829. if [[ $file == *"/agent/subagents/"* ]]; then
  830. subagents+=("$file")
  831. elif [[ $file == *"/agent/"* ]]; then
  832. agents+=("$file")
  833. elif [[ $file == *"/command/"* ]]; then
  834. commands+=("$file")
  835. elif [[ $file == *"/tool/"* ]]; then
  836. tools+=("$file")
  837. elif [[ $file == *"/plugin/"* ]]; then
  838. plugins+=("$file")
  839. elif [[ $file == *"/skills/"* ]]; then
  840. skills+=("$file")
  841. elif [[ $file == *"/context/"* ]]; then
  842. contexts+=("$file")
  843. else
  844. configs+=("$file")
  845. fi
  846. done
  847. # Display grouped collisions
  848. [ ${#agents[@]} -gt 0 ] && echo -e "${YELLOW} Agents (${#agents[@]}):${NC}" && printf ' %s\n' "${agents[@]}"
  849. [ ${#subagents[@]} -gt 0 ] && echo -e "${YELLOW} Subagents (${#subagents[@]}):${NC}" && printf ' %s\n' "${subagents[@]}"
  850. [ ${#commands[@]} -gt 0 ] && echo -e "${YELLOW} Commands (${#commands[@]}):${NC}" && printf ' %s\n' "${commands[@]}"
  851. [ ${#tools[@]} -gt 0 ] && echo -e "${YELLOW} Tools (${#tools[@]}):${NC}" && printf ' %s\n' "${tools[@]}"
  852. [ ${#plugins[@]} -gt 0 ] && echo -e "${YELLOW} Plugins (${#plugins[@]}):${NC}" && printf ' %s\n' "${plugins[@]}"
  853. [ ${#skills[@]} -gt 0 ] && echo -e "${YELLOW} Skills (${#skills[@]}):${NC}" && printf ' %s\n' "${skills[@]}"
  854. [ ${#contexts[@]} -gt 0 ] && echo -e "${YELLOW} Context (${#contexts[@]}):${NC}" && printf ' %s\n' "${contexts[@]}"
  855. [ ${#configs[@]} -gt 0 ] && echo -e "${YELLOW} Config (${#configs[@]}):${NC}" && printf ' %s\n' "${configs[@]}"
  856. echo ""
  857. }
  858. get_install_strategy() {
  859. echo -e "${BOLD}How would you like to proceed?${NC}\n" >&2
  860. echo " 1) ${GREEN}Skip existing${NC} - Only install new files, keep all existing files unchanged" >&2
  861. echo " 2) ${YELLOW}Overwrite all${NC} - Replace existing files with new versions (your changes will be lost)" >&2
  862. echo " 3) ${CYAN}Backup & overwrite${NC} - Backup existing files, then install new versions" >&2
  863. echo " 4) ${RED}Cancel${NC} - Exit without making changes" >&2
  864. echo "" >&2
  865. read -r -p "Enter your choice [1-4]: " strategy_choice
  866. case $strategy_choice in
  867. 1) echo "skip" ;;
  868. 2)
  869. echo "" >&2
  870. print_warning "This will overwrite existing files. Your changes will be lost!"
  871. read -r -p "Are you sure? Type 'yes' to confirm: " confirm
  872. if [ "$confirm" = "yes" ]; then
  873. echo "overwrite"
  874. else
  875. echo "cancel"
  876. fi
  877. ;;
  878. 3) echo "backup" ;;
  879. 4) echo "cancel" ;;
  880. *) echo "cancel" ;;
  881. esac
  882. }
  883. #############################################################################
  884. # Installation
  885. #############################################################################
  886. perform_installation() {
  887. print_step "Preparing installation..."
  888. # Create base directory only - subdirectories created on-demand when files are installed
  889. mkdir -p "$INSTALL_DIR"
  890. # Check for collisions
  891. local collisions=()
  892. for comp in "${SELECTED_COMPONENTS[@]}"; do
  893. local type="${comp%%:*}"
  894. local id="${comp##*:}"
  895. local path
  896. path=$(resolve_component_path "$type" "$id")
  897. if [ -n "$path" ] && [ "$path" != "null" ]; then
  898. local install_path
  899. install_path=$(get_install_path "$path")
  900. if [ -f "$install_path" ]; then
  901. collisions+=("$install_path")
  902. fi
  903. fi
  904. done
  905. # Determine installation strategy
  906. local install_strategy="fresh"
  907. if [ ${#collisions[@]} -gt 0 ]; then
  908. # In non-interactive mode, use default strategy (skip existing files)
  909. if [ "$NON_INTERACTIVE" = true ]; then
  910. print_info "Found ${#collisions[@]} existing file(s) - using 'skip' strategy (non-interactive mode)"
  911. print_info "To overwrite, download script and run interactively, or delete existing files first"
  912. install_strategy="skip"
  913. else
  914. show_collision_report ${#collisions[@]} "${collisions[@]}"
  915. install_strategy=$(get_install_strategy)
  916. if [ "$install_strategy" = "cancel" ]; then
  917. print_info "Installation cancelled by user"
  918. cleanup_and_exit 0
  919. fi
  920. fi
  921. # Handle backup strategy
  922. if [ "$install_strategy" = "backup" ]; then
  923. local backup_dir
  924. backup_dir="${INSTALL_DIR}.backup.$(date +%Y%m%d-%H%M%S)"
  925. print_step "Creating backup..."
  926. # Only backup files that will be overwritten
  927. local backup_count=0
  928. for file in "${collisions[@]}"; do
  929. if [ -f "$file" ]; then
  930. local backup_file="${backup_dir}/${file}"
  931. mkdir -p "$(dirname "$backup_file")"
  932. if cp "$file" "$backup_file" 2>/dev/null; then
  933. backup_count=$((backup_count + 1))
  934. else
  935. print_warning "Failed to backup: $file"
  936. fi
  937. fi
  938. done
  939. if [ $backup_count -gt 0 ]; then
  940. print_success "Backed up ${backup_count} file(s) to $backup_dir"
  941. install_strategy="overwrite" # Now we can overwrite
  942. else
  943. print_error "Backup failed. Installation cancelled."
  944. cleanup_and_exit 1
  945. fi
  946. fi
  947. fi
  948. # Perform installation
  949. print_step "Installing components..."
  950. local installed=0
  951. local skipped=0
  952. local failed=0
  953. for comp in "${SELECTED_COMPONENTS[@]}"; do
  954. local type="${comp%%:*}"
  955. local id="${comp##*:}"
  956. # Get component path (registry key resolved internally)
  957. local path
  958. path=$(resolve_component_path "$type" "$id")
  959. # Registry key needed for multi-file (skills) lookup
  960. local registry_key
  961. registry_key=$(get_registry_key "$type")
  962. if [ -z "$path" ] || [ "$path" = "null" ]; then
  963. print_warning "Could not find path for ${comp}"
  964. failed=$((failed + 1))
  965. continue
  966. fi
  967. # Check if component has additional files (for skills)
  968. local files_array
  969. files_array=$(jq_exec ".components.${registry_key}[]? | select(.id == \"${id}\") | .files[]?" "$TEMP_DIR/registry.json")
  970. if [ -n "$files_array" ]; then
  971. # Component has multiple files - download all of them
  972. local component_installed=0
  973. local component_failed=0
  974. while IFS= read -r file_path; do
  975. [ -z "$file_path" ] && continue
  976. local dest
  977. dest=$(get_install_path "$file_path")
  978. # Check if file exists and we're in skip mode
  979. if [ -f "$dest" ] && [ "$install_strategy" = "skip" ]; then
  980. continue
  981. fi
  982. # Download file
  983. local url="${RAW_URL}/${file_path}"
  984. mkdir -p "$(dirname "$dest")"
  985. if curl -fsSL "$url" -o "$dest"; then
  986. # Transform paths for global installation
  987. if [[ "$INSTALL_DIR" != ".opencode" ]] && [[ "$INSTALL_DIR" != *"/.opencode" ]]; then
  988. local expanded_path="${INSTALL_DIR/#\~/$HOME}"
  989. sed -i.bak -e "s|@\.opencode/context/|@${expanded_path}/context/|g" \
  990. -e "s|\.opencode/context|${expanded_path}/context|g" "$dest" 2>/dev/null || true
  991. rm -f "${dest}.bak" 2>/dev/null || true
  992. fi
  993. component_installed=$((component_installed + 1))
  994. else
  995. component_failed=$((component_failed + 1))
  996. fi
  997. done <<< "$files_array"
  998. if [ $component_failed -eq 0 ]; then
  999. print_success "Installed ${type}: ${id} (${component_installed} files)"
  1000. installed=$((installed + 1))
  1001. else
  1002. print_error "Failed to install ${type}: ${id} (${component_failed} files failed)"
  1003. failed=$((failed + 1))
  1004. fi
  1005. else
  1006. # Single file component - original logic
  1007. local dest
  1008. dest=$(get_install_path "$path")
  1009. # Check if file exists before we install (for proper messaging)
  1010. local file_existed=false
  1011. if [ -f "$dest" ]; then
  1012. file_existed=true
  1013. fi
  1014. # Check if file exists and we're in skip mode
  1015. if [ "$file_existed" = true ] && [ "$install_strategy" = "skip" ]; then
  1016. print_info "Skipped existing: ${type}:${id}"
  1017. skipped=$((skipped + 1))
  1018. continue
  1019. fi
  1020. # Download component
  1021. local url="${RAW_URL}/${path}"
  1022. # Create parent directory if needed
  1023. mkdir -p "$(dirname "$dest")"
  1024. if curl -fsSL "$url" -o "$dest"; then
  1025. # Transform paths for global installation (any non-local path)
  1026. # Local paths: .opencode or */.opencode
  1027. if [[ "$INSTALL_DIR" != ".opencode" ]] && [[ "$INSTALL_DIR" != *"/.opencode" ]]; then
  1028. # Expand tilde and get absolute path for transformation
  1029. local expanded_path="${INSTALL_DIR/#\~/$HOME}"
  1030. # Transform @.opencode/context/ references to actual install path
  1031. sed -i.bak -e "s|@\.opencode/context/|@${expanded_path}/context/|g" \
  1032. -e "s|\.opencode/context|${expanded_path}/context|g" "$dest" 2>/dev/null || true
  1033. rm -f "${dest}.bak" 2>/dev/null || true
  1034. fi
  1035. # Show appropriate message based on whether file existed before
  1036. if [ "$file_existed" = true ]; then
  1037. print_success "Updated ${type}: ${id}"
  1038. else
  1039. print_success "Installed ${type}: ${id}"
  1040. fi
  1041. installed=$((installed + 1))
  1042. else
  1043. print_error "Failed to install ${type}: ${id}"
  1044. failed=$((failed + 1))
  1045. fi
  1046. fi
  1047. done
  1048. # Handle additional paths for advanced profile
  1049. if [ "$PROFILE" = "advanced" ]; then
  1050. local additional_paths
  1051. additional_paths=$(jq_exec '.profiles.advanced.additionalPaths[]?' "$TEMP_DIR/registry.json")
  1052. if [ -n "$additional_paths" ]; then
  1053. print_step "Installing additional paths..."
  1054. while IFS= read -r path; do
  1055. # For directories, we'd need to recursively download
  1056. # For now, just note them
  1057. print_info "Additional path: $path (manual download required)"
  1058. done <<< "$additional_paths"
  1059. fi
  1060. fi
  1061. echo ""
  1062. print_success "Installation complete!"
  1063. echo -e " Installed: ${GREEN}${installed}${NC}"
  1064. [ $skipped -gt 0 ] && echo -e " Skipped: ${CYAN}${skipped}${NC}"
  1065. [ $failed -gt 0 ] && echo -e " Failed: ${RED}${failed}${NC}"
  1066. show_post_install
  1067. }
  1068. #############################################################################
  1069. # Post-Installation
  1070. #############################################################################
  1071. show_post_install() {
  1072. echo ""
  1073. print_step "Next Steps"
  1074. echo "1. Review the installed components in ${CYAN}${INSTALL_DIR}/${NC}"
  1075. # Check if env.example was installed
  1076. if [ -f "${INSTALL_DIR}/env.example" ] || [ -f "env.example" ]; then
  1077. echo "2. Copy env.example to .env and configure:"
  1078. echo -e " ${CYAN}cp env.example .env${NC}"
  1079. echo "3. Start using OpenCode agents:"
  1080. else
  1081. echo "2. Start using OpenCode agents:"
  1082. fi
  1083. echo -e " ${CYAN}opencode${NC}"
  1084. echo ""
  1085. # Show installation location info
  1086. print_info "Installation directory: ${CYAN}${INSTALL_DIR}${NC}"
  1087. # Check for backup directories
  1088. local has_backup=0
  1089. local backup_dir
  1090. local backup_dirs=()
  1091. shopt -s nullglob
  1092. backup_dirs=("${INSTALL_DIR}.backup."*)
  1093. shopt -u nullglob
  1094. for backup_dir in "${backup_dirs[@]}"; do
  1095. if [ -d "$backup_dir" ]; then
  1096. has_backup=1
  1097. break
  1098. fi
  1099. done
  1100. if [ "$has_backup" -eq 1 ]; then
  1101. print_info "Backup created - you can restore files from ${INSTALL_DIR}.backup.* if needed"
  1102. fi
  1103. print_info "Documentation: ${REPO_URL}"
  1104. echo ""
  1105. cleanup_and_exit 0
  1106. }
  1107. #############################################################################
  1108. # Component Listing
  1109. #############################################################################
  1110. list_components() {
  1111. clear || true
  1112. print_header
  1113. echo -e "${BOLD}Available Components${NC}\n"
  1114. local categories=("agents" "subagents" "commands" "tools" "plugins" "skills" "contexts" "config")
  1115. for category in "${categories[@]}"; do
  1116. local cat_display
  1117. cat_display=$(echo "$category" | awk '{print toupper(substr($0,1,1)) tolower(substr($0,2))}')
  1118. echo -e "${CYAN}${BOLD}${cat_display}:${NC}"
  1119. local components
  1120. components=$(jq_exec ".components.${category}[]? | \"\(.id)|\(.name)|\(.description)\"" "$TEMP_DIR/registry.json")
  1121. while IFS='|' read -r id name desc; do
  1122. echo -e " ${GREEN}${name}${NC} (${id})"
  1123. echo -e " ${desc}"
  1124. done <<< "$components"
  1125. echo ""
  1126. done
  1127. }
  1128. #############################################################################
  1129. # Cleanup
  1130. #############################################################################
  1131. cleanup_and_exit() {
  1132. rm -rf "$TEMP_DIR"
  1133. exit "$1"
  1134. }
  1135. trap 'cleanup_and_exit 1' INT TERM
  1136. #############################################################################
  1137. # Main
  1138. #############################################################################
  1139. main() {
  1140. # Parse command line arguments
  1141. while [ $# -gt 0 ]; do
  1142. case "$1" in
  1143. --install-dir=*)
  1144. CUSTOM_INSTALL_DIR="${1#*=}"
  1145. # Basic validation - check not empty
  1146. if [ -z "$CUSTOM_INSTALL_DIR" ]; then
  1147. echo "Error: --install-dir requires a non-empty path"
  1148. exit 1
  1149. fi
  1150. shift
  1151. ;;
  1152. --install-dir)
  1153. if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
  1154. CUSTOM_INSTALL_DIR="$2"
  1155. shift 2
  1156. else
  1157. echo "Error: --install-dir requires a path argument"
  1158. exit 1
  1159. fi
  1160. ;;
  1161. essential|--essential)
  1162. INSTALL_MODE="profile"
  1163. PROFILE="essential"
  1164. NON_INTERACTIVE=true
  1165. shift
  1166. ;;
  1167. developer|--developer)
  1168. INSTALL_MODE="profile"
  1169. PROFILE="developer"
  1170. NON_INTERACTIVE=true
  1171. shift
  1172. ;;
  1173. business|--business)
  1174. INSTALL_MODE="profile"
  1175. PROFILE="business"
  1176. NON_INTERACTIVE=true
  1177. shift
  1178. ;;
  1179. full|--full)
  1180. INSTALL_MODE="profile"
  1181. PROFILE="full"
  1182. NON_INTERACTIVE=true
  1183. shift
  1184. ;;
  1185. advanced|--advanced)
  1186. INSTALL_MODE="profile"
  1187. PROFILE="advanced"
  1188. NON_INTERACTIVE=true
  1189. shift
  1190. ;;
  1191. list|--list)
  1192. check_dependencies
  1193. fetch_registry
  1194. list_components
  1195. cleanup_and_exit 0
  1196. ;;
  1197. --help|-h|help)
  1198. print_header
  1199. echo "Usage: $0 [PROFILE] [OPTIONS]"
  1200. echo ""
  1201. echo -e "${BOLD}Profiles:${NC}"
  1202. echo " essential, --essential Minimal setup with core agents"
  1203. echo " developer, --developer Code-focused development tools"
  1204. echo " business, --business Content and business-focused tools"
  1205. echo " full, --full Everything except system-builder"
  1206. echo " advanced, --advanced Complete system with all components"
  1207. echo ""
  1208. echo -e "${BOLD}Options:${NC}"
  1209. echo " --install-dir PATH Custom installation directory"
  1210. echo " (default: .opencode)"
  1211. echo " list, --list List all available components"
  1212. echo " help, --help, -h Show this help message"
  1213. echo ""
  1214. echo -e "${BOLD}Environment Variables:${NC}"
  1215. echo " OPENCODE_INSTALL_DIR Installation directory"
  1216. echo " OPENCODE_BRANCH Git branch to install from (default: main)"
  1217. echo ""
  1218. echo -e "${BOLD}Examples:${NC}"
  1219. echo ""
  1220. echo -e " ${CYAN}# Interactive mode (choose location and components)${NC}"
  1221. echo " $0"
  1222. echo ""
  1223. echo -e " ${CYAN}# Quick install with default location (.opencode/)${NC}"
  1224. echo " $0 developer"
  1225. echo ""
  1226. echo -e " ${CYAN}# Install to global location (Linux/macOS)${NC}"
  1227. echo " $0 developer --install-dir ~/.config/opencode"
  1228. echo ""
  1229. echo -e " ${CYAN}# Install to global location (Windows Git Bash)${NC}"
  1230. echo " $0 developer --install-dir ~/.config/opencode"
  1231. echo ""
  1232. echo -e " ${CYAN}# Install to custom location${NC}"
  1233. echo " $0 essential --install-dir ~/my-agents"
  1234. echo ""
  1235. echo -e " ${CYAN}# Using environment variable${NC}"
  1236. echo " export OPENCODE_INSTALL_DIR=~/.config/opencode"
  1237. echo " $0 developer"
  1238. echo ""
  1239. echo -e " ${CYAN}# Install from URL (non-interactive)${NC}"
  1240. echo " curl -fsSL https://raw.githubusercontent.com/darrenhinde/OpenAgentsControl/main/install.sh | bash -s developer"
  1241. echo ""
  1242. echo -e "${BOLD}Platform Support:${NC}"
  1243. echo " ✓ Linux (bash 3.2+)"
  1244. echo " ✓ macOS (bash 3.2+)"
  1245. echo " ✓ Windows (Git Bash, WSL)"
  1246. echo ""
  1247. exit 0
  1248. ;;
  1249. *)
  1250. echo "Unknown option: $1"
  1251. echo "Run '$0 --help' for usage information"
  1252. exit 1
  1253. ;;
  1254. esac
  1255. done
  1256. # Apply custom install directory if specified (CLI arg overrides env var)
  1257. if [ -n "$CUSTOM_INSTALL_DIR" ]; then
  1258. local normalized_path
  1259. if normalized_path=$(normalize_and_validate_path "$CUSTOM_INSTALL_DIR"); then
  1260. INSTALL_DIR="$normalized_path"
  1261. if ! validate_install_path "$INSTALL_DIR"; then
  1262. print_warning "Installation path may have issues, but continuing..."
  1263. fi
  1264. else
  1265. print_error "Invalid installation directory: $CUSTOM_INSTALL_DIR"
  1266. exit 1
  1267. fi
  1268. fi
  1269. check_bash_version
  1270. check_dependencies
  1271. fetch_registry
  1272. if [ -n "$PROFILE" ]; then
  1273. # Non-interactive mode (compatible with bash 3.2+)
  1274. SELECTED_COMPONENTS=()
  1275. local temp_file="$TEMP_DIR/components.tmp"
  1276. get_profile_components "$PROFILE" > "$temp_file"
  1277. while IFS= read -r component; do
  1278. [ -n "$component" ] && SELECTED_COMPONENTS+=("$component")
  1279. done < "$temp_file"
  1280. expand_selected_components
  1281. # Resolve dependencies for profile installs
  1282. print_step "Resolving dependencies..."
  1283. local original_count=${#SELECTED_COMPONENTS[@]}
  1284. for comp in "${SELECTED_COMPONENTS[@]}"; do
  1285. resolve_dependencies "$comp"
  1286. done
  1287. local new_count=${#SELECTED_COMPONENTS[@]}
  1288. if [ "$new_count" -gt "$original_count" ]; then
  1289. local added=$((new_count - original_count))
  1290. print_info "Added $added dependencies"
  1291. fi
  1292. show_installation_preview
  1293. else
  1294. # Interactive mode - show location menu first
  1295. show_install_location_menu
  1296. show_main_menu
  1297. if [ "$INSTALL_MODE" = "profile" ]; then
  1298. show_profile_menu
  1299. elif [ "$INSTALL_MODE" = "custom" ]; then
  1300. show_custom_menu
  1301. fi
  1302. fi
  1303. }
  1304. main "$@"