#!/usr/bin/env bash

# To view the difference between your output and the desired output,
# replace "diff -bBwsq" by "diff -bBws" (i.e., remove the q (for
# 'quiet')) flag

# Alternatively, replace the following line
#    ./a.out < $f | diff -bBwsq - output${f#input}
# with
#    ./a.out < $f >| my-output${f#input}
# and compare the corresponding my-output and output files, e.g.,
# my-output1-01.txt and output1-01.txt.


if [ $# != 1 ]; then
    echo -e "Usage: ./check-submission <your roll number, e.g., cs2707>"
    exit 1
fi

rollno="$@"

if [[ ! (-d q1 && -d q2 && -d q3 && -d q4) ]]; then
    echo "One or more of required subdirectories (q1, q2, q3, q4) missing."
    exit 1
fi


function check_question() {
    qid=q$1
    cd $qid
    progfile=$rollno-$qid.c
    echo "Checking Question $1"
    if [[ -f a.out ]]; then
        for f in input*.txt; do
            # replace -bBwsq by -bBws to view details of differences
            # Also see alternative suggested above
            ./a.out < $f | diff -bBwsq - output${f#input}
            # (valgrind --tool=memcheck --leak-check=full --log-file=valgrind-log-q1.txt ./a.out < $f) > /dev/null 2>&1
        done
    else
        echo "Could not find ./a.out in $(pwd)"
    fi
    echo -e "Question $1 done\n"
    cd ..
}


#---------------------------------------------------------------------------#
# Now, run the actual tests. Comment / uncomment the following lines
# as desired to call the above functions.
# ---------------------------------------------------------------------------#

check_question 1

check_question 2

check_question 3

# check_question 4
