#!/bin/bash

# ----------------------------------------------------------
# RPC Project :
# RPC_rename_rff : rename a RFF file with update of file name inside
# P. Robert, LPP, 2011-03-23
# ----------------------------------------------------------

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

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

if test $# != $Narg || test $hh = 1
   then
   echo "$appli : rename a RFF file with update of file name inside"
   echo "$appli   require $Narg argument(s), ex:"
   echo "$appli   toto1.rff toto2.rff"
   echo "    toto1.rff : RFF file"
   echo "    toto2.rff : RFF new name"
   echo ""
   if test $hh = 1 ; then exit 0 ; fi
   echo "$appli36 : *** ERROR ! Command aborted." >&2
   exit 1
fi

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

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

RPC_copy_rff $1 $2

# test if copy is done without error
if [ $? -ne 0 ] 
   then
       echo "*** RPC ERROR : previous RPC_copy_rff not done correctly"
       echo "$appli36 : *** ERROR ! Command aborted." >&2
       exit 2
fi


if test -f $2
   then 
       echo "file $2 is created, removing $1..."
       \rm -f $1
       echo "done."
   else
       echo "*** RPC ERROR : file $2 not created"
       echo "$appli36 : *** ERROR ! Command aborted." >&2
       exit 3
fi

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

echo "RPC_rename_rff done."

datim2=`date +%F'  '%H':'%M':'%S `
julsec2=`date +%s`
diff=`expr $julsec2 - $julsec1`

echo ""
echo " Starting time $appli : $datim1"
echo " Ending   time $appli : $datim2"
echo " Duration      $appli : $diff sec."

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

