#!/bin/bash

#----------------------------------------------------------------------------
# MIT License
# 
# Copyright (c) 2018 Gabriel Capella
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
#----------------------------------------------------------------------------
#
# This source code is part of my final undergraduate thesis.
# For any suggestion, doubt or comment send an email to
# gabriel@capella.pro
#
#----------------------------------------------------------------------------

export GCC_PATH=${GCC_PATH:-"/cad/ti/gcc"}
export ISE_DS_DIR=${ISE_DS_DIR:-"/opt/Xilinx/14.7/ISE_DS"}

export LANG=''

INIT_TEST="================= START SIMULATION =============="
END_TEST="================== END SIMULATION ==============="

function red () {
    printf "\e[31m$1\e[0m"
}

function green () {
    printf "\e[32m$1\e[0m"
}

function get_result () {
    sed -n -e "/$INIT_TEST/,\$p" $1 | tail -n +2 > /tmp/$hash
    sed -e "/$END_TEST/,\$d" /tmp/$hash > $2
    rm /tmp/$hash
}

# This file will run tests in tests describe in tests
# set -e
status=0

source "$ISE_DS_DIR"/settings64.sh > /dev/null
echo "" > error

for file in ./tests/*; do
    echo "--------------------------- $file" >> error
    printf "%-40sRunning" "$file:"

    hash=$(md5sum "$file" | cut -d" " -f1 -)

    # get expected result
    get_result $file /tmp/smart_exp_$hash

    # get bash from test
    sed -e "/$INIT_TEST/,\$d" $file > /tmp/smart_bash_$hash

    # run bash
    bash /tmp/smart_bash_$hash > /tmp/smart_result_$hash 2>>error

    # get result and filter it
    get_result /tmp/smart_result_$hash /tmp/smart_result_$hash

    printf "\b\b\b\b\b\b\b"
    printf "       "
    printf "\b\b\b\b\b\b\b"


    result=$(diff /tmp/smart_exp_$hash /tmp/smart_result_$hash)

    if [ $? -eq 0 ]
    then
        green "OK\n"
    else
        red "ERROR\n"
        diff /tmp/smart_exp_$hash /tmp/smart_result_$hash
        printf "\n"
        status=1
    fi

    rm /tmp/smart_bash_$hash
    rm /tmp/smart_exp_$hash
done

if [ "$status" -eq "1" ]; then
    cat error
    exit 1
fi

exit 0
