#!/bin/sh
# Awre CLI installer.  Usage:  curl -fsSL https://get.awre.ai/install.sh | sh
#
# Downloads the right prebuilt `awre` binary for this OS+arch from get.awre.ai,
# verifies its sha256, and installs it to ~/.local/bin/awre. POSIX sh - no bash,
# no python, no package manager required. Only needs: sh, curl (or wget),
# sha256sum (or shasum), uname, mkdir, mv, chmod.
#
# Env overrides:
#   AWRE_INSTALL_DIR   where to put the binary   (default: $HOME/.local/bin)
#   AWRE_VERSION       pin a version             (default: contents of /latest)
#   AWRE_BASE_URL      artifact host             (default: https://get.awre.ai)
set -eu

BASE_URL="${AWRE_BASE_URL:-https://get.awre.ai}"
INSTALL_DIR="${AWRE_INSTALL_DIR:-$HOME/.local/bin}"

say()  { printf '%s\n' "awre-install: $*"; }
die()  { printf '%s\n' "awre-install: error: $*" >&2; exit 1; }

# --- downloader ---
if command -v curl >/dev/null 2>&1; then
  dl() { curl -fsSL "$1"; }                 # to stdout
  dlf() { curl -fsSL -o "$2" "$1"; }        # to file
elif command -v wget >/dev/null 2>&1; then
  dl() { wget -qO- "$1"; }
  dlf() { wget -qO "$2" "$1"; }
else
  die "need curl or wget"
fi

# --- sha256 verifier ---
if command -v sha256sum >/dev/null 2>&1; then
  sha256_of() { sha256sum "$1" | awk '{print $1}'; }
elif command -v shasum >/dev/null 2>&1; then
  sha256_of() { shasum -a 256 "$1" | awk '{print $1}'; }
else
  die "need sha256sum or shasum"
fi

# --- detect OS ---
uname_s="$(uname -s)"
case "$uname_s" in
  Darwin) OS="darwin" ;;
  Linux)  OS="linux" ;;
  *) die "unsupported OS '$uname_s' (Linux and macOS only)" ;;
esac

# --- detect arch (with Rosetta + general aliasing) ---
uname_m="$(uname -m)"
case "$uname_m" in
  arm64 | aarch64) ARCH="arm64" ;;
  x86_64 | amd64)  ARCH="x64" ;;
  *) die "unsupported arch '$uname_m'" ;;
esac
# On Apple Silicon under Rosetta 2, uname -m can report x86_64 - prefer native arm64.
if [ "$OS" = "darwin" ] && [ "$ARCH" = "x64" ]; then
  if [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || echo 0)" = "1" ]; then
    ARCH="arm64"
    say "detected Rosetta 2; using native arm64 build"
  fi
fi

PLAT="${OS}-${ARCH}"

# --- resolve version ---
if [ -n "${AWRE_VERSION:-}" ]; then
  VERSION="$AWRE_VERSION"
else
  VERSION="$(dl "${BASE_URL}/latest" | tr -d ' \t\r\n')" \
    || die "could not fetch ${BASE_URL}/latest"
  [ -n "$VERSION" ] || die "empty version from ${BASE_URL}/latest"
fi
say "installing awre ${VERSION} (${PLAT})"

ASSET="awre-${PLAT}"
ASSET_URL="${BASE_URL}/releases/${VERSION}/${ASSET}"
SHA_URL="${ASSET_URL}.sha256"

# --- download to a temp file, verify, then move into place ---
TMP="$(mktemp "${TMPDIR:-/tmp}/awre.XXXXXX")"
trap 'rm -f "$TMP" "$TMP.sha"' EXIT INT TERM

dlf "$ASSET_URL" "$TMP" \
  || die "download failed: $ASSET_URL  (is ${PLAT} built yet?)"

dlf "$SHA_URL" "$TMP.sha" \
  || die "checksum download failed: $SHA_URL"
# the .sha256 file is "<hash>  <name>"; take the first field
WANT="$(awk '{print $1}' "$TMP.sha")"
GOT="$(sha256_of "$TMP")"
[ "$WANT" = "$GOT" ] || die "checksum mismatch (want $WANT, got $GOT) - aborting"

mkdir -p "$INSTALL_DIR"
mv "$TMP" "$INSTALL_DIR/awre"
chmod +x "$INSTALL_DIR/awre"
trap - EXIT INT TERM
rm -f "$TMP.sha"

say "installed to $INSTALL_DIR/awre"

# --- PATH hint ---
case ":${PATH}:" in
  *":${INSTALL_DIR}:"*) ;;
  *)
    say ""
    say "$INSTALL_DIR is not on your PATH. Add this to your shell profile:"
    say "    export PATH=\"$INSTALL_DIR:\$PATH\""
    say "then restart your shell, or run it now to use awre this session."
    ;;
esac

say ""
say "done. Try:  awre ping"
