#!/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://jason.vn/download | sh): it prepares the machine itself"
  prepare "$bin/jason"; return 0
fi
case "$(uname -s)-$(uname -m)" in
  Linux-x86_64) file=jason.gz; sha=e11cef4fb98560956679e8a7c9d0087f6238118646e60435191fcd062fecdc4c; url=https://jason.vn/builds/c7a02a42993e1166011911046aea9a7be269ffd8/x86_64-unknown-linux-gnu/jason.gz ;;
  Linux-aarch64 | Linux-arm64) file=jason.gz; sha=3e314133711688b6f1d4898f006f4c4d13d0a6ba63e4034604bc77e532989944; url=https://jason.vn/builds/c7a02a42993e1166011911046aea9a7be269ffd8/aarch64-unknown-linux-gnu/jason.gz ;;
  Darwin-arm64) file=Jason.app.zip; sha=e32d34bf3dc516bfb44e84c409aa50c6979017d2539cd9b720c1ef30cc995b73; url=https://jason.vn/builds/6c1eb8029fcf2ad93d48cc94419e67ff17519fc8/aarch64-apple-darwin/Jason.app.zip ;;
  Darwin-x86_64) file=Jason.app.zip; sha=ec0e9855b6232582a6519ec5534e18a4d23d605047858553979c6677c2160066; url=https://jason.vn/builds/6c1eb8029fcf2ad93d48cc94419e67ff17519fc8/x86_64-apple-darwin/Jason.app.zip ;;
  *) fail "no build for $(uname -s) $(uname -m); open https://jason.vn/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 "$@"
