#!/bin/bash

# -------------------------------------------------------
# RPC Project :
# Run RPC_check_rff_rw : read RFF, re-write it and check difference
# P. Robert, ScientiDev, Jan 2022
# -------------------------------------------------------

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


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

if test $# != $Narg || test $hh = 1
   then
   echo "$appli : read RFF, re-write it and check difference"
   echo "$appli   require $Narg argument(s), ex:"
   echo "$appli   toto.rff"
   echo "    toto.rff : name of a RFF file"
   echo ""
   if test $hh = 1 ; then exit 0 ; fi
   echo "$appli36 : *** ERROR ! Command aborted." >&2
   exit 1
fi

filc=`basename $1 .rff`_clean.rff

RPC_clean_rff $1
tail -4 $1    | head -1 > toto1_$$.tmp
tail -4 $filc | head -1 > toto2_$$.tmp

echo "last data record of each file:"
cat toto1_$$.tmp
cat toto2_$$.tmp

diff toto1_$$.tmp toto2_$$.tmp
err=$?

if test $err = 0
   then
       echo "data are the same"
       echo "$appli36  : NORMAL TERMINATION"
   else
       echo "*** difference on data"
       echo "$appli36  : ***BAD TERMINATION"
fi

rm toto1_$$.tmp toto2_$$.tmp

err=$?
exit $err
