#! /bin/sh
# This script tests: pamundice
# Also requires: pamfile pgmmake pnmtile pamcat

tmpdir=${tmpdir:-/tmp}
fname_stem=${tmpdir}/part

echo "Test 1.  Should print 2096818803 2818 twice"

for x in 0 1 2
  do for y in 0 1 2 3 4
    do
    pgmmake "."$(( x* 25 + y )) 11 17 > ${fname_stem}_${y}_${x}.pgm
    done
  done

pamundice -across=3 -down=5 ${fname_stem}"_%1d_%1a".pgm | cksum

ls ${fname_stem}_?_?.pgm | \
    pamundice -across=3 -down=5 -listfile=- | cksum

echo "Test 2.  Should print 2096818803 2818 twice"

tempfile=${tmpdir}/temp

for y in 0 1 2 3 4
  do
  pamundice -across=3 ${fname_stem}"_"$y"_%1a".pgm > ${tempfile}_"$y"
  done

pamcat -tb ${tempfile}_[01234] | cksum
rm ${tempfile}_[01234]

# Note: the following 2 are valid.  There should not be warning messages.

for x in 0 1 2
  do
  pamundice -down=5 ${fname_stem}"_%1d_"$x.pgm > ${tempfile}"_"$x
  done

pamcat -lr ${tempfile}_[012] | cksum
rm ${tempfile}_[012]

rm ${fname_stem}_?_?.pgm

echo "Test 3.  Should print 2096818803 2818 twice"

for x in 0 1
  do for y in 0 1 2 3 4
    do
    pgmmake "."$(( x* 25 + y )) 12 17 > ${fname_stem}_${y}_${x}.pgm
    done
  done

for x in 2
  do for y in 0 1 2 3 4
    do
    pgmmake "."$(( x* 25 + y )) 11 17 > ${fname_stem}_${y}_${x}.pgm
    done
  done

pamundice -across=3 -down=5 \
  -hoverlap=1 ${fname_stem}"_%1d_%1a".pgm | cksum

rm ${fname_stem}_?_?.pgm

for x in 0 1 2
  do for y in 0 1 2 3
    do
    pgmmake "."$(( x* 25 + y )) 11 18 > ${fname_stem}_${y}_${x}.pgm
    done
  done

for x in 0 1 2
  do for y in 4
    do
    pgmmake "."$(( x* 25 + y )) 11 17 > ${fname_stem}_${y}_${x}.pgm
    done
  done

pamundice -across=3 -down=5 \
  -voverlap=1 ${fname_stem}"_%1d_%1a".pgm | cksum

rm ${fname_stem}_?_?.pgm


# Test 4.
echo "Test 4.  Should print 2434390296 4436 four times"

msize=$(pamfile -size maze.pbm)
mw=$(echo ${msize} | cut -d " " -f 1)
mh=$(echo ${msize} | cut -d " " -f 2)

pnmtile $((${mw} * 2)) $((${mh} * 5)) maze.pbm | cksum

for x in 0 1
  do for y in 0 1 2 3 4
    do cp maze.pbm ${fname_stem}_${y}_${x}.pbm; done
  done

for i in 0 1 2 3 4 5 6 7 8 9
  do
  echo maze.pbm
  done | pamundice -down=5 -across=2 -listfile=- | cksum

pamundice -down=5 -across=2 ${fname_stem}_"%1d"_"%1a".pbm | cksum

echo "A warning message should appear below the line." 1>&2
echo "-----------------------------------------------------------" 1>&2

pamundice -down=5 -across=2 maze.pbm | cksum

# Test Invalid.
echo "Test Invalid"

. ./test-invalid.inc

# No input file pattern specified
invCmd "pamundice -down=5 -across=2"

# -down=0
invCmd "pamundice -down=0 -across=2 ${fname_stem}_%1d_%1a.pbm"

# -across=0
invCmd "pamundice -down=5 -across=0 ${fname_stem}_%1d_%1a.pbm"

# -down too large
invCmd "pamundice -down=6 -across=2 ${fname_stem}_%1d_%1a.pbm"

# -across too large
invCmd "pamundice -down=5 -across=3 ${fname_stem}_%1d_%1a.pbm"

# precision does not match
invCmd "pamundice -down=5 -across=2 ${fname_stem}_%2d_%2a.pbm"

# precision set to zero
invCmd "pamundice -down=5 -across=2 ${fname_stem}_%0d_%0a.pbm"

# no precision
invCmd "pamundice -down=5 -across=2 ${fname_stem}_%d_%a.pbm"

# -hoverlap too large
invCmd "pamundice -d=5 -ac=2 -hoverlap=$((${mw}+1)) ${fname_stem}_%1d_%1a.pbm"

# -voverlap too large
invCmd "pamundice -d=5 -ac=2 -voverlap=$((${mh}+1)) ${fname_stem}_%1d_%1a.pbm"


listfile=${tmpdir}/listfile

# listfile with insufficient lines (insufficient file entries)
ls ${fname_stem}_*_*.pbm | tail -n +2  > ${listfile}
invCmd "pamundice -down=5 -across=2 -listfile=${listfile}"
rm -f ${listfile}

# corrupt listfile : file names do not exist
for i in 0 1 2 3 4 5 6 7 8 9
  do
  mktemp -u XXXXXXXXXX.${i} || echo :::::::::::${i}:::::::::::
  done > ${listfile}
invCmd "pamundice -down=5 -across=2 -listfile=$listfile"
rm -f ${listfile}

rm -f ${test_out}
rm ${fname_stem}*.pbm
