#! /bin/bash
source "$HOME/zoox/driving/scripts/shell/zooxrc.sh"
source "$HOME/zoox/driving/firmware/.firmwarerc"

export ZOOX_VEHICLE_CONFIG="/etc/zoox/calibration/param/vh6b_002.json"

set -xe
origin="$HOME/zoox/driving/"
echo $origin
cd $origin

# Entry point for EMUv4 Feature Tests

# Clean out anything that might be lurking around...
git clean -fdx

# Run the bazel precheck steps (shutdown, remove cache)
firmware/validation/common/ci/bazel_precheck.sh

# Build Docker containers
cd firmware/validation/docker/net_man
./build.sh emuv4_feature
cd $origin

# Set up virtualenv
if [ -d emuv4_smoke_env ]; then
    rm -rf emuv4_smoke_env
fi

python3.6 -m virtualenv -p python3.6 emuv4_smoke_env
. emuv4_smoke_env/bin/activate
python -m pip install -r firmware/validation/emuv4_feature/requirements.txt

# Generate DBCs first. If this fails there is no point in continuing to test
# Note that this does not upload DBCs to Artifactory. To enable upload use the
# upload_dbc.sh script instead
firmware/validation/common/ci/generate_dbc.sh

# Update dentist protos before the tests have started. This is necessary due
# to dentist being imported before pytest processes it's plug-ins
dentist_proto_updater

# Run Smoke Test
cd $origin
python -m pytest firmware/validation/emuv4_feature/test_deploy.py \
                 --verbose

cd $origin
python -m pytest firmware/validation/emuv4_feature/test_imu_gps_data_path.py \
                 -s \
                 --json-report \
                 --json-report-file="gps_imu_data_result.json" \
                 --verbose

# todo: FWVAL-2285 add the post processing and push to database
# Cleanup bazel job
cd $(bazel info workspace)
firmware/validation/common/ci/bazel_postcheck.sh
