#!/bin/sh
# Axon CLI installer
set -e

BASE_URL="${AXON_DOWNLOAD_URL:-https://get.potlucklabs.ai/latest}"

case "$(uname -s)" in
    Darwin) os="darwin" ;;
    Linux)  os="linux" ;;
    *)
        echo "Error: Unsupported operating system: $(uname -s)" >&2
        echo "Axon supports macOS and Linux. Windows users: download manually." >&2
        exit 1
        ;;
esac

case "$(uname -m)" in
    x86_64|amd64)   arch="amd64" ;;
    arm64|aarch64)  arch="arm64" ;;
    *)
        echo "Error: Unsupported architecture: $(uname -m)" >&2
        exit 1
        ;;
esac

binary="axon-${os}-${arch}"
url="${BASE_URL}/${binary}"

echo "Downloading axon for ${os}/${arch}..."

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

if command -v curl >/dev/null 2>&1; then
    curl -fsSL -o "$tmpdir/axon" "$url"
elif command -v wget >/dev/null 2>&1; then
    wget -qO "$tmpdir/axon" "$url"
else
    echo "Error: curl or wget required" >&2
    exit 1
fi

chmod +x "$tmpdir/axon"

if [ "$(id -u)" = "0" ]; then
    install_dir="/usr/local/bin"
else
    install_dir="$HOME/.local/bin"
    mkdir -p "$install_dir"
fi

mv "$tmpdir/axon" "$install_dir/axon"

echo ""
echo "Installed axon to $install_dir/axon"

case ":$PATH:" in
    *":$install_dir:"*) ;;
    *)
        echo ""
        echo "Add to your PATH:"
        echo "  export PATH=\"$install_dir:\$PATH\""
        ;;
esac

echo ""
echo "Set your API URL for this beta:"
echo "  export AXON_API_URL=https://api.potlucklabs.ai"
echo ""
echo "Then log in:"
echo "  axon auth login --token <your-token>"
