#!/bin/bash
#
# Count down (roughly) x (default: 10) seconds until exiting.
# Pressing a key will reset the countdown to the start value.
# The last 10 seconds will be accompanied by double beeps.
#
# Stefan Tomanek <stefan.tomanek@wertarbyte.de>
# May be used in conjunction with Status Quo Ante
# http://wertarbyte.de/status-quo-ante.shtml

L=1
START=${1:-10}
while [ $L = 1 ]; do
    L=0
    COUNTDOWN=$START
    while [ $COUNTDOWN -gt 0 ]; do
        C=""
        [ $COUNTDOWN -le 10 ] && { echo -ne '\a'; sleep 0.3; echo -ne '\a'; }
        read -t 1 -p "$COUNTDOWN seconds left: " -n 1 C || let COUNTDOWN--
        echo
        if [ -n "$C" ]; then
            L=1
            echo "Resetting countdown to $START seconds"
            break
        fi
    done
done

