#!/bin/sh
#
# Install a prebuilt hird release binary — no Rust toolchain required.
#
#   curl -fsSL https://raw.githubusercontent.com/aoprisan/hird/main/scripts/get.sh | sh
#   curl -fsSL https://raw.githubusercontent.com/aoprisan/hird/main/scripts/get.sh | sh -s -- --install-skill
#
# Downloads the release asset for this machine's platform, verifies it
# against the release's own checksum, and hands the binary its `--install`,
# so the result is byte-for-byte what scripts/install.sh produces: a
# standalone snapshot at ~/.local/bin/hird. Arguments are forwarded to
# `hird --install`, which is how `--install-skill` works here too.
#
# `--server` also installs hird-server, the central queue and the software
# factory, from the same release and checked the same way, next to it:
#
#   curl -fsSL https://raw.githubusercontent.com/aoprisan/hird/main/scripts/get.sh | sh -s -- --server
#   hird-server quickstart
#
# HIRD_VERSION pins a release tag (default: the latest release):
#
#   HIRD_VERSION=v0.3.1 sh scripts/get.sh
#
# Building from source instead is scripts/install.sh, which needs a Rust
# toolchain and nothing here.

set -eu

repo="aoprisan/hird"

# `--server` is this script's own; everything else goes to `hird --install`.
server=no
for arg do
    shift
    case $arg in
        --server) server=yes ;;
        *) set -- "$@" "$arg" ;;
    esac
done

say() { printf '%s\n' "$*" >&2; }
die() {
    say "get.sh: $*"
    exit 1
}

fetch() {
    if command -v curl >/dev/null 2>&1; then
        curl -fsSL "$1"
    elif command -v wget >/dev/null 2>&1; then
        wget -qO- "$1"
    else
        die "neither curl nor wget is available to download with"
    fi
}

os="$(uname -s)"
arch="$(uname -m)"
case "$os/$arch" in
    Linux/x86_64) target=x86_64-unknown-linux-musl ;;
    Linux/aarch64 | Linux/arm64) target=aarch64-unknown-linux-musl ;;
    # Releases carry no Intel Mac binary. Apple silicon under a Rosetta
    # shell also reports x86_64, so ask the kernel before refusing.
    Darwin/x86_64)
        if [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || echo 0)" = 1 ]; then
            target=aarch64-apple-darwin
        else
            die "no prebuilt binary for an Intel Mac; scripts/install.sh builds from source (needs a Rust toolchain)"
        fi
        ;;
    Darwin/arm64) target=aarch64-apple-darwin ;;
    *) die "no prebuilt binary for $os/$arch; scripts/install.sh builds from source (needs a Rust toolchain)" ;;
esac

tag="${HIRD_VERSION:-}"
if [ -z "$tag" ]; then
    tag="$(fetch "https://api.github.com/repos/$repo/releases/latest" |
        sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p' | head -n 1)" || true
    [ -n "$tag" ] || die "could not resolve the latest release; pin one with HIRD_VERSION=v0.x.y"
fi

tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT
cd "$tmp"

# Download one binary's archive for this platform, check it against the
# release's checksum, and unpack it here.
get() {
    asset="$1-$tag-$target.tar.gz"
    url="https://github.com/$repo/releases/download/$tag/$asset"
    say "downloading $asset"
    if ! fetch "$url" >"$asset"; then
        [ "$1" = hird-server ] &&
            die "release $tag has no $asset; hird-server binaries start with v0.3.1 (or: cargo install hird-server --locked)"
        die "release $tag has no asset $asset ($url)"
    fi
    fetch "$url.sha256" >"$asset.sha256" || die "release $tag has no checksum for $asset"
    if command -v sha256sum >/dev/null 2>&1; then
        sha256sum -c "$asset.sha256" >/dev/null || die "checksum mismatch on $asset"
    elif command -v shasum >/dev/null 2>&1; then
        shasum -a 256 -c "$asset.sha256" >/dev/null || die "checksum mismatch on $asset"
    else
        say "neither sha256sum nor shasum is available; skipping checksum verification"
    fi
    tar -xzf "$asset"
}

# Both downloads happen before anything is installed, so a release missing
# the server archive leaves the machine as it was.
get hird
[ "$server" = no ] || get hird-server

./hird --install "$@"

if [ "$server" = yes ]; then
    bin_dir="$HOME/.local/bin"
    mkdir -p "$bin_dir"
    # Copied to a temporary name and renamed over the old one, so a server
    # that is running keeps its binary rather than having it rewritten.
    cp hird-server "$bin_dir/.hird-server.new"
    chmod 755 "$bin_dir/.hird-server.new"
    mv -f "$bin_dir/.hird-server.new" "$bin_dir/hird-server"
    say "installed $bin_dir/hird-server — try: hird-server quickstart"
fi
