#!/bin/sh

usage()
{
    echo usage $0 old-test new-test [old-value new-value ...]
    echo
    echo Create a new test from an existing one. Replaces all apparitions of old-value with new-value in the source file. You could specify more than one pair of such values.
}

if [ $# -lt 2 ]; then
    usage
    exit
fi

if [ "`echo \( $# / 2 \) \* 2 | bc`" != "$#" ]; then
    usage
    echo
    echo ERROR: You should specify an even number of values.
    exit
fi

new_test=$2

echo creating test $new_test from $1...

if [ ! -d testsuite/$2 ]; then
    mkdir testsuite/$2;
fi;
echo copying and modifying the files from testsuite/$1 to testsuite/$2
cp testsuite/$1/*.exp testsuite/$2;

shift
shift

if [ $# -gt 0 ]; then
    sed_cmd=
    while [ $# -gt 0 ]; do
	sed_cmd="$sed_cmd -e '/$1 class/b' -e 's/$1/$2/g'"
	shift
	shift
    done
fi

cd testsuite/$new_test
for f in *.exp; do
    echo $f
    echo "$sed_cmd $f" | xargs sed >$f.new
    mv $f.new $f
done
