#!/bin/sh # SECTOR installer. # # curl -fsSL https://sector.social/install | sh # # What it does, and all it does: # 1. works out your OS and CPU (macOS or Linux; arm64 or x86-64); # 2. downloads one file, the `sector` client, and the list of SHA-256 sums; # 3. refuses to go on unless the file's SHA-256 matches the list; # 4. puts it in a directory on your PATH as `sector` (mode 0755), so that # typing `sector` opens the board. # # Where it installs: # - SECTOR_INSTALL_DIR, if you set it; otherwise # - the first of ~/.local/bin, ~/bin, /opt/homebrew/bin, /usr/local/bin that # is already on your PATH and that you can write to; otherwise # - ~/.local/bin, and ONE marked line is appended to your shell's start-up # file so that directory is on your PATH from the next terminal on. The # script names the file it edited. Set SECTOR_NO_MODIFY_PATH=1 to skip the # edit and get the line to add printed instead. # # It never uses sudo, starts nothing in the background, sends nothing about # you anywhere, and writes no configuration. Run it again to upgrade. # # Environment: # SECTOR_INSTALL_DIR install here instead (no start-up file is edited) # SECTOR_NO_MODIFY_PATH set to 1: never edit a shell start-up file # SECTOR_BASE_URL download from here instead (for testing a preview) # # To remove: delete the `sector` file, and the line marked # "# added by the SECTOR installer" if one was added. Settings and your key # are in ~/.config/sector. # # The whole script is one function, called on the last line, so a download # that is cut short runs nothing. set -eu MARKER='# added by the SECTOR installer' say() { printf '%s\n' "$*"; } err() { printf 'sector install: %s\n' "$*" >&2; } die() { err "$*"; exit 1; } have() { command -v "$1" >/dev/null 2>&1; } # fetch URL FILE: download with curl, else wget. Non-zero on any failure. fetch() { if have curl; then curl -fsSL "$1" -o "$2" elif have wget; then wget -q -O "$2" "$1" else die "need curl or wget to download" fi } # sha256_of FILE: print the file's SHA-256 in hex; non-zero if no tool exists. sha256_of() { if have shasum; then shasum -a 256 "$1" | awk '{print $1}' elif have sha256sum; then sha256sum "$1" | awk '{print $1}' else return 1 fi } # on_path DIR: is DIR one of the entries of PATH? on_path() { case ":${PATH:-}:" in *":$1:"*) return 0 ;; *) return 1 ;; esac } # append_once FILE LINE: add the marker and LINE to FILE unless the marker is # already there. Prints "added" or "present"; non-zero if it cannot write. append_once() { ao_file=$1 ao_line=$2 if [ -f "$ao_file" ] && grep -F -q "$MARKER" "$ao_file" 2>/dev/null; then say present return 0 fi mkdir -p "$(dirname "$ao_file")" 2>/dev/null || return 1 printf '\n%s\n%s\n' "$MARKER" "$ao_line" >>"$ao_file" 2>/dev/null || return 1 say added } # rc_files OS: the start-up file(s) of the user's login shell, one per line. rc_files() { case "$(basename "${SHELL:-sh}")" in zsh) say "${ZDOTDIR:-$HOME}/.zshrc" ;; bash) say "$HOME/.bashrc" if [ "$1" = darwin ]; then # Terminal on macOS starts bash as a login shell, which reads the first # of these that exists and not ~/.bashrc. An existing one is used so a # new ~/.bash_profile never hides the user's ~/.profile. if [ -f "$HOME/.bash_profile" ]; then say "$HOME/.bash_profile" elif [ -f "$HOME/.bash_login" ]; then say "$HOME/.bash_login" elif [ -f "$HOME/.profile" ]; then say "$HOME/.profile" else say "$HOME/.bash_profile" fi fi ;; fish) say "${XDG_CONFIG_HOME:-$HOME/.config}/fish/config.fish" ;; *) say "$HOME/.profile" ;; esac } main() { base=${SECTOR_BASE_URL:-https://sector.social} base=${base%/} [ -n "${HOME:-}" ] || die "HOME is not set" case "$(uname -s)" in Darwin) os=darwin ;; Linux) os=linux ;; *) die "unsupported system: $(uname -s). SECTOR runs on macOS and Linux." ;; esac case "$(uname -m)" in arm64 | aarch64) arch=arm64 ;; x86_64 | amd64) arch=amd64 ;; *) die "unsupported CPU: $(uname -m) on $os. SECTOR is built for arm64 and x86-64 (amd64)." ;; esac name="sector-$os-$arch" # Where to install, and whether a start-up file will need the PATH line. need_rc=0 if [ -n "${SECTOR_INSTALL_DIR:-}" ]; then dir=$SECTOR_INSTALL_DIR else dir= for d in "$HOME/.local/bin" "$HOME/bin" /opt/homebrew/bin /usr/local/bin; do if on_path "$d" && [ -d "$d" ] && [ -w "$d" ]; then dir=$d break fi done if [ -z "$dir" ]; then dir="$HOME/.local/bin" need_rc=1 fi fi tmp=$(mktemp -d "${TMPDIR:-/tmp}/sector-install.XXXXXX") || die "cannot make a temporary directory" staged= # shellcheck disable=SC2317 # reached through the trap cleanup() { rm -rf "$tmp" if [ -n "$staged" ]; then rm -f "$staged"; fi } trap cleanup EXIT trap 'exit 1' INT HUP TERM say "Downloading $name from $base ..." fetch "$base/dl/$name" "$tmp/$name" || die "download failed: $base/dl/$name" fetch "$base/dl/SHA256SUMS" "$tmp/SHA256SUMS" || die "download failed: $base/dl/SHA256SUMS" want=$(awk -v n="$name" '$2 == n || $2 == "*" n { print $1; exit }' "$tmp/SHA256SUMS") [ -n "$want" ] || die "SHA256SUMS has no entry for $name; nothing was installed" got=$(sha256_of "$tmp/$name") || die "need shasum or sha256sum to check the download; nothing was installed" if [ "$got" != "$want" ]; then err "checksum mismatch for $name" err " expected $want" err " got $got" die "nothing was installed" fi say "Checksum ok (sha256 $got)." mkdir -p "$dir" || die "cannot create $dir" [ -w "$dir" ] || die "cannot write to $dir (this installer never uses sudo; set SECTOR_INSTALL_DIR to a directory you own)" target="$dir/sector" staged="$dir/.sector.new.$$" # Staged inside the install directory, so the final mv is a rename: a running # `sector` keeps its old file and there is never a half-written one. cp "$tmp/$name" "$staged" || die "cannot write to $dir" chmod 0755 "$staged" if [ "$os" = darwin ] && have xattr; then xattr -d com.apple.quarantine "$staged" 2>/dev/null || true fi mv -f "$staged" "$target" || die "cannot install to $target" staged= if ver=$("$target" -version 2>/dev/null); then say "Installed $ver at $target" else say "Installed at $target" err "warning: $target did not run here; check that $dir allows programs to run" fi ready=1 if on_path "$dir"; then other=$(command -v sector 2>/dev/null || true) if [ -n "$other" ] && [ "$other" != "$target" ]; then err "note: an older sector at $other comes first on your PATH; remove it so \`sector\` runs the new one" fi elif [ "$need_rc" = 1 ] && [ "${SECTOR_NO_MODIFY_PATH:-}" != 1 ]; then ready=0 case "$(basename "${SHELL:-sh}")" in # fish has no export; this form works in every fish version. fish) line="contains \"\$HOME/.local/bin\" \$PATH; or set -gx PATH \"\$HOME/.local/bin\" \$PATH" ;; *) line="export PATH=\"\$HOME/.local/bin:\$PATH\"" ;; esac edited= failed= rc_files "$os" >"$tmp/rc" while IFS= read -r rc; do if state=$(append_once "$rc" "$line"); then if [ "$state" = added ]; then say "Added $dir to your PATH: one line appended to $rc" else say "$rc already has the SECTOR line that adds $dir to your PATH." fi edited=1 else failed="$failed $rc" fi done <"$tmp/rc" if [ -n "$failed" ]; then err "could not write to:$failed" fi if [ -z "$edited" ]; then say "Add this line to your shell's start-up file yourself:" say " $line" fi say "This terminal does not know about it yet. Open a new terminal (or run: exec \$SHELL -l)" else ready=0 say "$dir is not on your PATH. Add this line to your shell's start-up file:" if [ "$dir" = "$HOME/.local/bin" ]; then say " export PATH=\"\$HOME/.local/bin:\$PATH\"" else say " export PATH=\"$dir:\$PATH\"" fi say "then open a new terminal." fi say "" if [ "$ready" = 1 ]; then say "Type sector to open the board." else say "Then type sector to open the board." fi say "You need an invite code to make an account." } main "$@"