#!/usr/bin/env bash
# The sibling of the nix stub: `nix-store --query --references <drv>` over
# the derivations it writes. One opaque store path per input file, named by
# the file's content, which is what makes the closure content-addressed.
# It also names a derivation's one output, where the nix stub builds it,
# and says which outputs are not built yet, for attest's timing probe.
set -euo pipefail

case "${1:-} ${2:-}" in
  "--query --outputs")
    drv="${*: -1}"
    if [ -f "$drv.output" ]; then cat "$drv.output"; else printf '%s\n' "${drv%.drv}.out"; fi
    exit 0
    ;;
  "--check-validity --print-invalid")
    shift 2
    for path in "$@"; do [ -e "$path" ] || printf '%s\n' "$path"; done
    exit 0
    ;;
esac

drv="${*: -1}"
[ -f "$drv" ] || { echo "nix-store (stub): no such derivation: $drv" >&2; exit 1; }
while read -r sum path; do
  printf '/nix/store/%s-%s\n' "$sum" "${path//\//_}"
done < "$drv"
