#!/bin/bash

# -----------------------------------------------------------------
# RPC Project :
# Run RPC_join_vectime concatenate 2 rff file of same experiment in a single file
# P. Robert, ScientiDev, Dec 2020
# -----------------------------------------------------------------

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

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

if test $# != $Narg || test $hh = 1
   then
   echo "$appli : join 2 rff vectime files of same experiment in a single file"
   echo "$appli   require $Narg argument(s)"
   echo "$appli   file_1.rff file-2.rff file_conca.rff"

   if test $hh = 1 ; then exit 0 ; fi
   echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 1
fi

datim1=`date +%F'  '%H':'%M':'%S `
julsec1=`date +%s`

file1=$1
file2=$2
fileconca=$3

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

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

for file in $file1 $file2 $fileconca
  do
  ext=`echo $file | sed 's/.*\.//'`

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

set `RPC_info_rff $file1 s `

class1=$1

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

set `RPC_info_rff $file2 s `

class2=$1

if test $class2 != VecTime
   then
   echo " *** rff $file2 is not a VecTime data file" 
   echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 5
fi

# test same experiment/nature
# ---------------------------

RPC_give_rff_param $file1 > toto1_$$.tmp
RPC_give_rff_param $file2 > toto2_$$.tmp

# remove no mandatory info

sed -e '1,/FILE_NAME/d' -e '/FILE_CREATION_DATE/d' -e '/BLOCK_NUMBER/,$d' toto1_$$.tmp > toto3_$$.tmp
sed -e '1,/FILE_NAME/d' -e '/FILE_CREATION_DATE/d' -e '/BLOCK_NUMBER/,$d' toto2_$$.tmp > toto4_$$.tmp

cmp toto3_$$.tmp toto4_$$.tmp

if test $? != 0
   then
      diff toto3_$$.tmp toto4_$$.tmp
      echo "*** files are not compatible"
      rm toto1_$$.tmp toto3_$$.tmp toto2_$$.tmp  toto4_$$.tmp
      echo " $appli36 : *** ERROR ! Command aborted." >&2
      exit 6 
fi

# test on time: file2 data must be after file1 data

lastblo1=`grep BLOCK_LAST_INDEX  toto1_$$.tmp  | cut -c38-61`
firstblo2=`grep BLOCK_FIRST_INDEX toto2_$$.tmp | cut -c38-61`

set ` RPC_decode_datiso $lastblo1` 
num1=$1$2$3$4$5$6
set ` RPC_decode_datiso $firstblo2` 
num2=$1$2$3$4$5$6


if test $num1 -gt $num2
   then
   echo "***last block of file1 is > fistblock of file2"
   echo "*** $lastblo1 > $firstblo2"
   rm toto1_$$.tmp toto3_$$.tmp toto2_$$.tmp  toto4_$$.tmp
   echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 7 
fi

lastbloc=`grep BLOCK_LAST_INDEX toto2_$$.tmp` 
timeto=`grep TIME_SPAN_TO   toto2_$$.tmp`


# header 

nblo1=`grep BLOCK_NUMBER  toto1_$$.tmp | cut -c38-61`
nblo2=`grep BLOCK_NUMBER  toto2_$$.tmp | cut -c38-61`

nblo=`expr $nblo1 + $nblo2` 

set RPC_current_date

yymmdd=$1
hhmmss=$2
credate=$1'T'$2'.OOOZ'

sed '/START INDEXED_DATA/,/END ROPROC_FORMAT_FILE/d' $file1 > toto_$$.tmp

sed -e "s/PAR FILE_NAME                 (STR):.*/PAR FILE_NAME                 (STR): $fileconca/" \
    -e "s/PAR FILE_CREATION_DATE        (STR):.*/PAR FILE_CREATION_DATE        (STR): $credate/"   \
    -e "s/PAR BLOCK_NUMBER              (INT):.*/PAR BLOCK_NUMBER              (INT): $nblo/"   \
    -e "s/PAR TIME_SPAN_TO              (STR):.*/$timeto/"   \
    -e "s/PAR BLOCK_LAST_INDEX          (STR):.*/$lastbloc/" toto_$$.tmp >  $fileconca

#data
sed -n '/START INDEXED_DATA/,/END INDEXED_DATA/p' $file1 | sed '$d' > data1_$$.tmp
sed -n '/START INDEXED_DATA/,/END ROPROC_FORMAT_FILE/p' $file2 | sed '1d' > data2_$$.tmp
 

# concatened file
cat data1_$$.tmp >> $fileconca
cat data2_$$.tmp >> $fileconca

RPC_info_rff $fileconca l


rm toto1_$$.tmp toto3_$$.tmp toto2_$$.tmp  toto4_$$.tmp

# end of command
# --------------

datim2=`date +%F'  '%H':'%M':'%S `
julsec2=`date +%s`
diff=`expr $julsec2 - $julsec1`
diffm=`echo "scale=2; $diff/60." | bc `

echo ""
echo " Starting time $appli : $datim1"
echo " Ending   time $appli : $datim2"
echo " Duration      $appli : $diff sec.  ($diffm mn.)"

echo
echo " $appli36 : NORMAL TERMINATION - time exe= $diff s." >&2

echo " -------------------------------------------------------------------"

