# @brief     Script that upload modified PCAN GUI project directory into S3.
# @copyright Highly Confidential and Proprietary Zoox Information. Copyright (c) Zoox.

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

# check number of command line inputs
if [ "$#" -ne 1 ]; then
  echo "Usage: $0 <GUI DIRECTORY>" >&2
  exit 1
fi

vh6a_ui_project_dir="$1"
vh6a_ui_repository_bzl="$ZOOX_WORKSPACE_ROOT/build/peak_system/vh6a_ui/repositories.bzl"
vh6a_ui_repo_dbc="$ZOOX_FIRMWARE_ROOT/common/can/dbc/vh6"
vh6a_ui_tmp="$vh6a_ui_project_dir/vh6a_ui"

# check if input $1 is directory
if [ ! -d "$vh6a_ui_project_dir" ]
then
  echo "Please specify ia directory of PCAN GUI project." >&2
  exit 1
fi

# check there's no subdirectory
subdircount=(`find $vh6a_ui_project_dir/* -maxdepth 1 -type d | wc -l`)
if [ $subdircount -ne 0 ]; then
  echo "Expect 0 subdirectory in PCAN GUI project $vh6a_ui_project_dir." >&2
  exit 1
fi

# check predefined files' names
for file in VH6A_PCAN_MDI1_PANEL.ipf VH6A_PCAN_MDI2_PANEL.ipf VH6A_PCAN_PROJECT.dsk VH6A_PCAN_PROJECT.peproj scene_switching.sym; do
  if [ ! -f "$vh6a_ui_project_dir/$file" ]; then
    echo "Missing file: $file" >&2
    echo "A project directory must have:" >&2
    echo "<gui project directory>" >&2
    echo "  |-- VH6A_PCAN_MDI1_PANEL.ipf" >&2
    echo "  |-- VH6A_PCAN_MDI2_PANEL.ipf" >&2
    echo "  |-- VH6A_PCAN_PROJECT.dsk" >&2
    echo "  |-- VH6A_PCAN_PROJECT.peproj" >&2
    echo "  |-- scene_switching.sym" >&2
    exit 1
  fi
done

# @todo @jira{FW-8834} Use the new DBC locations and remove unneeded DBCs
# for dbc in BDY_CAN.dbc DR_CAN.dbc UI_CAN.dbc EMU_CAN.dbc; do
#   if [ ! -f "$vh6a_ui_project_dir/$dbc" ]; then
#     echo "Missing dbc file: $dbc in $vh6a_ui_project_dir" >&2
#     exit 1
#   fi
#   if ! diff "$vh6a_ui_project_dir/$dbc" "$vh6a_ui_repo_dbc/$dbc" >/dev/null; then
#     echo "$dbc in Modified PCAN GUI project: $vh6a_ui_project_dir"
#     echo "differs"
#     echo "$dbc in current workspace($vh6a_ui_repo_dbc)."
#     echo "Publish aborted."
#     exit 1
#   fi
# done

# create a temporary directory & tarball it to upload
mkdir $vh6a_ui_tmp
cp $vh6a_ui_project_dir/VH6A_PCAN_MDI1_PANEL.ipf $vh6a_ui_tmp
cp $vh6a_ui_project_dir/VH6A_PCAN_MDI2_PANEL.ipf $vh6a_ui_tmp
cp $vh6a_ui_project_dir/scene_switching.sym $vh6a_ui_tmp
cp $vh6a_ui_project_dir/VH6A_PCAN_PROJECT.dsk* $vh6a_ui_tmp
cp $vh6a_ui_project_dir/VH6A_PCAN_PROJECT.peproj* $vh6a_ui_tmp
cp $vh6a_ui_project_dir/*.dbc $vh6a_ui_tmp

# must tar with -z flag
tar -czf "$vh6a_ui_project_dir/vh6a_ui.tar.gz" -C $vh6a_ui_project_dir vh6a_ui

# calculate sha256
sha256="$(sha256sum $vh6a_ui_project_dir/vh6a_ui.tar.gz | awk '{print $1}')"
echo $sha256

# upload with script
if $ZOOX_WORKSPACE_ROOT/scripts/build/upload-to-third-party.sh $vh6a_ui_project_dir/vh6a_ui.tar.gz; then
  # remove temporaries.
  rm -r $vh6a_ui_tmp
  rm $vh6a_ui_project_dir/vh6a_ui.tar.gz
else
  echo "Access Z3 failed, may need to run driving/scripts/build/init_z3_profile.py" >&2
  # remove temporaries.
  rm -r $vh6a_ui_tmp
  rm $vh6a_ui_project_dir/vh6a_ui.tar.gz
  exit 1
fi

# update driving/build/peak_system/vh6a_ui/repositories.bzl file
sed -i "s|^ *\(sha256 *=\).*|    \1 \"${sha256}\",|g" $vh6a_ui_repository_bzl

# do a build of genrule to update local bazel cache files
( cd $ZOOX_WORKSPACE_ROOT && bazel build //firmware:vh6a_ui_files )
