aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/timer
blob: 04522d6540faff92376711c7acf25d4eaac6045d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash

############################################################
# Argument parsing
############################################################

# We need TEMP as the 'eval set --' would nuke the return value of getopt.
TEMP=$(getopt -o 'm:hr:' --long 'message:,help,repetition:' -n 'timer' -- "$@")

if [ $? -ne 0 ]; then
    echo 'Terminating...' >&2
    exit 1
fi

# Note the quotes around "$TEMP": they are essential!
eval set -- "$TEMP"
unset TEMP

message=''
repetition='60'
while :; do
    case "$1" in
    '-m'|'--message') message="$2"; shift 2; continue;;
    '-r'|'--repetition') repetition="$2"; shift 2; continue;;
    '-h'|'--help') cat <<EOF
timer [OPTIONS]... TIME

Options:

  -h, --help            show this help screen (TIME optional in this case)
  -m M, --message M     Use M as message to show and say
  -r S, --repetition S  Wait S seconds between each repetition
EOF
        exit 0
        ;;
    '--') shift; break;;
    *) echo 'Internal error !' >&2; exit 1 ;;
    esac
done


time="$1"
shift

if [[ "$message" = "" ]]; then message="Timer \"$time\" is over."; fi

############################################################
# Main code
############################################################

sleep=sleep
if type -p fancy-sleep >/dev/null; then sleep=fancy-sleep; fi

"$sleep" "$time" || exit 0

while :; do
    printf "\a"
    if test "$#" -gt 0; then
        "$@"
    else
        notify-send "$message"
        spd-say-en  "$message"
    fi
    "$sleep" "$repetition" || break
done