#! /bin/sh
#
# Script to view header info of MINC files.
#
# Usage: mincheader [-data] <file.mnc>
# Exit status: status of `mincdump'
#
#
# Modifications: 
#   $Log: mincheader,v $
#   Revision 6.4  2008-01-04 06:01:23  rotor
#    * updated shell scripting style and added more verbose help
#
#   Revision 6.3  2004/04/27 15:32:58  bert
#   Use mincdump instead of ncdump
#
#   Revision 6.2  2000/09/12 15:20:07  neelin
#   Rewrite in Bourne shell by Steve Robbins. Returns exit status from mincdump.
#
#   Revision 6.1  1999/10/19 14:45:23  neelin
#   Fixed Log subsitutions for CVS
#
#   Revision 6.0  1997/09/12 13:23:37  neelin
#   Release of minc version 0.6
#   
#   Revision 5.0  1997/08/21  13:24:37  neelin
#   Release of minc version 0.5
#  
#   Revision 4.0  1997/05/07  20:00:42  neelin
#   Release of minc version 0.4
#  
#   Revision 3.0  1995/05/15  19:31:32  neelin
#   Release of minc version 0.3
#  
#   Revision 2.2  1995/01/25  08:04:41  neelin
#   Added check for failure to read minc file.
#  
#   Revision 2.1  95/01/24  10:19:09  neelin
#   Added support for compressed files.
#   
#   Revision 2.0  94/09/28  10:34:10  neelin
#   Release of minc version 0.2
#   
#   Revision 1.6  94/09/28  10:34:06  neelin
#   Pre-release
#   
#   Revision 1.5  93/10/14  10:09:35  neelin
#   Fixed usage error message.
#   
#   Revision 1.4  93/08/25  11:24:20  neelin
#   Added checking for -h or -help options.
#   
#   Revision 1.3  93/08/11  15:20:55  neelin
#   Added RCS logging in source.
# 

set -o errexit
set -o nounset


# simple little function to emulate perl's die();
die () {
    echo >&2 "$@"
    echo ""
    exit 1
}

# create tmpdir
tmpdir=${TMPDIR:-/tmp}/mincedit.$$
trap 'rm -rf "$tmpdir"' 0
trap ' exit ' 1 2 15
(umask 077 && mkdir $tmpdir) || {
   echo "$0: Could not create temporary directory! Exiting." 1>&2
   echo ""
   exit 1
   }

me=`basename $0`
usage="Usage: $me [-data] <minc file>"

# set defaults and then get arguments
header_opts='-header_only'
header_only=yes
while [ $# -gt 0 ]
do 
   case "$1" in
      -h|-he|-hel|-help)
         echo $usage;
         echo ""
         exit 0
         ;;
   
      -d|-da|-dat|-data)
         header_opts='-all_data';
         header_only=no
         ;;
   
      *)
         break;
   
   esac
   shift
done

# only allow one input file and check for it
[ $# -eq 1 ] || die $usage
[ -f "$1" ] || die "$me: no such file '$1'"

tmpfile="$tmpdir/mincheader-$$-tmp"

# For a compressed file, dump header only unless directed otherwise
dumpfile=`mincexpand "$1" $tmpfile -name_only $header_opts`
if [ "$dumpfile" = $tmpfile -a $header_only = yes ]
then
    mincdump -h "$dumpfile"
else
   #  Get variables names (excluding image): remove leading "image", remove word 
   #  "image", collapse multiple blanks to one, remove leading blanks, remove
   #  trailing blanks, replace blanks with commas
   vars=`mincinfo -varnames "$dumpfile" | \
   sed -e 's/^image //g' \
       -e 's/ image / /g' \
       -e 's/  */ /g' \
       -e 's/^ //' \
       -e 's/ $//' \
       -e 's/ /,/g'`
   
   [ x$vars = x ] || mincdump -v "$vars" "$dumpfile"
fi

