mkogg.py: Fix 'self.get_mp4_metadata(self, source)'
[blog.git] / posts / Alarm / alarm.sh
1 #!/bin/sh
2 #
3 # Copyright (C) 2008-2010, William Trevor King <wking@drexel.edu>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 # Can't drag yourself away from your software? Set an alarm!
19 #
20 # Caveats:
21 #  You'll have to keep your current terminal open until the alarm goes off,
22 #  or the message woln't be printed to the screen.
23 #  The sound will still play.
24 #
25 #  Echoed text may not appear if the terminal is in curses mode (e.g. Emacs)
26 #
27 #  Your message text cannot contain any shell-sensitive characters,
28 #  since it gets expanded at least twice before AT sees it.
29 #
30 #  Minute resolution, since that's the best `at` can do.
31 #
32 #  Script is hugely larger than the actual code,
33 #  but hopefully well commented inside and user friendly outside. :p
34
35 # Alarms should make some noise.
36 # Adapt this command to generate something on your particular system.
37 # Googling `beep wav` worked pretty well for me, or record your own :).
38 ALARM_SOUND_CMD="aplay $HOME/Music/system/towerclo.wav"
39 ECHO="/bin/echo -e" # echo command must expand '\n' to newline
40
41 Usage ()
42 {
43     ERROR=$1
44     $ECHO "$ERROR"
45     $ECHO "
46 usage: alarm <some-time> [some message]
47
48 where
49   <some-time> will be recognized by at (see man at).
50   'some message' will be printed to the current terminal.
51 <some-time> must be the first argument, so if your time string is
52 several words long, enclose it in quotes ('6:00 tomorrow') to keep the
53 shell from breaking it into multiple arguments."
54 }
55
56 # If there is no time argument, print a usage string.
57 if [ $# -lt 1 ]
58 then
59     Usage 'Incorrect number of arguments'
60     return 1
61 fi
62
63 # Initialize AT_CMD, the command we will send AT, and read in arguments.
64 AT_CMD=""
65 TIME=$1
66 shift
67 MESSAGE="$*"
68
69 Append_cmd () # Add command.  In case you want to add whistles to the bells ;)
70 {
71     NEW_CMD=$1
72     AT_CMD="$AT_CMD\n$NEW_CMD"
73 }
74
75 Append_cmd "$ALARM_SOUND_CMD"
76
77 if [ -n "$MESSAGE" ] # if there is a non-zero message string...
78 then                 # ... append an $ECHO cmmd.
79     TERMINAL_NAME=$(tty)
80     Append_cmd "$ECHO \"$MESSAGE\" > $TERMINAL_NAME"
81 fi
82
83 if false
84 then # debugging printout
85     $ECHO "'$AT_CMD'"
86     $ECHO 
87     $ECHO "at '$TIME'"
88 fi
89
90 # send the command off to at.
91 $ECHO "$AT_CMD" | at "$TIME" || Usage 'Bad time?'
92
93 # use `atq` to list outstanding jobs.
94 # use `at -c <job-number>` to view the script at executes for debugging.