# @brief     Script that copies PCAN GUI project files in bazel cache to a specific directory.
# @copyright Highly Confidential and Proprietary Zoox Information. Copyright (c) Zoox.
#
# Copy files in bazel-driving/external/vh6a_ui that previously uploaded to S3 by calling "driving/scripts/build/upload-to-third-party.sh"
# Need to run bazel build //firmware/... first in case of empty bazel-driving.external/vh6a_ui directory.
# PCAN project config files(exclude dbc files) can be accessed by below URL as well
# URL: https://z3.zooxlabs.com/zoox-third-party/<sha256>/vh6a_ui.tar.gz

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

vh6a_ui_path_bazel="$ZOOX_WORKSPACE_ROOT/bazel-driving/external/vh6a_ui"
vh6a_ui_path_output="$ZOOX_FIRMWARE_ROOT/tools/pcan_gui/vh6a_ui"
vh6a_ui_path_dbc="$ZOOX_FIRMWARE_ROOT/common/can/dbc/vh6"
vh6a_ui_s3_sha256_file="$ZOOX_WORKSPACE_ROOT/build/peak_system/vh6a_ui/repositories.bzl"

# do a build to auto generate local copies under bazel-driving
( cd $ZOOX_WORKSPACE_ROOT && bazel build //firmware:vh6a_ui_files )

# Validate paths
if [ ! -d "$vh6a_ui_path_bazel" ]
then
  echo "source directory: $vh6a_ui_path_bazel does not exist."
  exit 1
fi
if [ -d "$vh6a_ui_path_output" ]
then
  echo "overwrite to destination directory: $vh6a_ui_path_output"
  rm -r $vh6a_ui_path_output
fi
mkdir -p $vh6a_ui_path_output

# copy PCAN EXPLORER project files which are symlinks from bazel-driving
cp -Lv $vh6a_ui_path_bazel/*.peproj* $vh6a_ui_path_output
cp -Lv $vh6a_ui_path_bazel/*.dsk $vh6a_ui_path_output
cp -Lv $vh6a_ui_path_bazel/*.ipf $vh6a_ui_path_output
cp -Lv $vh6a_ui_path_bazel/*.sym $vh6a_ui_path_output
cp -Lv $vh6a_ui_path_bazel/*.dbc $vh6a_ui_path_output

# attach s3 sha256 for convenience
touch $vh6a_ui_path_output/s3_sha256.txt | sed -n 's/^ *sha256 *= *"\([^"]*\)".*/\1/p' $vh6a_ui_s3_sha256_file > $vh6a_ui_path_output/s3_sha256.txt
echo ""
echo "PCAN GUI project fetched successfully."
echo "Checkout directory: $vh6a_ui_path_output"
echo "Corresponding sha256 on S3"
cat $vh6a_ui_path_output/s3_sha256.txt
