#!/bin/sh # The Rebasely CLI installer. Read it first: curl -fsSL https://rebasely.com/install # Install or update: curl -fsSL https://rebasely.com/install | sh # # Needs Node.js 22.18 or later and curl. Writes only under the CLI's home directory, which is # $REBASELY_HOME if set, else ~/.rebasely, or ~/.duragit when that one exists from an earlier install: # /rebasely.mjs the CLI (one file, from https://rebasely.com/cli/rebasely.mjs) # /duragit.mjs the same file, refreshed only if an earlier install left it there # /bin/rebasely a launcher that runs it with node # /bin/duragit the same launcher under the CLI's earlier name # and puts the launchers on your PATH: links in ~/.local/bin or ~/bin when one of them is already # on your PATH, otherwise one line added to your shell's startup file. # To remove it: rm -rf /bin /rebasely.mjs /duragit.mjs, and the line marked # "# rebasely" (or "# duragit"). set -eu fail() { printf 'rebasely install: %s\n' "$*" >&2; exit 1; } [ -n "${HOME:-}" ] || fail "HOME is not set" origin='https://rebasely.com' if [ -n "${REBASELY_HOME:-}" ]; then dir=$REBASELY_HOME elif [ -n "${DURAGIT_HOME:-}" ]; then dir=$DURAGIT_HOME elif [ -e "$HOME/.rebasely" ]; then dir="$HOME/.rebasely" elif [ -e "$HOME/.duragit" ]; then dir="$HOME/.duragit" else dir="$HOME/.rebasely" fi case "$dir" in /*) ;; *) dir="$(pwd)/$dir" ;; esac bin="$dir/bin" command -v curl >/dev/null 2>&1 || fail "curl is required" command -v node >/dev/null 2>&1 || fail "Node.js 22.18 or later is required: https://nodejs.org" ver=$(node -p 'process.versions.node' 2>/dev/null || echo 0.0.0) major=${ver%%.*} rest=${ver#*.} minor=${rest%%.*} case "$major.$minor" in *[!0-9.]* | .* | *.) fail "could not read the Node.js version ($ver)" ;; esac if [ "$major" -lt 22 ] || { [ "$major" -eq 22 ] && [ "$minor" -lt 18 ]; }; then fail "Node.js 22.18 or later is required (this one is $ver): https://nodejs.org" fi mkdir -p "$bin" # .mjs last: node --check reads the extension to know the file is an ES module. tmp="$dir/.rebasely-$$.mjs" trap 'rm -f "$tmp"' EXIT curl -fsSL "$origin/cli/rebasely.mjs" -o "$tmp" || fail "could not download $origin/cli/rebasely.mjs" head -n 1 "$tmp" | grep -q '^#!/usr/bin/env node' || fail "the download is not the Rebasely CLI" node --check "$tmp" >/dev/null 2>&1 || fail "the downloaded CLI does not load; run the installer again" # An earlier install's duragit.mjs is what older ssh ProxyCommand lines run: keep it current. if [ -e "$dir/duragit.mjs" ]; then cp -f "$tmp" "$dir/.duragit-$$.mjs" mv -f "$dir/.duragit-$$.mjs" "$dir/duragit.mjs" fi mv -f "$tmp" "$dir/rebasely.mjs" trap - EXIT # The launchers name the CLI by its full path, quoted for sh. q=$(printf '%s' "$dir/rebasely.mjs" | sed "s/'/'\\\\''/g") for name in rebasely duragit; do printf '#!/bin/sh\nexec node %s "$@"\n' "'$q'" > "$bin/$name" chmod 755 "$bin/$name" done on_path() { case ":$PATH:" in *":$1:"*) return 0 ;; esac; return 1; } ready="" if on_path "$bin"; then ready="$bin" else for d in "$HOME/.local/bin" "$HOME/bin"; do if on_path "$d" && [ -d "$d" ] && [ -w "$d" ]; then # Someone else's rebasely there is left alone, and so is someone else's duragit. if [ -e "$d/rebasely" ] && [ ! -L "$d/rebasely" ]; then continue; fi ln -sf "$bin/rebasely" "$d/rebasely" if [ ! -e "$d/duragit" ] || [ -L "$d/duragit" ]; then ln -sf "$bin/duragit" "$d/duragit"; fi ready="$d" break fi done fi rc="" if [ -z "$ready" ]; then sh_name=$(basename "${SHELL:-sh}") case "$sh_name" in zsh) rc="${ZDOTDIR:-$HOME}/.zshrc" ;; bash) if [ "$(uname -s)" = Darwin ]; then rc="$HOME/.bash_profile"; else rc="$HOME/.bashrc"; fi ;; fish) rc="$HOME/.config/fish/config.fish" ;; *) rc="$HOME/.profile" ;; esac # $HOME stays a variable in the line when the home is under it. case "$bin" in "$HOME"/*) shown="\$HOME${bin#"$HOME"}" ;; *) shown="$bin" ;; esac if [ "$sh_name" = fish ]; then line="fish_add_path \"$shown\"" else line="export PATH=\"$shown:\$PATH\"" fi if [ ! -f "$rc" ] || ! grep -qF "$shown" "$rc"; then mkdir -p "$(dirname "$rc")" printf '\n# rebasely\n%s\n' "$line" >> "$rc" fi fi echo "Rebasely is installed in $bin." if [ -n "$ready" ]; then echo "Next: rebasely auth login" else echo "It is on your PATH in new terminals ($rc). In this one, run:" echo " $line" echo "Then: rebasely auth login" fi