#!/bin/bash

# -----------------------------------------------------------
# RPC Project :
# Run RPC_cef_to_rff_dwf_CLUSTA : convert a STAFF DWF.cef file into a VTL1.rff file
# P. Robert, LPP, 2012-05-24
# -----------------------------------------------------------

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

# check we have a DWF.cef

chk=`echo $1 | grep DWF`.chk
if test $chk != $1.chk
   then echo " $1 is not a DWF cef file"
        echo " $appli36 : *** ERROR ! Command aborted." >&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 : *** ERROR ! Command aborted." >&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 DWF_to_rff.exe

if test -f data.tmp ; then rm data.tmp ; fi

# run appli.exe 


echo " -------------------------------------------------------------------"
echo " RPC_CLUSTA_DWF_cef_to_rff : convert a DWF.cef file into a vectime.rff file"
echo " -------------------------------------------------------------------"
echo " 1) create header of RFF file and put data in a temporary file"

. run_appli.bash $appli

rff_name=`grep 'rff_file_name=' cef_to_rff_dwf_CLUSTA.out | cut -d = -f 2`

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

echo
echo " RPC_$appli:"
echo " 2) add data to the RFF file $rff_name"
cat data.tmp  | tr -d '\r'    >> $rff_name
echo 'END INDEXED_DATA'       >> $rff_name
echo 'END DATA'               >> $rff_name
echo 'END ROPROC_FORMAT_FILE' >> $rff_name

rm data.tmp

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