#!/bin/bash

# Request a utility mission for a robot id.
#
# Usage:
#   $ source request_utility_mission.sh <robot_id> <mission_type>
#
# $1 - Robot ID to request a utility mission for.
# $2 - Which mission type to request
#      Utility mission types available:
#          - RANDOM_DESTINATION_SF_EXTENDED
#          - RANDOM_ROUTE_SF_EXTENDED
#          - RANDOM_DESTINATION_SLAC
#          - RANDOM_ROUTE_SLAC
#
# Examples
#
#   $ source request_utility_mission.sh 07cbecdf-cc41-4f47-b2c6-a56efe1059be RANDOM_DESTINATION_SF_EXTENDED

if [ "${ROBOT_GATEWAY_URL:-unset}" = unset ]
then
    echo "ROBOT_GATEWAY_URL is not set."
    echo "Please run set_gateway_urls.sh to configure Maestro URLs."
    return 1
fi

export MAESTRO_ROBOT_ID=$1
export MAESTRO_MISSION_TYPE=$2

export MAESTRO_MISSION_ID=$(curl -X POST \
  --header 'Content-Type: application/json' \
  --header 'Accept: application/json' \
  -d "{
       \"missionType\": \"${MAESTRO_MISSION_TYPE}\"
     }" \
  "${ROBOT_GATEWAY_URL}/v1/robots/${MAESTRO_ROBOT_ID}/mission-request" \
  | jq '.id'
  )

if [ $MAESTRO_MISSION_ID = null ]
then
    echo "Fail to request ${MAESTRO_MISSION_TYPE} mission for robot ${MAESTRO_ROBOT_ID}."
    return 1
else
    echo "Successfully requested ${MAESTRO_MISSION_TYPE} mission for robot ${MAESTRO_ROBOT_ID}."
fi
