#!/bin/bash
set -eo pipefail

# Check $TESTS_DIR is given
if [[ -z "$TESTS_DIR" ]]; then
  echo "Must provide \$TESTS_DIR env variable"
  exit 1
fi

# Check $INVOCATION_ID is given: we use it as cache key to be sure compiled tester-lcmp is actual (see file $TESTS_DIR/tester-lcmp/tester-lcmp.$INVOCATION_ID)
if [[ -z "$INVOCATION_ID" ]]; then
  echo "Must provide \$INVOCATION_ID env variable"
  exit 1
fi

mkdir -p "$TESTS_DIR"/tester-lcmp

# If compiled tester-lcmp is not actual
if [ ! -f "$TESTS_DIR"/tester-lcmp/tester-lcmp."$INVOCATION_ID" ]; then
  # Clear
  rm -f "$TESTS_DIR"/tester-lcmp/*

  # Compile it
  CPP_INCLUDE_DIR_=$CPP_INCLUDE_DIR
  export CPP_INCLUDE_DIR=$TESTS_DIR/lib
  bash "$TESTS_DIR"/scripts/compile "$TESTS_DIR"/src/tester-lcmp.cpp
  export CPP_INCLUDE_DIR=$CPP_INCLUDE_DIR_

  # Move from current dir to /tester-lcmp
  if [ ! -d ./tester-lcmp ]; then
    mv -f ./tester-lcmp "$TESTS_DIR"/tester-lcmp
  else
    mv -f ./tester-lcmp.exe "$TESTS_DIR"/tester-lcmp
  fi

  # Create file to inform that it was compiled to this $INVOCATION_ID
  touch "$TESTS_DIR"/tester-lcmp/tester-lcmp."$INVOCATION_ID"
fi

# First argument as a subdirectory in ./refs
ref_dir=$1
if [[ -z "$ref_dir" ]]; then
  echo "Must provide \$1 for test-ref"
  exit 1
fi

refs=refs/$1

shift 1

# Check if we don't have invocation reference files
if [ ! -d "$refs" ]; then
  if [[ "$TEST_REF_FORBID_GEN_REFS" == "true" ]]; then
    echo "You forgot to run push ref files for invocation: "$*""
    echo "Run test locally, it will produce ref files and push it into the repo"
    exit 1
  fi
  # Create them
  mkdir -p "$refs"
  echo Generating tester refs: "$*"
  exit_code=0
  # shellcheck disable=SC2048
  $* 1>"$refs"/stdout 2>"$refs"/stderr || exit_code=$?
  echo $exit_code >"$refs"/exit_code
  echo "[SUCCESS] generated execution reference files (exit code: $exit_code)"
else
  # Do invocation
  echo Testing refs: "$*"
  exit_code=0
  # shellcheck disable=SC2048
  $* 1>"$refs"/stdout.aux 2>"$refs"/stderr.aux || exit_code=$?
  echo $exit_code >"$refs"/exit_code.aux

  # Check exit code is the same
  tester_lcmp_exit_code=0
  "$TESTS_DIR"/tester-lcmp/tester-lcmp "$TESTS_DIR"/tester-lcmp/tester-lcmp."$INVOCATION_ID" "$refs"/exit_code.aux "$refs"/exit_code 2>tester-lcmp.out || tester_lcmp_exit_code=$?
  if [[ ! "$tester_lcmp_exit_code" == "0" ]]; then
    echo "[FAILED] exit_code mismatched"
    cat tester-lcmp.out
    rm -f tester-lcmp.out
    rm -f "$refs"/*.aux
    exit 1
  fi
  echo $exit_code >"$refs"/exit_code.aux

  # Check stdout is the same
  "$TESTS_DIR"/tester-lcmp/tester-lcmp "$TESTS_DIR"/tester-lcmp/tester-lcmp."$INVOCATION_ID" "$refs"/stdout.aux "$refs"/stdout 2>tester-lcmp.out || tester_lcmp_exit_code=$?
  if [[ ! "$tester_lcmp_exit_code" == "0" ]]; then
    echo "[FAILED] stdout mismatched"
    cat tester-lcmp.out
    rm -f tester-lcmp.out
    rm -f "$refs"/*.aux
    exit 1
  fi

  # Check stderr is the same
  "$TESTS_DIR"/tester-lcmp/tester-lcmp "$TESTS_DIR"/tester-lcmp/tester-lcmp."$INVOCATION_ID" "$refs"/stderr.aux "$refs"/stderr 2>tester-lcmp.out || tester_lcmp_exit_code=$?
  if [[ ! "$tester_lcmp_exit_code" == "0" ]]; then
    echo "[FAILED] stderr mismatched"
    cat tester-lcmp.out
    rm -f tester-lcmp.out
    rm -f "$refs"/*.aux
    exit 1
  fi

  # Invocation is the same!
  echo "[SUCCESS] execution matched with reference (exit code: $exit_code)"
  rm -f tester-lcmp.out
  rm -f "$refs"/*.aux
fi
