#!/bin/bash
# OpenClaw Mistral Support - Git-based Patcher
set -e

echo "🚀 Applying Mistral AI changes from PR #14730..."

if [ ! -d ".git" ]; then
    echo "❌ Error: This script requires OpenClaw to be installed via Git."
    exit 1
fi

# 1. Configure fork remote if it doesn't exist
if ! git remote | grep -q "patch-source"; then
    git remote add patch-source https://github.com/jaimegh-es/openclaw.git
fi

# 2. Fetch changes
echo "⬇️ Downloading code from fork..."
git fetch --progress patch-source feat/mistral-provider

# 3. Apply changes to current branch
echo "🔄 Merging changes..."
git config user.email "patch@example.com"
git config user.name "Patcher"
# -X theirs prioritizes fork changes in case of conflict
git merge patch-source/feat/mistral-provider --no-edit -X theirs

echo "📦 Installing dependencies and building..."
pkexec corepack enable || corepack enable
pnpm install --frozen-lockfile --prod=false
pnpm build

echo "✅ Patch applied successfully. PR changes are now in your instance."
