#!/usr/bin/env bash # ============================================================================= # orphan-wiki.sh — find source pages whose raw source no longer exists. # Reads source_path from each wiki/sources/*.md frontmatter. If the raw is gone, # the page is orphaned. Emits JSON envelope: {status, genome, count, files[], detail[]}. # Read-only: no lock needed (same policy as pending-raw). # ============================================================================= set -euo pipefail genome="${1:?usage: orphan-wiki.sh }" base_dir="${GENOMES_ROOT:-${HOME}/genomes}" cd "${base_dir}/${genome}" 2>/dev/null || { echo '{"status":"error","reason":"unknown genome"}'; exit 1; } # Clean start on the configured base (single source of truth in lib/clean-start.sh). : "${KG_LIB_DIR:=${HOME}/knowledge-genome-orchestrator/lib}" source "${KG_LIB_DIR}/clean-start.sh" 2>/dev/null \ || { echo '{"status":"error","reason":"clean-start.sh not found"}'; exit 1; } clean_start || { echo '{"status":"error","reason":"clean-start failed"}'; exit 1; } declare -a ORPH=() for page in wiki/sources/*.md; do [[ -e "$page" ]] || continue sp="$(sed -n 's/^source_path:[[:space:]]*//p' "$page" | tr -d '\r' | head -n1)" # Pages without source_path are pre-Step-2 legacy: ignore, don't false-positive. [[ -n "$sp" ]] || continue [[ -f "$sp" ]] || ORPH+=("$page") done if [[ ${#ORPH[@]} -eq 0 ]]; then echo '{"status":"ok","genome":"'"$genome"'","count":0,"files":[],"detail":[]}' else for x in "${ORPH[@]}"; do printf '%s\torphan\n' "$x"; done \ | jq -R 'split("\t") | {path: .[0], reason: .[1]}' \ | jq -s --arg g "$genome" '{status:"ok", genome:$g, count:length, files:[.[].path], detail:.}' fi