#!/usr/bin/env sh
# Installer for jotcli — the Jot authoring CLI.
#
# Usage:
#   curl -sSL https://jot.winnersight.ai/install.sh | sh
#
# Downloads the prebuilt binary on linux-amd64 and macos-arm64. No GitHub
# access required. Self-hosters can point at their instance via
# `JOT_CLI_HOST=https://my.instance curl -sSL .../install.sh | sh`.

set -eu

HOST="${JOT_CLI_HOST:-https://jot.winnersight.ai}"
BIN="${JOTCLI_BIN:-$HOME/.local/bin}"

os="$(uname -s | tr '[:upper:]' '[:lower:]')"
arch="$(uname -m)"
case "$os-$arch" in
    linux-x86_64|linux-amd64) asset="jotcli-linux-amd64" ;;
    darwin-arm64) asset="jotcli-macos-arm64" ;;
    *)
        cat <<EOF >&2
error: no prebuilt binary for $os-$arch.
Supported: linux-amd64, macos-arm64.
EOF
        exit 1
        ;;
esac

mkdir -p "$BIN"
url="${HOST%/}/bin/${asset}"
echo "Downloading ${asset} from ${HOST}..."
if command -v curl >/dev/null 2>&1; then
    curl -fsSL -o "$BIN/jotcli" "$url"
elif command -v wget >/dev/null 2>&1; then
    wget -q -O "$BIN/jotcli" "$url"
else
    echo "error: need curl or wget" >&2
    exit 1
fi
chmod +x "$BIN/jotcli"
echo "Installed: $BIN/jotcli"
case ":$PATH:" in
    *":$BIN:"*) ;;
    *) echo "note: $BIN is not on your PATH. Add it to use jotcli." ;;
esac

cat <<EOF

Next steps:
  1. jotcli auth login                # prompts for server URL + API key
  2. jotcli note publish post.md
EOF
