#!/usr/bin/env sh
# code-shot — install script
#
# Downloads the latest code-shot CLI to a directory on PATH.
# Usage:
#   curl -fsSL https://code-shot.labs.basenull.com/install.sh | sh
#
# Requires Node.js 18.17+ on PATH.

set -e

API="${CODE_SHOT_API:-https://code-shot.labs.basenull.com}"
PREFIX="${CODE_SHOT_PREFIX:-$HOME/.local/bin}"
TARGET="$PREFIX/code-shot"

# Verify Node.js is available
if ! command -v node >/dev/null 2>&1; then
  echo "error: node is not on PATH. Install Node.js 18.17+ first: https://nodejs.org" >&2
  exit 1
fi

NODE_MAJOR=$(node -e 'console.log(process.versions.node.split(".")[0])' 2>/dev/null || echo 0)
if [ "$NODE_MAJOR" -lt 18 ]; then
  echo "error: node $NODE_MAJOR is too old. code-shot needs Node.js 18.17 or newer." >&2
  exit 1
fi

# Pick a downloader
if command -v curl >/dev/null 2>&1; then
  FETCH="curl -fsSL"
elif command -v wget >/dev/null 2>&1; then
  FETCH="wget -qO-"
else
  echo "error: neither curl nor wget on PATH." >&2
  exit 1
fi

mkdir -p "$PREFIX"
$FETCH "$API/code-shot" > "$TARGET"
chmod +x "$TARGET"

echo "✓ installed code-shot to $TARGET"

# Hint about PATH if needed
case ":$PATH:" in
  *":$PREFIX:"*)
    ;;
  *)
    echo ""
    echo "⚠ $PREFIX is not on your PATH yet. Add this to your shell rc:"
    echo "    export PATH=\"\$PATH:$PREFIX\""
    ;;
esac

echo ""
echo "Try it:"
echo "    code-shot login"
echo "    echo 'const x = 1' | code-shot --theme dracula"
