Skip to content
quarry Artifact [e4e55498f5]
Login

Artifact [e4e55498f5]

Artifact e4e55498f5f56ead01dab1912531e78d24fdeff4304686673890376396a8591b:


#!/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.
#
# 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"
  trap 'rm -rf "$tmp"' EXIT INT 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"

  mkdir -p "$dir" || die "could not make $dir"
  chmod 755 "$tmp/$file"
  cp "$tmp/$file" "$dir/.quarry.$$" || die "could not write in $dir"
  mv -f "$dir/.quarry.$$" "$dir/quarry" || { rm -f "$dir/.quarry.$$"; die "could not install to $dir/quarry"; }
  say "  installed   $dir/quarry"

  mkdir -p "$cfg" || die "could not make $cfg"
  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
  say "Next: quarry auth login"
}

main "$@"