#!/bin/ksh

REFERENCE="$1"
COMPARISON="$2"
RESULT="$3"

if test -z "${REFERENCE}" -o -z "${COMPARISON}" -o -z "${RESULT}"
then
    echo "usage: $0 <reffile> <compfile> <resultfile>"
    exit
fi

if test ! -r ${REFERENCE}
then
    echo "Cannot read ref file ${REFERENCE}"
    exit
fi

if test ! -r ${COMPARISON}
then
    echo "Cannot read comp file ${COMPARISON}"
    exit
fi

if test -e ${RESULT} -a ! -w ${RESULT}
then
    echo "Cannot write res file ${RESULT}"
    exit
fi

if test ! -x evaluate
then
    if test -e evaluate.c
    then
	gcc -lm evaluate.c -o evaluate
    fi
    if test ! -x evaluate
    then
	echo "Cannot execute evaluate"
	exit
    fi
fi

rm -f tmp.ref tmp.comp ${RESULT}

cp ${REFERENCE} tmp.ref
cp ${COMPARISON} tmp.comp

sed "s#^p-##" tmp.ref | sed "s#\.# #" > ${REFERENCE}
sed "s#^p-##" tmp.comp | sed "s#\.# #" > ${COMPARISON}

evaluate ${REFERENCE} ${COMPARISON} > ${RESULT}

mv tmp.ref ${REFERENCE}
mv tmp.comp ${COMPARISON}

