#! /bin/sh
# This script tests: pamdice
# Also requires: pbmmake pgmmake ppmmake pamfile pamtopam

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

# Test 1.
echo "Test 1.  Should print P1@1 1@0@ 10 times"

pbmmake -w 2 5 | pamdice -height=1 -width=1 -outstem=${fname_stem} -plain
ls ${fname_stem}*.pbm | while read file
  do
  cat ${file} | tr '\n'  '@' ; echo
  done

rm ${fname_stem}*.pbm

# Test 2.
echo "Test 2."

pbmmake -g 2 5 | pamdice -height=1 -width=1 -outstem=${fname_stem} -plain
ls ${fname_stem}*.pbm | while read file
  do
  cat ${file} | tr '\n'  '@' ; echo
  done

rm ${fname_stem}*.pbm

# Test 3.
echo "Test 3.  Should print 344787840 178 twice"
pbmmake -g 7 8 | \
    pamdice -height=1 -width=1 -outstem=${fname_stem} -indexfile=- -dry-run | \
    cksum

pamdice -height=2 -width=2 -outstem=${fname_stem} -indexfile=- -dry-run testgrid.pbm | \
    cksum

# Test 4.
echo "Test 4."
pbmmake -g 2 2 | pamdice -height=1 -width=1 -outstem=stem -listfile=- -dry-run
echo
pamdice -height=8 -width=7 -outstem=stem -listfile=- -dry-run testgrid.pbm

# Test 5.
echo "Test 5."

pbmmake -b   1   1 | pamtopam | \
    pamdice -height=1 -width=1 -outstem=stem -numberwidth=4 \
            -listfile=- -dry-run | \
    head -n 1
pbmmake -b   1   1 | \
    pamdice -height=1 -width=1 -outstem=stem -numberwidth=4 \
            -outsuffix=".pnm" \
            -listfile=- -dry-run | \
    head -n 1
ppmmake rgb:00/77/AA 10 10 | \
    pamdice -height=1 -width=1 -outstem=stem -listfile=- -dry-run | \
    head -n 1
pgmmake 0.5 11 11 | \
    pamdice -height=1 -width=1 -outstem=stem -listfile=- -dry-run | \
    head -n 1
pbmmake -g 100 101 | \
    pamdice -height=1 -width=1 -outstem=stem -listfile=- -dry-run | \
    head -n 1
pbmmake -g 101 100 | \
    pamdice -height=1 -width=1 -outstem=stem -listfile=- -dry-run | \
    head -n 1 
pbmmake -g 256 256 | \
    pamdice -height=1 -width=1 -outstem=stem   -listfile=- -dry-run | \
    head -n 1
pbmmake -g 256 256 | \
    pamdice -height=1 -width=1 -outstem=stem -numberwidth=3 \
            -listfile=- -dry-run | \
    head -n 1
pbmmake -g 256 256 | \
    pamdice -height=1 -width=1 -outstem=stem -numberwidth=4 \
            -outsuffix=".pbm" \
            -listfile=- -dry-run | \
    head -n 1

# Test 6.
echo "Test 6."

pamdice -height=1 -width=1 -outstem=/tmp/stem \
        -indexfile=- -dry-run testgrid.pbm | \
    pamfile -machine

pamdice -height=2 -width=2 -outstem=/tmp/stem \
        -indexfile=- -dry-run testgrid.pbm | \
    pamfile -machine

pamdice -height=1000 -width=10000 -outstem=/tmp/stem \
        -indexfile=- -dry-run testgrid.pbm | \
    pamfile -machine

pbmmake -g 256 256 | \
    pamdice -height=1 -width=1 -outstem=stem \
            -indexfile=- -dry-run | \
    pamfile -machine    

pbmmake -g 257 256 | \
    pamdice -height=1 -width=1 -outstem=stem \
            -indexfile=- -dry-run | \
    pamfile -machine

pbmmake -g 257 256 | \
    pamdice -height=1 -width=1 \
            -outstem=stem -indexfile=- -dry-run | \
    pamfile -machine    

# Test Invalid.
echo "Test Invalid"

. ./test-invalid.inc

# No output files should be producd.  With nothing to remove,
# the rm commands should always fail.

common_opt="-width=10 -height=10 -outstem=${fname_stem}"

# No input file
invCmd "pamdice ${common_opt} /dev/null"
invCmd "rm ${fname_stem}*"

# No -outstem
invCmd "pamdice -width=10 -height=10 testgrid.pbm"
invCmd "rm ${fname_stem}*"

# -width=0
invCmd "pamdice -width=0 -height=10 -outstem=${fname_stem} testgrid.pbm"
invCmd "rm ${fname_stem}*"

# -height=0
invCmd "pamdice -width=10 -height=0 -outstem=${fname_stem} testgrid.pbm"
invCmd "rm ${fname_stem}*"

# -hoverlap larger than width
invCmd "pamdice -width=10 -height=10 -hoverlap=11 -outstem=${fname_stem} testgrid.pbm"
invCmd "rm ${fname_stem}*"

# -voverlap larger than height
invCmd "pamdice -width=10 -height=10 -voverlap=11 -outstem=${fname_stem} testgrid.pbm"
invCmd "rm ${fname_stem}*"

# Invalid listfile
invCmd "pamdice ${common_opt} -listfile=${tmp} testgrid.pbm"
invCmd "rm ${fname_stem}*"

# No listfile
invCmd "pamdice ${common_opt} testgrid.pbm -listfile"
invCmd "rm ${fname_stem}*"

# Invalid indexfile
invCmd "pamdice ${common_opt} -indexfile=${tmp} testgrid.pbm"
invCmd "rm ${fname_stem}*"

# No indexfile
invCmd "pamdice ${common_opt} testgrid.pbm -indexfile"
invCmd "rm ${fname_stem}*"

# No outsuffix
invCmd "pamdice ${common_opt} testgrid.pbm -outsuffix"
invCmd "rm ${fname_stem}*"

# The following specifies outsuffix as "testgrid.pbm"
# pamdice will wait for input from stdin

# pamdice ${common_opt} -outsuffix testgrid.pbm


# -numberwidth=0
invCmd "pamdice ${common_opt} -indexfile=${tmp}  -numberwidth=0 testgrid.pbm"
invCmd "rm ${fname_stem}*"

# -numberwidth too large
invCmd "pamdice ${common_opt} -indexfile=${tmp} -numberwidth=10 testgrid.pbm"
invCmd "rm ${fname_stem}*"

test_pbm=${tmpdir}/test.pbm

# -numberwidth insufficient for input file width
pbmmake -g 100 99 > ${test_pbm}
invCmd "pamdice ${common_opt} -indexfile=${tmp} -numberwidth=2 ${test_pbm}"
invCmd "rm ${fname_stem}*"
rm ${test_pbm}

# -numberwidth insufficient for input file height
pbmmake -g 10 100 > ${test_pbm}
invCmd "pamdice ${common_opt} -indexfile=${tmp} -numberwidth=2 ${test_pbm}"
invCmd "rm ${fname_stem}*"
rm ${test_pbm}

invfile=${tmpdir}/invalid_file

# Same name for listfile indexfile
invCmd "pamdice ${common_opt} -listfile=${invfile} -indexfile=${invfile} testgrid.pbm"
invCmd "rm ${fname_stem}* ${invfile} "

# listfile indexfile both stdout
invCmd "pamdice ${common_opt} -listfile=- -indexfile=- testgrid.pbm"
invCmd "rm ${fname_stem}*"
