#!/bin/bash

# --------------------------------------------------------------
# RPC Project :
# Run RPC_VTL1_to_DWF : transform a RFF VTL1 into a cef DWF file
#
# P. Robert, LPP, 2011-03-21
# --------------------------------------------------------------

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

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

if test $# != $Narg || test $hh = 1
   then
   echo "$appli : tranform a RFF VTL1 into a cef DWF file"
   echo "$appli   require $Narg argument(s), ex:"
   echo "$appli   toto_VT.rff"
   echo "    toto_VT.rff : name of an input RFF VTL1 file"
   echo ""
   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`

# remove RPC_ from RPC command

appli=`echo $appli | cut -c5-`

# creating appli.in from arguments list

if test -f $appli.in ; then \mv $appli.in $appli.in.old ; fi

for i in $*
    do
    echo $i >> $appli.in
done

# rename possible existing appli.out file

if test -f $appli.out ; then \mv $appli.out $appli.out.old ; fi

# run appli.exe 
# -------------

echo " -------------------------------------------------------------------"
path=`dirname $0`/../bin/$appli.exe
echo " Run of $appli.exe :"
echo "     - arguments are taken from $appli.in text file"
echo "     - execution report will be available in $appli.out text file"
echo " Please wait..."
echo ""

$path < $appli.in > $appli.out
echo " ==> $appli.out text file available"

# complete cef header by VTL1 data
# --------------------------------

cefname=`tail -1 $appli.out`
echo ""
echo " cef file : $cefname"
echo " header created !"

valid=`echo $cefname | cut -c3-10`
if test "$valid" != "_CP_STA_"
   then 
       echo "file $cefname "
       echo "is not a valid name"
       echo ""
       echo " $appli36 : *** ERROR ! Command aborted." >&2
       exit 2
fi

# line number to start the copie of the data

head -300 $1 | sed -n '1,/START INDEXED_DATA/p' > toto2_$$.tmp
nlhead=`wc -l toto2_$$.tmp | cut -f1 -d" "`
\rm toto2_$$.tmp

echo ""
echo " nb lines of the RFF header, included START INDEXED_DATA : $nlhead"
nlut=`expr $nlhead + 1 `
echo "           line number to start the copie of the data    : $nlut"

# case of empty files

firstli=`sed -n "$nlut p" $1 | cut -c1-16 `
echo "           first data=$firstli"
if test "$firstli" = "END INDEXED_DATA"
   then
       data=n
       echo "           no data to copy, empty file"
   else
       data=y
fi
echo "           data=$data"

nlfile1=`wc -l $1| cut -f1 -d" "`
# removing RFF terminator file
nlfile1=`expr $nlfile1 - 3 `

echo "           line number to end   the copie of the data    : $nlfile1"
nlcop=`expr $nlfile1 - $nlut `
nlcop=`expr $nlcop + 1 `

echo "                                nb lines to be copied    : $nlcop"


echo ""
echo " Copy of $nlcop  data blocks, Please wait..."
echo 'DATA_UNTIL = "END_OF_FILE"'     >> $cefname
if test $data = y
   then 
       sed -n  "$nlut,$nlfile1 p" $1  >> $cefname
       echo " $nlcop lines copied"
fi
echo "END_OF_FILE"                    >> $cefname
echo " Done..."

shortname=`echo $cefname | cut -c14-`
shortname=`basename $shortname .cef`

echo ""
   
# 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 " -------------------------------------------------------------------"

