#!/usr/bin/env nu
# reflection/modules/env.nu — environment bootstrap for ontoref.
# Sets: ONTOREF_ROOT, NICKEL_IMPORT_PATH, ONTOREF_ACTOR

export-env {
  let root = (
    $env.CURRENT_FILE
    | path dirname   # reflection/modules
    | path dirname   # reflection
    | path dirname   # <root>
  )
  $env.ONTOREF_ROOT = $root

  if ($env.NICKEL_IMPORT_PATH? | is-empty) {
    let config_file = $"($root)/.ontoref/config.ncl"

    # Guard: config must exist and nickel must be in PATH before reading
    let paths = if ($config_file | path exists) and (which nickel | is-not-empty) {
      do { ^nickel export $config_file } | complete
      | if $in.exit_code == 0 {
          $in.stdout | from json | get nickel_import_paths
          | each { |p| $"($root)/($p)" }
        } else { [$root] }
    } else { [$root] }

    $env.NICKEL_IMPORT_PATH = ($paths | str join ":")
  }

  if ($env.ONTOREF_ACTOR? | is-empty) {
    $env.ONTOREF_ACTOR = "developer"
  }
}
