File size: 7,969 Bytes
0722e92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env bash
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# IniClaw curl-pipe-bash installer.
#
# Usage:
#   curl -fsSL https://raw.githubusercontent.com/Inmodel/ini_claw/main/scripts/install.sh | bash

set -euo pipefail

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

info()  { echo -e "${GREEN}[install]${NC} $1"; }
warn()  { echo -e "${YELLOW}[install]${NC} $1"; }
fail()  { echo -e "${RED}[install]${NC} $1"; exit 1; }

MIN_NODE_MAJOR=20
MIN_NPM_MAJOR=10
RECOMMENDED_NODE_MAJOR=22
RUNTIME_REQUIREMENT_MSG="IniClaw requires Node.js >=${MIN_NODE_MAJOR} and npm >=${MIN_NPM_MAJOR} (recommended Node.js ${RECOMMENDED_NODE_MAJOR})."

OS="$(uname -s)"
ARCH="$(uname -m)"

case "$OS" in
  Darwin) OS_LABEL="macOS" ;;
  Linux)  OS_LABEL="Linux" ;;
  *)      fail "Unsupported OS: $OS" ;;
esac

case "$ARCH" in
  x86_64|amd64) ARCH_LABEL="x86_64" ;;
  aarch64|arm64) ARCH_LABEL="aarch64" ;;
  *)             fail "Unsupported architecture: $ARCH" ;;
esac

info "Detected $OS_LABEL ($ARCH_LABEL)"

# ── Detect Node.js version manager ──────────────────────────────

NODE_MGR="none"
NEED_RESHIM=false

if command -v asdf > /dev/null 2>&1 && asdf plugin list 2>/dev/null | grep -q nodejs; then
  NODE_MGR="asdf"
elif [ -n "${NVM_DIR:-}" ] && [ -s "${NVM_DIR}/nvm.sh" ]; then
  NODE_MGR="nvm"
elif command -v fnm > /dev/null 2>&1; then
  NODE_MGR="fnm"
elif command -v brew > /dev/null 2>&1 && [ "$OS" = "Darwin" ]; then
  NODE_MGR="brew"
elif [ "$OS" = "Linux" ]; then
  NODE_MGR="nodesource"
fi

info "Node.js manager: $NODE_MGR"

version_major() {
  printf '%s\n' "${1#v}" | cut -d. -f1
}

ensure_supported_runtime() {
  command -v node > /dev/null 2>&1 || fail "${RUNTIME_REQUIREMENT_MSG} Node.js was not found on PATH."
  command -v npm > /dev/null 2>&1 || fail "${RUNTIME_REQUIREMENT_MSG} npm was not found on PATH."

  local node_version npm_version node_major npm_major
  node_version="$(node -v 2>/dev/null || true)"
  npm_version="$(npm --version 2>/dev/null || true)"
  node_major="$(version_major "$node_version")"
  npm_major="$(version_major "$npm_version")"

  [[ "$node_major" =~ ^[0-9]+$ ]] || fail "Could not determine Node.js version from '${node_version}'. ${RUNTIME_REQUIREMENT_MSG}"
  [[ "$npm_major" =~ ^[0-9]+$ ]] || fail "Could not determine npm version from '${npm_version}'. ${RUNTIME_REQUIREMENT_MSG}"

  if (( node_major < MIN_NODE_MAJOR || npm_major < MIN_NPM_MAJOR )); then
    fail "Unsupported runtime detected: Node.js ${node_version:-unknown}, npm ${npm_version:-unknown}. ${RUNTIME_REQUIREMENT_MSG} Upgrade Node.js and rerun the installer."
  fi

  info "Runtime OK: Node.js ${node_version}, npm ${npm_version}"
}

# ── Install Node.js 22 if needed ────────────────────────────────

install_node() {
  local current_major=""
  if command -v node > /dev/null 2>&1; then
    current_major="$(node -v 2>/dev/null | sed 's/^v//' | cut -d. -f1)"
  fi

  if [ "$current_major" = "22" ]; then
    info "Node.js 22 already installed: $(node -v)"
    return 0
  fi

  info "Installing Node.js 22..."

  case "$NODE_MGR" in
    asdf)
      local latest_22
      latest_22="$(asdf list all nodejs 2>/dev/null | grep '^22\.' | tail -1)"
      [ -n "$latest_22" ] || fail "Could not find Node.js 22 in asdf"
      asdf install nodejs "$latest_22"
      asdf global nodejs "$latest_22"
      NEED_RESHIM=true
      ;;
    nvm)
      # shellcheck source=/dev/null
      . "${NVM_DIR}/nvm.sh"
      nvm install 22
      nvm use 22
      nvm alias default 22
      ;;
    fnm)
      fnm install 22
      fnm use 22
      fnm default 22
      ;;
    brew)
      brew install node@22
      brew link --overwrite node@22 2>/dev/null || true
      ;;
    nodesource)
      curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash - > /dev/null 2>&1
      sudo apt-get install -y -qq nodejs > /dev/null 2>&1
      ;;
    none)
      fail "No Node.js version manager found. Install Node.js 22 manually, then re-run."
      ;;
  esac

  info "Node.js $(node -v) installed"
}

