返回 CodeWhale
install.sh
根目录 / web / public / install.sh
1 #!/bin/sh
2 set -eu
3
4 repo="codewhale-hq/CodeWhale"
5 version="${CODEWHALE_VERSION:-latest}"
6 release_base="${CODEWHALE_RELEASE_BASE_URL:-${DEEPSEEK_TUI_RELEASE_BASE_URL:-}}"
7
8 usage() {
9 cat <<'USAGE'
10 Codewhale GitHub release installer for macOS and Linux.
11 It also upgrades an earlier Codewhale in the install directory; the old files
12 move to ~/.codewhale/recovery and are kept for 30 days.
13
14 Usage:
15 curl -fsSL https://codewhale.net/install.sh | sh
16
17 Options:
18 --no-modify-path Do not add the install directory to your shell profile.
19 By default, when the directory is not on PATH, the
20 installer appends a marked three-line block to your
21 shell profile, prints it, and prints the command that
22 removes it. Also: CODEWHALE_NO_MODIFY_PATH=1
23 -h, --help Show this help
24
25 Environment:
26 CODEWHALE_INSTALL_DIR Install directory. Default: $HOME/.local/bin
27 CODEWHALE_VERSION Release tag for a fresh directory. Default: latest
28 CODEWHALE_FORCE_REINSTALL=1
29 Also replace a codewhale/codew you own that is not a
30 Codewhale release binary (moved to recovery, not deleted)
31 CODEWHALE_RELEASE_BASE_URL
32 Custom release asset base URL ending in /download
33 CODEWHALE_INSTALL_COMPILED_HOST=1
34 Also install a qualified optional Bun image + notices/source
35 Node remains the default; missing eligibility fails loudly
36 CODEWHALE_SKIP_GLIBC_CHECK=1
37 Skip Linux arm64 glibc compatibility preflight
38
39 Examples:
40 curl -fsSL https://codewhale.net/install.sh | CODEWHALE_INSTALL_DIR="$HOME/.local/codewhale/bin" sh
41 curl -fsSL https://codewhale.net/install.sh | CODEWHALE_VERSION=vX.Y.Z sh
42 curl -fsSL https://codewhale.net/install.sh | sh -s -- --no-modify-path
43 USAGE
44 }
45
46 modify_path=1
47 case "${CODEWHALE_NO_MODIFY_PATH:-}" in
48 1|true|yes) modify_path=0 ;;
49 esac
50 for arg in "$@"; do
51 case "$arg" in
52 -h|--help)
53 usage
54 exit 0
55 ;;
56 --no-modify-path) modify_path=0 ;;
57 *)
58 printf 'codewhale install: unknown option: %s (see --help)\n' "$arg" >&2
59 exit 2
60 ;;
61 esac
62 done
63
64 say() {
65 printf '%s\n' "$*"
66 }
67
68 fail() {
69 printf 'codewhale install: %s\n' "$*" >&2
70 exit 1
71 }
72
73 if [ -n "${CODEWHALE_INSTALL_DIR:-}" ]; then
74 install_dir="$CODEWHALE_INSTALL_DIR"
75 else
76 [ -n "${HOME:-}" ] || fail "HOME is not set; set CODEWHALE_INSTALL_DIR"
77 install_dir="$HOME/.local/bin"
78 fi
79
80 need_cmd() {
81 command -v "$1" >/dev/null 2>&1 || fail "missing required command: $1"
82 }
83
84 download() {
85 url="$1"
86 out="$2"
87 if command -v curl >/dev/null 2>&1; then
88 download_tool=curl
89 elif command -v wget >/dev/null 2>&1; then
90 download_tool=wget
91 else
92 fail "curl or wget is required"
93 fi
94 attempt=1
95 while :; do
96 if [ "$download_tool" = curl ]; then
97 curl --proto '=https' --tlsv1.2 -fsSL "$url" -o "$out" && return 0
98 else
99 wget --https-only -q "$url" -O "$out" && return 0
100 fi
101 rm -f "$out"
102 if [ "$attempt" -ge 3 ]; then
103 fail "Download failed. This is usually a network blip; run the same command again."
104 fi
105 attempt=$((attempt + 1))
106 sleep "${CODEWHALE_INSTALL_RETRY_DELAY:-1}"
107 done
108 }
109
110 sha256_file() {
111 file="$1"
112 if command -v sha256sum >/dev/null 2>&1; then
113 sha256sum "$file" | awk '{print $1}'
114 elif command -v shasum >/dev/null 2>&1; then
115 shasum -a 256 "$file" | awk '{print $1}'
116 else
117 fail "sha256sum or shasum is required to verify downloads"
118 fi
119 }
120
121 manifest_digest() {
122 asset="$1"
123 manifest="$2"
124 awk -v name="$asset" '
125 {
126 digest = tolower($1)
127 file = $2
128 sub(/^\*/, "", file)
129 if (file == name && length(digest) == 64 && digest !~ /[^0-9a-f]/) {
130 print digest
131 exit
132 }
133 }
134 ' "$manifest"
135 }
136
137 verify_asset() {
138 asset="$1"
139 file="$2"
140 manifest="$3"
141 expected="$(manifest_digest "$asset" "$manifest")"
142 [ -n "$expected" ] || fail "checksum not found for $asset"
143 actual="$(sha256_file "$file" | tr '[:upper:]' '[:lower:]')"
144 [ "$actual" = "$expected" ] || fail "checksum mismatch for $asset"
145 }
146
147 glibc_version() {
148 if command -v getconf >/dev/null 2>&1; then
149 getconf GNU_LIBC_VERSION 2>/dev/null | awk '{ print $NF; exit }'
150 return
151 fi
152 if command -v ldd >/dev/null 2>&1; then
153 ldd --version 2>/dev/null | awk 'NR == 1 {
154 for (i = 1; i <= NF; i++) {
155 if ($i ~ /^[0-9]+\.[0-9]+/) {
156 print $i
157 exit
158 }
159 }
160 }'
161 fi
162 }
163
164 version_at_least() {
165 have="$1"
166 need="$2"
167 awk -v have="$have" -v need="$need" '
168 BEGIN {
169 split(have, h, ".")
170 split(need, n, ".")
171 for (i = 1; i <= 3; i++) {
172 hv = h[i] + 0
173 nv = n[i] + 0
174 if (hv > nv) exit 0
175 if (hv < nv) exit 1
176 }
177 exit 0
178 }
179 '
180 }
181
182 check_glibc() {
183 case "$target" in
184 linux-arm64) ;;
185 *) return ;;
186 esac
187
188 # Linux arm64 assets became static musl builds in v0.9.6. `latest` and
189 # explicit v0.9.6+ installs therefore have no glibc floor. Keep the
190 # preflight only for explicitly requested older releases, whose arm64
191 # assets were linked against GNU libc on Ubuntu 24.04.
192 if [ "$version" = "latest" ]; then
193 return
194 fi
195 numeric_version="${version#v}"
196 if awk -v have="$numeric_version" '
197 BEGIN {
198 if (have !~ /^[0-9]+\.[0-9]+\.[0-9]+$/) exit 1
199 split(have, h, ".")
200 if (h[1] > 0) exit 0
201 if (h[1] < 0) exit 1
202 if (h[2] > 9) exit 0
203 if (h[2] < 9) exit 1
204 exit !(h[3] >= 6)
205 }
206 '; then
207 return
208 fi
209
210 [ "${CODEWHALE_SKIP_GLIBC_CHECK:-}" = "1" ] && return
211 [ "${DEEPSEEK_TUI_SKIP_GLIBC_CHECK:-}" = "1" ] && return
212 [ "${DEEPSEEK_SKIP_GLIBC_CHECK:-}" = "1" ] && return
213
214 required="2.39"
215 host="$(glibc_version || true)"
216 if [ -z "$host" ] || ! version_at_least "$host" "$required"; then
217 cat >&2 <<EOF
218 codewhale install: Codewhale $version $target assets require glibc $required or newer.
219 This system reports glibc ${host:-unavailable}.
220
221 Linux arm64 assets before v0.9.6 were GNU libc builds from Ubuntu 24.04.
222 Current v0.9.6+ assets are static musl builds. Build this older release from
223 source with Cargo or set
224 CODEWHALE_SKIP_GLIBC_CHECK=1 to bypass this check at your own risk.
225 EOF
226 exit 1
227 fi
228 }
229
230 detect_platform() {
231 os="$(uname -s)"
232 arch="$(uname -m)"
233
234 if [ -n "${TERMUX_VERSION:-}" ] || [ "$(uname -o 2>/dev/null || true)" = "Android" ]; then
235 fail "Android/Termux needs the Android arm64 preview archive, not a Linux binary. See https://github.com/codewhale-hq/CodeWhale/blob/main/docs/INSTALL.md"
236 fi
237
238 case "$os" in
239 Darwin) platform="macos" ;;
240 Linux) platform="linux" ;;
241 *) fail "unsupported OS: $os. Use the matching asset at https://github.com/codewhale-hq/CodeWhale/releases/latest; npm and Cargo are secondary options." ;;
242 esac
243
244 case "$arch" in
245 x86_64|amd64) cpu="x64" ;;
246 arm64|aarch64) cpu="arm64" ;;
247 riscv64) fail "Linux riscv64 prebuilt assets are temporarily unavailable because the locked rquickjs-sys dependency does not ship riscv64gc bindings." ;;
248 *) fail "unsupported CPU architecture: $arch. Use Cargo or build from source." ;;
249 esac
250
251 printf '%s-%s' "$platform" "$cpu"
252 }
253
254 if [ -z "$release_base" ]; then
255 if [ "$version" = "latest" ]; then
256 release_base="https://github.com/$repo/releases/latest/download"
257 else
258 release_base="https://github.com/$repo/releases/download/$version"
259 fi
260 fi
261
262 target="$(detect_platform)"
263 check_glibc
264 cli_asset="codewhale-$target"
265 shim_asset="codew-$target"
266 manifest_asset="codewhale-artifacts-sha256.txt"
267
268 # Resolve the real directory before applying managed-prefix checks. Never use
269 # sudo or allow an install directory symlink to obscure which files will change.
270 case "$install_dir" in
271 /*) ;;
272 *) fail "CODEWHALE_INSTALL_DIR must be an absolute path" ;;
273 esac
274 case "/$install_dir/" in
275 */./*|*/../*) fail "CODEWHALE_INSTALL_DIR must not contain . or .. path components" ;;
276 esac
277 while [ "$install_dir" != / ] && [ "${install_dir%/}" != "$install_dir" ]; do install_dir="${install_dir%/}"; done
278 [ ! -L "$install_dir" ] || fail "install directory is a symlink: $install_dir; choose a fresh user directory"
279 preflight_dir="$install_dir"
280 install_suffix=""
281 while [ ! -d "$preflight_dir" ]; do
282 { [ ! -e "$preflight_dir" ] && [ ! -L "$preflight_dir" ]; } || fail "not a directory: $preflight_dir"
283 install_suffix="/$(basename "$preflight_dir")$install_suffix"
284 preflight_dir="$(dirname "$preflight_dir")"
285 done
286 preflight_dir="$(cd -P "$preflight_dir" && pwd)"
287 install_dir="$preflight_dir$install_suffix"
288 case "$install_dir/" in
289 /bin/*|/sbin/*|/usr/bin/*|/usr/sbin/*|/nix/store/*|/gnu/store/*|*/node_modules/*|*/Cellar/*|*/.linuxbrew/*|*/linuxbrew/*|*/.cargo/bin/*|/opt/homebrew/bin/*|/usr/local/bin/*|/opt/local/bin/*)
290 fail "refusing managed/system directory $install_dir; use a fresh user directory"
291 ;;
292 esac
293 if [ "$install_dir" = / ] || { [ -n "${HOME:-}" ] && [ "$install_dir" = "$(cd -P "$HOME" 2>/dev/null && pwd)" ]; }; then
294 fail "refusing $install_dir; use a fresh directory such as \$HOME/.local/bin"
295 fi
296 [ -O "$preflight_dir" ] && [ -w "$preflight_dir" ] || fail "$preflight_dir must be a writable directory owned by you (no sudo is used)"
297 if [ -n "$(find "$preflight_dir" -prune \( -perm -020 -o -perm -002 \) -print)" ]; then
298 fail "$preflight_dir is writable by its group or others; run chmod go-w '$preflight_dir' or choose a directory only you can write"
299 fi
300 for cargo_receipt in "$(dirname "$install_dir")/.crates.toml" "$(dirname "$install_dir")/.crates2.json"; do
301 { [ -e "$cargo_receipt" ] || [ -L "$cargo_receipt" ]; } || continue
302 cargo_owned=0
303 if [ -L "$cargo_receipt" ] || [ ! -f "$cargo_receipt" ] || [ ! -r "$cargo_receipt" ]; then
304 cargo_owned=1
305 else
306 cargo_size="$(wc -c < "$cargo_receipt" 2>/dev/null | tr -d ' ')"
307 case "$cargo_size" in
308 ''|*[!0-9]*) cargo_owned=1 ;;
309 *) [ "$cargo_size" -le 4194304 ] || cargo_owned=1 ;;
310 esac
311 cargo_grep=0
312 grep -F -q -e '"codewhale"' -e '"codew"' "$cargo_receipt" 2>/dev/null || cargo_grep=$?
313 [ "$cargo_grep" -eq 1 ] || cargo_owned=1
314 fi
315 if [ "$cargo_owned" -eq 1 ]; then
316 fail "Cargo installed Codewhale beside this directory, or its record $cargo_receipt cannot be read; manage it with cargo install or cargo uninstall, or use a fresh directory instead"
317 fi
318 done
319
320 (umask 077; mkdir -p "$install_dir") || fail "cannot create $install_dir; choose a writable user directory (no sudo is used)"
321 [ ! -L "$install_dir" ] || fail "install directory changed to a symlink: $install_dir"
322 install_dir="$(cd -P "$install_dir" && pwd)"
323
324 tmpdir="$(mktemp -d 2>/dev/null || mktemp -d -t codewhale-install)"
325 trap 'rm -rf "$tmpdir"' EXIT INT TERM
326
327 forced_backups="$tmpdir/.forced-backups"
328 : > "$forced_backups"
329
330 if [ -n "${CODEWHALE_HOME:-}" ]; then
331 case "$CODEWHALE_HOME" in
332 /*) recovery_root="$CODEWHALE_HOME/recovery" ;;
333 *) recovery_root="${HOME:-}/.codewhale/recovery" ;;
334 esac
335 else
336 recovery_root="${HOME:-}/.codewhale/recovery"
337 fi
338 recovery_dir=""
339 recovery_seq=0
340 open_recovery() {
341 [ -z "$recovery_dir" ] || return 0
342 case "$recovery_root" in /*) ;; *) fail "HOME is not set; cannot keep the previous files" ;; esac
343 mkdir -p "$recovery_root" || fail "cannot create $recovery_root"
344 chmod 700 "$recovery_root" 2>/dev/null || true
345 stamp="$(date -u +%Y%m%dT%H%M%SZ)"
346 candidate="$stamp"
347 attempt=1
348 while ! mkdir "$recovery_root/$candidate" 2>/dev/null; do
349 [ "$attempt" -lt 100 ] || fail "cannot create a recovery folder under $recovery_root"
350 candidate="$stamp-$attempt"
351 attempt=$((attempt + 1))
352 done
353 recovery_dir="$recovery_root/$candidate"
354 }
355 is_codewhale_binary() {
356 [ -f "$1" ] && [ ! -L "$1" ] && grep -aq -e 'codewhale-updater' -e 'deepseek-tui' "$1" 2>/dev/null
357 }
358 move_to_recovery() {
359 file="$1"
360 open_recovery
361 recovery_seq=$((recovery_seq + 1))
362 stored="$recovery_dir/$(printf '%03d' "$recovery_seq")-$(basename "$file")"
363 digest="$(sha256_file "$file" | tr '[:upper:]' '[:lower:]')"
364 size="$(wc -c < "$file" | tr -d ' ')"
365 mv "$file" "$stored" || fail "cannot move $file to the recovery folder; it was not replaced"
366 printf '%s\t%s\t%s\t%s\n' "$file" "$stored" "$digest" "$size" >> "$recovery_dir/manifest.tsv"
367 printf '%s\t%s\n' "$stored" "$file" >> "$forced_backups"
368 }
369
370 stage=""
371 stage_dir=""
372 # Commands this run published. If a later publication fails they are removed
373 # again, but only while each is still the exact file this run wrote, so a
374 # failed install leaves no half-installed pair and never touches a file that
375 # was already installed.
376 published="$tmpdir/.published"
377 : > "$published"
378 rollback_published() {
379 while IFS= read -r name; do
380 destination="$install_dir/$name"
381 if [ ! -L "$destination" ] && [ -f "$destination" ] && cmp -s "$tmpdir/$name" "$destination"; then
382 rm -f "$destination"
383 say "Removed $destination: this install did not complete." >&2
384 fi
385 done < "$published"
386 }
387 restore_forced_backups() {
388 [ -f "$forced_backups" ] || return 0
389 tab="$(printf '\t')"
390 while IFS="$tab" read -r backup destination; do
391 [ -n "$backup" ] && [ -n "$destination" ] || continue
392 [ -e "$backup" ] || continue
393 if [ ! -e "$destination" ] && [ ! -L "$destination" ]; then
394 mv "$backup" "$destination" && say "Restored $destination: this install did not complete." >&2
395 fi
396 done < "$forced_backups"
397 }
398 on_exit() {
399 status=$?
400 if [ -n "$stage" ]; then rm -f "$stage"; fi
401 if [ -n "$stage_dir" ]; then rmdir "$stage_dir"; fi
402 if [ "$status" -ne 0 ]; then
403 rollback_published
404 restore_forced_backups
405 fi
406 rm -rf "$tmpdir"
407 }
408 trap on_exit EXIT
409 trap 'exit 130' INT
410 trap 'exit 143' TERM
411
412 say "Installing Codewhale for $target"
413 say "Release assets: $release_base"
414 say "Install dir: $install_dir"
415
416 download "$release_base/$manifest_asset" "$tmpdir/$manifest_asset"
417 download "$release_base/$cli_asset" "$tmpdir/codewhale"
418 cli_digest="$(manifest_digest "$cli_asset" "$tmpdir/$manifest_asset")"
419 shim_digest="$(manifest_digest "$shim_asset" "$tmpdir/$manifest_asset")"
420 if [ -n "$cli_digest" ] && [ "$cli_digest" = "$shim_digest" ]; then
421 if ! ln "$tmpdir/codewhale" "$tmpdir/codew" 2>/dev/null; then
422 cp "$tmpdir/codewhale" "$tmpdir/codew"
423 fi
424 else
425 download "$release_base/$shim_asset" "$tmpdir/codew"
426 fi
427
428 verify_asset "$cli_asset" "$tmpdir/codewhale" "$tmpdir/$manifest_asset"
429 verify_asset "$shim_asset" "$tmpdir/codew" "$tmpdir/$manifest_asset"
430 say "Checksums verified"
431
432 chmod 755 "$tmpdir/codewhale" "$tmpdir/codew"
433 if command -v xattr >/dev/null 2>&1; then
434 xattr -d com.apple.quarantine "$tmpdir/codewhale" "$tmpdir/codew" 2>/dev/null || true
435 fi
436
437 check_destination() {
438 destination="$1"
439 source="$2"
440 mode="${3:-755}"
441 destination_exists=0
442 if [ -e "$destination" ] || [ -L "$destination" ]; then
443 if [ ! -L "$destination" ] && [ -f "$destination" ] && { [ "$mode" != 755 ] || [ -x "$destination" ]; } && cmp -s "$source" "$destination"; then
444 destination_exists=1
445 return
446 fi
447 upgradable=0
448 if [ ! -L "$destination" ] && [ -f "$destination" ] && [ -O "$destination" ]; then
449 case "$(basename "$destination")" in
450 codewhale|codew) is_codewhale_binary "$destination" && upgradable=1 ;;
451 .codewhale-install.sha256) upgradable=1 ;;
452 esac
453 [ "${CODEWHALE_FORCE_REINSTALL:-}" != 1 ] || upgradable=1
454 fi
455 if [ "$upgradable" -eq 1 ]; then
456 move_to_recovery "$destination"
457 say "Upgrading $destination (previous file kept in $recovery_dir for 30 days)"
458 return
459 fi
460 cat >&2 <<EOF
461 codewhale install: $destination is not a Codewhale release file this installer can upgrade
462 (it is a symlink, belongs to another user, or is a different program). It was not changed.
463 To install beside it, choose a fresh directory:
464 curl -fsSL https://codewhale.net/install.sh | CODEWHALE_INSTALL_DIR="\$HOME/.local/codewhale/bin" sh
465 To move a regular file you own aside and install anyway (it goes to ~/.codewhale/recovery, not deleted):
466 curl -fsSL https://codewhale.net/install.sh | CODEWHALE_FORCE_REINSTALL=1 sh
467 EOF
468 exit 1
469 fi
470 }
471
472 # Check every command before publishing any of them. Existing identical release
473 # files are an idempotent install; anything different uses the canonical updater.
474 # An enabled companion must exist in this same checksummed release. No
475 # installer fetches a Bun runtime or invents Android/musl/cross-arch support.
476 if [ "${CODEWHALE_INSTALL_COMPILED_HOST:-}" = 1 ]; then
477 host_asset="codewhale-extension-host-$target"
478 for companion in "$host_asset" "$host_asset-LICENSES.txt" "$host_asset-relink-source.tar.gz" codewhale-extension-hosts.json; do
479 download "$release_base/$companion" "$tmpdir/$companion"
480 verify_asset "$companion" "$tmpdir/$companion" "$tmpdir/codewhale-artifacts-sha256.txt"
481 done
482 mv "$tmpdir/$host_asset" "$tmpdir/codewhale-extension-host"
483 mv "$tmpdir/$host_asset-LICENSES.txt" "$tmpdir/codewhale-extension-host.LICENSES.txt"
484 mv "$tmpdir/$host_asset-relink-source.tar.gz" "$tmpdir/codewhale-extension-host.relink-source.tar.gz"
485 mv "$tmpdir/codewhale-extension-hosts.json" "$tmpdir/codewhale-extension-host.release.json"
486 if [ "$target" = linux-x64 ] || [ "$target" = linux-arm64 ]; then
487 required="$(grep -aoE 'GLIBC_[0-9]+\.[0-9]+(\.[0-9]+)?' "$tmpdir/codewhale-extension-host" 2>/dev/null | sed 's/GLIBC_//' | awk -F. '{ code=$1*1000000+$2*1000+$3; if(code>best){best=code; value=$0} } END {print value}' || true)"
488 if [ -n "$required" ]; then
489 available="$(glibc_version || true)"
490 [ -n "$available" ] && version_at_least "$available" "$required" || fail "optional Bun host requires GLIBC_$required; CLI remains static musl. Use Node on this installation."
491 fi
492 fi
493 for companion in codewhale-extension-host codewhale-extension-host.LICENSES.txt codewhale-extension-host.relink-source.tar.gz codewhale-extension-host.release.json; do
494 mode=644
495 [ "$companion" != codewhale-extension-host ] || mode=755
496 check_destination "$install_dir/$companion" "$tmpdir/$companion" "$mode"
497 done
498 fi
499
500 receipt_name=".codewhale-install.sha256"
501 for command_name in codewhale codew; do
502 command_digest="$(sha256_file "$tmpdir/$command_name" | tr '[:upper:]' '[:lower:]')"
503 [ "${#command_digest}" -eq 64 ] || fail "invalid command digest for $command_name"
504 case "$command_digest" in *[!0-9a-f]*) fail "invalid command digest for $command_name" ;; esac
505 printf '%s %s\n' "$command_digest" "$command_name"
506 done > "$tmpdir/$receipt_name"
507 check_destination "$install_dir/$receipt_name" "$tmpdir/$receipt_name" 644
508 check_destination "$install_dir/codewhale" "$tmpdir/codewhale"
509 check_destination "$install_dir/codew" "$tmpdir/codew"
510
511 install_binary() {
512 source="$1"
513 destination="$2"
514 # Recheck immediately before publication. Never replace a file another
515 # process created since preflight: linking the staged inode is no-clobber.
516 check_destination "$destination" "$source" "${3:-755}"
517 if [ "$destination_exists" -eq 1 ]; then
518 say "Already installed: $destination"
519 return
520 fi
521 stage_dir="$(mktemp -d "$install_dir/.codewhale-install.XXXXXX")"
522 stage="$stage_dir/$(basename "$destination")"
523 if [ "$(basename "$destination")" = "codew" ] \
524 && [ ! -L "$install_dir/codewhale" ] \
525 && [ -f "$install_dir/codewhale" ] \
526 && [ -x "$install_dir/codewhale" ] \
527 && cmp -s "$source" "$install_dir/codewhale"; then
528 if ! ln "$install_dir/codewhale" "$stage" 2>/dev/null; then
529 rm -f "$stage"
530 cp "$source" "$stage"
531 chmod "${3:-755}" "$stage"
532 fi
533 else
534 cp "$source" "$stage"
535 chmod "${3:-755}" "$stage"
536 fi
537 # Pass the intended parent as the directory operand. Passing destination
538 # itself would make ln treat a raced-in directory/symlink as a container.
539 ln "$stage" "$install_dir/" || fail "destination appeared during install: $destination; it was not replaced"
540 [ ! -L "$destination" ] && [ -f "$destination" ] && cmp -s "$stage" "$destination" || fail "installed path changed during publication: $destination"
541 basename "$destination" >> "$published"
542 rm -f "$stage"
543 rmdir "$stage_dir"
544 stage=""
545 stage_dir=""
546 }
547
548 install_binary "$tmpdir/codewhale" "$install_dir/codewhale"
549 install_binary "$tmpdir/codew" "$install_dir/codew"
550 if [ "${CODEWHALE_INSTALL_COMPILED_HOST:-}" = 1 ]; then
551 install_binary "$tmpdir/codewhale-extension-host" "$install_dir/codewhale-extension-host"
552 for companion in codewhale-extension-host.LICENSES.txt codewhale-extension-host.relink-source.tar.gz codewhale-extension-host.release.json; do
553 install_binary "$tmpdir/$companion" "$install_dir/$companion" 644
554 done
555 say "Installed qualified opt-in image and its notice/relink-source closure; Node remains default."
556 fi
557
558 install_binary "$tmpdir/$receipt_name" "$install_dir/$receipt_name" 644
559 supports_uninstall=0
560 installed_help="$("$install_dir/codewhale" --help </dev/null 2>/dev/null || true)"
561 if printf '%s\n' "$installed_help" | grep -E '^[[:space:]]+uninstall([[:space:]]|$)' >/dev/null; then
562 supports_uninstall=1
563 fi
564 if [ "$supports_uninstall" -eq 1 ]; then
565 say "Uninstall this receipted copy: \"$install_dir/codewhale\" uninstall --yes"
566 else
567 say "Uninstall: this release has no uninstall command. Remove the files: rm \"$install_dir/codewhale\" \"$install_dir/codew\" \"$install_dir/.codewhale-install.sha256\""
568 say "Your data in ~/.codewhale is separate; delete it only if you want it gone."
569 fi
570 say "Installed checksummed release commands:"
571 say " $install_dir/codewhale"
572 say " $install_dir/codew"
573
574 if [ -n "$recovery_dir" ]; then
575 say "Previous files are kept in $recovery_dir for 30 days. Undo: \"$install_dir/codewhale\" doctor --restore $(basename "$recovery_dir")"
576 fi
577 CODEWHALE_TELEMETRY=0 CODEWHALE_NO_UPDATE_CHECK=1 "$install_dir/codewhale" doctor --installs --brief </dev/null 2>/dev/null || true
578 say "Usage reporting is on by default (counts only, never your code or conversations). Turn it off: \"$install_dir/codewhale\" config set telemetry false"
579
580 say ""
581 say "Use this installation: \"$install_dir/codewhale\""
582 say "Future updates: \"$install_dir/codewhale\" update"
583 path_selected=1
584 for command_name in codewhale codew; do
585 resolved="$(command -v "$command_name" 2>/dev/null || true)"
586 if [ "$resolved" != "$install_dir/$command_name" ]; then
587 say "PATH selects ${resolved:-no $command_name command}; this install is $install_dir/$command_name"
588 path_selected=0
589 fi
590 done
591 path_marker_start="# >>> codewhale PATH >>>"
592 path_marker_end="# <<< codewhale PATH <<<"
593
594 install_dir_on_path() {
595 case ":${PATH:-}:" in
596 *":$install_dir:"*) return 0 ;;
597 esac
598 return 1
599 }
600
601 pick_profile() {
602 profile_file=""
603 profile_shown=""
604 [ -n "${HOME:-}" ] || return 0
605 case "$1" in
606 zsh)
607 if [ -n "${ZDOTDIR:-}" ]; then
608 case "$ZDOTDIR" in
609 /*) ;;
610 *) return 0 ;;
611 esac
612 case "$ZDOTDIR" in
613 *[!A-Za-z0-9_./+-]*) return 0 ;;
614 esac
615 profile_file="$ZDOTDIR/.zshrc"
616 profile_shown="\"$profile_file\""
617 return 0
618 fi
619 profile=".zshrc"
620 ;;
621 bash)
622 case "$target" in
623 macos-*)
624 profile=".bash_profile"
625 for candidate in .bash_profile .bash_login .profile; do
626 if [ -e "$HOME/$candidate" ]; then
627 profile="$candidate"
628 break
629 fi
630 done
631 ;;
632 *) profile=".bashrc" ;;
633 esac
634 ;;
635 *) profile=".profile" ;;
636 esac
637 profile_file="$HOME/$profile"
638 profile_shown="~/$profile"
639 }
640
641 add_path_block() {
642 path_note=""
643 path_state=added
644 if [ -L "$profile_file" ]; then
645 path_note="$profile_shown is a symlink"
646 return 1
647 fi
648 lead_newline=0
649 if [ -e "$profile_file" ]; then
650 if [ ! -f "$profile_file" ] || [ ! -w "$profile_file" ]; then
651 path_note="$profile_shown is not a writable regular file"
652 return 1
653 fi
654 if grep -qxF "$path_marker_start" "$profile_file" 2>/dev/null; then
655 path_state=present
656 return 0
657 fi
658 if [ -s "$profile_file" ] && [ -n "$(tail -c 1 "$profile_file" 2>/dev/null)" ]; then
659 lead_newline=1
660 fi
661 else
662 profile_parent="$(dirname "$profile_file")"
663 if [ ! -d "$profile_parent" ] || [ ! -w "$profile_parent" ]; then
664 path_note="the folder for $profile_shown is missing or not writable"
665 return 1
666 fi
667 fi
668 if ! {
669 if [ "$lead_newline" -eq 1 ]; then printf '\n'; fi
670 printf '%s\n' "$path_marker_start" "export PATH=\"$path_dir:\$PATH\"" "$path_marker_end"
671 } >> "$profile_file" 2>/dev/null; then
672 path_note="could not write to $profile_shown"
673 return 1
674 fi
675 return 0
676 }
677
678 undo_path_block_command() {
679 printf '%s' "cw_tmp=\"\$(mktemp)\" && sed -e '/^# >>> codewhale PATH >>>\$/{\$!N;\$!N;/\\n# <<< codewhale PATH <<<\$/d;}' $profile_shown > \"\$cw_tmp\" && cat \"\$cw_tmp\" > $profile_shown; rm -f \"\$cw_tmp\""
680 }
681
682 if [ "$path_selected" -eq 1 ]; then
683 say ""
684 say "Next: codewhale --version"
685 else
686 path_dir="$install_dir"
687 if [ -n "${HOME:-}" ] && [ "$install_dir" = "$(cd -P "$HOME/.local/bin" 2>/dev/null && pwd)" ]; then
688 path_dir="\$HOME/.local/bin"
689 fi
690 shell_name="${SHELL:-}"
691 shell_name="${shell_name##*/}"
692 case "$path_dir" in
693 *[\'\"\`\\\$]*|*"
694 "*)
695 # Only the literal $HOME form may carry a shell-special character. Any
696 # other one would break the printed quoting, or run as a command on
697 # every shell start once the line is in a profile.
698 if [ "$path_dir" != "\$HOME/.local/bin" ]; then
699 shell_name="unsafe-path"
700 fi
701 ;;
702 esac
703 say ""
704 if [ -z "$(command -v codewhale 2>/dev/null || true)" ]; then
705 say "codewhale is not on your PATH in this shell yet."
706 else
707 say "A different codewhale comes first on your PATH in this shell."
708 fi
709 case "$shell_name" in
710 fish)
711 say "This installer does not edit fish's configuration. Put $install_dir first on PATH in future shells (run once):"
712 say " fish_add_path \"$path_dir\""
713 say "It takes effect in this fish shell and in new ones (fish 3.2 or newer)."
714 say "PATH help: https://github.com/codewhale-hq/CodeWhale/blob/main/docs/INSTALL.md#put-it-on-your-path"
715 say ""
716 say "Then check it:"
717 say " codewhale --version"
718 ;;
719 zsh|bash|sh|dash|ksh|mksh|ash)
720 pick_profile "$shell_name"
721 persist_line="printf '%s\\n' '$path_marker_start' 'export PATH=\"$path_dir:\$PATH\"' '$path_marker_end' >> $profile_shown"
722 if [ -z "$profile_file" ]; then
723 say "This installer could not pick a profile file for $shell_name, so it changed nothing. To keep it in future shells (run once):"
724 say " Add $install_dir first to PATH in your shell's startup file."
725 elif [ "$modify_path" -eq 0 ]; then
726 say "Not editing your shell profile (--no-modify-path). To keep it in future shells, run once:"
727 say " $persist_line"
728 elif install_dir_on_path; then
729 say "$install_dir is on PATH but not first, so your shell profile was not edited. To put it first in future shells, run once:"
730 say " $persist_line"
731 elif add_path_block; then
732 if [ "$path_state" = present ]; then
733 say "$profile_shown already has the codewhale PATH block; it was left as is."
734 else
735 say "Added to $profile_shown so new shells find codewhale (skip this with --no-modify-path):"
736 say " $path_marker_start"
737 say " export PATH=\"$path_dir:\$PATH\""
738 say " $path_marker_end"
739 say "Only those three lines were added; nothing else in the file was changed. Shells that are already open do not reread the profile."
740 fi
741 say "To undo, run:"
742 say " $(undo_path_block_command)"
743 if [ "$supports_uninstall" -eq 1 ]; then
744 say "The marker lines let codewhale uninstall remove exactly this block; a bare export line is yours and is never touched."
745 fi
746 else
747 say "Did not edit your shell profile: $path_note. To keep it in future shells, run once:"
748 say " $persist_line"
749 say "To undo that later, delete the three lines between and including the marker lines."
750 fi
751 say "PATH help: https://github.com/codewhale-hq/CodeWhale/blob/main/docs/INSTALL.md#put-it-on-your-path"
752 say ""
753 say "Run this now, in this shell:"
754 say " export PATH=\"$path_dir:\$PATH\"; hash -r"
755 say "Then check it:"
756 say " codewhale --version"
757 ;;
758 unsafe-path)
759 say "Add $install_dir first to PATH in your shell's startup file; its name contains shell-special characters, so no command line is printed for it and nothing was edited."
760 say "PATH help: https://github.com/codewhale-hq/CodeWhale/blob/main/docs/INSTALL.md#put-it-on-your-path"
761 ;;
762 *)
763 say "Add $install_dir first to PATH in your shell's startup file; this installer has no PATH line for ${shell_name:-an unknown shell} and edited nothing."
764 say "PATH help: https://github.com/codewhale-hq/CodeWhale/blob/main/docs/INSTALL.md#put-it-on-your-path"
765 say ""
766 say "Until then, run it by its full path:"
767 say " \"$install_dir/codewhale\" --version"
768 ;;
769 esac
770 fi
771
771 lines BASH