install.sh 51 KB

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