#!/bin/bash

score=0
numofcases=10
special=special

echo "Current directory:" $PWD

# Argument for Python 2
python="python3"
if [ $# -gt 0 ]
then
   if [ "$1" == "Python2" ]
   then
       python="python2"
   elif [ "$1" == "python2" ] 
   then  
       python="python2"
   else
       echo  "Wrong arguments. Does not need any arguments for Java, C, C++, Go, and Python3."
       exit
   fi
fi

# compile the program
javafiles=(*.java)
cfiles=(*.c)
cfiles2=(*.cc)
cppfiles=(*.cpp)
gofiles=(*.go)
pyfiles=(*.py)
if [ -e "${javafiles[0]}" ] 
then
    echo "Java files"
    javac *.java
    if [ $? == 0 ]
    then
        echo "Successfully compiles: 5 points"
        let "score=$score+5"
    else
        echo "Could not compile the code"
        echo "Total score = $score points"
        exit
    fi
    if [ -e Project1.class ] 
    then
        echo "Successfully finds the executable file: 5 points"
        let "score=$score+5"
        cmd="java Project1"
    elif [ -e project1.class ] 
    then
        echo "Successfully finds the executable file: 5 points"
        let "score=$score+5"
        cmd="java project1"
    else
        echo "Could not find class Project1.class"
        echo "Total score = $score points"
        exit
    fi        
    grep java.util.regex *.java
    if [ $? == 0 ]
    then
        echo "You may not use java.util.regex"
        echo "Total score = $score points"
        exit
    fi
    grep package *.java
    if [ $? == 0 ]
    then
        echo "Please remove your own Java package; otherwise \"java Project1\" does not run."
        echo "Total score = $score points"
        exit
    fi
elif [ -e "${cfiles[0]}" ] || [ -e "${cfiles2[0]}" ] || [ -e "${cppfiles[0]}" ]
then
    echo "C/C++ files"
    if [ -e makefile ] 
    then
        mv makefile Makefile
    fi
    if [ ! -e Makefile ] 
    then
        echo "Could not find Makefile"
        echo "Total score = $score points"
        exit
    fi
    make
    if [ $? == 0 ]
    then
        echo "Successfully compiles: 5 points"
        let "score=$score+5"
    else
        echo "Could not compile the code"
        echo "Total score = $score points"
        exit
    fi
    if [ -e project1 ] 
    then
        echo "Successfully finds the executable file: 5 points"
        let "score=$score+5"
        cmd="./project1"
    elif [ -e Project1 ] 
    then
        echo "Successfully finds the executable file: 5 points"
        let "score=$score+5"
        cmd="./Project1"
    else
        echo "Could not find executable file project1"
        echo "Total score = $score points"
        exit
    fi        
    grep "<regex>" *.c* 
    if [ $? == 0 ]
    then
        echo "You may not use <regex>"
        echo "Total score = $score points"
        exit
    fi
elif  [ -e "${gofiles[0]}" ]
then
    echo "Go files"
    if [ -e makefile ] 
    then
        mv makefile Makefile
    fi
    if [ ! -e Makefile ] 
    then
        echo "Could not find Makefile"
        echo "Total score = $score points"
        exit
    fi
    make
    if [ $? == 0 ]
    then
        echo "Successfully compiles: 5 points"
        let "score=$score+5"
    else
        echo "Could not compile the code"
        echo "Total score = $score points"
        exit
    fi
    if [ -e project1 ] 
    then
        echo "Successfully finds the executable file: 5 points"
        let "score=$score+5"
        cmd="./project1"
    elif [ -e Project1 ] 
    then
        echo "Successfully finds the executable file: 5 points"
        let "score=$score+5"
        cmd="./Project1"
    else
        echo "Could not find executable file project1"
        echo "Total score = $score points"
        exit
    fi        
    grep "\"regexp\"" *.go
    if [ $? == 0 ]
    then
        echo "You may not import \"regex\""
        echo "Total score = $score points"
        exit
    fi
elif  [ -e "${pyfiles[0]}" ]
then
    echo "Python files"
    if [ -e Project1.py ]
    then
        mv Project1.py project1.py
    fi
    if [ -e project1.py ] 
    then
        echo "Successfully finds the Python file: 5 points"
        let "score=$score+5"
        cmd="$python project1.py"
    else
        echo "Could not find Python file project1.py"
        echo "Total score = $score points"
        exit
    fi
    $python -m py_compile project1.py
    if [ $? == 0 ]
    then
        echo "Successfully compiles using $python: 5 points"
        let "score=$score+5"
    else
        if [ "$python" == "python3" ]; then
            echo "Could not compile the code using Python3"
            echo "If you use Python2, please type ./runproj1.sh Python2"
        else
            echo "Could not compile the code using Python2"
            echo "If you use Python3, please type ./runproj1.sh"
        fi
        echo "Total score = $score points"
        exit
    fi
    grep -E -e "import\s+re" -e "from\s+re" *.py
    if [ $? == 0 ]
    then
        echo "You may not import re"
        echo "Total score = $score points"
        exit
    fi
else
   echo "Could not find Java, C, C++, Go, or Python files:"
   ls
   exit
fi

# download testcase input and output files
for i in $(seq 1 $numofcases)
do
    inputfile="q$i.txt"
    outputfile="a$i.txt"
    wget -q http://cse.unl.edu/~xu/courses/automata/proj1/$inputfile -O $inputfile
    if [ $? != 0 ]
    then
        echo "Could not download $inputfile from cse, please contact the instructor"
        exit
    fi
    wget -q http://cse.unl.edu/~xu/courses/automata/proj1/$outputfile -O $outputfile
    if [ $? != 0 ]
    then
        echo "Could not download $outputfile from cse, please contact the instructor"
        exit
    fi
done

# run the program
for i in $(seq 1 $numofcases)
do
    inputfile="q$i.txt"
    outputfile="a$i.txt"
    echo -ne "Running testcase $i ...\r"
    timeout 30s $cmd < $inputfile > projectoutput.txt
    if [ $? != 124 ]
    then
        # remove possible punctuation and empty lines, add line numbers
        cat projectoutput.txt | grep '\S' | head -n 6 | nl > projectoutput_nl.txt
        cat $outputfile | grep '\S' | head -n 6 | nl > correctoutput_nl.txt
        # do not care about white spaces, do not care about cases
        wrong=$(diff -i -w -y --suppress-common-lines projectoutput_nl.txt correctoutput_nl.txt | wc -l)
        let "correct=6-$wrong"
        let "score=$score+$correct"
        echo "Testcase $i: Number of correct outputs="$correct", Total Score="$score" points"
    else
        echo "Testcase $i: Running longer than 30 seconds. Terminated."
    fi
done

# test the special testcase

inputfile="q$special.txt"
outputfile="a$special.txt"
    wget -q http://cse.unl.edu/~xu/courses/automata/proj1/$inputfile -O $inputfile
    if [ $? != 0 ]
    then
        echo "Could not download $inputfile from cse, please contact the instructor"
        exit
    fi
    wget -q http://cse.unl.edu/~xu/courses/automata/proj1/$outputfile -O $outputfile
    if [ $? != 0 ]
    then
        echo "Could not download $outputfile from cse, please contact the instructor"
        exit
    fi
    echo -ne "Running the special testcase ...\r"
    timeout 120s $cmd < $inputfile > projectoutput.txt
    if [ $? != 124 ]
    then
        cat projectoutput.txt | grep '\S' | head -n 6 | nl > projectoutput_nl.txt
        cat $outputfile | grep '\S' | head -n 6 | nl > correctoutput_nl.txt
        wrong=$(diff -i -w -y --suppress-common-lines projectoutput_nl.txt correctoutput_nl.txt | wc -l)
        let "correct=6-$wrong"
        let "score=$score+$correct+$correct+$correct+$correct+$correct"
        echo "Special case: Number of correct outputs="$correct", Total Score="$score" points"
        if [ $wrong == 0 ]
        then
            echo -ne "Measuring the running time of the special testcase for the first time  \r"
            /usr/bin/time -f "%U\n%S\n" -o time.txt $cmd <$inputfile > projectoutput.txt
            timeresult=( $(cat time.txt) )
            totaltime1=$(echo "${timeresult[0]}+${timeresult[1]}" | bc)
            echo -ne "Measuring the running time of the special testcase for the second time  \r"
            /usr/bin/time -f "%U\n%S\n" -o time.txt $cmd <$inputfile > projectoutput.txt
            timeresult=( $(cat time.txt) )
            totaltime2=$(echo "${timeresult[0]}+${timeresult[1]}" | bc)
            echo -ne "Measuring the running time of the special testcase for the third time  \r"
            /usr/bin/time -f "%U\n%S\n" -o time.txt $cmd <$inputfile > projectoutput.txt
            timeresult=( $(cat time.txt) )
            totaltime3=$(echo "${timeresult[0]}+${timeresult[1]}" | bc)
            avgtime=$(echo "scale=2;($totaltime1+$totaltime2+$totaltime3)/3" | bc)
            echo "Special case: Running time of three runs: " $totaltime1"," $totaltime2"," $totaltime3" seconds "
            echo "Special case: Average running time: " $avgtime "seconds"
        fi
    else
        echo "Special case: Running longer than 120 seconds. Terminated."
    fi



echo "Final score = $score points"
