#!/usr/bin/env bash
set -euo pipefail

BASE_URL="https://get.lns.run"

OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)

case "$OS" in
  darwin|linux) ;;
  *) echo "Unsupported OS: $OS" >&2; exit 1 ;;
esac

echo "System: ${OS}/${ARCH}"
echo ""

MANIFEST=$(curl -fsSL "${BASE_URL}/latest.json" 2>/dev/null) || {
  echo "No releases available yet." >&2
  exit 0
}

MATCHES=$(echo "$MANIFEST" | python3 -c "
import json, sys
data = json.load(sys.stdin)
matches = [e for e in data if e.get('os') == '${OS}' and e.get('arch') == '${ARCH}']
for m in matches:
    print(m['name'], m['version'], m['file'])
")

if [ -z "$MATCHES" ]; then
  echo "No binaries available for ${OS}/${ARCH}."
  exit 0
fi

echo "Would install:"
while IFS=' ' read -r name version file; do
  echo "  ${name} v${version}  (${file})"
done <<< "$MATCHES"
