#!/bin/bash

# -----------------------------------------------------------
# RPC Project :
# Run RPC_cef_to_rff_cwf_CLUSTA : convert a STAFF CWF.cef file into a VTL2.rff file
# P. Robert, Nov 2020
# -----------------------------------------------------------

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 : convert a STAFF CWF.cef file into a VTL2.rff"
   echo "$appli   require $Narg argument(s), ex:"
   echo "$appli   toto.cef "
   echo "    toto.cef : name of an input CWF CEF file"
   echo ""
   if test $hh = 1 ; then exit 0 ; fi
   echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 1
fi

# check we have a CWF.cef

chk=`echo $1 | grep CWF`.chk
if test $chk != $1.chk
   then echo " $1 is not a CWF cef file"
        echo "  $appli36     : BAD TERMINATION " >&2
        exit 1
fi

# check we have a cef file

chk=`echo $1 | sed 's/.*\.//g'`
if test $chk != cef
   then echo " $1 is not a cef file"
        echo "  $appli36     : BAD TERMINATION " >&2
        exit 2
fi

# 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

# menage eventuel du tmp utilise par CWF_to_rff.exe

if test -f data_NBR.tmp ; then rm data_NBR.tmp ; fi
if test -f data_HBR.tmp ; then rm data_HBR.tmp ; fi


# run appli.exe 

echo " --------------------------------------------------------------------------"
echo " RPC_cef_to_rff_cwf_CLUSTA : convert a CWF.cef file into a vectime.rff file"
echo " --------------------------------------------------------------------------"
echo " 1) create header of RFF files (NBR & HBR)"

. run_appli.bash $appli

rff_name_NBR=`grep 'rff_file_name_NBR=' cef_to_rff_cwf_CLUSTA.out | cut -d = -f 2`
rff_name_HBR=`grep 'rff_file_name_HBR=' cef_to_rff_cwf_CLUSTA.out | cut -d = -f 2`

# recup du champ de donnees pour les deux fichiers

echo " 2) add NBR data to the RFF file $rff_name_NBR"

# problem des ^M sous windows/Msys : cat | tr

cat  data_NBR.tmp  | tr -d '\r' >> $rff_name_NBR
echo 'END INDEXED_DATA'         >> $rff_name_NBR
echo 'END DATA'                 >> $rff_name_NBR
echo 'END ROPROC_FORMAT_FILE'   >> $rff_name_NBR

echo
echo " 3) add HBR data to the RFF file $rff_name_HBR"

cat  data_HBR.tmp  | tr -d '\r' >> $rff_name_HBR
echo 'END INDEXED_DATA'         >> $rff_name_HBR
echo 'END DATA'                 >> $rff_name_HBR
echo 'END ROPROC_FORMAT_FILE'   >> $rff_name_HBR
 
test_vide=`grep "PAR BLOCK_NUMBER              (INT): 0" $rff_name_NBR`
if test $? = 0
   then echo "    !no NBR data in this file / no NBR file produced"
        rm $rff_name_NBR
fi
 
test_vide=`grep "PAR BLOCK_NUMBER              (INT): 0" $rff_name_HBR`
if test $? = 0
   then echo "    !no HBR data in this file / no HBR file produced"
        rm $rff_name_HBR
fi

rm data_NBR.tmp
rm data_HBR.tmp

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