# Copyright 2013 Samsung Information Systems America, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Author: Koushik Sen


rm -rf jalangi_tmp
mkdir jalangi_tmp
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export JALANGI_HOME=$DIR/..
cd jalangi_tmp

if [ "$USE_NODE_COVER" == "true" ]
then
    if [ "$USE_TIME" == "true" ]
    then
        export CMD='time cover run '
    else
        export CMD='cover run '
    fi
    cp ../.coverignore .
    mv ../.coverage_data .
else
    if [ "$USE_TIME" == "true" ]
    then
        export CMD='time node '
    else
        export CMD='node '
    fi
fi

echo $CMD

echo --------------- Instrumenting $2.js --------------
$CMD $JALANGI_HOME/src/js/instrument/esnstrument.js ../$2.js


i=0
iter=1

while [[ $i -le $iter &&  $i -le $3 ]]
do

    rm inputs.js 2> /dev/null
    touch inputs.js

    echo "*********************************************************************************"
    echo "******************************** Input $i ************************************"
    echo "*********************************************************************************"

    export JALANGI_MODE=symbolic
    export JALANGI_ANALYSIS=$1
    $CMD ../$2_jalangi_.js

    declare -i iter=$(sed -n 1p jalangi_tail 2>/dev/null)
    i=$((i+1))

done

if [ $iter -eq $3 ]
then
    echo "$2.js $3 passed" >> $JALANGI_HOME/jalangi_sym_test_results
else
    echo "################# $2.js $3 Failed!!! # of tests $iter" >> $JALANGI_HOME/jalangi_sym_test_results
fi

#($DIR/purererun $2 > test)
#declare -i x=$(cat test | grep "^[0-9][0-9]*$" | sort -n -r | uniq | sed -n 1p)
#declare -i y=$(cat test | grep "^[0-9][0-9]*$" | sort -n -r | uniq | wc -l)

#if [ $x -eq $y ]
#then
#    echo "$2.js $3 passed on coverage test" >> $JALANGI_HOME/jalangi_sym_test_results
#else
#    echo "***************** $2.js $3 failed on coverage test!!! # of tests $iter" >> $JALANGI_HOME/jalangi_sym_test_results
#fi


echo "Generated $iter tests"

if [ "$USE_NODE_COVER" == "true" ]
then
    mv .coverage_data ..
fi

