| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | # CodeWhale Unix installer |
| 4 | # Copies codewhale and codew to ~/.local/bin (or $PREFIX/bin) |
| 5 | |
| 6 | modify_path=1 |
| 7 | case "${CODEWHALE_NO_MODIFY_PATH:-}" in |
| 8 | 1|true|yes) modify_path=0 ;; |
| 9 | esac |
| 10 | for arg in "$@"; do |
| 11 | case "$arg" in |
| 12 | -h|--help) |
| 13 | echo "Usage: ./install.sh [--no-modify-path]" |
| 14 | echo " Installs codewhale and codew to \$PREFIX/bin (default ~/.local/bin)." |
| 15 | echo " --no-modify-path Do not add that directory to your shell profile." |
| 16 | exit 0 |
| 17 | ;; |
| 18 | --no-modify-path) modify_path=0 ;; |
| 19 | *) echo "codewhale install: unknown option: $arg (see --help)" >&2; exit 2 ;; |
| 20 | esac |
| 21 | done |
| 22 | |
| 23 | PREFIX="${PREFIX:-$HOME/.local}" |
| 24 | BIN_DIR="${PREFIX}/bin" |
| 25 | SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 26 | |
| 27 | sha256_file() { |
| 28 | if command -v sha256sum >/dev/null 2>&1; then |
| 29 | sha256sum "$1" | awk '{print $1}' |
| 30 | elif command -v shasum >/dev/null 2>&1; then |
| 31 | shasum -a 256 "$1" | awk '{print $1}' |
| 32 | else |
| 33 | echo "ERROR: sha256sum or shasum is required for the ownership receipt" >&2 |
| 34 | return 1 |
| 35 | fi |
| 36 | } |
| 37 | |
| 38 | version_code() { |
| 39 | local version="$1" |
| 40 | local major minor patch |
| 41 | IFS=. read -r major minor patch <<< "$version" |
| 42 | printf '%d%03d%03d\n' "${major:-0}" "${minor:-0}" "${patch:-0}" |
| 43 | } |
| 44 | |
| 45 | detect_host_glibc() { |
| 46 | local out |
| 47 | if out="$(getconf GNU_LIBC_VERSION 2>/dev/null)"; then |
| 48 | printf '%s\n' "$out" | awk '{print $NF; exit}' |
| 49 | return 0 |
| 50 | fi |
| 51 | if out="$(ldd --version 2>&1 | head -n 1)"; then |
| 52 | printf '%s\n' "$out" | grep -Eo '[0-9]+\.[0-9]+(\.[0-9]+)?' | head -n 1 |
| 53 | return 0 |
| 54 | fi |
| 55 | return 1 |
| 56 | } |
| 57 | |
| 58 | required_glibc_for_binary() { |
| 59 | local bin="$1" |
| 60 | local versions |
| 61 | versions="$(grep -aoE 'GLIBC_[0-9]+\.[0-9]+(\.[0-9]+)?' "$bin" 2>/dev/null | sed 's/^GLIBC_//' || true)" |
| 62 | if [[ -z "$versions" ]]; then |
| 63 | return 1 |
| 64 | fi |
| 65 | printf '%s\n' "$versions" | awk -F. ' |
| 66 | { |
| 67 | patch = ($3 == "" ? 0 : $3) |
| 68 | code = ($1 * 1000000) + ($2 * 1000) + patch |
| 69 | if (code > best) { |
| 70 | best = code |
| 71 | value = $0 |
| 72 | } |
| 73 | } |
| 74 | END { |
| 75 | if (value != "") print value |
| 76 | } |
| 77 | ' |
| 78 | } |
| 79 | |
| 80 | preflight_glibc() { |
| 81 | local bin="$1" |
| 82 | local role="${2:-CLI}" |
| 83 | if [[ "$(uname -s)" != "Linux" ]]; then |
| 84 | return 0 |
| 85 | fi |
| 86 | if [[ "${role}" != "compiled host" && ( "${CODEWHALE_SKIP_GLIBC_CHECK:-}" == "1" || "${DEEPSEEK_TUI_SKIP_GLIBC_CHECK:-}" == "1" || "${DEEPSEEK_SKIP_GLIBC_CHECK:-}" == "1" ) ]]; then |
| 87 | return 0 |
| 88 | fi |
| 89 | |
| 90 | local required |
| 91 | if ! required="$(required_glibc_for_binary "$bin")" || [[ -z "$required" ]]; then |
| 92 | return 0 |
| 93 | fi |
| 94 | |
| 95 | local host |
| 96 | if ! host="$(detect_host_glibc)" || [[ -z "$host" ]]; then |
| 97 | echo "ERROR: $(basename "$bin") requires GLIBC_$required, but no GNU libc was detected." >&2 |
| 98 | if [[ "$(basename "$bin")" == codewhale-extension-host ]]; then |
| 99 | echo "The optional compiled Bun image has a separate libc floor; use Node on this installation. The Codewhale CLI remains static musl." >&2 |
| 100 | else |
| 101 | echo "Official Codewhale Linux release assets (x64 and arm64) are static musl builds" >&2 |
| 102 | echo "with no glibc dependency, so this binary is not an official release asset." >&2 |
| 103 | fi |
| 104 | echo "Check where it came from, or build from source on this host." >&2 |
| 105 | echo "Build from source instead: cargo install codewhale-cli --locked" >&2 |
| 106 | if [[ "$role" != "compiled host" ]]; then echo "Set CODEWHALE_SKIP_GLIBC_CHECK=1 to bypass this check at your own risk." >&2; fi |
| 107 | return 1 |
| 108 | fi |
| 109 | |
| 110 | if [[ "$(version_code "$host")" -lt "$(version_code "$required")" ]]; then |
| 111 | echo "ERROR: $(basename "$bin") requires GLIBC_$required, but this system has glibc $host." >&2 |
| 112 | if [[ "$(basename "$bin")" == codewhale-extension-host ]]; then |
| 113 | echo "The optional compiled Bun image has a separate libc floor; use Node on this installation. The Codewhale CLI remains static musl." >&2 |
| 114 | else |
| 115 | echo "Official Codewhale Linux release assets (x64 and arm64) are static musl builds" >&2 |
| 116 | echo "with no glibc dependency, so this binary is not an official release asset." >&2 |
| 117 | fi |
| 118 | echo "Check where it came from, or build from source on this host." >&2 |
| 119 | echo "Build from source instead: cargo install codewhale-cli --locked" >&2 |
| 120 | if [[ "$role" != "compiled host" ]]; then echo "Set CODEWHALE_SKIP_GLIBC_CHECK=1 to bypass this check at your own risk." >&2; fi |
| 121 | return 1 |
| 122 | fi |
| 123 | } |
| 124 | |
| 125 | # This script installs an already checksum-verified archive. An earlier |
| 126 | # Codewhale in BIN_DIR is upgraded: its files move to the recovery folder |
| 127 | # (~/.codewhale/recovery, kept 30 days), never beside the commands on PATH. |
| 128 | case "$BIN_DIR" in |
| 129 | /*) ;; |
| 130 | *) echo "ERROR: PREFIX must be an absolute user path" >&2; exit 1 ;; |
| 131 | esac |
| 132 | case "/$BIN_DIR/" in |
| 133 | */./*|*/../*) echo "ERROR: PREFIX must not contain . or .. path components" >&2; exit 1 ;; |
| 134 | esac |
| 135 | [[ ! -L "$BIN_DIR" ]] || { echo "ERROR: $BIN_DIR is a symlink; choose a fresh PREFIX" >&2; exit 1; } |
| 136 | preflight_dir="$BIN_DIR" |
| 137 | install_suffix="" |
| 138 | while [[ ! -d "$preflight_dir" ]]; do |
| 139 | [[ ! -e "$preflight_dir" && ! -L "$preflight_dir" ]] || { echo "ERROR: not a directory: $preflight_dir" >&2; exit 1; } |
| 140 | install_suffix="/$(basename "$preflight_dir")$install_suffix" |
| 141 | preflight_dir="$(dirname "$preflight_dir")" |
| 142 | done |
| 143 | preflight_dir="$(cd -P "$preflight_dir" && pwd)" |
| 144 | BIN_DIR="$preflight_dir$install_suffix" |
| 145 | case "$BIN_DIR/" in |
| 146 | /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/*) |
| 147 | echo "ERROR: refusing managed/system directory $BIN_DIR; choose a fresh user PREFIX" >&2 |
| 148 | exit 1 |
| 149 | ;; |
| 150 | esac |
| 151 | [[ -O "$preflight_dir" && -w "$preflight_dir" ]] || { echo "ERROR: $preflight_dir must be a writable directory owned by you (no sudo)" >&2; exit 1; } |
| 152 | if [[ -n "$(find "$preflight_dir" -prune \( -perm -020 -o -perm -002 \) -print)" ]]; then |
| 153 | echo "ERROR: $preflight_dir is writable by its group or others; choose a private PREFIX" >&2 |
| 154 | exit 1 |
| 155 | fi |
| 156 | for cargo_receipt in "$(dirname "$BIN_DIR")/.crates.toml" "$(dirname "$BIN_DIR")/.crates2.json"; do |
| 157 | [[ -e "$cargo_receipt" || -L "$cargo_receipt" ]] || continue |
| 158 | cargo_owned=0 |
| 159 | if [[ -L "$cargo_receipt" || ! -f "$cargo_receipt" || ! -r "$cargo_receipt" ]]; then |
| 160 | cargo_owned=1 |
| 161 | else |
| 162 | cargo_size="$(wc -c < "$cargo_receipt" 2>/dev/null | tr -d ' ')" || cargo_size="" |
| 163 | case "$cargo_size" in |
| 164 | ''|*[!0-9]*) cargo_owned=1 ;; |
| 165 | *) (( cargo_size <= 4194304 )) || cargo_owned=1 ;; |
| 166 | esac |
| 167 | cargo_grep=0 |
| 168 | grep -F -q -e '"codewhale"' -e '"codew"' "$cargo_receipt" 2>/dev/null || cargo_grep=$? |
| 169 | [[ "$cargo_grep" -eq 1 ]] || cargo_owned=1 |
| 170 | fi |
| 171 | if [[ "$cargo_owned" -eq 1 ]]; then |
| 172 | echo "ERROR: Cargo installed Codewhale beside this directory, or its record $cargo_receipt cannot be read; manage it with cargo install or cargo uninstall, or choose a fresh PREFIX" >&2 |
| 173 | exit 1 |
| 174 | fi |
| 175 | done |
| 176 | |
| 177 | (umask 077; mkdir -p "$BIN_DIR") |
| 178 | [[ ! -L "$BIN_DIR" ]] || { echo "ERROR: $BIN_DIR changed to a symlink" >&2; exit 1; } |
| 179 | BIN_DIR="$(cd -P "$BIN_DIR" && pwd)" |
| 180 | |
| 181 | check_destination() { |
| 182 | local src="$1" dst="$2" mode="${3:-0755}" |
| 183 | destination_exists=0 |
| 184 | if [[ -e "$dst" || -L "$dst" ]]; then |
| 185 | if [[ ! -L "$dst" && -f "$dst" ]] && [[ "$mode" != 0755 || -x "$dst" ]] && cmp -s "$src" "$dst"; then |
| 186 | destination_exists=1 |
| 187 | return 0 |
| 188 | fi |
| 189 | local upgradable=0 |
| 190 | if [[ ! -L "$dst" && -f "$dst" && -O "$dst" ]]; then |
| 191 | case "$(basename "$dst")" in |
| 192 | codewhale|codew) grep -aq -e 'codewhale-updater' -e 'deepseek-tui' "$dst" 2>/dev/null && upgradable=1 ;; |
| 193 | .codewhale-install.sha256) upgradable=1 ;; |
| 194 | esac |
| 195 | [[ "${CODEWHALE_FORCE_REINSTALL:-}" != 1 ]] || upgradable=1 |
| 196 | fi |
| 197 | if [[ "$upgradable" == 1 ]]; then |
| 198 | move_to_recovery "$dst" |
| 199 | echo "Upgrading $dst (previous file kept in $recovery_dir for 30 days)" |
| 200 | return 0 |
| 201 | fi |
| 202 | echo "ERROR: $dst is not a Codewhale release file this installer can upgrade; it was not changed." >&2 |
| 203 | echo "It is a symlink, belongs to another user, or is a different program." >&2 |
| 204 | echo "Install into a fresh prefix instead:" >&2 |
| 205 | echo ' PREFIX="$HOME/.local/codewhale" ./install.sh' >&2 |
| 206 | echo "Or move a regular file you own to the recovery folder and install anyway:" >&2 |
| 207 | echo ' CODEWHALE_FORCE_REINSTALL=1 ./install.sh' >&2 |
| 208 | return 1 |
| 209 | fi |
| 210 | } |
| 211 | |
| 212 | if [[ -n "${CODEWHALE_HOME:-}" && "$CODEWHALE_HOME" == /* ]]; then |
| 213 | recovery_root="$CODEWHALE_HOME/recovery" |
| 214 | else |
| 215 | recovery_root="$HOME/.codewhale/recovery" |
| 216 | fi |
| 217 | recovery_dir="" |
| 218 | recovery_seq=0 |
| 219 | open_recovery() { |
| 220 | [[ -z "$recovery_dir" ]] || return 0 |
| 221 | mkdir -p "$recovery_root" |
| 222 | chmod 700 "$recovery_root" 2>/dev/null || true |
| 223 | local stamp candidate attempt=1 |
| 224 | stamp="$(date -u +%Y%m%dT%H%M%SZ)" |
| 225 | candidate="$stamp" |
| 226 | until mkdir "$recovery_root/$candidate" 2>/dev/null; do |
| 227 | (( attempt < 100 )) || { echo "ERROR: cannot create a recovery folder under $recovery_root" >&2; exit 1; } |
| 228 | candidate="$stamp-$attempt" |
| 229 | attempt=$((attempt + 1)) |
| 230 | done |
| 231 | recovery_dir="$recovery_root/$candidate" |
| 232 | } |
| 233 | move_to_recovery() { |
| 234 | local file="$1" stored digest size |
| 235 | open_recovery |
| 236 | recovery_seq=$((recovery_seq + 1)) |
| 237 | stored="$recovery_dir/$(printf '%03d' "$recovery_seq")-$(basename "$file")" |
| 238 | digest="$(sha256_file "$file" | tr '[:upper:]' '[:lower:]')" |
| 239 | size="$(wc -c < "$file" | tr -d ' ')" |
| 240 | mv "$file" "$stored" |
| 241 | printf '%s\t%s\t%s\t%s\n' "$file" "$stored" "$digest" "$size" >> "$recovery_dir/manifest.tsv" |
| 242 | forced_backups+=("$stored"$'\t'"$file") |
| 243 | } |
| 244 | |
| 245 | # The image is an explicit installer choice; presence never changes Core's |
| 246 | # Node default. Archives carry notices/source beside any qualified image. |
| 247 | receipt_tmp="$(mktemp -d "$BIN_DIR/.codewhale-receipt.XXXXXX")" |
| 248 | receipt_name=".codewhale-install.sha256" |
| 249 | forced_backups=() |
| 250 | stage="" |
| 251 | stage_dir="" |
| 252 | # Commands this run published, as "source<TAB>destination" lines. If a later |
| 253 | # publication fails, they are removed again (only while each is still the |
| 254 | # exact file this run wrote), so a failed install leaves no half-installed |
| 255 | # pair; files that were already installed are never touched. |
| 256 | published="" |
| 257 | rollback_published() { |
| 258 | local src dst |
| 259 | while IFS=$'\t' read -r src dst; do |
| 260 | [[ -n "$dst" ]] || continue |
| 261 | if [[ ! -L "$dst" && -f "$dst" ]] && cmp -s "$src" "$dst"; then |
| 262 | rm -f "$dst" |
| 263 | echo "Removed $dst: this install did not complete." >&2 |
| 264 | fi |
| 265 | done <<< "$published" |
| 266 | } |
| 267 | on_exit() { |
| 268 | local status=$? dst backup |
| 269 | if [[ -n "$stage" ]]; then rm -f "$stage"; fi |
| 270 | if [[ -n "$stage_dir" ]]; then rmdir "$stage_dir"; fi |
| 271 | if [[ "$status" -ne 0 && -n "$published" ]]; then rollback_published; fi |
| 272 | if [[ "$status" -ne 0 ]]; then |
| 273 | for entry in ${forced_backups[@]+"${forced_backups[@]}"}; do |
| 274 | backup="${entry%%$'\t'*}" |
| 275 | dst="${entry#*$'\t'}" |
| 276 | [[ -e "$backup" ]] || continue |
| 277 | if [[ ! -e "$dst" && ! -L "$dst" ]]; then |
| 278 | mv "$backup" "$dst" && echo "Restored $dst: this install did not complete." >&2 |
| 279 | fi |
| 280 | done |
| 281 | fi |
| 282 | rm -f "$receipt_tmp/$receipt_name" |
| 283 | rmdir "$receipt_tmp" |
| 284 | } |
| 285 | trap on_exit EXIT |
| 286 | payloads=(codewhale codew) |
| 287 | if [[ "${CODEWHALE_INSTALL_COMPILED_HOST:-}" == 1 ]]; then |
| 288 | for file in codewhale-extension-host codewhale-extension-host.LICENSES.txt codewhale-extension-host.relink-source.tar.gz codewhale-extension-host.release.json; do |
| 289 | [[ -f "$SCRIPT_DIR/$file" && ! -L "$SCRIPT_DIR/$file" ]] || { echo "ERROR: compiled host requested but archive has no complete qualified payload ($file); use Node" >&2; exit 1; } |
| 290 | payloads+=("$file") |
| 291 | done |
| 292 | fi |
| 293 | # Validate every source and destination before the first write. |
| 294 | for bin in "${payloads[@]}"; do |
| 295 | src="$SCRIPT_DIR/$bin" |
| 296 | [[ -f "$src" ]] || { echo "ERROR: $src not found in archive" >&2; exit 1; } |
| 297 | mode=0644 |
| 298 | case "$bin" in codewhale|codew|codewhale-extension-host) mode=0755; preflight_glibc "$src" "${bin/codewhale-extension-host/compiled host}" ;; esac |
| 299 | check_destination "$src" "$BIN_DIR/$bin" "$mode" |
| 300 | done |
| 301 | |
| 302 | for command_name in codewhale codew; do |
| 303 | command_digest="$(sha256_file "$SCRIPT_DIR/$command_name" | tr '[:upper:]' '[:lower:]')" |
| 304 | [[ "$command_digest" =~ ^[0-9a-f]{64}$ ]] || { echo "ERROR: invalid command digest for $command_name" >&2; exit 1; } |
| 305 | printf '%s %s\n' "$command_digest" "$command_name" |
| 306 | done > "$receipt_tmp/$receipt_name" |
| 307 | check_destination "$receipt_tmp/$receipt_name" "$BIN_DIR/$receipt_name" 0644 |
| 308 | |
| 309 | install_binary() { |
| 310 | local src="$1" dst="$2" mode="${3:-0755}" |
| 311 | check_destination "$src" "$dst" "$mode" |
| 312 | if [[ "$destination_exists" == 1 ]]; then |
| 313 | echo " $dst (already installed)" |
| 314 | return |
| 315 | fi |
| 316 | stage_dir="$(mktemp -d "$BIN_DIR/.codewhale-install.XXXXXX")" |
| 317 | stage="$stage_dir/$(basename "$dst")" |
| 318 | if [[ "$(basename "$dst")" == "codew" && ! -L "$BIN_DIR/codewhale" && -f "$BIN_DIR/codewhale" && -x "$BIN_DIR/codewhale" ]] && cmp -s "$src" "$BIN_DIR/codewhale"; then |
| 319 | if ! ln "$BIN_DIR/codewhale" "$stage" 2>/dev/null; then |
| 320 | rm -f "$stage" |
| 321 | cp "$src" "$stage" |
| 322 | chmod "$mode" "$stage" |
| 323 | fi |
| 324 | else |
| 325 | cp "$src" "$stage" |
| 326 | chmod "$mode" "$stage" |
| 327 | fi |
| 328 | # Same-directory hard-link publication is atomic and never overwrites a |
| 329 | # destination created between preflight and this operation. |
| 330 | # The explicit parent operand avoids treating a raced-in destination |
| 331 | # directory (or directory symlink) as an alternate publication location. |
| 332 | ln "$stage" "$BIN_DIR/" |
| 333 | [[ ! -L "$dst" && -f "$dst" ]] && cmp -s "$stage" "$dst" || { |
| 334 | echo "ERROR: installed path changed during publication: $dst" >&2 |
| 335 | return 1 |
| 336 | } |
| 337 | published+="$src"$'\t'"$dst"$'\n' |
| 338 | rm -f "$stage" |
| 339 | rmdir "$stage_dir" |
| 340 | stage="" |
| 341 | stage_dir="" |
| 342 | echo " $dst" |
| 343 | } |
| 344 | |
| 345 | echo "Installing codewhale to $BIN_DIR ..." |
| 346 | for bin in "${payloads[@]}"; do |
| 347 | mode=0644 |
| 348 | case "$bin" in codewhale|codew|codewhale-extension-host) mode=0755 ;; esac |
| 349 | install_binary "$SCRIPT_DIR/$bin" "$BIN_DIR/$bin" "$mode" |
| 350 | done |
| 351 | |
| 352 | echo "" |
| 353 | install_binary "$receipt_tmp/$receipt_name" "$BIN_DIR/$receipt_name" 0644 |
| 354 | echo "Done. Commands installed to $BIN_DIR." |
| 355 | supports_uninstall=0 |
| 356 | installed_help="$("$BIN_DIR/codewhale" --help </dev/null 2>/dev/null || true)" |
| 357 | if printf '%s\n' "$installed_help" | grep -E '^[[:space:]]+uninstall([[:space:]]|$)' >/dev/null; then |
| 358 | supports_uninstall=1 |
| 359 | fi |
| 360 | if [[ "$supports_uninstall" == 1 ]]; then |
| 361 | echo "Uninstall this receipted copy: \"$BIN_DIR/codewhale\" uninstall --yes" |
| 362 | else |
| 363 | echo "Uninstall: this release has no uninstall command. Remove the files: rm \"$BIN_DIR/codewhale\" \"$BIN_DIR/codew\" \"$BIN_DIR/.codewhale-install.sha256\"" |
| 364 | echo "Your data in ~/.codewhale is separate; delete it only if you want it gone." |
| 365 | fi |
| 366 | if [[ -n "$recovery_dir" ]]; then |
| 367 | echo "Previous files are kept in $recovery_dir for 30 days. Undo: \"$BIN_DIR/codewhale\" doctor --restore $(basename "$recovery_dir")" |
| 368 | fi |
| 369 | CODEWHALE_TELEMETRY=0 CODEWHALE_NO_UPDATE_CHECK=1 "$BIN_DIR/codewhale" doctor --installs --brief 2>/dev/null || true |
| 370 | echo "Usage reporting is on by default (counts only, never your code or conversations). Turn it off: \"$BIN_DIR/codewhale\" config set telemetry false" |
| 371 | echo "Future updates: \"$BIN_DIR/codewhale\" update" |
| 372 | path_selected=1 |
| 373 | for bin in codewhale codew; do |
| 374 | resolved="$(command -v "$bin" || true)" |
| 375 | if [[ "$resolved" != "$BIN_DIR/$bin" ]]; then |
| 376 | echo "PATH selects ${resolved:-no $bin command}; this install is $BIN_DIR/$bin" |
| 377 | path_selected=0 |
| 378 | fi |
| 379 | done |
| 380 | say() { printf '%s\n' "$*"; } |
| 381 | install_dir="$BIN_DIR" |
| 382 | case "$(uname -s)" in Darwin) target=macos-host ;; *) target=other ;; esac |
| 383 | path_marker_start="# >>> codewhale PATH >>>" |
| 384 | path_marker_end="# <<< codewhale PATH <<<" |
| 385 | |
| 386 | install_dir_on_path() { |
| 387 | case ":${PATH:-}:" in |
| 388 | *":$install_dir:"*) return 0 ;; |
| 389 | esac |
| 390 | return 1 |
| 391 | } |
| 392 | |
| 393 | pick_profile() { |
| 394 | profile_file="" |
| 395 | profile_shown="" |
| 396 | [ -n "${HOME:-}" ] || return 0 |
| 397 | case "$1" in |
| 398 | zsh) |
| 399 | if [ -n "${ZDOTDIR:-}" ]; then |
| 400 | case "$ZDOTDIR" in |
| 401 | /*) ;; |
| 402 | *) return 0 ;; |
| 403 | esac |
| 404 | case "$ZDOTDIR" in |
| 405 | *[!A-Za-z0-9_./+-]*) return 0 ;; |
| 406 | esac |
| 407 | profile_file="$ZDOTDIR/.zshrc" |
| 408 | profile_shown="\"$profile_file\"" |
| 409 | return 0 |
| 410 | fi |
| 411 | profile=".zshrc" |
| 412 | ;; |
| 413 | bash) |
| 414 | case "$target" in |
| 415 | macos-*) |
| 416 | profile=".bash_profile" |
| 417 | for candidate in .bash_profile .bash_login .profile; do |
| 418 | if [ -e "$HOME/$candidate" ]; then |
| 419 | profile="$candidate" |
| 420 | break |
| 421 | fi |
| 422 | done |
| 423 | ;; |
| 424 | *) profile=".bashrc" ;; |
| 425 | esac |
| 426 | ;; |
| 427 | *) profile=".profile" ;; |
| 428 | esac |
| 429 | profile_file="$HOME/$profile" |
| 430 | profile_shown="~/$profile" |
| 431 | } |
| 432 | |
| 433 | add_path_block() { |
| 434 | path_note="" |
| 435 | path_state=added |
| 436 | if [ -L "$profile_file" ]; then |
| 437 | path_note="$profile_shown is a symlink" |
| 438 | return 1 |
| 439 | fi |
| 440 | lead_newline=0 |
| 441 | if [ -e "$profile_file" ]; then |
| 442 | if [ ! -f "$profile_file" ] || [ ! -w "$profile_file" ]; then |
| 443 | path_note="$profile_shown is not a writable regular file" |
| 444 | return 1 |
| 445 | fi |
| 446 | if grep -qxF "$path_marker_start" "$profile_file" 2>/dev/null; then |
| 447 | path_state=present |
| 448 | return 0 |
| 449 | fi |
| 450 | if [ -s "$profile_file" ] && [ -n "$(tail -c 1 "$profile_file" 2>/dev/null)" ]; then |
| 451 | lead_newline=1 |
| 452 | fi |
| 453 | else |
| 454 | profile_parent="$(dirname "$profile_file")" |
| 455 | if [ ! -d "$profile_parent" ] || [ ! -w "$profile_parent" ]; then |
| 456 | path_note="the folder for $profile_shown is missing or not writable" |
| 457 | return 1 |
| 458 | fi |
| 459 | fi |
| 460 | if ! { |
| 461 | if [ "$lead_newline" -eq 1 ]; then printf '\n'; fi |
| 462 | printf '%s\n' "$path_marker_start" "export PATH=\"$path_dir:\$PATH\"" "$path_marker_end" |
| 463 | } >> "$profile_file" 2>/dev/null; then |
| 464 | path_note="could not write to $profile_shown" |
| 465 | return 1 |
| 466 | fi |
| 467 | return 0 |
| 468 | } |
| 469 | |
| 470 | undo_path_block_command() { |
| 471 | 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\"" |
| 472 | } |
| 473 | |
| 474 | if [ "$path_selected" -eq 1 ]; then |
| 475 | say "" |
| 476 | say "Next: codewhale --version" |
| 477 | else |
| 478 | path_dir="$install_dir" |
| 479 | if [ -n "${HOME:-}" ] && [ "$install_dir" = "$(cd -P "$HOME/.local/bin" 2>/dev/null && pwd)" ]; then |
| 480 | path_dir="\$HOME/.local/bin" |
| 481 | fi |
| 482 | shell_name="${SHELL:-}" |
| 483 | shell_name="${shell_name##*/}" |
| 484 | case "$path_dir" in |
| 485 | *[\'\"\`\\\$]*|*" |
| 486 | "*) |
| 487 | if [ "$path_dir" != "\$HOME/.local/bin" ]; then |
| 488 | shell_name="unsafe-path" |
| 489 | fi |
| 490 | ;; |
| 491 | esac |
| 492 | say "" |
| 493 | if [ -z "$(command -v codewhale 2>/dev/null || true)" ]; then |
| 494 | say "codewhale is not on your PATH in this shell yet." |
| 495 | else |
| 496 | say "A different codewhale comes first on your PATH in this shell." |
| 497 | fi |
| 498 | case "$shell_name" in |
| 499 | fish) |
| 500 | say "This installer does not edit fish's configuration. Put $install_dir first on PATH in future shells (run once):" |
| 501 | say " fish_add_path \"$path_dir\"" |
| 502 | say "It takes effect in this fish shell and in new ones (fish 3.2 or newer)." |
| 503 | say "PATH help: https://github.com/codewhale-hq/CodeWhale/blob/main/docs/INSTALL.md#put-it-on-your-path" |
| 504 | say "" |
| 505 | say "Then check it:" |
| 506 | say " codewhale --version" |
| 507 | ;; |
| 508 | zsh|bash|sh|dash|ksh|mksh|ash) |
| 509 | pick_profile "$shell_name" |
| 510 | persist_line="printf '%s\\n' '$path_marker_start' 'export PATH=\"$path_dir:\$PATH\"' '$path_marker_end' >> $profile_shown" |
| 511 | if [ -z "$profile_file" ]; then |
| 512 | say "This installer could not pick a profile file for $shell_name, so it changed nothing. To keep it in future shells (run once):" |
| 513 | say " Add $install_dir first to PATH in your shell's startup file." |
| 514 | elif [ "$modify_path" -eq 0 ]; then |
| 515 | say "Not editing your shell profile (--no-modify-path). To keep it in future shells, run once:" |
| 516 | say " $persist_line" |
| 517 | elif install_dir_on_path; then |
| 518 | 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:" |
| 519 | say " $persist_line" |
| 520 | elif add_path_block; then |
| 521 | if [ "$path_state" = present ]; then |
| 522 | say "$profile_shown already has the codewhale PATH block; it was left as is." |
| 523 | else |
| 524 | say "Added to $profile_shown so new shells find codewhale (skip this with --no-modify-path):" |
| 525 | say " $path_marker_start" |
| 526 | say " export PATH=\"$path_dir:\$PATH\"" |
| 527 | say " $path_marker_end" |
| 528 | say "Only those three lines were added; nothing else in the file was changed. Shells that are already open do not reread the profile." |
| 529 | fi |
| 530 | say "To undo, run:" |
| 531 | say " $(undo_path_block_command)" |
| 532 | if [ "$supports_uninstall" -eq 1 ]; then |
| 533 | say "The marker lines let codewhale uninstall remove exactly this block; a bare export line is yours and is never touched." |
| 534 | fi |
| 535 | else |
| 536 | say "Did not edit your shell profile: $path_note. To keep it in future shells, run once:" |
| 537 | say " $persist_line" |
| 538 | say "To undo that later, delete the three lines between and including the marker lines." |
| 539 | fi |
| 540 | say "PATH help: https://github.com/codewhale-hq/CodeWhale/blob/main/docs/INSTALL.md#put-it-on-your-path" |
| 541 | say "" |
| 542 | say "Run this now, in this shell:" |
| 543 | say " export PATH=\"$path_dir:\$PATH\"; hash -r" |
| 544 | say "Then check it:" |
| 545 | say " codewhale --version" |
| 546 | ;; |
| 547 | unsafe-path) |
| 548 | 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." |
| 549 | say "PATH help: https://github.com/codewhale-hq/CodeWhale/blob/main/docs/INSTALL.md#put-it-on-your-path" |
| 550 | ;; |
| 551 | *) |
| 552 | 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." |
| 553 | say "PATH help: https://github.com/codewhale-hq/CodeWhale/blob/main/docs/INSTALL.md#put-it-on-your-path" |
| 554 | say "" |
| 555 | say "Until then, run it by its full path:" |
| 556 | say " \"$install_dir/codewhale\" --version" |
| 557 | ;; |
| 558 | esac |
| 559 | fi |
| 560 |