#!/usr/bin/env bash
set -euo pipefail

RED='\033[0;31m'
NC='\033[0m' # No Color

if [ -z "$1" ] ; then
  echo "Usage: $0 ZIMBOT_IP [coredash args...]"
  exit 1
fi

ZIMBOT_IP="$1"
shift

# Set the DDS initial peers to the default values + the main-ai so we don't depend on multicast
export NDDS_DISCOVERY_PEERS="5@$ZIMBOT_IP"
echo "Using NDDS_DISCOVERY_PEERS=$NDDS_DISCOVERY_PEERS"

# Build the command line as an array:
LAUNCH_CD_CMD=(
    "../coredash_build/launch_coredash"
    "-ZFR"
    "-LOCAL_MISSION_SIDE_HUSTLE"
    "-WINDOWED"
    "-MAESTRO"
    "-DAY_NIGHT_AUTO"
)

# Conditionally add the imagery option, depending on whether there's an
# imagery or not (e.g. get_imagery returns the empty string when
# default_imagery_name is missing, see CLM-10230)
MAP2D=$(mapping/imagery/get_imagery --get map2d)
if [ ! -z ${MAP2D} ]; then
    LAUNCH_CD_CMD+=("-IMAGERY_DIR" "${MAP2D}")
fi

# Add all arguments to this script to the command line:
LAUNCH_CD_CMD+=("$@")

# Run the command
"${LAUNCH_CD_CMD[@]}"
