#! /usr/bin/env bash
#
# Helper script that asks whether the user wants to update a baseline.
#
# Return code:
#
# 0: Yes, update and continue.
# 1: No, don't update but continue.
# 200: No, don't update and abort.

while true; do
    printf "\033[0K" >>/dev/tty # Delete any augmented output.
    echo " failed" >>/dev/tty
    echo ">> Type 'c' to continue, 'd' to see diagnostics, 'u' to update baseline, and 'a' to abort." >/dev/tty

    read -r -s -n 1 key </dev/tty

    case $key in
        [uU])
            echo ">> Updating baseline ..." >/dev/tty
            exit 0
            ;;
        [cC])
            echo ">> Continuing ..." >/dev/tty
            exit 1
            ;;
        [aA])
            echo ">> Aborting ..." >/dev/tty
            exit 200
            ;;

        [dD])
            if [ "$TEST_DIAGNOSTICS" != "" ] && [ "$TEST_DIAGNOSTICS" != "/dev/stdout" ]; then
                less -S "$TEST_DIAGNOSTICS" </dev/tty >/dev/tty
            else
                echo "Do not have diagnostics." >/dev/tty
            fi
            ;;
        *) echo ">> Answer not recognized, try again ..." >/dev/tty ;;
    esac
done