install_node
ensure_supported_runtime

# ── Install Docker ───────────────────────────────────────────────

install_docker() {
  if command -v docker > /dev/null 2>&1 && docker info > /dev/null 2>&1; then
    info "Docker already running"
    return 0
  fi

  if command -v docker > /dev/null 2>&1; then
    # Docker installed but not running
    if [ "$OS" = "Darwin" ]; then
      if command -v colima > /dev/null 2>&1; then
        info "Starting Colima..."
        colima start
        return 0
      fi
    fi
    fail "Docker is installed but not running. Please start Docker and re-run."
  fi

  info "Installing Docker..."

  case "$OS" in
    Darwin)
      if ! command -v brew > /dev/null 2>&1; then
        fail "Homebrew required to install Docker on macOS. Install from https://brew.sh"
      fi
      info "Installing Colima + Docker CLI via Homebrew..."
      brew install colima docker
      info "Starting Colima..."
      colima start
      ;;
    Linux)
      sudo apt-get update -qq > /dev/null 2>&1
      sudo apt-get install -y -qq docker.io > /dev/null 2>&1
      sudo usermod -aG docker "$(whoami)"
      info "Docker installed. You may need to log out and back in for group changes."
      ;;
  esac

  if ! docker info > /dev/null 2>&1; then
    fail "Docker installed but not running. Start Docker and re-run."
  fi

  info "Docker is running"
}

install_docker

# ── Install OpenShell CLI binary ─────────────────────────────────

install_openshell() {
  if command -v openshell > /dev/null 2>&1; then
    info "openshell already installed: $(openshell --version 2>&1 || echo 'unknown')"
    return 0
  fi

  info "Installing openshell CLI..."

  case "$OS" in
    Darwin)
      case "$ARCH_LABEL" in
        x86_64)  ASSET="openshell-x86_64-apple-darwin.tar.gz" ;;
        aarch64) ASSET="openshell-aarch64-apple-darwin.tar.gz" ;;
      esac
      ;;
    Linux)
      case "$ARCH_LABEL" in
        x86_64)  ASSET="openshell-x86_64-unknown-linux-musl.tar.gz" ;;
        aarch64) ASSET="openshell-aarch64-unknown-linux-musl.tar.gz" ;;
      esac
      ;;
  esac

  tmpdir="$(mktemp -d)"
  if command -v gh > /dev/null 2>&1; then
    GH_TOKEN="${GITHUB_TOKEN:-}" gh release download --repo NVIDIA/OpenShell \
      --pattern "$ASSET" --dir "$tmpdir"
  else
    # Fallback: curl latest release
    curl -fsSL "https://github.com/NVIDIA/OpenShell/releases/latest/download/$ASSET" \
      -o "$tmpdir/$ASSET"
  fi

  tar xzf "$tmpdir/$ASSET" -C "$tmpdir"

  if [ -w /usr/local/bin ]; then
    install -m 755 "$tmpdir/openshell" /usr/local/bin/openshell
  else
    sudo install -m 755 "$tmpdir/openshell" /usr/local/bin/openshell
  fi

  rm -rf "$tmpdir"
  info "openshell $(openshell --version 2>&1 || echo '') installed"
}

install_openshell

# ── Install IniClaw CLI ─────────────────────────────────────────

info "Installing iniclaw CLI..."
npm install -g iniclaw

if [ "$NEED_RESHIM" = true ]; then
  info "Reshimming asdf..."
  asdf reshim nodejs
fi

# ── Verify ───────────────────────────────────────────────────────

if ! command -v iniclaw > /dev/null 2>&1; then
  fail "iniclaw not found in PATH after install. Check your Node.js bin directory."
fi

echo ""
info "Installation complete!"
info "iniclaw $(iniclaw --version 2>/dev/null || echo 'v0.1.0') is ready."
echo ""
echo "  Run \`iniclaw setup\` to get started"
echo ""