#!/bin/sh

#
#        Example Script to run tests and check results.
#
#        This is an example script for running QA tests on a
#        model and then checking the simulated results against
#        reference results. A separate target is defined for each
#        variant of the model. The program runQaTests.pl runs the
#        tests, and that program expects a perl module SIMULATOR.pm
#        to be provided for each simulator that is tested.
#        Examples of these are provided.
#

qaSpecFile="qaSpec"
qaResultsDirectory="results"
testProgramName="$(dirname $0)/runQaTests.pl"
if [ -z "$testProgramFlags" ] ; then
    testProgramFlags="-nw"
fi

# uncomment this to preserve netlist after test:
# testProgramFlags="-d -V"


help() {
    cat <<-EOF
        Valid targets are:

        all                run tests and compare results for all simulators

        ngspice            run tests and compare results ngspice

        hspice             run tests and compare results hspice

        ads             run tests and compare results ads

        clean              remove all previously generated simulation results

        NOTE: if test results exist they are not resimulated
        NOTE: to force resimulation run "make clean" first
EOF
}


run_test() {

    simname="$1"

    for i in $(${testProgramName} ${testProgramFlags} -s ${simname} --tuple) ; do
        case "${i%%=*}" in
            platform)  localPlatform="${i#*=}";;
            version)   localVersion="${i#*=}";;
            vaVersion) localVaVersion="${i#*=}";;
        esac
    done

    echo ""
    echo "******"
    echo "****** ${qaSpecFile} tests for ${simname}"
    echo "****** (for version ${localVersion} on platform ${localPlatform})"
    echo "******"

    for test in `${testProgramName} -lt -s ${simname} ${qaSpecFile}` ; do

        echo ""
        echo "****** Checking test (${simname}): ${test}"

        for variant in `${testProgramName} -lv -s ${simname} ${qaSpecFile}` ; do
            ${testProgramName} \
                ${testProgramFlags} \
                -s ${simname} \
                -r -t ${test} \
                -var ${variant} \
                --version=${localVersion} \
                --vaVersion=${localVaVersion} \
                --platform=${platform} \
                --results=${qaResultsDirectory} \
                ${qaSpecFile}
        done
    done

    for version in `ls -C1 ${qaResultsDirectory}/${simname}` ; do
        for platform in `ls -C1 ${qaResultsDirectory}/${simname}/${version}` ; do

            if [ "${version}" = "${localVersion}" -a "${platform}" = "${localPlatform}" ]
            then
                break
            fi

            echo ""
            echo "******"
            echo "****** Comparing previously run ${qaSpecFile} tests for ${simname}"
            echo "****** (for version ${version} on platform ${platform})"
            echo "******"

            for test in `${testProgramName} -lt -s ${simname} ${qaSpecFile}` ; do

                echo ""
                echo "****** Checking test (${simname}): ${test}"

                for variant in `${testProgramName} -lv -s ${simname} ${qaSpecFile}` ; do
                    ${testProgramName} \
                        -c ${version} ${platform} \
                        -s ${simname} \
                        -t ${test} \
                        -var ${variant} \
                        --version=${localVersion} \
                        --vaVersion=${localVaVersion} \
                        --platform=${platform} \
                        --results=${qaResultsDirectory} \
                        ${qaSpecFile}
                done
            done
        done
    done
}


#####
##### tests
#####

ngspice() {
    run_test ngspice
}

hspice() {
    run_test hspice
}

ads() {
    run_test ads
}


#####
##### clean
#####

clean() {
    rm -rf ${qaResultsDirectory}/ngspice ngspiceCkt*
    rm -rf ${qaResultsDirectory}/hspice hspiceCkt*
    rm -rf ${qaResultsDirectory}/ads adsCkt*
}

clean_ngspice() {
    rm -rf ${qaResultsDirectory}/ngspice ngspiceCkt*
}

clean_hspice() {
    rm -rf ${qaResultsDirectory}/hspice hspiceCkt*
}

clean_ads() {
    rm -rf ${qaResultsDirectory}/ads adsCkt*
}


all() {
    ngspice hspice ads
}


while test $# -gt 0; do
  case "$1" in
      --srcdir=* | --executable=*)
          testProgramFlags="$testProgramFlags $1"
          shift
          ;;
      --results)
          qaResultsDirectory="$2"
          shift ; shift
          ;;
      -qa)
          qaSpecFile="$2"
          shift ; shift
          ;;
      *)
          break
          ;;
  esac
done

for arg in $@ ; do
    case "$arg" in
        all | clean | clean_ngspice | ngspice | clean_hspice | hspice | clean_ads | ads)
            "$arg"
            ;;
        *)
            help
            ;;
    esac
done
