#!/usr/bin/env bash

set -euo pipefail

function main() {
  # Determine the working directories
  collect_logs=$(readlink -f "$PWD/vehicle/tools/logging/collect_logs.py")
  workspace_root=$(readlink -f "$(dirname $collect_logs)/../../../")
  # This normally gets set when depending on the hardware config library but
  # this is an sh_test and the collect_logs py_binary does not have a
  # dependency on the hardware config library because it is run in production
  # and should use the system copy rather than the bazel copy.
  calibration_root=$(readlink -f "../calibration")

  # Set up a temporary PYTHONPATH, which matches a non-deployed vehicle plus
  # the current directory so bazel deps work.
  export HOME=$(dirname $workspace_root)
  export PYTHONPATH=$workspace_root:$calibration_root:.
  export PYTHONDONTWRITEBYTECODE=1

  # Remove any '__init__.pyc' files that were cached from previous executions
  find -name '__init__.pyc' -exec rm {} \;

  # Run the script! Test will fail if this exits non-zero
  python "$collect_logs" --output_dir="${TEST_TMPDIR:-/tmp}" --dryrun
}

main
