#!/bin/sh
# Test if chmd_md5 expands chm files identically to Microsoft's HH.EXE -DECOMPILE

[ -d .cache ] || mkdir .cache
BASEDIR=`dirname $0`

cnt=1
orig=.$$.orig
test=.$$.test
for chm in "$@"; do
    name=`printf '%d/%d %s' $cnt $# $chm`; cnt=`expr $cnt + 1`

    echo "test $name"
    cached=`echo $chm | sed -e 's/\//-/g' -e 's/^/.cache\//'`
    if [ ! -s $cached ]; then
        $BASEDIR/msdecompile_md5 $chm >$orig 2>$orig.err
        if [ -s $orig.err ]; then
            echo "FAIL $name: MS errors" >&2
            cat $orig.err >&2
            continue
        fi
        LANG=C sort -k2 $orig >$cached
    fi

    $BASEDIR/chmd_md5 $chm >$test.unsorted 2>$test.errwarn
    perl -pe 'if($.>1){$_=""if/^[0-9a-f]{32} \/[#\$]/;s{ /}{  }}' $test.unsorted | LANG=C sort -k2 >$test
    sed '/^WARNING; /d' $test.errwarn > $test.err
    if [ -s $test.err ]; then
        echo "FAIL $name: errors" >&2
        cat $test.errwarn >&2
        continue
    fi

    if cmp $cached $test >/dev/null; then
        echo "OK   $name"
    else
       if [ `diff $cached $test | grep -c '^<'` -gt 0 ]; then
           echo "FAIL $name: differences" >&2
           diff -u $cached $test >&2
       else
           echo "OK   $name (better than hh.exe)"
       fi
    fi
done
rm -f $orig $orig.err $test $test.err $test.errwarn $test.unsorted
