#!/bin/sh # Installs the Sakiori CLI from a release archive on the distribution's origin. # Needs only sh, tar, sha256sum or shasum, and curl or GNU Wget 1.x; no Node, # npm, or compiler. On Linux the CLI itself needs glibc 2.28 or later. # # curl --proto '=https' --tlsv1.2 -fsSL https://releases.sakiori.app/channels/nightly/install.sh | sh # # Environment: # SAKIORI_CHANNEL release train to follow; this distribution # publishes only nightly (the default) # SAKIORI_VERSION exact version to install (overrides SAKIORI_CHANNEL) # SAKIORI_HOME Sakiori home directory (default: ~/.sakiori) # SAKIORI_INSTALL_BIN_DIR where the `sakiori` command goes (default: ~/.local/bin) # SAKIORI_RELEASE_BASE_URL https:// mirror of the origin, serving the same # v/ and channels// layout # # The archive is unpacked into $SAKIORI_HOME/runtime/versions/, the # same layout `sakiori service install` uses, so the service reuses this # download instead of fetching the release again. The command on the PATH, # `sakiori`, is a script that runs the executable of the same name inside. # # Everything below is one braced group that closes on the last line, so a download # cut short anywhere is a syntax error and runs nothing; even a cut inside a line # that leaves a real command name, such as `script` of `script_target`. { fail() { printf '\nsakiori install: %s\n' "$1" >&2 exit 1 } step() { if "$interactive"; then printf '\r\033[2K %s%s%s' "$muted" "$1" "$reset" >&2 else printf ' %s\n' "$1" >&2; fi } # Every transfer is HTTPS with TLS 1.2 or later. curl's --proto also governs redirects; wget's # --https-only does not, so its response headers are checked and the download refused after the # fact on any Location naming a scheme other than https, reported before anything the insecure # hop answered, a 404 included. refuse_insecure_redirect() { if grep -iE '^ *location: *[a-z][a-z0-9+.-]*:' "$2" 2>/dev/null | grep -qivE '^ *location: *https:'; then rm -f "$3" "$2" fail "GET $1 was redirected off HTTPS" fi } # Sleeps 1, 2, then 4 s before retrying a 408, 429, 5xx, or network error (000). Not curl # --retry: older curl waits out any Retry-After, however long. A redirect curl refused, off # HTTPS or past the limit, ends with its 3xx and is not retried. backoff() { case "$1" in 000 | 408 | 429 | 5??) ;; *) return 1 ;; esac [ "$2" -lt 4 ] && sleep "$((1 << ($2 - 1)))" } # Exit 44 on a 404 so callers can tell "no such asset" from a network failure. fetch() { if [ "$downloader" = curl ]; then attempt=1 while :; do code=0; status="$(curl --proto '=https' --tlsv1.2 -sSL --max-redirs "$max_redirects" -w '%{http_code}' "$1" -o "$2")" || code=$? case "$code:$status" in 0:2??) break ;; esac backoff "$status" "$attempt" || break attempt=$((attempt + 1)) done [ "$code" -eq 0 ] || return 1 case "$status" in 2??) return 0 ;; 404) return 44 ;; *) printf 'GET %s returned HTTP %s\n' "$1" "$status" >&2; return 1 ;; esac fi wget_status=0 wget --https-only --secure-protocol=TLSv1_2 -q --max-redirect="$max_redirects" --server-response "$1" -O "$2" 2>"$2.headers" || wget_status=$? refuse_insecure_redirect "$1" "$2.headers" "$2" if [ "$wget_status" -eq 0 ]; then rm -f "$2.headers"; return 0; fi if grep -q ' 404 ' "$2.headers" 2>/dev/null; then rm -f "$2.headers"; return 44; fi cat "$2.headers" >&2; rm -f "$2.headers"; return 1 } mb() { tenths=$((($1 * 10 + 524288) / 1048576)) printf '%s.%s' "$((tenths / 10))" "$((tenths % 10))" } download_start() { if [ "$downloader" = curl ]; then curl --proto '=https' --tlsv1.2 -fsSL --max-redirs "$max_redirects" -w '%{http_code}' -D "$2.headers" "$1" -o "$2" >"$2.status" 2>"$2.errors" & else wget --https-only --secure-protocol=TLSv1_2 -q --max-redirect="$max_redirects" --server-response "$1" -O "$2" 2>"$2.headers" & fi download_pid=$! } # Reaps the finished download, then restarts it if curl hit a transient error. download_retry() { result=0; wait "$download_pid" || result=$? download_pid= [ "$result" -ne 0 ] && [ -f "$2.status" ] && backoff "$(cat "$2.status")" "$attempt" || return 1 attempt=$((attempt + 1)) download_start "$1" "$2" } # Poll the file written by the downloader; no progress-output parsing or extra request. download() { if ! "$interactive"; then fetch "$1" "$2"; return; fi attempt=1 download_start "$1" "$2" previous=-1 cr="$(printf '\r')" while kill -0 "$download_pid" 2>/dev/null || download_retry "$1" "$2"; do bytes=0; total=0 if [ -f "$2" ]; then bytes="$(wc -c < "$2")"; fi if [ -f "$2.headers" ]; then while read -r key value; do case "$key" in HTTP/*) total=0 ;; [Cc]ontent-[Ll]ength:) total="${value%"$cr"}" ;; esac done < "$2.headers" fi case "$total" in ''|*[!0-9]*) total=0 ;; esac if [ "$bytes" -ne "$previous" ]; then if [ "$total" -gt 0 ]; then percent=$((bytes * 100 / total)); [ "$percent" -le 100 ] || percent=100 filled=$((percent * 32 / 100)); bar=; rest=; n=0 while [ "$n" -lt 32 ]; do if [ "$n" -lt "$filled" ]; then bar="${bar}■"; else rest="${rest}·"; fi n=$((n + 1)) done printf '\r\033[2K %s%s%s%s%s %3d%% %s%s / %s MB%s' "$accent" "$bar" "$reset$muted" "$rest" "$reset" "$percent" "$muted" "$(mb "$bytes")" "$(mb "$total")" "$reset" >&2 else printf '\r\033[2K %sDownloading%s %s MB' "$muted" "$reset" "$(mb "$bytes")" >&2 fi previous="$bytes" fi sleep 0.1 done if [ "$downloader" = wget ]; then refuse_insecure_redirect "$1" "$2.headers" "$2"; fi if [ "$result" -ne 0 ]; then printf '\n' >&2 if [ -f "$2.errors" ]; then cat "$2.errors" >&2; else cat "$2.headers" >&2; fi return "$result" fi size="$(mb "$(wc -c < "$2")")" printf '\r\033[2K %s■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■%s 100%% %s%s / %s MB%s\n' "$accent" "$reset" "$muted" "$size" "$size" "$reset" >&2 rm -f "$2.headers" "$2.errors" "$2.status" } # The `sakiori` script, which execs the executable by absolute path: through a symlink, argv0 is # the link, and the Cursor SDK looks for its helpers beside it. `sakiori update` and # `sakiori install` write the same script. The x keeps a trailing newline in the path through # command substitution. launcher_script() { quoted="$(printf '%sx' "$1" | sed "s/'/'\\\\''/g")" printf '%s%s%s' "$launcher_head" "${quoted%x}" "$launcher_tail" } # The executable an unedited launcher script at $1 runs; a user's own wrapper prints nothing. script_target() { [ -f "$1" ] && [ ! -L "$1" ] && [ "$(wc -c < "$1")" -le 4096 ] || return 0 contents="$(cat "$1"; printf x)" contents="${contents%x}" case "$contents" in "$launcher_head"*"$launcher_tail") ;; *) return 0 ;; esac quoted="${contents#"$launcher_head"}" unquoted="$(printf '%sx' "${quoted%"$launcher_tail"}" | sed "s/'\\\\''/'/g")" unquoted="${unquoted%x}" [ -n "$unquoted" ] && [ "$(launcher_script "$unquoted"; printf x)" = "${contents}x" ] && printf '%s' "$unquoted" return 0 } # Exactly //sakiori ($1 under $2), or the t3 an earlier version installed # there, never another program inside a version. version_executable() { case "$1" in "$2"/*) ;; *) return 1 ;; esac case "${1#"$2"/}" in */*/*) return 1 ;; */sakiori | */t3) return 0 ;; *) return 1 ;; esac } # Whether $link is this home's own, or the previous default home's: a launcher script or symlink # into its versions, or a symlink to the desktop app's launcher. Anything else (a user's script, a # copied binary) belongs to someone else. owned_link() { if [ -L "$link" ]; then existing="$(readlink "$link" 2>/dev/null || true)" case "$existing" in /*) ;; *) existing="${bin_dir}/${existing}" ;; esac else existing="$(script_target "$link")" fi [ -n "$existing" ] || return 1 real_versions_dir="$(CDPATH='' cd -P -- "$versions_dir" && pwd)" # Through a symlinked home the path names the alias; resolve its folder to compare. existing_dir="$(CDPATH='' cd -P -- "$(dirname -- "$existing")" 2>/dev/null && pwd || true)" # The text alone counts only without . or .. segments (a pruned version leaves a dangling path). case "$existing" in */./* | */../* | */. | */..) ;; *) if version_executable "$existing" "$versions_dir" || version_executable "$existing" "$real_versions_dir" || [ "$existing" = "${sakiori_home}/bin/sakiori" ] || version_executable "$existing" "${previous_home}/runtime/versions" || [ "$existing" = "${previous_home}/bin/sakiori" ]; then return 0; fi ;; esac [ -n "$existing_dir" ] && version_executable "${existing_dir}/${existing##*/}" "$real_versions_dir" } # The folder a shell reads its startup file from: $1 when set and safe to paste, else $2 when # unset. A set but unsafe folder prints nothing, since the default would never be read. read_from() { case "$1" in "") printf '%s' "$2" ;; /*) case "$1" in *[!A-Za-z0-9_./-]*) ;; *) printf '%s' "$1" ;; esac ;; esac } main() { set -eu base_url="${SAKIORI_RELEASE_BASE_URL:-https://releases.sakiori.app}" base_url="${base_url%/}" sakiori_home="${SAKIORI_HOME:-$HOME/.sakiori}" # The home before Sakiori had its own: an earlier version linked the command into it. previous_home="$HOME/.t3" case "$sakiori_home" in /*) ;; *) # Preserve cwd's trailing newlines; trim only pwd's newline and the sentinel. sakiori_cwd="$(pwd -P && printf .)" sakiori_home="${sakiori_cwd%??}/$sakiori_home" ;; esac bin_dir="${SAKIORI_INSTALL_BIN_DIR:-$HOME/.local/bin}" # Trailing slashes would put `//` in the paths the command guard compares. while [ "${sakiori_home%/}" != "$sakiori_home" ] && [ "$sakiori_home" != / ]; do sakiori_home="${sakiori_home%/}"; done while [ "${bin_dir%/}" != "$bin_dir" ] && [ "$bin_dir" != / ]; do bin_dir="${bin_dir%/}"; done # Absolute, so the PATH hint never adds a folder that changes with the working directory. case "$bin_dir" in /*) ;; *) bin_dir="$PWD/$bin_dir" ;; esac # The most redirects a download follows: a mirror may redirect. max_redirects=5 # ANSI stays on stderr, so `curl ... | sh` still gets progress. interactive=false if [ -t 2 ] && [ "${TERM:-}" != dumb ]; then interactive=true; fi reset='' bold='' muted='' accent='' green='' if "$interactive" && [ -z "${NO_COLOR:-}" ]; then reset="$(printf '\033[0m')"; bold="$(printf '\033[1m')" muted="$(printf '\033[2m')"; accent="$(printf '\033[94m')"; green="$(printf '\033[32m')" fi # URL schemes are case-insensitive. case "$base_url" in [Hh][Tt][Tt][Pp][Ss]://*) ;; *) fail "SAKIORI_RELEASE_BASE_URL must be an https:// URL" ;; esac if "$interactive"; then printf '\n%s' "$bold" >&2 printf ' %s\n' '██████████ ████████ ' >&2 printf ' %s\n' ' ███ ▄██▀ Sakiori' >&2 printf ' %s%s %sCLI installer%s\n' ' ███ ████▄ ' "$reset" "$muted" "$reset$bold" >&2 printf ' %s\n' ' ███ ▄ ███' >&2 printf ' %s\n' ' ███ ███████▀ ' >&2 printf '%s\n' "$reset" >&2 fi step "Finding your release..." case "$(uname -s)" in Darwin) platform="darwin" ;; Linux) platform="linux" ;; *) fail "unsupported operating system $(uname -s); use the desktop app" ;; esac case "$(uname -m)" in arm64 | aarch64) arch="arm64" ;; x86_64 | amd64) arch="x64" ;; *) fail "unsupported architecture $(uname -m)" ;; esac if [ "${platform}-${arch}" = darwin-x64 ]; then fail "there is no Sakiori build for Intel Macs; see https://sakiori.app/download" fi command -v tar >/dev/null 2>&1 || fail "tar is required" if command -v sha256sum >/dev/null 2>&1; then checksum() { sha256sum < "$1" | cut -d' ' -f1; } elif command -v shasum >/dev/null 2>&1; then checksum() { shasum -a 256 < "$1" | cut -d' ' -f1; } else fail "sha256sum or shasum is required" fi # Of the wgets, only GNU Wget 1.x prints the response headers fetch reads: # BusyBox wget rejects the options, and Wget2 prints no headers under -q, so # neither could tell a 404 or a redirect off HTTPS. if command -v curl >/dev/null 2>&1; then downloader=curl elif command -v wget >/dev/null 2>&1 && wget --version 2>/dev/null | grep -q '^GNU Wget 1\.'; then downloader=wget else fail "curl or GNU Wget 1.x is required" fi channel="${SAKIORI_CHANNEL:-nightly}" version="${SAKIORI_VERSION:-}" if [ -z "$version" ]; then # Each channel's latest.json names its newest build. The version must be # one of the requested train, so a nightly install can never pick up a # build of another train by a publishing mistake. case "$channel" in nightly) ;; *) fail "SAKIORI_CHANNEL must be nightly, the only channel this distribution publishes" ;; esac tmp_index="$(mktemp)" fetch "${base_url}/channels/${channel}/latest.json" "$tmp_index" || { rm -f "$tmp_index"; fail "could not read the ${channel} release index"; } version="$(sed -n 's/.*"version": *"\([0-9][0-9.]*[0-9]\(-[0-9A-Za-z.-]*\)\{0,1\}\)".*/\1/p' "$tmp_index" | head -n 1)" rm -f "$tmp_index" case "$version" in *-nightly.*) version_channel=nightly ;; *-preview.*) version_channel=preview ;; *) version_channel=stable ;; esac [ -n "$version" ] && [ "$version_channel" = "$channel" ] || fail "could not find a ${channel} release; set SAKIORI_VERSION" fi case "$version" in *-preview.*) printf '%s\n' \ "Sakiori ${version} is a preview build." \ " Preview builds are cut by maintainers from unreleased branches to exercise the release" \ " pipeline. They can be broken, receive no fixes, and are never offered as updates." \ " Set SAKIORI_CHANNEL=nightly (the default) for a supported build." >&2 if [ "$channel" != "preview" ] && [ -z "${SAKIORI_VERSION:-}" ]; then fail "refusing a preview build that was not explicitly requested" fi ;; esac stem="sakiori-${version}-${platform}-${arch}" archive="${stem}.tar.gz" versions_dir="${sakiori_home}/runtime/versions" target_dir="${versions_dir}/${version}" if [ -f "${target_dir}/.install-complete" ] && [ "$(cat "${target_dir}/.install-complete")" = "$version" ]; then step "Version ${version} is already downloaded." else mkdir -p "$versions_dir" staging="$(mktemp -d "${versions_dir}/.staging-XXXXXX")" download_pid= trap '[ -z "$download_pid" ] || { kill "$download_pid" 2>/dev/null || true; wait "$download_pid" 2>/dev/null || true; }; rm -rf "$staging"' EXIT trap 'printf "\n" >&2; exit 130' INT trap 'printf "\n" >&2; exit 143' TERM if "$interactive"; then printf '\r\033[2K' >&2; fi printf ' %sInstalling%s Sakiori %s%s%s\n\n' "$muted" "$reset" "$bold" "$version" "$reset" >&2 step "Downloading..." fetch_status=0 fetch "${base_url}/v${version}/SHA256SUMS" "${staging}/SHA256SUMS" || fetch_status=$? if [ "$fetch_status" -eq 44 ]; then fail "Sakiori ${version} is not published" elif [ "$fetch_status" -ne 0 ]; then fail "could not download the release checksums" fi # The checksums list exactly the files a release carries; a release can be # published without some platforms' builds. expected="$(grep " \*\{0,1\}${archive}\$" "${staging}/SHA256SUMS" | cut -d' ' -f1)" [ -n "$expected" ] || fail "Sakiori ${version} has no build for ${platform}-${arch}" download "${base_url}/v${version}/${archive}" "${staging}/${archive}" || fail "could not download ${archive}" step "Verifying the download..." actual="$(checksum "${staging}/${archive}")" [ "$actual" = "$expected" ] || fail "checksum mismatch for ${archive}" step "Extracting Sakiori..." tar -xzf "${staging}/${archive}" -C "$staging" --strip-components=1 rm -f "${staging}/${archive}" "${staging}/SHA256SUMS" if ! run_error="$("${staging}/sakiori" --version 2>&1 >/dev/null)"; then # The dynamic loader names a system library the machine lacks, such as libatomic.so.1. missing="$(printf '%s\n' "$run_error" | sed -n 's/.*error while loading shared libraries: \([^:]*\):.*/\1/p' | head -n 1)" [ -z "$missing" ] || fail "the downloaded executable needs the system library ${missing}; install it, then run the installer again" printf '%s\n' "$run_error" >&2 fail "the downloaded executable does not run" fi printf '%s\n' "$version" > "${staging}/.install-complete" rm -rf "$target_dir" mv "$staging" "$target_dir" # When a concurrent install published first, mv moved this copy inside it; keep only theirs. rm -rf "${target_dir}/${staging##*/}" trap - EXIT fi step "Setting up the sakiori command..." mkdir -p "$bin_dir" nl=' ' launcher_head="#!/bin/sh${nl}exec '" launcher_tail="' \"\$@\"${nl}" link="${bin_dir}/sakiori" slot_empty=true if [ -e "$link" ] || [ -L "$link" ]; then slot_empty=false owned_link || fail "Sakiori ${version} is installed in ${target_dir}, but ${link} is not this installer's and was left as it is; remove it and run the installer again, or set SAKIORI_INSTALL_BIN_DIR to another directory" fi launcher_tmp="$(mktemp "${bin_dir}/.sakiori.XXXXXX")" trap 'rm -f "$launcher_tmp"' EXIT trap 'exit 130' INT trap 'exit 143' TERM launcher_script "${target_dir}/sakiori" > "$launcher_tmp" chmod 755 "$launcher_tmp" # An empty spot is hard-linked without -f, so a file that appears after the check above is kept. if "$slot_empty"; then ln "$launcher_tmp" "$link" || fail "could not write ${link}; something else was created there" else # Known race, accepted: a file written here after the ownership check above is overwritten. # mv replaces a legacy symlink rather than writing through it. mv -f "$launcher_tmp" "$link" fi rm -f "$launcher_tmp" trap - EXIT # Pruning keeps what this launcher points at; a background service cannot see bin_dir otherwise. launcher="$(CDPATH='' cd -P -- "$bin_dir" && pwd)/sakiori" launchers="${sakiori_home}/runtime/launchers" grep -qxF "$launcher" "$launchers" 2>/dev/null || printf '%s\n' "$launcher" >> "$launchers" if "$interactive"; then printf '\r\033[2K' >&2; fi printf ' %sInstalled Sakiori %s%s\n\n' "$green" "$version" "$reset" >&2 # A command to paste for zsh, Linux bash, or fish, like `sakiori install` prints. macOS bash # reads only one of several startup files, so no single file is safe to change there. path_command= path_rest="$bin_dir" path_prefix= case "$bin_dir" in "$HOME"/*) path_rest="${bin_dir#"$HOME"}" path_prefix='$HOME' ;; esac case "$path_rest" in *[!A-Za-z0-9_./-]*) path_rest= ;; esac if [ -n "$path_rest" ]; then # Last on PATH, so it never shadows another sakiori. path_line="export PATH=\"\$PATH:${path_prefix}${path_rest}\"" shell_name="${SHELL:-}" case "${shell_name##*/}" in zsh) # zsh reads .zshrc from ZDOTDIR when it is set; ~/.zshrc would never be read then. zsh_dir="$(read_from "${ZDOTDIR:-}" '~')" [ -z "$zsh_dir" ] || path_command="printf '\\n%s\\n' '${path_line}' >> ${zsh_dir}/.zshrc" ;; bash) [ "$platform" = darwin ] || path_command="printf '\\n%s\\n' '${path_line}' >> ~/.bashrc" ;; fish) # --path changes $PATH itself; fish_user_paths would go ahead of the system folders. config_dir="$(read_from "${XDG_CONFIG_HOME:-}" '~/.config')" fish_dir="${config_dir}/fish" [ -z "$config_dir" ] || path_command="mkdir -p ${fish_dir} && printf '\\n%s\\n' 'fish_add_path --append --path ${path_prefix}${path_rest}' >> ${fish_dir}/config.fish" ;; esac fi # A sakiori earlier on PATH runs instead; the PATH command adds this folder last. # A PATH entry with a trailing slash gives `dir//sakiori`. first_command="$(command -v sakiori 2>/dev/null | tr -s / || true)" case "$first_command" in "" | "$link") first_command= ;; esac if [ -n "$first_command" ]; then printf ' Another sakiori at %s runs first, so run %s%s%s to use this one.\n\n' "$first_command" "$bold" "$link" "$reset" else case ":${PATH}:" in *":${bin_dir}:"* | *":${bin_dir}/:"*) printf ' Run %ssakiori%s to use Sakiori here, or %ssakiori up%s to use it from your other devices.\n\n' "$bold" "$reset" "$bold" "$reset" ;; *) if [ -n "$path_command" ]; then printf ' %s is not on your PATH yet. Add it with:\n\n %s\n\n Then open a new terminal and run %ssakiori%s.\n\n' "$bin_dir" "$path_command" "$bold" "$reset" else printf ' Add %s to your PATH, then run %ssakiori%s.\n\n' "$bin_dir" "$bold" "$reset" fi ;; esac fi } main "$@" }