#!/bin/sh
# vim: ts=4 sw=4 et ft=sh
#
# A program to test different versions of the compiler.

usage="Usage: speedtest [-dsmtz] [-l | -1 | -N N] [-e dir] [-g grade] [-c cmd] [-o option]* [-nN] [-ON] [-fFILE] batchname"

cmd=""
debug=false
execdir="arena"
grade="--grade asm_fast.gc"
framesizefile=""
mix=false
num_modules=10
limit=6
options=""
optlevel="-O2"
size=false
table_io=false
zip=false

while test $# -gt 0
do
    case $1 in

        -c|--cmd)
            cmd="$2"
            shift
            ;;
        -c*)
            cmd="`expr $1 : '-c\(.*\)'`"
            ;;

        -d)
            debug=true
            ;;

        -e)
            execdir="$2"
            shift
            ;;
        -e*)
            execdir="`expr $1 : '-e\(.*\)'`"
            ;;

        -g)
            grade="--grade $2"
            shift
            ;;
        -g*)
            grade="--grade `expr $1 : '-g\(.*\)'`"
            ;;

        -f)
            framesizefile="$2"
            shift
            ;;
        -f*)
            framesizefile="`expr $1 : '-f\(.*\)'`"
            ;;

        -l)
            num_modules=20
            ;;

        -1)
            num_modules=1
            ;;

        -m)
            mix=true
            ;;

        -n)
            limit="$2"
            shift
            ;;
        -n*)
            limit="`expr $1 : '-n\(.*\)'`"
            ;;

        -N)
            num_modules="$2"
            shift
            ;;
        -N*)
            num_modules="`expr $1 : '-N\(.*\)'`"
            ;;

        -o)
            options="${options} $2"
            shift
            ;;
        -o*)
            options="${options} ` expr $1 : '-f\(.*\)' `"
            ;;

        -O)
            optlevel="-O$2"
            shift
            ;;
        -O*)
            optlevel="$1"
            ;;

        -s)
            size=true
            ;;

        -t)
            table_io=true
            ;;

        -z)
            zip=true
            ;;

        -*)
            echo "$0: unknown option \`$1'" 2>&1
            echo ${usage}
            exit 1
            ;;

        *)
            break
            ;;

    esac
    shift
done

root=`/bin/pwd`

# Put src_lines in ${PATH}.
PATH="${root}/tools:${PATH}"
export PATH

if test "${cmd}" = ""
then
    cd ${execdir}
    modulelist=`src_lines *.m | head -${num_modules} | awk -e '{print $2;}'`
    cd ${root}
    # In case the programmer wants to know, e.g. for comparison purposes.
    echo ${modulelist} > .module_list
    cmd="mmc -C ${optlevel} ${options} ${grade} ${modulelist}"
fi

if test $# != 1
then
    echo ${usage}
    exit 1
fi

batch=$1

if $zip
then
    trap 'gzip ${root}/batch/${batch}.mercury_compile.*[0-9] > /dev/null 2>&1; exit 0' 0 1 2 3 15
fi

if test -x /usr/ucb/echo
then
    ECHO=/usr/ucb/echo
else
    ECHO=echo
fi

maybe_zipped_files=`ls batch/$batch.mercury_compile.*`
for maybe_zipped_file in ${maybe_zipped_files}
do
    case ${maybe_zipped_file} in
    *.gz)
        gunzip ${maybe_zipped_file}
        ;;
    esac
done

files=`ls batch/$batch.mercury_compile.*`
if ${mix}
then
    count=1
    cd ${execdir}
    while test ${count} -le ${limit}
    do
        for file in ${files}
        do
            MERCURY_COMPILER=${root}/${file}
            export MERCURY_COMPILER
            briefname=`echo "${file}" | sed "s:batch/$batch.::"`
            $ECHO -n "${briefname} "
            if ${debug}
            then
                if ${table_io}
                then
                    (echo "table_io start" ; echo "c" ) \
                        | ${root}/tools/dotime mdb ${cmd}
                else
                    echo "c" | ${root}/tools/dotime mdb ${cmd}
                fi
            else
                ${root}/tools/dotime ${cmd}
            fi
        done
        count=`expr ${count} + 1`
    done
    cd ${root}
else
    for file in ${files}
    do
        paramfile=`echo ${file} | sed 's/mercury_compile/params/'`
        if test -r ${paramfile}
        then
            cat ${paramfile}
        fi

        if ${size}
        then
            size ${file}
        fi

        MERCURY_COMPILER=${root}/${file}
        export MERCURY_COMPILER
        cd ${execdir}
        count=1
        while test ${count} -le ${limit}
        do
            if test "${framesizefile}" != ""
            then
                rm ${framesizefile} > /dev/null 2>&1
            fi

            briefname=`echo "${file}" | sed "s:batch/$batch.::"`
            $ECHO -n "${briefname} "
            if ${debug}
            then
                if ${table_io}
                then
                    (echo "table_io start" ; echo "c" ) \
                        | ${root}/tools/dotime mdb ${cmd}
                else
                    echo "c" | ${root}/tools/dotime mdb ${cmd}
                fi
            else
                ${root}/tools/dotime ${cmd}
            fi

            if test -s Deep.data
            then
                mv Deep.data \
                    ${root}/batch/Deep.data.`basename ${file} .gz`.run${count}
            fi

            if test -s Deep.procrep
            then
                mv Deep.procrep \
                    ${root}/batch/Deep.procrep.`basename ${file} .gz`.run${count}
            fi

            if test "${count}" -eq 1 -a "${framesizefile}" != ""
            then
                echo
                cat ${framesizefile}
                echo
            fi

            count=`expr ${count} + 1`
        done

        cd ${root}
        if ${zip}
        then
            gzip ${file}
        fi
    done
fi

exit 0
