#!/usr/bin/env bash
DESCRIPTION="\
Description: Run the LunarGLASS test suite, record output
"

USAGE="\
Usage: ./run [options]

       Options:
         -h --help                 Print out this Usage info
         -o --enable-obfuscate     Do obfuscation tests
         -t --testfile <testfile>  Run on a single test file
"

# Individual isolated tests, which will also catch the IRs, but slower than doing multithreaded
ISOLATEDTESTS=(flowControl.frag \
       AofA.frag               \
       aep.vert                \
       aep.tesc                \
       aep.tese                \
       aep.geom                \
       aep.frag                \
       Operations.frag         \
       conversion.frag         \
       prepost.frag            \
       swizzle.frag            \
       simpleFunctionCall.frag \
       functionCall.frag       \
       functionSemantics.frag  \
       forwardFun.frag         \
       voidFunction.frag       \
       loopsArtificial.frag    \
       earlyReturnDiscard.frag \
       discard-dce.frag        \
       always-discard.frag     \
       always-discard2.frag    \
       depthOut.frag           \
       dataOut.frag            \
       dataOutIndirect.frag    \
       dataOutIndirect.vert    \
       varyingArray.frag       \
       varyingArrayIndirect.frag \
       variableArrayIndex.frag \
       simpleMat.vert          \
       matrix.frag             \
       matFun.vert             \
       nonSquare.vert          \
       structure.frag          \
       structAssignment.frag   \
       structDeref.frag        \
       localAggregates.frag    \
       aggOps.frag             \
       types.frag              \
       uint.frag               \
	   intOps.vert             \
       conditionalDiscard.frag \
       qualifiers.vert         \
       precision.frag          \
       100ops.frag             \
       300layout.vert          \
       300layout.frag          \
       310.comp                \
       simpleSwitch.vert       \
       switch.frag             \
       whileLoop.frag          \
       doWhileLoop.frag        \
       loops.frag              \
       forLoop.frag            \
       atomic.comp             \
       metaMat.frag            \
       130.frag                \
       140.frag                \
       150.vert                \
       150.geom                \
       330.geom                \
       400.frag                \
       400.tesc                \
       400.tese                \
       420.geom                \
       430.vert                \
      )

# $1 is the test
# Rest are options to the runcommand

function runParallelTests {
    printf "Running Parallel tests\n"

    $RUNCMD -t test.frag test.vert deepRvalue.frag block.frag length.frag matrix2.frag matrixAgg.frag texture.frag texture.vert \
               300BuiltIns.frag 300BuiltIns.vert uniformArray.frag newTexture.frag qualifiers.frag > localResults/parallel.out
    diff -b baseResults/parallel.out localResults || HASERROR=1

    $RUNCMD --disable unroll -t doWhileLoop.frag forLoop.frag loops.frag whileLoop.frag > localResults/noUnroll.out
    diff -b baseResults/noUnroll.out localResults || HASERROR=1
}

function runSingleTest {
    $RUNCMD -a ${@:2:${#@}} $1 > temp.stdout 2> temp.errout
}

function runIsolatedTests {
    for t in ${ISOLATEDTESTS[@]}; do
        printf "Running $t\n"
        
        runSingleTest $t 
        EXTENSION=${t##*.}
        cat temp.errout temp.stdout > localResults/$t.out.tmp
        cp temp.stdout tempglsl.$EXTENSION
        runSingleTest tempglsl.$EXTENSION
        cat temp.stdout >> localResults/$t.out.tmp
        tr -d '\r' < localResults/$t.out.tmp > localResults/$t.out
        rm localResults/$t.out.tmp
        rm tempglsl.$EXTENSION
        diff -b baseResults/$t.out localResults || HASERROR=1

    done
    rm temp.stdout* temp.errout
}

RUNCMD=../build/install/bin/LunarGOO
HASERROR=0
GLSL_OP="--glsl"
OBFUSCATE_OP="-f"
DO_OBFUSCATE=""

# Command-line argument Handling

# Arguments passed
while [ $# -gt 0 ]; do
    case "$1" in
        -h|--h)
            showHelp
            exit 0
            ;;
        -o|--do-obfuscate)
            DO_OBFUSCATE="true"
            ;;
        *)
            showHelp
            exit 1
            ;;
    esac
    shift
done

mkdir -p localResults
runParallelTests
runIsolatedTests

if [ $HASERROR -eq 0 ]
then
    echo Tests Succeeded.
else
    echo Tests Failed.
fi

exit $HASERROR
