#!/usr/bin/env bash
# Merge upstream pi-mono into upstream-sync, then into main.
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"

export GIT_HTTP_VERSION="${GIT_HTTP_VERSION:-1.1}"

echo "==> Fetch upstream (badlogic/pi-mono)"
git fetch upstream

CURRENT="$(git branch --show-current)"
if [[ "$CURRENT" != "main" && "$CURRENT" != "upstream-sync" ]]; then
	echo "Switch to main or upstream-sync first (current: $CURRENT)" >&2
	exit 1
fi

echo "==> Update upstream-sync from main"
git checkout upstream-sync 2>/dev/null || git checkout -b upstream-sync main
git merge main --ff-only

echo "==> Merge upstream/main into upstream-sync"
if ! git merge upstream/main -m "merge: sync upstream pi-mono main"; then
	echo
	echo "Conflicts detected. Resolve them, then:"
	echo "  git add <resolved-files>"
	echo "  git commit"
	echo "  git checkout main && git merge upstream-sync"
	echo "  ./scripts/push-sproutclaw.sh"
	exit 1
fi

echo "==> Merge upstream-sync into main"
git checkout main
git merge upstream-sync --ff-only

echo "==> Install deps and build"
npm install --ignore-scripts
npm run build

echo
echo "Done. Push with: ./scripts/push-sproutclaw.sh"
echo "Conflict hints:"
echo "  README.md, sproutclaw patches     -> keep ours"
echo "  package-lock.json, *.generated.ts -> take upstream, then npm install"
