#!/usr/bin/env bash

JUNITPATH="/libs/junit5.jar"

if [[ ! -f groupAndRole.txt ]]; then
  echo "Warning Submission Incomplete: submission does not contain groupAndRole.txt, which should not have been deleted."
  exit 1
fi

# Identify the student's role
ROLE=$(cat groupAndRole.txt | grep -Pio "Backend|Frontend")
ROLEFILE="${ROLE~}.java"
ROLETEST="${ROLE~}Tests.java"
ROLEINTRF="${ROLE~}Interface.java"

# 1. Check if role file exists
if [[ ! -f ${ROLEFILE} ]]; then
  echo "Warning Submission Incomplete: submission does not contain ${ROLEFILE}, but should"
  exit 1
fi

# 2. Check if role file implements correct interface
if ! grep -Pzq "implements[^\{]{1,100}${ROLEINTRF%\.java}" ${ROLEFILE}; then
  echo "Warning Submission Incomplete: ${ROLEFILE} does not implement ${ROLEINTRF%\.java}, but should"
  exit 1
fi

# 3. Check if all Java files compile
if ! javac -cp .:../junit5.jar:../lib/junit5.jar:/libs/junit5.jar *.java; then
    echo "Submission Failed Check: cannot compile Java files."
    exit 1
fi

# 4. Verify P106 Submission Checker (Midweek requirement)
if ! java -jar /libs/junit5.jar --class-path=. --select-class=P106SubmissionChecker; then    
    echo "Submission Failed Check: one or more JUnit tests failed."
    exit 1
fi

# 5. Verify the required role tests exist in the test file
for testNum in 1 2 3; do
  if ! grep -Pzq "@(Parameterized)?Test[^\(]{1,15}(public )?void roleTest$testNum" ${ROLETEST}; then
    echo "Warning Submission Incomplete: ${ROLETEST} does not contain roleTest$testNum"
    exit 1
  fi
done

echo "Submission Passed Basic Scan"
exit 0
