#!/bin/sh
# quarry installer for @ORIGIN@
#
# curl -fsSL @ORIGIN@/install.sh | sh
#
# Downloads the quarry program for this machine from this forge over TLS,
# checks it against the forge's SHA256SUMS, and puts it in
# ~/.local/bin (or $QUARRY_INSTALL_DIR). It never uses sudo and never
# edits a shell profile. It records this forge as quarry's default host.
#
# The checksum comes from the same forge as the program, so it catches a
# corrupted download, not a compromised forge. First install trusts TLS
# to the forge, as every curl | sh does.
#
# Everything is inside main(), called on the last line: a download cut
# short runs nothing.
main() {
origin='@ORIGIN@'
host=${origin#*://}
say() { printf '%s\n' "$*"; }
die() { printf 'quarry installer: %s\n' "$*" >&2; exit 1; }
command -v curl >/dev/null 2>&1 || die "curl is needed and was not found"
os=$(uname -s | tr '[:upper:]' '[:lower:]')
case "$os" in
linux|darwin) ;;
*) die "no build for $os here; Windows and others: download from $origin/cli/" ;;
esac
arch=$(uname -m)
case "$arch" in
x86_64|amd64) arch=amd64 ;;
arm64|aarch64) arch=arm64 ;;
*) die "no build for $arch" ;;
esac
file="quarry-$os-$arch"
dir=${QUARRY_INSTALL_DIR:-$HOME/.local/bin}
cfg=${XDG_CONFIG_HOME:-$HOME/.config}/quarry
tmp=$(mktemp -d "${TMPDIR:-/tmp}/quarry-install.XXXXXX") || die "could not make a temporary directory"
stage=
cleanup() { rm -rf "$tmp"; [ -z "$stage" ] || rm -f "$stage"; }
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM HUP
say "quarry installer for $host"
say " platform $os/$arch"
say " download $origin/cli/$file"
fetch() { curl --proto '=https' --tlsv1.2 --fail --silent --show-error --location --output "$2" "$1"; }
fetch "$origin/cli/$file" "$tmp/$file" || die "could not download $origin/cli/$file"
fetch "$origin/cli/SHA256SUMS" "$tmp/SHA256SUMS" || die "could not download $origin/cli/SHA256SUMS"
# TODO(signatures, design step 12): fetch $origin/cli/SHA256SUMS.minisig
# and have the downloaded program check it against the release key
# compiled into it (`quarry upgrade --verify`) before trusting the sums.
want=$(awk -v f="$file" '$2 == f || $2 == "*" f { print $1; exit }' "$tmp/SHA256SUMS")
[ -n "$want" ] || die "SHA256SUMS does not list $file; nothing installed"
if command -v sha256sum >/dev/null 2>&1; then
got=$(sha256sum "$tmp/$file" | awk '{ print $1 }')
elif command -v shasum >/dev/null 2>&1; then
got=$(shasum -a 256 "$tmp/$file" | awk '{ print $1 }')
else
die "sha256sum or shasum is needed to check the download; nothing installed"
fi
[ "$got" = "$want" ] || die "checksum mismatch for $file (SHA256SUMS says $want, the download is $got); nothing installed"
say " checksum sha256 matches SHA256SUMS (from this same forge: this catches a corrupted download, not a compromised forge)"
mkdir -p "$dir" || die "could not make $dir"
if [ -d "$dir/quarry" ]; then
die "$dir/quarry is a directory (or points at one); nothing installed"
elif [ -e "$dir/quarry" ] && [ ! -f "$dir/quarry" ]; then
die "$dir/quarry exists and is not a regular file; nothing installed"
fi
if [ -e "$dir/quarry" ]; then
# Only a client build is ours to replace (the update path): a forge
# server build, or anything that will not run, is left alone.
have=$("$dir/quarry" version 2>/dev/null </dev/null | head -n 1) || have=
case "$have" in
*"(client)"*) ;;
*)
if [ "${QUARRY_INSTALL_FORCE:-}" = 1 ]; then
say " replacing $dir/quarry (not a client build; QUARRY_INSTALL_FORCE=1)"
else
[ -n "$have" ] || have="it does not print a version"
die "$dir/quarry is not a quarry client build ($have); nothing installed. QUARRY_INSTALL_DIR picks another directory; QUARRY_INSTALL_FORCE=1 replaces it"
fi
;;
esac
fi
stage=$(mktemp "$dir/.quarry.XXXXXX") || die "could not write in $dir"
cp "$tmp/$file" "$stage" || die "could not write in $dir"
chmod 755 "$stage"
mv -f "$stage" "$dir/quarry" || die "could not install to $dir/quarry"
stage=
say " installed $dir/quarry"
[ -d "$cfg" ] || mkdir -p -m 700 "$cfg" || die "could not make $cfg"
( umask 077; printf '{"host":"%s"}\n' "$origin" > "$cfg/config.json" ) || die "could not write $cfg/config.json"
say " default $host"
case ":$PATH:" in
*":$dir:"*) ;;
*)
say ""
say "$dir is not on your PATH. Add this line to your shell profile:"
say " export PATH=\"$dir:\$PATH\""
;;
esac
say ""
"$dir/quarry" version
# Only a machine with no login for this forge needs one; ask the
# installed program rather than guess.
if "$dir/quarry" auth status >/dev/null 2>&1 </dev/null; then
say "Already signed in here: quarry auth status shows as whom."
else
say "Next: quarry auth login"
fi
}
main "$@"