#!/bin/bash

# --------------------------------------------------------------
# RPC Project :
# Run RPC_spectro_to_polar : transform a RFF spectro into a copolar.resu file
#
# P. Robert, LPP, June 2014
# --------------------------------------------------------------

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 : tranform a RFF SP file into a copolar.resu file"
   echo "$appli   require $Narg argument(s), ex:"
   echo "$appli   SPL2_mfa.rff polar.resu"
   echo "    SPL2_mfa.rff : name of an input RFF SPL2 file in MFA"
   echo "    polar.resu   : file name for polar results"
   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"

echo "Done !"

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

