#!/usr/bin/env bash
# init-dataset.sh <model-slug> — creates a per-model dataset repo: bare canonical at
# ~/remotes/fact.ngo/<model-slug>.git on the self-hosted remote, with working clones at
# ~/coherence/fact.ngo/<model-slug> (the self-hosted remote) and the local mono-folder
# (Documents/fact.ngo/<model-slug>). Dataset repos are disposable: drop them
# with drop-dataset.sh when superseded.

set -euo pipefail

SLUG="${1:?usage: init-dataset.sh <model-slug>}"
REMOTE_HOST="${REMOTE_HOST:-remote}"
LOCAL_ROOT="${XRAY_DATASETS:-$HOME/Documents/fact.ngo}"

ssh "$REMOTE_HOST" "set -e
  mkdir -p ~/remotes/fact.ngo ~/coherence/fact.ngo
  if [ ! -d ~/remotes/fact.ngo/$SLUG.git ]; then
    git init --bare -b main ~/remotes/fact.ngo/$SLUG.git
  fi
  if [ ! -d ~/coherence/fact.ngo/$SLUG/.git ]; then
    git clone -q ~/remotes/fact.ngo/$SLUG.git ~/coherence/fact.ngo/$SLUG
    cd ~/coherence/fact.ngo/$SLUG
    git config user.name zauh
    git config user.email zauh@coherence.ngo
    if git rev-parse --verify -q HEAD >/dev/null; then :; else
      echo \"# $SLUG — fact.ngo model-dataset\" > README.md
      git add -A
      git commit -m \"Init $SLUG dataset repo\"
      git push -q origin main
    fi
  fi"

mkdir -p "$LOCAL_ROOT"
if [ -d "$LOCAL_ROOT/$SLUG" ]; then
  echo "local clone already exists: $LOCAL_ROOT/$SLUG"
else
  git clone "$REMOTE_HOST:remotes/fact.ngo/$SLUG.git" "$LOCAL_ROOT/$SLUG"
fi

echo "dataset ready: $LOCAL_ROOT/$SLUG  (origin: remote:remotes/fact.ngo/$SLUG.git)"
