#!/bin/sh

execdir="$PWD"

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


banner="A file is found under another spelling of its name"
dashes=`echo "$banner" | sed s/./-/g`

echo $dashes
echo $banner
echo $dashes

# The set records the name with the accent on the letter it belongs to, and
# the file on disk has the accent as a character of its own, which is how some
# file systems write it.
composed=`printf 'fr\303\250nch_demo.bin'`
decomposed=`printf 'fre\314\200nch_demo.bin'`

i=0
while [ $i -lt 64 ]; do
    printf '%s%063d' 1 $i
    i=$((i + 1))
done > "$composed"

$PARBINARY c -q -s1024 -c2 test.par2 "$composed" || { echo "ERROR: Could not create PAR2 files" ; exit 1; } >&2

mv "$composed" "$decomposed" || { echo "ERROR: Could not write the other spelling" ; exit 1; } >&2
cp "$decomposed" original.bin

if [ -f "$composed" ]
then
  # This file system looks a name up under either spelling, so the file is
  # found as it is named.
  $PARBINARY v -q test.par2 || { echo "ERROR: The file was not found under the other spelling" ; exit 1; } >&2
else
  # The file has the wrong name, and a repair renames it to the one the set
  # records.
  $PARBINARY v test.par2 > verify.out 2>&1
  result=$?
  if [ $result -ne 1 ]
  then
    echo "ERROR: Verifying the file under the other spelling gave $result, expected 1" >&2
    exit 1
  fi

  grep -F "\"$decomposed\" - is a match for \"$composed\"" verify.out > /dev/null \
    || { echo "ERROR: The file was not matched to the name the set records" ; exit 1; } >&2

  $PARBINARY r -q test.par2 || { echo "ERROR: Repair failed" ; exit 1; } >&2

  cmp "$composed" original.bin || { echo "ERROR: The file was not renamed to the name the set records" ; exit 1; } >&2
  test ! -f "$decomposed" || { echo "ERROR: The file is still under the other spelling" ; exit 1; } >&2
fi

# A file whose directory is on disk under another spelling of the directory's
# name is found, and a repair moves it into the directory the set records.
mkdir "dir_$composed" && cp original.bin "dir_$composed/plain.bin" || { echo "ERROR: Could not write the directory" ; exit 1; } >&2

$PARBINARY c -q -s1024 -c2 directory.par2 "dir_$composed/plain.bin" || { echo "ERROR: Could not create PAR2 files" ; exit 1; } >&2

mv "dir_$composed" "dir_$decomposed" || { echo "ERROR: Could not write the other spelling" ; exit 1; } >&2

if [ -d "dir_$composed" ]
then
  $PARBINARY v -q directory.par2 || { echo "ERROR: The file was not found under the other spelling of its directory" ; exit 1; } >&2
else
  $PARBINARY v -q directory.par2 > /dev/null 2>&1
  result=$?
  if [ $result -ne 1 ]
  then
    echo "ERROR: Verifying the file in the other spelling of its directory gave $result, expected 1" >&2
    exit 1
  fi

  $PARBINARY r -q directory.par2 || { echo "ERROR: Repair failed" ; exit 1; } >&2

  cmp "dir_$composed/plain.bin" original.bin || { echo "ERROR: The file was not moved into the directory the set records" ; exit 1; } >&2
  test ! -f "dir_$decomposed/plain.bin" || { echo "ERROR: The file is still in the other spelling of its directory" ; exit 1; } >&2
fi

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

exit 0
