#!/usr/bin/env bash
DIR="$(dirname $0)"
PREV_BQN="$DIR/obj2/for_build2"
if [ ! -f "$PREV_BQN" ]; then
  TMP_BQN="$DIR/obj2/for_build_tmp"
  if make -C "$DIR/.." for-build OUTPUT="$TMP_BQN"; then
    if [ "4" = "$("$TMP_BQN" -p "2+2")" ]; then
      true
    else
      echo "automatically-built bootstrap CBQN doesn't behave as expected; run 'make for-build' with necessary configuration manually"
      exit 1
    fi
  else
    echo "failed to build bootstrap CBQN; run 'make for-build' with necessary configuration manually"
    exit 1
  fi
  
  cp "$TMP_BQN" "$PREV_BQN"
fi

"$PREV_BQN" "$DIR/src/build.bqn" "$@"
