#!/bin/sh # Installs the selan CLI. Published to https://dl-dev.selan.ai/install.sh set -eu DL_HOST="dl-dev.selan.ai" BINARY="selan-dev" # The same binary under a second name: running it means `$BINARY claude # --dangerously-skip-permissions`, which it decides from argv[0]. The 'd' is # inserted rather than appended so both channels read the same way — selan → # seland, selan-dev → seland-dev. DANGER_BINARY="seland${BINARY#selan}" # The developer's own bin directory, so neither this install nor any later # `selan update` — which reinstalls wherever the running binary is — asks for # sudo. ~/.local/bin is the XDG one and where Claude Code's installer puts # claude, so it is often on PATH already; the PATH fix below adds it when it is # not. root, as in a container image, has no sudo to avoid and often no shell # that reads a startup file, so it keeps /usr/local/bin, on every default PATH. if [ -n "${SELAN_INSTALL_DIR:-}" ]; then DEST="$SELAN_INSTALL_DIR" elif [ "$(id -u)" = 0 ] || [ -z "${HOME:-}" ]; then DEST=/usr/local/bin else DEST="$HOME/.local/bin" fi os=$(uname -s | tr '[:upper:]' '[:lower:]') case "$os" in darwin|linux) ;; *) echo "error: unsupported operating system: $os" >&2; exit 1 ;; esac case "$(uname -m)" in x86_64|amd64) arch=amd64 ;; arm64|aarch64) arch=arm64 ;; *) echo "error: unsupported architecture: $(uname -m)" >&2; exit 1 ;; esac # macOS ships shasum; most Linux images ship sha256sum. Both support -c. if command -v shasum >/dev/null 2>&1; then checksum="shasum -a 256" elif command -v sha256sum >/dev/null 2>&1; then checksum="sha256sum" else echo "error: need shasum or sha256sum to verify the download" >&2 exit 1 fi # `latest` is served no-cache, so this is never stale. Everything below is # fetched from an immutable versioned path. version=$(curl -fsSL "https://$DL_HOST/latest") if [ -z "$version" ]; then echo "error: couldn't determine the latest version from https://$DL_HOST/latest" >&2 exit 1 fi # The tarball always contains a file named "selan" regardless of channel — # only the name it gets installed as (BINARY, below) differs, so a dev and a # prod install can coexist on the same machine without one clobbering the # other. asset="selan_${os}_${arch}.tar.gz" tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT echo "downloading $BINARY $version ($os/$arch)..." curl -fsSL -o "$tmp/$asset" "https://$DL_HOST/v$version/$asset" curl -fsSL -o "$tmp/checksums.txt" "https://$DL_HOST/v$version/checksums.txt" echo "verifying checksum..." # Capture the matching line first rather than piping grep straight into the # checksum tool: in a pipeline, the exit status is the checksum tool's alone, # so a grep match failure would be silently swallowed. Worse, macOS's # /sbin/sha256sum exits 0 on empty input, so an empty match would read as a # pass. This is the only integrity control in a curl-|-sh install. line=$(cd "$tmp" && grep " $asset\$" checksums.txt || true) if [ -z "$line" ]; then echo "error: no checksum entry for $asset in checksums.txt" >&2 exit 1 fi if ! printf '%s\n' "$line" | (cd "$tmp" && $checksum -c -) >/dev/null 2>&1; then echo "error: checksum verification failed for $asset" >&2 exit 1 fi tar -xzf "$tmp/$asset" -C "$tmp" # A destination that does not exist yet is not writable either, so without # this a perfectly ordinary SELAN_INSTALL_DIR=$HOME/.local/bin — a directory # many people simply have not created — would ask for sudo to write inside # the user's own home. Just attempt it: checking the parent instead would # still fail for a nested path like ~/.local/bin where ~/.local is missing # too. If we lack permission the mkdir fails harmlessly and the sudo path # below handles it, exactly as before. if [ ! -d "$DEST" ]; then mkdir -p "$DEST" 2>/dev/null || true fi # /usr/local/bin was the default before ~/.local/bin, and `selan update` and the # launch's auto-update reinstall beside the running binary, so an install from # then would ask for sudo on every update — and a selan from 1.27.0 on skips the # auto-update rather than ask. That install moves to ~/.local/bin instead, once, # and updates from there. Only the old default: a SELAN_INSTALL_DIR anywhere # else was somebody's choice. In the old default the two cannot be told apart — # `selan update` names it either way — so an install meant to stay there is # installed and updated as root, which this never moves, needing no sudo. Nor # into a ~/.local/bin this user cannot write either. OLD_DEFAULT=/usr/local/bin moved_from="" if [ "${DEST%/}" = "$OLD_DEFAULT" ] && [ ! -w "$DEST" ] && [ "$(id -u)" != 0 ] && [ -n "${HOME:-}" ]; then mkdir -p "$HOME/.local/bin" 2>/dev/null || true if [ -w "$HOME/.local/bin" ]; then moved_from="$DEST" DEST="$HOME/.local/bin" echo "$moved_from needs sudo to write, so $BINARY moves to $DEST, which does not" fi fi # A relative symlink, not a second copy: it cannot go stale between updates, # and it stays valid if $DEST is ever moved wholesale. Windows gets a copy # instead — see install.ps1.tmpl for why. if [ -w "$DEST" ]; then install -m 0755 "$tmp/selan" "$DEST/$BINARY" ln -sf "$BINARY" "$DEST/$DANGER_BINARY" else echo "installing to $DEST (needs sudo)" sudo install -m 0755 "$tmp/selan" "$DEST/$BINARY" sudo ln -sf "$BINARY" "$DEST/$DANGER_BINARY" fi echo "installed $BINARY $version to $DEST/$BINARY" echo "and $DANGER_BINARY, the same launch with permissions skipped" # An older copy elsewhere — the one just moved from, or an install from before # ~/.local/bin was the default — is named, never removed: deleting it may need # sudo, which is what this default exists to avoid. Where PATH lists it first it # is the one a shell runs, which the PATH fix below sees to; elsewhere it is # merely stale. -ef rather than comparing text: `selan update` names $DEST from # the running binary's resolved path, which a symlinked PATH entry or a # trailing slash spells differently. found=$(command -v "$BINARY" 2>/dev/null || true) shadowed="" old_copy="" if [ -n "$found" ] && ! [ "$found" -ef "$DEST/$BINARY" ]; then shadowed="$found" old_copy="$found" elif [ -n "$moved_from" ] && [ -e "$moved_from/$BINARY" ]; then old_copy="$moved_from/$BINARY" fi if [ -n "$old_copy" ]; then rm_cmd="rm" [ -w "$(dirname "$old_copy")" ] || rm_cmd="sudo rm" echo echo "note: $old_copy is another $BINARY, left where it is. To remove it:" echo echo " $rm_cmd \"$old_copy\" \"$(dirname "$old_copy")/$DANGER_BINARY\"" fi # The manual, written by the binary just installed so it describes this version # and no other, beside the prefix: /share/man/man1. That is where man # looks for a PATH entry ending in /bin — macOS's man adds /share/man for # each one, and man-db does the same for any entry its manpath.config does not # map — so a destination not named bin gets no manual. Written as the binary was, # but never with a second password prompt: sudo -n, and a note if that is not # enough. Best effort throughout; the binary is already in place. # # manual_dir is that directory for a destination, or nothing: for one not named # bin, and for a bin directory whose parent is no prefix — ~/bin's parent is the # home directory, where share/man would be a new folder at its top, and /bin's # is the root, where it would be /share, made with sudo. Neither is where a # manual belongs, so neither gets one. manual_dir() { # dir="${1%/}" case "$dir" in */bin) prefix="${dir%/bin}" ;; *) return 0 ;; esac home="${HOME:-}" if [ -n "$prefix" ] && [ "$prefix" != "${home%/}" ]; then echo "$prefix/share/man/man1" fi } case "${DEST%/}" in */bin) man_dir=$(manual_dir "$DEST") as_root="" [ -w "$DEST" ] || as_root="sudo -n" if [ -z "$man_dir" ]; then echo "note: no manual installed, since $DEST's parent is not a prefix man searches; \`$BINARY man\` prints it" elif "$DEST/$BINARY" man > "$tmp/$BINARY.1" 2>/dev/null && [ -s "$tmp/$BINARY.1" ] && $as_root mkdir -p "$man_dir" 2>/dev/null && $as_root install -m 0644 "$tmp/$BINARY.1" "$man_dir/$BINARY.1" 2>/dev/null; then echo "and its manual, for \`man $BINARY\`, to $man_dir/$BINARY.1" else echo "note: couldn't write the manual to $man_dir; \`$BINARY man\` prints it" fi ;; esac # Before this change, every channel installed a binary literally named # "selan" — a dev install and a prod install clobbered each other. Existing # dev users therefore have a $DEST/selan that is really an old dev build, # now orphaned: it stays at its old version forever and keeps nagging, and # nothing in this script ever touches it, because $BINARY is "selan-dev" now. # We cannot tell that apart from a deliberately installed prod "selan" on the # same machine, so we only ever print — never delete a binary we didn't just # write ourselves. if [ "$BINARY" != "selan" ] && [ -e "$DEST/selan" ]; then echo echo "note: $DEST/selan is also present. If that's a leftover from before" echo "$BINARY existed as a separate name, it won't be updated by this" echo "installer and can be removed:" echo echo " rm $DEST/selan" fi # On PATH but behind another copy is a PATH to fix too: a shell would run the # other one, and an older selan run that way updates into $DEST on every launch # without ever running what it installed. The line the fix appends puts $DEST # first in every shell started afterwards. on_path="" case ":$PATH:" in *":$DEST:"*) on_path=1 ;; esac if [ -n "$on_path" ] && [ -n "$shadowed" ]; then why="$DEST comes after $(dirname "$shadowed") on your PATH" else why="$DEST is not on your PATH" fi case "$on_path$shadowed" in 1) # $DEST is on PATH, but that is not enough on a first install: zsh and bash # hash command lookups per PATH directory, so a shell that was already # running when we wrote the binary keeps reporting "command not found" # until its hash is cleared. That is what makes people reach for # `source ~/.zshrc`, which only works as a side effect. fish rescans on its # own and has no `hash`, so telling it to run one would just error. if [ "$(basename "${SHELL:-unknown}")" != "fish" ]; then echo echo "if your shell can't find $BINARY yet, run:" echo echo " hash -r" fi ;; *) shell_name=$(basename "${SHELL:-unknown}") export_line="export PATH=\"$DEST:\$PATH\"" # The line to paste into the shell that is running right now. `hash -r` is # the part people miss: zsh and bash cache command lookups, including # failed ones, so a shell that has already tried "$BINARY" once keeps # reporting "command not found" even after PATH is fixed. fish needs # neither — fish_add_path affects the running session and persists. # --move, because a $DEST already on PATH behind another copy is one plain # fish_add_path leaves where it is. if [ "$shell_name" = "fish" ]; then now_line="fish_add_path --move \"$DEST\"" else now_line="$export_line; hash -r" fi rc_file="" rc_file2="" case "$shell_name" in zsh) rc_file="$HOME/.zshrc" ;; bash) # .bashrc is what non-login/interactive shells source on both Linux # and macOS, and is where a PATH addition is most likely to already # live, so it is always the primary target. But on macOS, Terminal.app # (and any other login shell) reads .bash_profile ONLY — not .bashrc — # unless .bash_profile itself sources .bashrc. Writing only .bashrc # would then silently fail to fix PATH for exactly the shell most # macOS users actually get. So: if .bash_profile exists and does not # already reference .bashrc, fix it too, in addition to (not instead # of) .bashrc — plenty of bash setups, especially on Linux, have no # .bash_profile at all and rely on .bashrc alone. rc_file="$HOME/.bashrc" if [ -f "$HOME/.bash_profile" ] && ! grep -q '\.bashrc' "$HOME/.bash_profile"; then rc_file2="$HOME/.bash_profile" fi ;; fish) rc_file="$HOME/.config/fish/config.fish" ;; *) rc_file="" ;; esac echo if [ -z "$rc_file" ]; then echo "$why. Add this to your shell's startup file:" echo echo " $export_line" echo echo "then restart your shell, or open a new terminal." echo echo "to use $BINARY in THIS shell without restarting, run:" echo echo " $now_line" else if [ "$shell_name" = "fish" ]; then line="fish_add_path --move \"$DEST\"" else line="$export_line" fi for f in "$rc_file" "$rc_file2"; do [ -n "$f" ] || continue mkdir -p "$(dirname "$f")" touch "$f" # -x for an exact whole-line match: a plain -F also matches inside a # line the user has commented out, which would wrongly report PATH as # already fixed and leave it broken. if grep -qxF "$line" "$f"; then echo "$why, but $f already adds it." else printf '\n# added by the selan installer\n%s\n' "$line" >> "$f" echo "$why, so the selan installer added it to $f:" echo echo " $line" fi echo done # A child process cannot change its parent's environment, so this # script physically cannot put $DEST on the PATH of the shell that ran # it — only on the PATH of shells started later. Printing the line that # does it now saves the user from a "command not found" on the very # first thing they try. echo "restart your shell, or run this to use $BINARY right now:" echo echo " $now_line" fi ;; esac # Tab completion, only where a shell loads it by itself: fish's completions # directory, and the one bash-completion reads — so no startup file is ever # edited for it — and only for a shell this machine has. The script is the # binary's own, and asks it for every answer, so it never needs rewriting for a # new command; this refreshes it anyway. A file there that this binary did not # write is somebody's own and is left alone. Never as root: its files in a home # would lock that home's owner out of the next update. zsh and PowerShell load # nothing by themselves, so the line to add is printed instead, until it is there. write_completion() { # if [ -e "$2" ] && ! head -n 1 "$2" 2>/dev/null | grep -qF "$BINARY completion $1"; then echo "note: left $2 alone; $BINARY did not write it" elif "$DEST/$BINARY" completion "$1" > "$tmp/completion.$1" 2>/dev/null && [ -s "$tmp/completion.$1" ] && mkdir -p "$(dirname "$2")" 2>/dev/null && cp "$tmp/completion.$1" "$2" 2>/dev/null; then echo "tab completion for $1, which $3 loads, is in $2" else echo "note: couldn't write tab completion for $1 to $2; \`$BINARY completion $1\` prints it" fi } if [ "$(id -u)" != 0 ] && [ -n "${HOME:-}" ]; then login_shell=$(basename "${SHELL:-unknown}") fish_dir="${XDG_CONFIG_HOME:-$HOME/.config}/fish" # bash-completion reads a list; the first entry is the one it writes to. bash_dir="${BASH_COMPLETION_USER_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion}" bash_dir="${bash_dir%%:*}" if command -v fish >/dev/null 2>&1 || [ -d "$fish_dir" ]; then echo write_completion fish "$fish_dir/completions/$BINARY.fish" fish fi if [ "$login_shell" = bash ] || [ -d "$bash_dir" ]; then echo bash_first=yes [ -e "$bash_dir/completions/$BINARY" ] && bash_first= write_completion bash "$bash_dir/completions/$BINARY" bash-completion # Nothing loads that file without bash-completion, which stock macOS bash # does not have. Said by the install that first writes it, not by every # `selan update`: where bash-completion loads the file, the line would only # register the completion twice. if [ -n "$bash_first" ] && [ "$login_shell" = bash ] && ! grep -qF "$BINARY completion bash" "$HOME/.bashrc" 2>/dev/null; then echo "without bash-completion, add this line to $HOME/.bashrc:" echo echo " eval \"\$($BINARY completion bash)\"" fi fi zshrc="${ZDOTDIR:-$HOME}/.zshrc" if [ "$login_shell" = zsh ] && ! grep -qF "$BINARY completion zsh" "$zshrc" 2>/dev/null; then echo echo "for tab completion in zsh, add this line to $zshrc:" echo echo " source <($BINARY completion zsh)" fi fi # selan launches Claude Code, it does not bundle it, and it finds it exactly # the way this check does — a PATH lookup. Worth saying now rather than leaving # it to the first `$BINARY claude`. # # A note, never a gate. `command -v` runs under this script's /bin/sh, which # has none of the user's interactive shell configuration, so a claude that # exists only as an alias or a shell function is invisible here: exiting # non-zero would refuse to install on a machine that works perfectly. And # installing selan first is a legitimate order anyway — login and whoami need # nothing else. if command -v claude >/dev/null 2>&1; then echo echo "next: $BINARY claude" elif [ -n "${HOME:-}" ] && [ -x "$HOME/.local/bin/claude" ]; then # Where Claude Code's own installer puts its launcher. Reaching this branch # means it is installed but not on PATH — command -v above would have found # it otherwise. That distinction matters: $BINARY resolves claude by PATH # lookup and would fail at launch even on a machine where the user's own # `claude` works, via an alias or a full path. # # Unless that directory is $DEST, which the PATH fix above has just added: # its line again would be a duplicate in the startup file. echo if [ "$DEST" -ef "$HOME/.local/bin" ]; then echo "next, once $DEST is on PATH: $BINARY claude" else echo "note: found $HOME/.local/bin/claude, but that directory is not on your" echo "PATH. $BINARY resolves claude by PATH lookup, so add it:" echo if [ "$(basename "${SHELL:-unknown}")" = "fish" ]; then echo " fish_add_path \"\$HOME/.local/bin\"" else echo " export PATH=\"\$HOME/.local/bin:\$PATH\"" fi echo echo "then: $BINARY claude" fi else echo echo "note: couldn't find claude on your PATH. $BINARY launches Claude Code" echo "rather than bundling it, so install that too:" echo echo " curl -fsSL https://claude.ai/install.sh | bash" echo echo "then: $BINARY claude" fi