#!/bin/bash

# -------------------------------------------------------
# RPC Project :
# Run RPC_info_rff : return main infos of given RFF file
# P. Robert, LPP, 2011-08-24
# -------------------------------------------------------

appli=`basename $0`
appli36=`echo "$appli                                 " | cut -c1-36`
Narg=2

if( (test $# = 1 ) && (test $1 = -h ) ) ; then hh=1 ; else hh=0 ; fi

if test $# != $Narg || test $hh = 1
   then
   echo "$appli : return main infos of given RFF file"
   echo "$appli   require $Narg argument(s), ex:"
   echo "$appli   toto.rff option"
   echo "    toto.rff : name of a RFF file"
   echo "    option   : l (long) s (short)"
   echo ""
   if test $hh = 1 ; then exit 0 ; fi
   echo "$appli36 : *** ERROR ! Command aborted." >&2
   exit 1
fi

if ! test -f $1
   then
       echo "file $1 does not exist"
       echo ""
       echo "$appli36 : *** ERROR ! Command aborted." >&2
       exit 2
fi

ext=`echo $1 | sed 's/.*\.//'`

if test $ext != rff
   then
   echo " *** file is not a rff file"
   echo "$appli36 : *** ERROR ! Command aborted." >&2
   exit 3
fi

# Option test
# ------------

if ((test $2 != l) && (test $2 != s))
   then 
        echo "$appli : Option must be only l or s"
        echo "$appli36 : *** ERROR ! Command aborted." >&2
        exit 4
fi


head -300 $1 > /tmp/toto1_$$.tmp

grep FILE_CLASS                /tmp/toto1_$$.tmp  > /tmp/toto2_$$.tmp
grep MISSION_NAME              /tmp/toto1_$$.tmp >> /tmp/toto2_$$.tmp
grep OBSERVATORY_NUMBER        /tmp/toto1_$$.tmp >> /tmp/toto2_$$.tmp
grep EXPERIMENT_NAME           /tmp/toto1_$$.tmp >> /tmp/toto2_$$.tmp
grep EXPERIMENT_MODE           /tmp/toto1_$$.tmp >> /tmp/toto2_$$.tmp
grep COORDINATE_SYSTEM         /tmp/toto1_$$.tmp >> /tmp/toto2_$$.tmp
grep BLOCK_FIRST_INDEX         /tmp/toto1_$$.tmp >> /tmp/toto2_$$.tmp
grep BLOCK_LAST_INDEX          /tmp/toto1_$$.tmp >> /tmp/toto2_$$.tmp
grep BLOCK_NUMBER              /tmp/toto1_$$.tmp >> /tmp/toto2_$$.tmp
grep DATA_DIMENSION            /tmp/toto1_$$.tmp >> /tmp/toto2_$$.tmp

if test $2 = l
   then cat /tmp/toto2_$$.tmp
   \rm /tmp/toto1_$$.tmp /tmp/toto2_$$.tmp
   exit 0
fi

sed -e "s/ /_/g" /tmp/toto2_$$.tmp > /tmp/toto3_$$.tmp

sed -e "s/.*):_//"  -e "7,8s/-/ /"g  -e "s/UNKNOWN/&  /" \
    -e "7,8s/T/ /" -e "7,8s/Z//"   -e "s/:/ /"g -e "s/\..*$//"  \
    -e "4s/^ *//"  -e "/None/s/$/                    /" \
     /tmp/toto3_$$.tmp | sed \
                                      -e 'N;1,2s/\n/ /' \
                                      -e 'N;1,2s/\n/ /' \
                                      -e 'N;1,2s/\n/ /' \
                                      -e 'N;1,2s/\n/ /' \
                                      -e 'N;1,2s/\n/ /' \
                                      -e 'N;1,2s/\n/ /' \
                                      -e 'N;1,2s/\n/ /' \
                                      -e 'N;1,2s/\n/ Nbloc=/' \
                                      -e 'N;1,2s/\n/ Ncomp=/' 

                                      
\rm /tmp/toto1_$$.tmp /tmp/toto2_$$.tmp /tmp/toto3_$$.tmp


