# 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

# Assumes that JALANGI_HOME is different from project home.
# Assumes that the script is run from the project home.


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

export CMD='node '

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

i=0
iter=1



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

    rm inputs.js 2> /dev/null
    cp jalangi_inputs$i.js inputs.js 2> /dev/null
    if [ ! -f inputs.js ];
    then
        touch inputs.js
    fi

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

    echo --------------- Recording execution of $1.js ---------------
    rm jalangi_trace 2> /dev/null
    export JALANGI_MODE=record
    export JALANGI_ANALYSIS=none
    $CMD ../$1_jalangi_.js

    echo --------------- Replaying $1.js ---------------
    export JALANGI_MODE=replay
    export JALANGI_ANALYSIS=analyses/concolic/SymbolicEngine
    $CMD $JALANGI_HOME/src/js/commands/replay.js

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

done

iter=$((iter+1))

if [ $iter -eq $2 ]
then
    echo "$1.js passed" >> $JALANGI_HOME/jalangi_sym_test_results
else
    echo "***************** $1.js Failed!!!" >> $JALANGI_HOME/jalangi_sym_test_results
fi

echo "***************** Generated (jalangi_tmp/inputs.js:1:1) for ($1.js:1:1) ********************"
for i in jalangi_inputs*
do
    echo "***************** Generated (jalangi_tmp/$i:1:1) for ($1.js:1:1) ********************"
done



echo "**********************************************************************************"
echo "******************************Done with Test Generation***************************"
echo "**********************************************************************************"