install.sh 48 KB

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