#!/bin/bash

# -----------------------------------------------------------
# RPC Project :
# Run RPC_visu_2_vectime_3D : visualization in 3D planes of 2 VT.rff file
# P. Robert, ScientiDev, Nov. 2020
# -----------------------------------------------------------

appli=`basename $0`
appli36=`echo "$appli                                 " | cut -c1-36`
Narg=4
version=`RPC_version`

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

if test $# != $Narg || test $hh = 1
  then
  echo "$appli : visualization 3D of 2 Vectime RFF file"
  echo "$appli   require $Narg argument(s), ex:"
  echo "$appli   toto1_VT.rff toto2_VT.rff datiso_deb datiso_fin"
  echo "$appli   toto1_VT.rff toto2_VT.rff  0 0"
  echo "    toto*_VT.rff : name of a vectime RFF 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`

echo " -------------------------------------------------------------------"
echo " Run of $appli :"
echo " $appli $*"

VT1=$1
VT2=$2


datiso_deb=$5
datiso_fin=$6

for VT in $VT1 $VT2 
    do
    suffix=`echo $VT | cut -f2 -d.`
    if test $suffix != rff
       then echo "$VT file has suffix .$suffix, not .rff"
          echo " $appli36 : *** ERROR ! Command aborted." >&2
          exit 2
    fi

    if test -f $VT
       then echo " content of $VT file will be visualized"
       else echo " $VT file does not exists"
          echo " $appli36 : *** ERROR ! Command aborted." >&2
          exit 3
    fi
done

echo " -------------------------------------------------------------------"
echo " Run of $appli.exe :"

info=`RPC_info_rff $VT1 s`
proj1=`echo $info | cut -d ' ' -f2 | cut -c1-3`
expe1=`echo $info | cut -d ' ' -f4 | cut -c1-3`
yy=`echo $info | cut -d ' ' -f7 | cut -c1-4`
mm=`echo $info | cut -d ' ' -f8 | cut -c1-2`
dd=`echo $info | cut -d ' ' -f9 | cut -c1-2`

info=`RPC_info_rff $VT2 s`
proj2=`echo $info | cut -d ' ' -f2 | cut -c1-3`
expe2=`echo $info | cut -d ' ' -f4 | cut -c1-3`

PS=$proj1"_"$expe1"_"$proj2"_"$expe2"_"$yy$mm$dd"_3D.ps"
echo " PS file=$PS"

if test -f $PS ; then rm $PS ; fi

# remove RPC_ from RPC command

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

# rename possible existing appli.out file

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

# creating appli.in from arguments list

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

echo $VT1         > $appli.in
echo $VT2        >> $appli.in
echo $VT3        >> $appli.in
echo $VT4        >> $appli.in
echo $datiso_deb >> $appli.in
echo $datiso_fin >> $appli.in

# run appli.exe 

. run_appli.bash 
err=$?
if test $err != 0
   then exit $err
fi

# creation eventuelle du pdf,png, visu a l'ecran

run_options_visu.bash $PS 
err=$?
if test $err != 0
   then exit $err
fi

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