#!/bin/sh
# Installs Jason on this computer (no sudo), prepares a Linux machine with
# what only root gives (`jason prepare`: sudo once, only what is missing),
# then links it to its person's Jason: `jason link` shows a code to
# confirm in the web app. `sh -s prepare` only prepares, with the Jason
# already installed.
# All in one function called on the last line: a download cut short runs nothing.
set -eu
fail() { echo "jason: $*" >&2; exit 1; }
warn() { echo "jason: $*" >&2; }
# What only root gives a Linux machine is the binary's own list (`jason
# prepare`, docs/nodes.md "The standard"): sudo once, only what is
# missing; a failed need is said, never stops the install. The node runs
# the same list at its start and every hour after.
prepare() {
[ "$(uname -s)" = Linux ] || return 0
"$1" prepare || warn "not everything root gives is here yet: run `sudo jason prepare` later; System › Machines says what is missing"
}
main() {
if [ $# -gt 0 ] && [ "$1" = prepare ]; then
  bin=${JASON_BIN_DIR:-$HOME/.local/bin}
  [ -x "$bin/jason" ] || fail "install Jason first (curl -fsSL https://heyjason.work/download | sh): it prepares the machine itself"
  prepare "$bin/jason"; return 0
fi
case "$(uname -s)-$(uname -m)" in
  Linux-x86_64) file=jason-computer.gz; sha=8af9c867dfa2ae25adc5b057848186d3311e0113110c5a933639bd9ae41503a9; url=https://heyjason.work/builds/a45cf74a1a6cada34c9349c27226270125da0b3e/x86_64-unknown-linux-gnu/jason-computer.gz ;;
  Linux-aarch64 | Linux-arm64) file=jason-computer.gz; sha=77c18bfdaa56c2b463f0a33ad182d4dc4bd95a748da038ee2e0d7e802691641e; url=https://heyjason.work/builds/a45cf74a1a6cada34c9349c27226270125da0b3e/aarch64-unknown-linux-gnu/jason-computer.gz ;;
  Darwin-arm64) file=Jason.app.zip; sha=50bc45ff6dae1da48e1f960647d3715e9bad8db8719794c029f05c5200b0a277; url=https://heyjason.work/builds/41823b27a06986355d3994955c4487e4fffd6cc7/aarch64-apple-darwin/Jason.app.zip ;;
  Darwin-x86_64) file=Jason.app.zip; sha=6d1257f99dce11a973057485bad5568adb5d8e86e388d13e1cbd5e6ea7b24ab5; url=https://heyjason.work/builds/41823b27a06986355d3994955c4487e4fffd6cc7/x86_64-apple-darwin/Jason.app.zip ;;
  *) fail "no build for $(uname -s) $(uname -m); open https://heyjason.work/download in a browser" ;;
esac
command -v curl >/dev/null 2>&1 || fail "curl is required"
work=$(mktemp -d "${TMPDIR:-/tmp}/jason.XXXXXX")
trap 'rm -rf "$work"' EXIT HUP INT TERM
echo "Downloading Jason"
curl -fsSL --retry 2 --connect-timeout 10 -o "$work/$file" "$url" || fail "could not download $url"
if command -v sha256sum >/dev/null 2>&1; then have=$(sha256sum "$work/$file" | cut -d ' ' -f 1)
else have=$(shasum -a 256 "$work/$file" | cut -d ' ' -f 1); fi
[ "$have" = "$sha" ] || fail "the download is damaged (SHA-256 $have, expected $sha); run the line again"
bin=${JASON_BIN_DIR:-$HOME/.local/bin}
mkdir -p "$bin"
case $file in
  *.zip)
    if [ -n "${JASON_APP_DIR:-}" ]; then apps=$JASON_APP_DIR
    elif [ -w /Applications ]; then apps=/Applications
    else apps=$HOME/Applications; fi
    mkdir -p "$apps"
    ditto -x -k "$work/$file" "$work/unpacked"
    rm -rf "$apps/Jason.app.new" "$apps/Jason.app.old"
    mv "$work/unpacked/Jason.app" "$apps/Jason.app.new"
    [ ! -e "$apps/Jason.app" ] || mv "$apps/Jason.app" "$apps/Jason.app.old"
    mv "$apps/Jason.app.new" "$apps/Jason.app"
    rm -rf "$apps/Jason.app.old"
    ln -sfn "$apps/Jason.app/Contents/MacOS/jason" "$bin/jason" ;;
  *)
    gunzip -c "$work/$file" > "$bin/.jason.new.$$"
    chmod 755 "$bin/.jason.new.$$"
    mv -f "$bin/.jason.new.$$" "$bin/jason" ;;
esac
"$bin/jason" version >/dev/null || fail "the downloaded Jason does not run on this computer"
prepare "$bin/jason"
exec "$bin/jason" link </dev/tty
}
main "$@"
