#!/usr/bin/env bash
# "Enable strict mode"
set -euo pipefail
IFS=$'\n\t'

CLEAN_MODE=none
DEPLOY_L3=false

show_usage() {
    cat <<EOF
Usage: $0 [OPTIONS...] TARGET_DIR

This script is used to deploy a Canalyzer configuration package and vehicle DBC
files for the Zoox vehicle platforms.

OPTIONS:

    -l, --l3            Deploy CANalyzer configs for L3 analysis as well.
    -c, --clean         Clean the TARGET_DIR prior to deployment; preserves the
                        'logs' directory.
    -f, --force-clean   Force clean the TARGET_DIR prior to deployment. If the
                        target directory is not empty, all contents will
                        be deleted.
    -h, --help          Show this help information.

EXAMPLES:

Deploy to a specified target directory; if the directory already exists,
remove all contents prior to deploying new package.
> $0 -f ~/desired_path_to_config_files/

EOF
}

parse_args() {
  # Process command line arguments.
  while [ "$1" != "" ]; do
    case $1 in
            -c | --clean)
    CLEAN_MODE=clean
    ;;
            -f | --force-clean)
    CLEAN_MODE=realclean
    ;;
            -h | --help)
    show_usage
    exit
    ;;
            -l | --l3)
    DEPLOY_L3=true
    ;;
            --)
    shift
    break
    ;;
            *)
    break
    ;;
    esac
    shift
    done

    TARGET_DIR=$*
}

parse_args "$@"
if [ -z "${TARGET_DIR}" ]; then
  show_usage
  exit 1
fi

# Resolve the absolute target path.
FILEPATH="$(readlink -f ${TARGET_DIR})"

# Prepare the target directory.
if [ -d "${FILEPATH}" ]; then
  if [[ ${CLEAN_MODE} == "none" ]]; then
    echo "The specified path '${FILEPATH}' already exists; specifiy '-c' or '-f' to overwrite contents or select a new target path."
    exit 1
  elif [[ ${CLEAN_MODE} == "clean" ]]; then
    echo "Cleaning ${FILEPATH}, the logs dir will be preserved"
    find "${FILEPATH}" -mindepth 2 -name logs -prune -o -exec rm -rf {} +
  elif [[ ${CLEAN_MODE} == "realclean" ]]; then
    echo "Cleaning all files and dir in ${FILEPATH}"
    rm -rf "${FILEPATH}" && mkdir -p "${FILEPATH}"
  else
    echo "Unexpected clean mode: '$CLEAN_MODE'"
    exit 1
  fi
else
  mkdir -p "${FILEPATH}"
  if [ $? -ne 0 ]; then
    echo "Could not create the specified target path: '${FILEPATH}'"
    exit 1
  fi
fi

echo "Building all VH6 bus DBCs for your branch..."
pushd "$ZOOX_WORKSPACE_ROOT"
bazel build //firmware/common/can/...
popd

VH6_DBC="$ZOOX_WORKSPACE_ROOT/bazel-bin/firmware/common/can/bus_dbcs/vh6"
CONFIG_SRC="$ZOOX_FIRMWARE_ROOT/tools/vector/canalyzer"
KNOWN_PLATFORMS=("vh6a" "vh6b")

for vh in ${KNOWN_PLATFORMS[@]}; do
  DEST_PATH="$FILEPATH/$vh"
  echo "Deploying configuration package for '$vh' to '$DEST_PATH'"
  VARIANT_DBC="$VH6_DBC/$vh"
  VARIANT_CONFIG_SRC="$CONFIG_SRC/$vh"

  mkdir -p "$DEST_PATH/dbc" # For DBC files
  mkdir -p "$DEST_PATH/logs" # For logging data
  cp $VARIANT_DBC/* $DEST_PATH/dbc/
  cp $VARIANT_CONFIG_SRC/* $DEST_PATH/
done

if [[ ${DEPLOY_L3} ]]; then
  DEST_PATH="$FILEPATH/l3"
  echo "Deploying L3 config to $DEST_PATH"
  DBC_BASE_PATH="$ZOOX_WORKSPACE_ROOT/vehicle/common/dbc"
  DBC_LIST=("AI_CAN.dbc" "Chassis_CAN.dbc" "DAS_CAN.dbc" "LUX_VOX_CAN_V3.dbc")

  for dbc_filename in ${DBC_LIST[@]}; do
    DBC_FULL_PATH="$DBC_BASE_PATH/$dbc_filename"
    mkdir -p "$DEST_PATH/dbc" # For DBC files
    mkdir -p "$DEST_PATH/logs" # For logging data
    cp $DBC_FULL_PATH $DEST_PATH/dbc/
  done
  cp $CONFIG_SRC/l3/l3_highlander.cfg $DEST_PATH/
fi


# Exit successfully
exit 0
