#!/bin/sh
# chanio starter — one-line installer for macOS.   https://chanio.com/setup
#   curl -fsSL https://chanio.com/install.sh | sh
# Puts the starter in ~/chanio/bin and makes sure Node and Ollama exist WITHOUT an administrator
# password: if Node is missing it downloads the official Node build into ~/chanio/node; if Ollama is
# missing it downloads the Ollama app into ~/Applications. Then it runs `chanio init` (pulls a small
# local model, creates ~/chanio/notes, schedules the nightly run at 05:00).
set -eu
BASE="${CHANIO_HOME:-$HOME/chanio}"
URL="https://chanio.com/dl/chanio-starter.tgz"
say() { printf '%s\n' "$*"; }
have() { command -v "$1" >/dev/null 2>&1; }
case "$(uname -s)" in
  Darwin) ;;
  *) say "This installer is for macOS. Windows & Android land in 2027 — details at https://chanio.com/setup"; exit 1 ;;
esac
MACOS_MAJOR=$(sw_vers -productVersion 2>/dev/null | cut -d. -f1)
if [ -n "$MACOS_MAJOR" ] && [ "$MACOS_MAJOR" -lt 14 ]; then
  say "This Mac runs macOS $(sw_vers -productVersion). The local model needs Ollama, which requires macOS 14 or newer. Update macOS, or use the in-browser demo at https://chanio.com/try"
  exit 1
fi
TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT
mkdir -p "$BASE/bin"

# ── Node 18+: use the one you have, else download the official build into ~/chanio/node ──
NODE_BIN=""
if have node; then
  MAJOR=$(node -p 'process.versions.node.split(".")[0]' 2>/dev/null || echo 0)
  [ "$MAJOR" -ge 18 ] && NODE_BIN="$(command -v node)"
fi
[ -z "$NODE_BIN" ] && [ -x "$BASE/node/bin/node" ] && NODE_BIN="$BASE/node/bin/node"
if [ -z "$NODE_BIN" ]; then
  case "$(uname -m)" in arm64) NARCH=arm64 ;; x86_64) NARCH=x64 ;; *) say "Unsupported CPU: $(uname -m)"; exit 1 ;; esac
  say "Node is not installed. Downloading the official Node 22 build into $BASE/node (about 50 MB, no administrator password)…"
  TARBALL=$(curl -fsSL https://nodejs.org/dist/latest-v22.x/ | grep -oE "node-v22\.[0-9.]+-darwin-$NARCH\.tar\.gz" | head -1)
  [ -n "$TARBALL" ] || { say "Could not find the Node download. Install Node from https://nodejs.org and re-run."; exit 1; }
  curl -fSL --progress-bar "https://nodejs.org/dist/latest-v22.x/$TARBALL" -o "$TMP/node.tgz"
  rm -rf "$BASE/node"; mkdir -p "$BASE/node"
  tar -xzf "$TMP/node.tgz" -C "$BASE/node" --strip-components=1
  NODE_BIN="$BASE/node/bin/node"
  say "Node $("$NODE_BIN" -v) ready in $BASE/node."
fi

# ── Ollama: the command, or the app in /Applications or ~/Applications; else download the app ──
if ! have ollama && [ ! -d "/Applications/Ollama.app" ] && [ ! -d "$HOME/Applications/Ollama.app" ]; then
  say "Ollama is not installed. Downloading the Ollama app into ~/Applications (about 200 MB, no administrator password)…"
  mkdir -p "$HOME/Applications"
  curl -fSL --progress-bar https://ollama.com/download/Ollama-darwin.zip -o "$TMP/ollama.zip"
  ditto -x -k "$TMP/ollama.zip" "$HOME/Applications/"
  [ -d "$HOME/Applications/Ollama.app" ] || { say "The Ollama download did not unpack. Install it from https://ollama.com/download, open it once, and re-run."; exit 1; }
  say "Ollama app ready in ~/Applications. It runs in your menu bar; chanio starts it when needed."
fi

# ── The starter itself ──
say "Downloading the starter…"
curl -fsSL "$URL" -o "$TMP/starter.tgz"
tar -xzf "$TMP/starter.tgz" -C "$BASE/bin"
chmod +x "$BASE/bin/chanio" "$BASE/bin/chanio.mjs"
LINE='export PATH="$HOME/chanio/bin:$PATH"'
for RC in "$HOME/.zshrc" "$HOME/.bash_profile"; do
  if [ -f "$RC" ] && ! grep -qs 'chanio/bin' "$RC"; then printf '\n# chanio starter\n%s\n' "$LINE" >> "$RC"; fi
done
[ -f "$HOME/.zshrc" ] || printf '# chanio starter\n%s\n' "$LINE" > "$HOME/.zshrc"
say "Installed to $BASE/bin. Setting up…"
say "(When this finishes, open a NEW Terminal window so the chanio command is found — or run ~/chanio/bin/chanio directly.)"
say ""
exec "$NODE_BIN" "$BASE/bin/chanio.mjs" init "$@"
