#!/bin/sh

# This script is meant to be run by msys (the mingw build environment). You will need to have the
# proper gzip (with the rsyncable patch) in your path, or at the same directory as rsyncrypto.exe

set -e

RSC=$1
options=$2

if [ ! "$1" ]
then
    cat <<EOF
Usage: $0 <path> <gzip> <options>
path - path to rsyncrypto to run regression tests on
options - specify "v" for verbose output
EOF
    exit 1
fi

compare()
{
    echo -n "Comparing $1 to $2:"
    if (diff $1 $2 >/dev/null)
    then
        echo " identical"
    else
        echo " diff failed"
        exit 1
    fi
}

runreg()
{
    cmdline=$1
    testname=$2
    shift 2
    if [ "$options" = "v" ]
    then
        echo "Testing $testname: $cmdline"
    else
        echo "Testing $testname:"
    fi
    if /bin/sh -c "$cmdline"
    then
        while [ "$1" ]
        do
            compare $1 $2
            shift 2
        done
        echo "$testname passed"
    else
        echo "rsyncrypto failed to run"
        exit 1
    fi
}

function dotest()
{
    echo "Running regressions on $1"
    runreg "$RSC -d $1.enc $1.dec $1.key cert.crt" "decryption using symmetric key" $1 $1.dec
    rm $1.dec
    runreg "$RSC -d $1.enc $1.dec $1.key2 cert.key" "decryption using asymmetric key" $1 $1.dec $1.key $1.key2
    rm $1.dec $1.key2
    runreg "$RSC $1 $1.enc2 $1.key cert.key" "encryption using existing key"
    runreg "$RSC -d $1.enc2 $1.dec $1.key cert.crt" "decryption of our own encryption - symmetric" $1 $1.dec
    rm $1.dec
    runreg "$RSC -d $1.enc2 $1.dec $1.key2 cert.key" "decryption of our own encryption - asymmetric" $1 $1.dec $1.key $1.key2
    rm $1.dec $1.key2 $1.enc2
    runreg "$RSC $1 $1.enc2 $1.key2 cert.key" "encryption using new key"
    runreg "$RSC -d $1.enc2 $1.dec $1.key2 cert.crt" "decryption of our own encryption - symmetric" $1 $1.dec
    rm $1.dec
    runreg "$RSC -d $1.enc2 $1.dec $1.key3 cert.key" "decryption of our own encryption - asymmetric" $1 $1.dec $1.key3 $1.key2
    rm $1.dec $1.key2 $1.key3 $1.enc2

    # Let's test some command line options
	# No ready null command available - don't run this test.
    runreg "$RSC -d --gzip=nullgzip.exe $1.enc $1.dec.gz $1.key cert.crt && gunzip $1.dec.gz" "test seperate gunzip" $1 $1.dec
    rm $1.dec

    echo "All tests on $1 successful"
    echo ""
}

echo $RSC

rm -rf regtestdir.* || echo -n ""
mkdir regtestdir.orig
mkdir regtestdir.orig/dummy
mkdir regtestdir.key
mkdir regtestdir.key/dummy
mkdir regtestdir.enc
mkdir regtestdir.enc/dummy
shopt -s extglob
for reg in reg+([0-9])
do
    dotest $reg
    cp $reg regtestdir.orig/dummy
    cp $reg.key regtestdir.key/dummy/$reg
    cp $reg.enc regtestdir.enc/dummy/$reg
done

#echo "$RSC -vv -r -d regtestdir.enc regtestdir.dec regtestdir.key cert.crt"
#exit 0
runreg "$RSC -vv -r -d regtestdir.enc regtestdir.dec regtestdir.key cert.crt" "recursive decryption using symmetric"
for reg in reg+([0-9])
do
    compare regtestdir.orig/dummy/$reg regtestdir.dec/dummy/$reg
done
rm -rf regtestdir.dec
runreg "$RSC -vv -r -d regtestdir.enc regtestdir.dec regtestdir.key2 cert.key" "recursive decryption using asymmetric"
for reg in reg+([0-9])
do
    compare regtestdir.orig/dummy/$reg regtestdir.dec/dummy/$reg
    compare regtestdir.key/dummy/$reg regtestdir.key2/dummy/$reg
done
rm -rf regtestdir.dec
rm -rf regtestdir.key2
runreg "$RSC -vv -r --trim=2 'regtestdir.orig\\dummy' 'regtestdir.enc2\\dummy' 'regtestdir.key\\dummy' cert.key" "recursive encryption using existing"
runreg "$RSC -vv -r --trim=2 -d 'regtestdir.enc2\\dummy' 'regtestdir.dec\\' regtestdir.key2 cert.key" "recursive decryption using asymmetric with trim"
for reg in reg+([0-9])
do
    compare regtestdir.orig/dummy/$reg regtestdir.dec/$reg
    compare regtestdir.key/dummy/$reg regtestdir.key2/$reg
done
rm -rf regtestdir.dec
rm -rf regtestdir.key2

rm -rf regtestdir.*
echo "All tests completed successfully"
