#!/bin/bash
"""":"

# ── Cron Expertise Integrator ─────────────────────────────────────────────────
#
# Injects expertise load at cron job start and stores new insights at end.
#
# Usage — add to top of cron prompt:
#   ```
#   Load your expertise file:
#   python -m pipeline.expertise --job <cron-job-name> --load
#   ```
#
# Usage — add to end of cron prompt:
#   ```
#   Before finishing, store any new learnings:
#   python -m pipeline.expertise --job <cron-job-name> --append \
#     --domain "<domain>" --insight "<insight>"
#   ```
#
# Or wrap a shell job:
#   pipeline/expertise_cron.sh --job triage --domain github-issues -- bash ./triage.sh
# ────────────────────────────────────────────────────────────────────────────────

# Shell wrapper for bash-based cron jobs that want to store expertise.
# Calls python -m pipeline.expertise under the hood.

set -euo pipefail

JOB=""
DOMAIN=""
COMMAND=()

while [[ $# -gt 0 ]]; do
    case "$1" in
        --job) JOB="$2"; shift 2 ;;
        --domain) DOMAIN="$2"; shift 2 ;;
        --) shift; COMMAND=("$@"); break ;;
        *) echo "Unknown: $1"; exit 1 ;;
    esac
done

if [[ -z "$JOB" || ${#COMMAND[@]} -eq 0 ]]; then
    echo "Usage: $0 --job <name> --domain <domain> -- <command...>"
    exit 1
fi

# Load expertise context
echo "=== Expertise ($JOB) ==="
python -m pipeline.expertise --job "$JOB" --load || true
echo "=== End Expertise ==="

# Run the actual command
"${COMMAND[@]}"

# Store the exit code
RC=$?

# On success, prompt the user/agent to store what they learned
if [[ $RC -eq 0 ]]; then
    echo ""
    echo "Job succeeded. If you learned something worth remembering, store it:"
    echo "  python -m pipeline.expertise --job $JOB --append --domain \"$DOMAIN\" --insight \"<what you learned>\""
fi

exit $RC
