#!/bin/sh

execdir="$PWD"

# valgrind tests memory usage.
# wine allow for windows testing on linux
if [ -n "${PARVALGRINDOPTS+set}" ]
then
    PARBINARY="valgrind $PARVALGRINDOPTS $execdir/par2"
elif [ "`which wine`" != "" ] && [ -f "$execdir/par2.exe" ]
then
    PARBINARY="wine $execdir/par2.exe"
else
    PARBINARY="$execdir/par2"
fi


if [ -z "$srcdir" ] || [ "." = "$srcdir" ]; then
  srcdir="$PWD"
  TESTDATA="$srcdir/tests"
else
  srcdir="$PWD/$srcdir"
  TESTDATA="$srcdir/tests"
fi

TESTROOT="$PWD"

testname=$(basename $0)
rm -f "$testname.log"
rm -rf "run$testname"

mkdir "run$testname" && cd "run$testname" || { echo "ERROR: Could not change to test directory" ; exit 1; } >&2

# Data which does not repeat, so that every block is found at its own offset
for f in test-1.data test-2.data test-3.data
do
    dd if=/dev/urandom of=$f bs=1024 count=200 2>/dev/null \
        || { echo "ERROR: Could not create test data" ; exit 1; } >&2
done
cp test-1.data test-1.data.orig
cp test-2.data test-2.data.orig

# 24 recovery blocks rebuild a whole file of 20 blocks
$PARBINARY c -b60 -c24 testdata.par2 test-1.data test-2.data test-3.data \
    || { echo "ERROR: creating repair files using PAR 2.0 failed" ; exit 1; } >&2


banner="repair a damaged file in place"
dashes=`echo "$banner" | sed s/./-/g`

echo $dashes
echo $banner
echo $dashes

inode_before=`ls -i test-1.data | awk '{print $1}'`
printf '\336\255\276\357' | dd of=test-1.data bs=1 seek=100000 count=4 conv=notrunc 2>/dev/null

$PARBINARY r -i testdata.par2 > inplace.out 2>&1 \
    || { cat inplace.out ; echo "ERROR: in-place repair using PAR 2.0 failed" ; exit 1; } >&2
cat inplace.out

grep 'Rewrote .* in place' inplace.out > /dev/null \
    || { echo "ERROR: the file was not repaired in place" ; exit 1; } >&2
[ ! -e test-1.data.1 ] || { echo "ERROR: in-place repair created a backup copy" ; exit 1; } >&2
[ "`ls -i test-1.data | awk '{print $1}'`" = "$inode_before" ] \
    || { echo "ERROR: the file was replaced rather than repaired in place" ; exit 1; } >&2
cmp test-1.data test-1.data.orig \
    || { echo "ERROR: the in-place repaired file does not match the original" ; exit 1; } >&2


banner="fall back to a new copy when the file is the wrong size"
dashes=`echo "$banner" | sed s/./-/g`

echo $dashes
echo $banner
echo $dashes

head -c 204799 test-1.data.orig > test-1.data

$PARBINARY r -i testdata.par2 > fallback.out 2>&1 \
    || { cat fallback.out ; echo "ERROR: fallback repair using PAR 2.0 failed" ; exit 1; } >&2
cat fallback.out

grep 'Cannot repair' fallback.out > /dev/null \
    || { echo "ERROR: no reason was given for not repairing in place" ; exit 1; } >&2
[ -e test-1.data.1 ] || { echo "ERROR: fallback repair did not create a backup copy" ; exit 1; } >&2
cmp test-1.data test-1.data.orig \
    || { echo "ERROR: the repaired file does not match the original" ; exit 1; } >&2
rm -f test-1.data.1


banner="do not repair in place a file which holds another file's data"
dashes=`echo "$banner" | sed s/./-/g`

echo $dashes
echo $banner
echo $dashes

# test-1.data holds the only copy of test-2.data
mv test-2.data test-1.data

$PARBINARY r -i testdata.par2 > shared.out 2>&1 \
    || { cat shared.out ; echo "ERROR: repair using PAR 2.0 failed" ; exit 1; } >&2
cat shared.out

grep 'Cannot repair' shared.out > /dev/null \
    || { echo "ERROR: no reason was given for not repairing in place" ; exit 1; } >&2
cmp test-1.data test-1.data.orig \
    || { echo "ERROR: test-1.data does not match the original" ; exit 1; } >&2
cmp test-2.data test-2.data.orig \
    || { echo "ERROR: test-2.data was destroyed" ; exit 1; } >&2

cd "$TESTROOT"
rm -rf "run$testname"

exit 0
