#! /bin/sh
#
# Script to find differences between minc files
#
# Usage: mincdiff [-l] [diff options] <file1.mnc> <file2.mnc>
#
# Modifications: 
#   $Log: mincdiff,v $
#   Revision 6.3  2000-09-12 15:22:06  neelin
#   Removed copyright notice.
#
#   Revision 6.2  2000/09/12 15:11:04  neelin
#   Rewrite in Bourne shell by Steve Robbins. Addition of -header and -body
#   options, plus allowing of diff options.
#
#   Revision 6.1  1999/10/19 14:45:20  neelin
#   Fixed Log subsitutions for CVS
#
#   Revision 6.0  1997/09/12 13:23:46  neelin
#   Release of minc version 0.6
#   
#   Revision 5.0  1997/08/21  13:24:47  neelin
#   Release of minc version 0.5
#  
#   Revision 4.0  1997/05/07  20:01:01  neelin
#   Release of minc version 0.4
#  
#   Revision 3.0  1995/05/15  19:31:40  neelin
#   Release of minc version 0.3
#  
#   Revision 2.1  1995/01/24  09:12:29  neelin
#   Added support for compressed minc files.
#  
#   Revision 2.0  94/09/28  10:34:46  neelin
#   Release of minc version 0.2
#   
#   Revision 1.4  94/09/28  10:34:44  neelin
#   Pre-release
#   
#   Revision 1.3  94/07/18  10:41:26  neelin
#   Added -nonormalize option to mincextract for comparing image data.
#   
#   Revision 1.2  93/11/01  16:50:03  neelin
#   Some fixes.
# 

die () {
    echo >&2 $@
    exit 1
}

header_diff=yes
header_diff_opt=
body_cmp=yes
body_cmp_opt=
while test $# -gt 2; do 
    case $1 in
	-header) header_diff=yes; body_cmp=no ;;
	-body)   header_diff=no; body_cmp=yes ;;
	-l) body_cmp_opt=-l ;;
	-*) header_diff_opt="$header_diff_opt $1" ;;
	*) break;
    esac
    shift
done
test $# -eq 2 || die "Usage: $0 [-header|-body] [-l] [diff options] <file1.mnc> <file2.mnc>"

if test x$TMPDIR = x; then
    for TMPDIR in /usr/tmp /var/tmp /tmp; do
	test -d $TMPDIR && break;
    done
fi
test -d $TMPDIR || die "TMPDIR $TMPDIR does not exist."


# Files created
tmp1="$TMPDIR/mincdiff-$$-tmp1"
tmp2="$TMPDIR/mincdiff-$$-tmp2"
header1="$TMPDIR/mincdiff-$$-header1"
header2="$TMPDIR/mincdiff-$$-header2"
body1="$TMPDIR/mincdiff-$$-body1"
body2="$TMPDIR/mincdiff-$$-body2"

# Clean up upon exit
trap "/bin/rm -f $tmp1 $tmp2 $header1 $header2 $body1 $body2" 0 1 2 15

# Expand the files
dumpfile1=`mincexpand $1 $tmp1 -name_only`
dumpfile2=`mincexpand $2 $tmp2 -name_only`

# Dump the headers and compare them
if test $header_diff = yes; then
    mincheader $dumpfile1 > $header1 || exit 1
    mincheader $dumpfile2 > $header2 || exit 1
    diff $header_diff_opt $header1 $header2
    diff_status=$?
else
    diff_status=0
fi

# Dump the raw data and compare them
if test $body_cmp = yes; then
    echo "Binary image comparison:"
    mincextract -nonormalize -file $dumpfile1 > $body1 \
	|| die "Error extracting image data from $dumpfile1"
    mincextract -nonormalize -file $dumpfile2 > $body2 \
	|| die "Error extracting image data from $dumpfile2"
    cmp $body_cmp_opt $body1 $body2 \
	&& echo "Images are identical."
    cmp_status=$?
else
    cmp_status=0
fi

# Exit with SUCCESS if and only if the parts (header and/or body)
# that we compared are identical.
test $diff_status -eq 0 -a $cmp_status -eq 0
