#!/bin/bash

##
## Dispatch, RSM and related component launcher for doing local development
## and testing running simple_sim scenarios directly, primarily for testing
## RideStateMachine and Dispatch changes while watching output from all
## nodes in tabbed terminals

## OLD "dev" fleedash for seeing/canceling missions:
## https://dispatch-admin-dev.zooxlabs.com/

##
## COREDASH visualization by running: sim/argus_sim/launch_coredash.sh
##

## Full instructions for running this stack can be found at:
## https://confluence.zooxlabs.com/display/ridex/Simulation

##
## Bash "strict" mode not set. Script NOT intended to be chained or used
## outside of development testing
set -e

## -- Scripts

TMP_SCRIPT_DIR=$ZOOX_WORKSPACE_ROOT/bazel-out/run-scripts
mkdir -p $TMP_SCRIPT_DIR

MOCK_SERVER_SCRIPT=mock-server.sh
WATCHDOG_SCRIPT=watchdog.sh
DISPATCH_SCRIPT=dispatch.sh
VITALS_SCRIPT=vitals.sh

## -- Arguments

ROBOT_CAL=$1
SCENARIO_NAME=$2
ENVIRONMENT=$3

ROBOT_CAL_FILE=/etc/zoox/calibration/param/${ROBOT_CAL}.json

## -- Validation

if [[ -z $1 || -z $2 || -z $3 || $1 == "-h" || $1 == "--help"  ]] ; then
  echo 'USAGE: dmr-launch.sh <robot_calibration> <scenario> <environment>'
  echo '    robot_calibration = kitt_01, vh6a_08, etc'
  echo '    scenario = rsm_slac, rsm_sf, rsm_altamont, rsm_chess, etc. (as defined in //sim/sim_tests/BUILD)'
  echo '    environment = maestro-dev, maestro-staging, maestro-prod, maestro-<ephemeral>'
  echo '.'
  echo "\$robot_id = $robot_id"
  echo "\$rider_email = $rider_email"
  echo "\$ZOOX_WORKSPACE_ROOT = $ZOOX_WORKSPACE_ROOT"
  exit 1
fi

# Avoid manual robot ID management and generating new one for each run
if [[ -z "$robot_id" && -f "$TMP_SCRIPT_DIR/.robot_id" ]] ; then
  export robot_id=$(<$TMP_SCRIPT_DIR/.robot_id)
  echo "Read robot_id from file in script dir: $robot_id"
fi

if [ -z "$robot_id" ] ; then
  echo "Generating new 'robot_id' var to pair with Maestro. See Confluence docs."
  export robot_id=`uuidgen`
  echo "New robot_id: $robot_id"
  echo -n $robot_id > "$TMP_SCRIPT_DIR/.robot_id"
fi

if [ -z "$rider_email" ]; then
  echo "You must set 'rider_email' env var so that login and pairing can be done."
  exit 1
fi

if [[ -z $ZOOX_WORKSPACE_ROOT ]] ; then
 echo "ZOOX_WORKSPACE_ROOT is not set"
 exit 1
fi

if [[ ! -f "$ROBOT_CAL_FILE" ]] ; then
  echo "The robot you provided \"${ROBOT_CAL}\" does not have a cal file in /etc/zoox/calibration, git pull?"
  exit 1
fi

## -- Pairing

source $ZOOX_WORKSPACE_ROOT/dispatch/scripts/env_setup/set_gateway_urls.sh $ENVIRONMENT
echo -e "\nPairing $rider_email to $robot_id"
source $ZOOX_WORKSPACE_ROOT/dispatch/scripts/env_setup/pair_user_robot.sh $rider_email $robot_id

## -- Scenario

SCENARIO_TARGET=""
if [[ $ROBOT_CAL == kitt_* ]]
then
  SCENARIO_TARGET="${SCENARIO_NAME}_kitt"
else
  SCENARIO_TARGET="${SCENARIO_NAME}_vh6"
fi


echo -e "\n\nWill launch scenario with:"
echo "  environment=$ENVIRONMENT"
echo "  robot_calibration=$ROBOT_CAL"
echo "  robot_id=$robot_id"
echo "  rider_email=$rider_email"
echo "  scenario:=$SCENARIO_TARGET"
echo -e "\n"

read -n 1 -s -r -p "PRESS ANY KEY TO START SIM ROBOT..."
echo -e "\n"

# if in ECS, be sure to set hail mode = FIXED for location-helper.py, P2P for app hailing, VH6_DEMO_CHESS for rsm_chess_demo
# you can do this with //dispatch/scripts/update_hail_mode.py
SCENARIO=//sim/sim_tests/ride_state_machine:$SCENARIO_TARGET

## -- Launch

cp $ROBOT_CAL_FILE /etc/zoox/current_config/vehicle.json

bazel run --script_path=$TMP_SCRIPT_DIR/$MOCK_SERVER_SCRIPT //dispatch/tools/mock_server:run -- $robot_id --robot-gateway-url $ROBOT_GATEWAY_URL --port=5010
bazel run --script_path=$TMP_SCRIPT_DIR/$WATCHDOG_SCRIPT //vehicle/dispatch/mission_watchdog:mission_watchdog -- $ZOOX_WORKSPACE_ROOT/vehicle/dispatch/mission_watchdog/{component,transport}_params.pbtxt
bazel run --script_path=$TMP_SCRIPT_DIR/$DISPATCH_SCRIPT //vehicle/dispatch_v3:dispatch_main -- $ZOOX_WORKSPACE_ROOT/vehicle/dispatch_v3/config/dispatch_config_test.pbtxt use_sim_time:=true
bazel run --script_path=$TMP_SCRIPT_DIR/$VITALS_SCRIPT //vehicle/dispatch_v3:vitals_main -- $ZOOX_WORKSPACE_ROOT/vehicle/dispatch_v3/vitals_{component,transport}_params.pbtxt use_sim_time:=true --params-yml $ZOOX_WORKSPACE_ROOT/vehicle/dispatch_v3/config/vitals_params_test.yaml

gnome-terminal --window --geometry 100x40+50+400 --title="MOCK server" --command="bash -c '$TMP_SCRIPT_DIR/$MOCK_SERVER_SCRIPT ; $SHELL'" \
--tab --title="SimpleSim" --command="bash -c '$ZOOX_WORKSPACE_ROOT/sim/launch.sh ros visualize $SCENARIO ; $SHELL'" \
--tab --title="Watchdog" --command="bash -c '$TMP_SCRIPT_DIR/$WATCHDOG_SCRIPT ; $SHELL'" \
--tab --title="Dispatch" --command="bash -c 'ZOOX_ROBOT_NAME=$ROBOT_CAL GLOG_logtostderr=1 GLOG_vmodule=dispatch*=2 $TMP_SCRIPT_DIR/$DISPATCH_SCRIPT ; $SHELL'" \
--tab --title="Vitals" --command="bash -c 'ZOOX_ROBOT_NAME=$ROBOT_CAL GLOG_logtostderr=1 $TMP_SCRIPT_DIR/$VITALS_SCRIPT ; $SHELL'"
