install.sh 44 KB

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