# the file should be run from the root: i.e.:
# desktop:~/driving$ experimental/viktor/static_analysis/klocwork/run.sh

kwservice start

execute() {
  # defining an absolute base location; change it for your machine
  BAZEL_BASE=/home/viktor/bazel_temp

  # location of the bazel exec to trace log converter
  EKW_SCRIPT_LOCATION=~/driving/experimental/viktor/static_analysis/klocwork/ekw_bazel_exec_to_trace/ekw_bazel_exec_to_trace.py

  EXEC_LOCATION='test/'$(date --rfc-3339=seconds | sed 's/[-: +]/-/g')
  mkdir $EXEC_LOCATION
  rm -rf ~/bazel_temp
  mkdir ~/bazel_temp
  TARGET=$1
  PROJECT=$2
  echo 'target: '$TARGET > $EXEC_LOCATION/desc.txt
  echo 'project: '$PROJECT >> $EXEC_LOCATION/desc.txt
  bazel --output_base=$BAZEL_BASE clean
  bazel --output_base=$BAZEL_BASE build $TARGET --experimental_execution_log_file=$EXEC_LOCATION/exec.log &> $EXEC_LOCATION/1_bazel_build.log
  python $EKW_SCRIPT_LOCATION $EXEC_LOCATION/exec.log -o $EXEC_LOCATION/trace.log &> $EXEC_LOCATION/2_ekw_bazel_exec_to_trace.log
  kwinject -t $EXEC_LOCATION/trace.log --debug &> $EXEC_LOCATION/3_kwinject.log
  kwbuildproject --url http://localhost:8080/$PROJECT -o $EXEC_LOCATION/kwtables kwinject.out --force &> $EXEC_LOCATION/4_kwbuildproject.log
  kwadmin load $PROJECT $EXEC_LOCATION/kwtables &> $EXEC_LOCATION/5_kwadmin.log
  mv kwinject.out $EXEC_LOCATION/
}

# execute <target> <project name in KW>
execute //log_tests/ci_test:generate_offline_logs_test generate_offline_logs_test
execute //vehicle:l3_deploy_package l3_deploy_package
