#!/bin/sh # Installs `surf` into ~/.surf/bin (ADR-0101). # # curl -fsSL https://git.solenovis.com/henning.liemant/surface/raw/branch/main/scripts/install.sh | sh # # SURF_HOME changes the folder ~/.surf. SURF_VERSION chooses a version: the default is the newest # release. set -eu # The address of the releases. crates/surf-cli/src/toolchain.rs has the same address. RELEASES=${SURF_RELEASES:-https://git.solenovis.com/henning.liemant/surface/releases} fail() { echo "install.sh: $1" >&2 exit 1 } case "$(uname -s) $(uname -m)" in "Darwin arm64") host=aarch64-apple-darwin ;; "Linux x86_64") host=x86_64-unknown-linux-gnu ;; *) fail "surf has no release for $(uname -s) $(uname -m)" ;; esac for tool in curl tar; do command -v "$tool" > /dev/null 2>&1 || fail "this script needs $tool" done # An https address must not go to http after a redirect. A mirror on http keeps its redirects. case "$RELEASES" in https://*) https_only="--proto =https --proto-redir =https" ;; *) https_only= ;; esac # Downloads with the options of this script. The options of https_only are separate words. get() { # shellcheck disable=SC2086 curl -fsSL $https_only "$@" } sha256() { if command -v sha256sum > /dev/null 2>&1; then sha256sum "$1" | cut -d ' ' -f 1 elif command -v shasum > /dev/null 2>&1; then shasum -a 256 "$1" | cut -d ' ' -f 1 else fail "this script needs sha256sum or shasum" fi } # The page of the newest release ends with its tag. if [ -z "${SURF_VERSION:-}" ]; then page=$(get -o /dev/null -w '%{url_effective}' "$RELEASES/latest") || fail "cannot read $RELEASES/latest" SURF_VERSION=${page##*/v} case "$SURF_VERSION" in [0-9]*) ;; *) fail "$RELEASES/latest does not name a version. It goes to $page" ;; esac fi archive=surf-$host.tar.gz address=$RELEASES/download/v$SURF_VERSION work=$(mktemp -d) trap 'rm -rf "$work"' EXIT echo "downloading surf $SURF_VERSION for $host" get -o "$work/SHA256SUMS" "$address/SHA256SUMS" || fail "cannot download $address/SHA256SUMS" get -o "$work/$archive" "$address/$archive" || fail "cannot download $address/$archive" wanted=$(awk -v file="$archive" '$2 == file { print $1 }' "$work/SHA256SUMS") [ -n "$wanted" ] || fail "SHA256SUMS has no line for $archive" found=$(sha256 "$work/$archive") [ "$found" = "$wanted" ] || fail "the SHA-256 of $archive is not correct. Expected $wanted, found $found" tar -xzf "$work/$archive" -C "$work" bin=${SURF_HOME:-$HOME/.surf}/bin mkdir -p "$bin" mv "$work/surf" "$bin/surf" echo "surf $SURF_VERSION is in $bin" case ":$PATH:" in *":$bin:"*) ;; *) echo "Add this line to the profile of your shell:" echo " export PATH=\"$bin:\$PATH\"" ;; esac