#!/bin/sh

if [ $# -lt 1 ]
then
    echo ""
    echo "$0 input.hdr"
    echo ""
    exit 1
fi

if [ ! -f $1 ] 
then
   echo "No such data: $1"
   exit 1
else
   #inputO=`echo $1 | cut -d. -f1`
   inputO="${1%.hdr}"

   input=${inputO}".img" 
   inputHdr=${inputO}".hdr"
     echo "input image       = $input"
     echo "input image header= $inputHdr"
fi
echo "printavwheader ${inputHdr} "
xdim=`printavwheader ${inputHdr} | grep --text '^dim\[1\]' | cut -d\< -f2 | cut -d \> -f1`
ydim=`printavwheader ${inputHdr} | grep --text '^dim\[2\]' | cut -d\< -f2 | cut -d \> -f1` 
zdim=`printavwheader ${inputHdr} | grep --text '^dim\[3\]' | cut -d\< -f2 | cut -d \> -f1` 
xres=`printavwheader ${inputHdr} | grep --text '^pixdim\[1\]' | cut -d\< -f2 | cut -d \> -f1` 
yres=`printavwheader ${inputHdr} | grep --text '^pixdim\[2\]' | cut -d\< -f2 | cut -d \> -f1` 
zres=`printavwheader ${inputHdr} | grep --text '^pixdim\[3\]' | cut -d\< -f2 | cut -d \> -f1`
dataType=`printavwheader ${inputHdr} | grep --text '^bitpix' | cut -d\< -f2 | cut -d \> -f1`


echo "-------------------------"
echo "Image information:"
echo "${xdim} ${ydim} ${zdim}, ${xres} ${yres} ${zres}, ${dataType}"
echo "-------------------------"
echo ""

if [ ${dataType} == 32 ]
then
echo "float" 
triv ${input} -d${xdim},${ydim},${zdim} -V${xres},${yres},${zres} -ff,1,0
else if [ ${dataType} == 16 ]
then
echo "unsigned short" 
triv ${input} -d${xdim},${ydim},${zdim} -V${xres},${yres},${zres} -fu 
else if [ ${dataType} == 8 ]
then
echo "unsigned char"
echo "triv ${input} -d${xdim},${ydim},${zdim} -V${xres},${yres},${zres}"
triv ${input} -d${xdim},${ydim},${zdim} -V${xres},${yres},${zres} 
else
echo "unrecognized datatype: ${dataType}"
fi
fi
fi
