#!/bin/bash -e

#----------------------------------------------------------
#  Script: launch.sh
#  Author: Michael Benjamin
#  LastEd: May 17th 2019
#----------------------------------------------------------
#  Part 1: Set global var defaults
#----------------------------------------------------------
# 注意书写格式，不能有空格
TIME_WARP=1
JUST_MAKE="no"

LatOrigin="39.0935" 
LongOrigin="117.5361" 


server_host="192.168.1.88"
#----------------------------------------------------------
#  Part 2: Check for and handle command-line arguments
#----------------------------------------------------------
for ARGI; do
    if [ "${ARGI}" = "--help" -o "${ARGI}" = "-h" ]; then
	echo "launch.sh [SWITCHES] [time_warp]    "
	echo "  --help, -h           Show this help message            " 
	echo "  --just_make, -j      Just create targ files, no launch " 
	echo "  --fast, -f           Init positions for fast encounter " 
	exit 0;
    elif [ "${ARGI//[^0-9]/}" = "$ARGI" -a "$TIME_WARP" = 1 ]; then 
        TIME_WARP=$ARGI
    elif [ "${ARGI}" = "--just_make" -o "${ARGI}" = "-j" ] ; then
	JUST_MAKE="yes"
    elif [ "${ARGI}" = "--fast" -o "${ARGI}" = "-f" ] ; then
	START_POS1="170,-80,270"         
	START_POS2="-30,-80,90"        
	LOITER_POS1="x=0,y=-95"
	LOITER_POS2="x=125,y=-65"
    else 
        echo "launch.sh Bad arg:" $ARGI " Exiting with code: 1"
        exit 1
    fi
done

#----------------------------------------------------------
#  Part 3: Create the .moos and .bhv files. 
#----------------------------------------------------------
nsplug alpha_vehicle.moos targ_vehicle.moos -i -f WARP=$TIME_WARP \
       LatOrigin=$LatOrigin   \
       LongOrigin=$LongOrigin  \
       server_host=$server_host

# nsplug meta_vehicle.bhv targ_gilda.bhv -i -f VNAME=$VNAME2    \
#        START_POS=$START_POS2 LOITER_POS=$LOITER_POS2       

if [ ${JUST_MAKE} = "yes" ] ; then
    echo "Files assembled; nothing launched; exiting per request."
    exit 0
fi

#----------------------------------------------------------
#  Part 4: Launch the processes
#----------------------------------------------------------
echo "Launching Shoreside MOOS Community. WARP is" $TIME_WARP
pAntler targ_vehicle.moos >& /dev/null &

uMAC targ_vehicle.moos

kill -- -$$
