#! /bin/sh
# This script tests: pamcat
# Also requires:

tmpdir=${tmpdir:-/tmp}
list=${tmpdir}/list
list2=${tmpdir}/list2
list3=${tmpdir}/list3
liste1=${tmpdir}/liste1
liste2=${tmpdir}/liste2
liste3=${tmpdir}/liste3
files="maze.pbm testgrid.pbm testimg.ppm"

ls ${files} | tee ${list} | awk '{print ""; print $0; print ""}' > ${list3}
sed 's/maze.pbm/-/' ${list} > ${list2}

echo "Test 1.   Should print 585134073 133221 four times"
pamcat -lr -jt ${files} | cksum
pamcat -lr -jt -listfile=${list} | cksum
cat maze.pbm | pamcat -lr -jt -listfile=${list2} | cksum
pamcat -lr -jt -listfile=${list3} | cksum

echo "Test 2.   Should print 1331083756 152559 three times"
pamcat -tb -jl ${files} | cksum
pamcat -tb -jl -listfile=${list} | cksum
cat ${list} | pamcat -tb -jl -listfile=- | cksum

echo "Test Invalid"

test_out=${tmpdir}/test_out

echo 1>&2
echo "Invalid command-line argument combinations & listfile content." 1>&2
echo "Error messages should appear below the line." 1>&2
echo "-----------------------------------------------------------" 1>&2

# listfile not specified
pamcat -lr -listfile > ${test_out} || \
  printf "Expected failure 1 "
  test -s ${test_out} && echo "unexpected output" || echo "(no output)"
  rm -f ${test_out}

# listfile does not exist
pamcat -lr -listfile=`mktemp -u` > ${test_out} || \
  printf "Expected failure 2 "
  test -s ${test_out} && echo "unexpected output" || echo "(no output)"
  rm -f ${test_out}

# listfile empty
pamcat -lr -listfile=/dev/null > ${test_out} || \
  printf "Expected failure 3 "
  test -s ${test_out} && echo "unexpected output" || echo "(no output)"
  rm -f ${test_out}

# listfile from stdin, empty
cat /dev/null | pamcat -lr -listfile=- > ${test_out} || \
  printf "Expected failure 4 "
  test -s ${test_out} && echo "unexpected output" || echo "(no output)"
  rm -f ${test_out}

# Files provided from command line in addition to listfile
pamcat -lr -listfile=${list} testgrid.pbm testgrid.pbm > ${test_out} || \
  printf "Expected failure 5 "
  test -s ${test_out} && echo "unexpected output" || echo "(no output)"
  rm -f ${test_out}

# "-" (stdin) provided from command line in addition to listfile
pamcat -lr -listfile=${list} - > ${test_out} || \
  printf "Expected failure 6 "
  test -s ${test_out} && echo "unexpected output" || echo "(no output)"
  rm -f ${test_out}

# listfile has nothing but blank lines
sed 's/^.*$//' ${list3} > ${liste1}  
pamcat -lr -listfile=${liste1} > ${test_out} || \
  printf "Expected failure 7 "
  test -s ${test_out} && echo "unexpected output" || echo "(no output)"
  rm -f ${test_out} ${liste1}

# Non-existing file in listfile
( cat ${list} ; mktemp -u ) > ${liste2}
pamcat -lr -listfile=${liste2} > ${test_out} || \
  printf "Expected failure 8 "
  test -s ${test_out} && echo "unexpected output" || echo "(no output)"
  rm -f ${test_out} ${liste2}

# Multiple instances of "-" in listfile
( echo "-"; cat ${list}; echo "-"; echo "-" ) > ${liste3}
pamcat -lr -listfile=${liste3} > ${test_out} || \
  printf "Expected failure 9 "
  test -s ${test_out} && echo "unexpected output" || echo "(no output)"
  rm -f ${test_out} ${liste3}


rm ${list} ${list2} ${list3}


