#!/bin/bash
# smoke_test_sentinel_file_checker.sh:
# @copyright Highly Confidential and Proprietary Zoox Information. Copyright (c) Zoox
#
# Author: agurusami
# Date Created: July 30, 2020
#
# This file inspects the Bamboo working directory for sentinel files on the Agent this runs on
# for specific Smoke Test jobs associated with the Firmware Test Global Bamboo plan.
#
# If a sentinel file is found, we know that the test script passed without issue because in Bamboo
# subsequent tasks in a given stage are not executed on a failure of a previous task, and the
# sentinel creation script runs right after the smoke test script
#
# This particular script runs as a "Final Task" which always runs regardless of any previous
# Task failures.
#
# If a sentinel file is not found, we know that the smoke test failed. Based on whether the file
# is present, we need to write a new file which is immediately read by the task following this one
# to inject the status of the smoke job as a Result variable (which persists across Stages) and
# can be used to inform the health of the overall smoker jobs to GitHub for pull request health
#
# Usage/Instructions: Copy template_smoke_test_sentinel_file_checker.sh to a ECU/Smoke/Feature test
# directory and rename it as smoke_test_sentinel_file_checker.sh (simply remove "template_")
#
# E.g. /firmware/validation/bbc_smoke/smoke_test_sentinel_file_checker.sh
#
# Make sure to create and/or enable the ECU (in this case BBC Smoke) job with the Firmware Test
# Global plan in Bamboo
#

# Get name of parent directory
# First get the full path to where this script is stored
parent_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# Next grab just the name of the directory
parent_dir_name=`basename $parent_path`

file_exists=`ls $bamboo_build_working_directory | grep "github.sentinel"`
if [ "$file_exists" = "" ]; then
    echo "${parent_dir_name}=FAIL" >> "${parent_dir_name}_smoke.result"
else
    echo "${parent_dir_name}=PASS" >> "${parent_dir_name}_smoke.result"
fi
