#!/bin/bash

# ----------------------------------------------------------------
# RPC Project :
# Run RPC_compute_sat_trajectory : Compute elliptical trajectory
#                                  of a Earth satellite
#
# P. Robert, LPP, 2011-03-21
# ----------------------------------------------------------------

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

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

if test $# != $Narg || test $hh = 1
   then
    # On n'a pas le bon nombre d'arguments ou on demande un "help"
    echo 
    echo "rpc_$appli : Compute elliptical trajectory of a Earth satellite"
    echo "----------------------"
    echo ""
    echo "Arguments of $appli must be:"
    echo "$appli Apo, Per, PerTime, a1,a2,a3, b1,b2,b3,dt, rff_out,scale"
    echo " "
    echo "     Apo      : Apogee  in km, real*8"
    echo "     Per      : Parigee in km, real*8"
    echo "     PerTime  : Perigee Time, ISO format"
    echo "     a1,a2,a3 : direction of semi-major axis, in GEI system"
    echo '                (from ellipse center to perigee, unit vector)'
    echo "     b1,b2,b3 : direction of semi-minor axis, in GEI system"
    echo '                (a,b vector in the sens of the motion, unit vector)'
    echo "     dt       : time resolution for output positions"
    echo "     rff_out  : output rff_file"
    echo "     scale    : factor to apply to x,y,z"
    echo ""
    echo "example:"
    echo "$appli 124252.735203443d0 26321.1522350593d0  \ "
    echo "                       2001-02-24T15:17:23.000Z              \ "
    echo "                       -0.9493840   0.3011743   0.0892410     \ "
    echo "                        0.0820478  -0.0364760   0.9959607  5.0 \ "
    echo "                        trajectory.rff "
    echo "                            1."
    echo ""
    echo "The input  file correspond to the argument,"
    echo "The output file correspond to the result of the command execution,"
    echo "and will be created in the current directory"
    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
    
echo "$1   # Apogee "        > $appli.in
echo "$2   # Perigee"       >> $appli.in
echo "$3   # Perigee Time"  >> $appli.in
echo "$4 $5 $6 # Dir. of semi-major axis"  >> $appli.in
shift 6
echo "$1 $2 $3 # Dir. of semi-minor axis"  >> $appli.in
echo "$4"       >> $appli.in
echo "$5"       >> $appli.in
echo "$6"       >> $appli.in
echo "Done"
echo " "

# run appli.exe 

. run_appli.bash $appli
err=$?
exit $err
