#!/bin/sh
#  nibc — the *published* compiler, resolved from the deployment pin.
#
#  Why this exists: `_build/default/lib/nibc.exe` inside a checkout is whatever
#  the last `dune build` in *that* checkout put there. In a git worktree that is
#  routinely older than the tree you are reading, and it is the wrong answer to
#  "what does nibc do?" — it can lack a subcommand that is documented, or (worse)
#  still have one whose semantics changed. The pinned artifact is a fact about a
#  commit: `apps`-level deploy.json names the store path built from that commit,
#  and this script resolves the one the delivery last pinned.
#
#  Resolution, in order, first hit wins:
#    1. $NIBC_STORE                           (a store path, or a directory with bin/nibc)
#    2. the store path in the deployment record ($NIBC_RECORD, default
#       <repo>/areas/home/nibble/deploy.json, found by walking up from here)
#    3. an error naming the record it read and the command that realises the pin
#
#  There is deliberately no fallback to the worktree build: a silent fallback is
#  the bug this script exists to remove. If the pin is not realised here, say so
#  and exit 1 — the caller can then realise it in one command, printed.
#
#  `nibc --which` prints the resolved binary and exits 0 without running it; it
#  answers "which nibc am I about to talk to" without the store being present.
set -eu

prog=$(basename "$0")

die() {
  echo "$prog: $*" >&2
  exit 1
}

#  The deployment record: $NIBC_RECORD, else the first ancestor of this script
#  that carries one. Resolved without realpath (a symlinked checkout is a normal
#  layout) by walking up on the *spelling* of $0.
find_record() {
  if [ -n "${NIBC_RECORD:-}" ]; then
    echo "$NIBC_RECORD"
    return 0
  fi
  d=$(cd "$(dirname "$0")" && pwd)
  while [ "$d" != "/" ]; do
    if [ -f "$d/areas/home/nibble/deploy.json" ]; then
      echo "$d/areas/home/nibble/deploy.json"
      return 0
    fi
    d=$(dirname "$d")
  done
  return 1
}

#  The store path the record names. Read with sed rather than a JSON parser:
#  the file is a deployment record in this repo's own commit, one line of it
#  carries the build, and this script must not need jq on a box that may not
#  have it. A leading `-` is not possible in a store name, and a record with no
#  store path is an error rather than an empty answer.
store_of_record() {
  sed -n 's|.*\(/nix/store/[a-z0-9][a-z0-9]*\(-[a-zA-Z0-9._+?=-]*\)\{0,\}\).*|\1|p' "$1" \
    | head -n 1
}

bin=""
if [ -n "${NIBC_STORE:-}" ]; then
  case "$NIBC_STORE" in
    */bin/nibc) bin=$NIBC_STORE ;;
    *)          bin=$NIBC_STORE/bin/nibc ;;
  esac
  from="\$NIBC_STORE"
else
  record=$(find_record) || die "no deployment record found above $(dirname "$0") — \
set NIBC_RECORD to areas/home/nibble/deploy.json, or NIBC_STORE to the store path"
  [ -f "$record" ] || die "deployment record not found: $record"
  store=$(store_of_record "$record")
  [ -n "$store" ] || die "$record names no /nix/store path — \
it is built by areas/home/nibble/deploy.sh from the pin in that repository"
  bin=$store/bin/nibc
  from=$record
fi

if [ "${1:-}" = "--which" ]; then
  echo "$bin"
  exit 0
fi

if [ ! -x "$bin" ]; then
  echo "$prog: the pinned compiler is not realised on this machine" >&2
  echo "$prog:   wants  ${bin%/bin/nibc}" >&2
  echo "$prog:   pin    $from" >&2
  echo "$prog: realise it with: nix-store -r ${bin%/bin/nibc} --add-root ./result --indirect" >&2
  echo "$prog: (or set NIBC_STORE to a compiler you mean, e.g. a worktree build)" >&2
  exit 1
fi

exec "$bin" "$@"
