#!/bin/bash
#
# bamboo_hummingbird_results_hook.sh: Call to upload results data into Hummingbird
#                                     Firmware Validation Test Database
# Usage: ./bamboo_hummingbird_results_hook.sh <result> <type1> <typeN>
#
# @copyright Highly Confidential and Proprietary Zoox Information. Copyright (c) Zoox
#
# Author: trawat, sdaleo

# If old environment exists, remove it
if [ -d hummingbird_client_env ]; then
    rm -rf hummingbird_client_env
fi

# Make clean environment
virtualenv -p python3.6 hummingbird_client_env

# Activate hummingbird env
. hummingbird_client_env/bin/activate

# Force reinstall newest version of hummingbird from pypi
python -m pip install hummingbird \
    --index-url https://pypi.zooxlabs.com/simple \
    --extra-index-url https://pypi.python.org/simple \
    --force-reinstall

# CLI tool takes in Bamboo variable as CLI input argument and converts it to a float to use with
# hummingbird CLI tool
start_time=$(convert_timestamp $bamboo_buildTimeStamp)

# Get end time in the same format as $bamboo_buildTimeStamp
# and then convert it using the same utility script as the above
end_datetime=`date "+%Y-%m-%dT%T.%3N%:z"`
end_time=$(convert_timestamp $end_datetime)

# Determine test status
test_result="$1"

# Determine test type (EMU, BMU, TBC smoke, etc.)
test_type="$2"

# Which node is this running on?
nodename=`hostname`

# CLI tool takes in Bamboo variable as CLI input argument and converts it to a float to use with
# hummingbird CLI tool
hummingbird \
    --remotehost hummingbird.zooxlabs.com \
    --port 32100 \
    --username postgres \
    --databasename fwval \
    --result $test_result \
    --criticality NONE \
    --start_time $start_time \
    --end_time $end_time \
    --other ''  \
    --fw_hash ${bamboo_planRepository_revision} \
    --test_id ${bamboo_buildResultKey} \
    --tester ${nodename} \
    smoketest ${test_type}

