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

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

if [[ ! -f .gitmodules ]] || ! git config --file .gitmodules --get submodule.deepseek-harness.path >/dev/null; then
  echo 'DeepSeek Harness submodule is not configured.' >&2
  exit 1
fi

git submodule sync -- deepseek-harness
git submodule update --init deepseek-harness

if [[ -n "$(git -C deepseek-harness status --porcelain)" ]]; then
  echo 'DeepSeek Harness has local changes. Commit, stash, or discard them before updating.' >&2
  exit 1
fi

git -C deepseek-harness fetch --prune origin master
if git -C deepseek-harness show-ref --verify --quiet refs/heads/master; then
  git -C deepseek-harness switch master
else
  git -C deepseek-harness switch --track -c master origin/master
fi

git -C deepseek-harness merge --ff-only origin/master

echo "DeepSeek Harness updated to $(git -C deepseek-harness rev-parse --short HEAD)."
echo 'Commit the updated deepseek-harness gitlink in this repository to share the new pin.'
