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

# check that zcore is not running, script will fail if it is.
source $HOME/driving/scripts/vehicle/zcore_safe_command.sh

ZOOX_SANITY_CHECK_JSON="/tmp/sanity_checks/sanity-check.json"

function finish {
    # ensure direcotry exists for dumping file
    mkdir $HOME/.log/ --parents
    mkdir $HOME/.log/electron --parents
    mkdir $HOME/.log/electron/sanity_checks --parents
    temp_file="/tmp/puffin_sanity_check"
    # Move the output to a temp file on operator. Use a temp file because
    # file watchers can try to parse the file prior to the pipe closing.
    remote_command cat ${ZOOX_SANITY_CHECK_JSON} > ${temp_file}
    mv ${temp_file} /tmp/sanity_checks/sanity-check.json
}

trap finish EXIT

# run the checker without prompts on failure.
remote_command mkdir /tmp/sanity_checks/ --parents
remote_command  ZOOX_SANITY_CHECK_JSON=${ZOOX_SANITY_CHECK_JSON} sanity_checker_cli --no-prompt

