# LOKF knowledge-bundle recipes for Knowledge Trust Ladder.
#
# The `lokf` toolkit is a Python sidecar (see pyproject.toml); these recipes
# drive it against the knowledge/ bundle. Run `just --list` to see them.

bundle := "knowledge"

# List available recipes
default:
    @just --list

# Install the lokf sidecar toolkit into a local .venv
lokf-install:
    uv sync

# Assemble and validate the knowledge bundle against the LOKF schema.
# A bundle that extends the vocabulary with a domain schema passes it here as
# `--schema <slug>.yaml` (and in both workflows' validate steps);
# see ktl-librarian/references/domain-schema.md for the recipe.
lokf-validate:
    uv run lokf validate {{ bundle }}

# Project the bundle (or a concept file) to RDF (Turtle) on stdout
lokf-convert FILE=bundle:
    uv run lokf convert --format ttl {{ FILE }}

# Serve the bundle locally: SPARQL endpoint + live graph explorer
lokf-serve:
    uv run lokf serve {{ bundle }}

# Audit: every typed-relation target must resolve to a concept in the bundle.
# Schema validation alone reads a fabricated or stale IRI as a valid string.
# --check-refs (lokf 0.8.0+) takes the relation slots from the schema, so
# reified `relations` entries and a domain schema's own slots are covered too;
# a host with a domain schema passes `--schema <slug>.yaml` here as well as on
# lokf-validate. Targets outside base_iri are left alone - `source` and
# `definedBy` are documented as taking an external resource.
lokf-check-refs:
    uv run lokf validate --check-refs {{ bundle }}

# The knowledge_bundle doorway: the bundle under an ordinary, visible name at the
# host root, for folder pickers and file managers that hide dot-folders and, for
# Obsidian users, its "Open folder as vault" (open the link itself, never the host root).
# Git carries the link; a sync service such as OneDrive does not, so a machine
# may need it recreated. POSIX only - on Windows: mklink /J knowledge_bundle .lokf\knowledge

# Create (or recreate) the knowledge_bundle -> .lokf/knowledge doorway link at the host root
lokf-link:
    #!/usr/bin/env bash
    set -euo pipefail
    if [ -L knowledge ]; then echo "knowledge is itself a link onto $(readlink knowledge) - that folder is already the visible name; nothing to do"; exit 0; fi
    if [ ! -d knowledge ]; then echo "no knowledge/ folder here to link to" >&2; exit 1; fi
    if [ -L ../knowledge_bundle ]; then
      if [ "$(readlink ../knowledge_bundle)" = ".lokf/knowledge" ]; then echo "knowledge_bundle already present - nothing to link"; exit 0; fi
      echo "../knowledge_bundle is a link elsewhere ($(readlink ../knowledge_bundle)) - remove it and re-run" >&2; exit 1
    fi
    if [ -e ../knowledge_bundle ]; then echo "../knowledge_bundle exists and is not a link - leaving it alone" >&2; exit 1; fi
    ln -s .lokf/knowledge ../knowledge_bundle && echo "linked knowledge_bundle -> .lokf/knowledge"
