#! /bin/sh

target_ecu=$1

working_directory=`pwd`

# FWVAL-1196 temporary hack to get jobs running again... see todo below
cd $(bazel info workspace)
bazel build --config=kraken //firmware/common/can/...

# set value for VEHICLE_ARCHITECTURE if not set
if [ -z "$VEHICLE_ARCHITECTURE" ]; then
  export VEHICLE_ARCHITECTURE=vh6b/b1
  echo "Setting VEHICLE_ARCHITECTURE to 'vh6b/b1'"
fi

# TODO: refactor this code to support multiple platforms
#     : and make the code easier to maintain
#     : https://jira.zooxlabs.com/browse/FWVAL-1197
dbcs_directory="$(bazel info workspace)/bazel-bin/firmware/common/can/bus_dbcs/vh6/${VEHICLE_ARCHITECTURE}/"

cd ${working_directory}

# Create go files for each DBC
if [ ! -d can_man_builder_scratch ]; then
    python3.6 -m virtualenv -p python3.6 can_man_builder_scratch
fi

. can_man_builder_scratch/bin/activate
python py/go_builder.py ../../$target_ecu/can/ $dbcs_directory -o go/ -g go/go_template.sh


# Get copy of canassist source.
if [ -d py/canassist ]
then
    rm -rf py/canassist
fi

mkdir py/canassist
# Find the path to the canassist_test.py file
canassist_test_file_path=`find ../.. -name canassist_test.py 2> /dev/null`
# Strip the tail end including the package and test directories and test file name
# and keep only the first part of the path
# E.g.
# echo "../py_packages/canassist/canassist/test/canassist_test.py" | rev | cut -d'/' -f4- | rev
# ../py_packages/canassist
#
canassist_path=`echo $canassist_test_file_path | rev | cut -d'/' -f3- | rev`
# Copy the files from that remaining path into py/
cp -R $canassist_path py/

# Build docker
docker build -t can_man .
