#!/usr/bin/env bash
#
# zork one-line installer (ZORK 1 client)
#   curl -fsSL https://zork.app/install.sh | sh
#

set -eu

DL="${ZORK_DL:-https://dl.zork.app}"
INSTALL_DIR="${ZORK_INSTALL_DIR:-$HOME/.local/bin}"

usage() {
  cat <<'EOF'
zork installer

  curl -fsSL https://zork.app/install.sh | sh

Environment:
  ZORK_INSTALL_DIR   install directory (default: ~/.local/bin)
  ZORK_DL            download host (default: https://dl.zork.app)
EOF
}

for arg in "$@"; do
  case "$arg" in
    -h|--help) usage; exit 0 ;;
    *) printf 'zork: unknown installer arg: %s\n' "$arg" >&2; usage; exit 2 ;;
  esac
done

RED='\033[0;31m'; GREEN='\033[0;32m'; BOLD='\033[1m'; NC='\033[0m'
info(){ printf "${GREEN}[+]${NC} %s\n" "$*"; }
die(){ printf "${RED}[x]${NC} %s\n" "$*" >&2; exit 1; }

need(){ command -v "$1" >/dev/null 2>&1 || die "missing required command: $1"; }
need curl
need awk
need grep
need install
need mktemp

OS="$(uname -s)"
ARCH="$(uname -m)"
case "$OS-$ARCH" in
  Linux-x86_64|Linux-amd64) asset="zork-linux-x64" ;;
  # Apple Silicon ships a prebuilt binary (opt-in per release, tracked by its own
  # VERSION-zork-macos-arm64 pointer). Intel macs and linux-aarch64 are still a
  # fast-follow — until then, build from source.
  Darwin-arm64|Darwin-aarch64) asset="zork-macos-arm64" ;;
  Darwin-x86_64)
    die "no prebuilt binary for Intel macs yet — it's a fast-follow.
      On Apple Silicon this installs; on Intel, build from source for now." ;;
  Linux-aarch64|Linux-arm64)
    die "no prebuilt linux-arm64 binary yet — it's a fast-follow.
      Watch https://zork.app for the release." ;;
  *) die "unsupported platform: $OS-$ARCH" ;;
esac

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

info "platform: $asset"
# Per-asset "latest" pointer: platforms track their latest independently (macOS
# ships per-release, so it can lag linux). Linux keeps the bare VERSION file;
# every other asset reads VERSION-<asset>.
case "$asset" in
  zork-linux-x64) ver_file="VERSION" ;;
  *) ver_file="VERSION-$asset" ;;
esac
VERSION="$(curl -fsSL "$DL/$ver_file")" || die "no published release for $asset yet (this platform ships per-release).
      Build from source, or watch https://zork.app:
        git clone https://github.com/zork-app/zork && cargo build --release --bin zork"
base="$DL/v$VERSION"
info "latest: v$VERSION"

curl -fsSL "$base/SHA256SUMS" -o "$tmp/SHA256SUMS" || die "could not fetch checksums"

# macOS is opt-in per release — a given tag may be linux-only. If this version
# didn't ship our asset, say so plainly instead of a checksum-list error.
if ! grep -q "  $asset\$" "$tmp/SHA256SUMS"; then
  case "$asset" in
    zork-macos-*) die "v$VERSION didn't ship a macOS binary (it's built per-release).
      Try again after a release that includes mac, or build from source:
        git clone https://github.com/zork-app/zork && cargo build --release --bin zork" ;;
    *) die "v$VERSION has no $asset asset" ;;
  esac
fi

sha256_file() {
  if command -v sha256sum >/dev/null 2>&1; then
    sha256sum "$1" | awk '{print $1}'
  else
    shasum -a 256 "$1" | awk '{print $1}'
  fi
}

# Fetch $1 to $2 and verify it against SHA256SUMS (integrity over HTTPS).
download_verified() {
  name="$1"; out="$2"
  want="$(grep "  $name\$" "$tmp/SHA256SUMS" | awk '{print $1}')"
  [ -n "$want" ] || die "$name not listed in SHA256SUMS"
  curl -fsSL "$base/$name" -o "$out" || die "download failed: $name"
  got="$(sha256_file "$out")"
  [ "$want" = "$got" ] || die "checksum mismatch for $name"
  info "verified: $name"
}

download_verified "$asset" "$tmp/zork"

mkdir -p "$INSTALL_DIR"
install -m 0755 "$tmp/zork" "$INSTALL_DIR/zork"
printf "\n${BOLD}zork v%s installed -> %s/zork${NC}\n" "$VERSION" "$INSTALL_DIR"

case ":$PATH:" in
  *":$INSTALL_DIR:"*) path_note="" ;;
  *) path_note="  Add to PATH:  export PATH=\"$INSTALL_DIR:\$PATH\"" ;;
esac

printf "\n${BOLD}Ready.${NC} No systemd, no config — the first zork command spawns the daemon\n"
cat <<EOF
and mints your identity. The installer does nothing else — no MCP
registration, no background services.

  Pair another machine:      zork pair
  On the other machine:      zork join <code>

Using Claude Code? MCP registration is a separate, explicit opt-in — see
https://zork.app/mcp when you want it.

$path_note
EOF
