#!/bin/sh
# Wagahai installer — https://wagahai.org
#
#   curl -fsSL https://wagahai.org/install.sh | sh
#
# Installs the wagahai client to ~/.local/bin, checksum-verified.
# Free, experimental; probably doesn't work. We use it daily anyway.
set -eu

say() { printf '%s\n' "$*"; }

os=$(uname -s); arch=$(uname -m)
case "$os-$arch" in
  Darwin-arm64) target="aarch64-apple-darwin" ;;
  *)
    say "wagahai is built for Apple Silicon macOS today; $os/$arch isn't — yet."
    say "Tell us to hurry: info@katayama.dev"
    exit 1 ;;
esac

v="0.1.1"
base="https://wagahai.org/dl"
tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT

say "fetching wagahai v$v ($target)…"
curl -fsSL "$base/wagahai-v$v-$target.tar.gz" -o "$tmp/wagahai.tgz"
curl -fsSL "$base/SHA256SUMS" -o "$tmp/SHA256SUMS"
want=$(grep "wagahai-v$v-$target.tar.gz" "$tmp/SHA256SUMS" | cut -d' ' -f1)
got=$(shasum -a 256 "$tmp/wagahai.tgz" | cut -d' ' -f1)
[ "$want" = "$got" ] || { say "✗ checksum mismatch — not installing."; exit 1; }

tar -xzf "$tmp/wagahai.tgz" -C "$tmp"
mkdir -p "$HOME/.local/bin"
install -m 755 "$tmp/wagahai" "$HOME/.local/bin/wagahai"
say "✓ installed: ~/.local/bin/wagahai (v$v, checksum verified)"
case ":$PATH:" in
  *":$HOME/.local/bin:"*) ;;
  *) say "  (add ~/.local/bin to your PATH)" ;;
esac

say ""
say "Meet the cat first — no account, no setup, a fully fictional mailbox:"
say ""
say "    wagahai --demo"
say ""
say "Real mail wants the daemon on an always-on machine, reached over ssh."
say "Early access and instructions: info@katayama.dev · https://wagahai.org"
