#!/bin/bash
#
# nixpunk
# 06-April-2015
# A simple timer using system notification daemon
#

usage() {
    if [[ $# != 1 ]]; then
        echo "Usage: timer [unit] value"
        echo " [unit] can be -s for seconds, -m for minutes, or -h for hours."
        echo " Default is seconds." 
    fi
}

timer() {
    sleep $1 && notify-send 'Timer is up!' -u critical -i face-monkey
}

# This script depends on notify-send
if ! (/bin/which notify-send >/dev/null 2>&1); then
    echo "notify-send not found!"
    exit 1
fi

if [[ $# < 1 ]]; then
    usage 
    exit 1
elif [[ $# == 1 ]]; then # default, no-options passed
    if [ $1 -eq $1 ] 2>/dev/null; then
        timer $1
    else
        echo "timer: invalid value '$1'"
        echo "Use 'timer' for usage."
        exit 1
    fi
fi

while getopts ":s:m:h:" opt; do
    case $opt in
        s)
            timer $OPTARG
            ;;
        m)
            timer $((OPTARG*60))
            ;;
        h)
            timer $((OPTARG*60*60))
            ;;
        '?')
            echo "Invalid option: -$OPTARG" >&2
            usage
            exit 1
            ;;
        :)
            echo "Option -$OPTARG requires an argument." >&2
            usage
            exit 1
            ;;
    esac
done
