#!/bin/sh
set -eu

OS="$(uname -s)"
ARCH="$(uname -m)"

if command -v redline >/dev/null 2>&1; then
  echo "Redline is already installed at $(command -v redline)."
  echo "Run 'redline doctor' to verify your setup."
  exit 0
fi

if [ "$OS" != "Darwin" ]; then
  echo "Redline CLI installer currently supports macOS only." >&2
  echo "Use the macOS app and bundled CLI installer on an Apple Silicon Mac." >&2
  exit 1
fi

if [ "$ARCH" != "arm64" ]; then
  echo "Redline CLI installer currently supports Apple Silicon (arm64) only." >&2
  echo "Use the Apple Silicon macOS app and bundled CLI installer." >&2
  exit 1
fi

if command -v cargo >/dev/null 2>&1; then
  echo "Installing Redline CLI with cargo..."
  if cargo install --git https://github.com/bro4all/RedlineAI redline-cli; then
    echo "Redline CLI installed with cargo."
    echo "Ensure \$HOME/.cargo/bin is on your PATH if 'redline' is not found yet."
    echo "Run 'redline doctor' to verify your setup."
    exit 0
  fi

  echo "cargo install failed." >&2
fi

echo "Automatic CLI installation is not available on this machine." >&2
echo "Download the Redline macOS DMG and run its bundled Install CLI.command instead." >&2
exit 1
