-
Notifications
You must be signed in to change notification settings - Fork 11
/
medlog
92 lines (77 loc) · 2.72 KB
/
medlog
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
#----------------------------------------------------------------------------------
# Project Name - miscellaneous/medlog
# Started On - Fri 18 Nov ??:??:?? BST 2016
# Last Change - Thu 8 Mar 06:40:45 GMT 2018
# Author E-Mail - [email protected]
# Author GitHub - https://github.com/terminalforlife
#----------------------------------------------------------------------------------
# WARNING: This program (older one, compared to the GUI, medlogip) logs in the
# opposite direction to medlogip. When I wrote medlogip, I realised it was
# silly having the latest entries at the bottom. An easy fix in medlog, -
# so I may include that at some point.
#----------------------------------------------------------------------------------
_VERSION_="2018-03-08"
XERR(){ printf "[L%0.4d] ERROR: %s\n" "$1" "$2" 1>&2; exit 1; }
ERR(){ printf "[L%0.4d] ERROR: %s\n" "$1" "$2" 1>&2; }
USAGE(){
while read -r; do
printf "%s\n" "$REPLY"
done <<-EOF
MEDLOG ($_VERSION_)
Written by terminalforlife ([email protected])
Need help logging when you've taken your daily medication?
SYNTAX: medlog [OPTS]
OPTS: --help|-h|-? - Displays this help information.
--version|-v - Output only the version datestamp.
--debug|-D - Enables the built-in bash debugging.
EOF
}
while [ "$1" ]; do
case "$1" in
--help|-h|-\?)
USAGE; exit 0 ;;
--version|-v)
printf "%s\n" "$_VERSION_"
exit 0 ;;
--debug|-D)
DEBUGME="true" ;;
--quiet|-q)
BEQUIET="true" ;;
*)
XERR "$LINENO" "Incorrect argument(s) specified." ;;
esac
shift
done
declare -i DEPCOUNT=0
for DEP in /usr/bin/{xmessage,notify-send} /bin/mkdir; {
if ! [ -x "$DEP" ]; then
ERR "$LINENO" "Dependency '$DEP' not met."
DEPCOUNT+=1
fi
}
[ $DEPCOUNT -eq 0 ] || exit 1
[ "$DEBUGME" == "true" ] && set -x
MAINDIR="$HOME/.medlog"
LOGFILE="$MAINDIR/medlog.log"
[ -d "$MAINDIR" ] || /bin/mkdir -p "$MAINDIR"
[ -f "$LOGFILE" ] || > "$LOGFILE"
[ -w "$LOGFILE" ] || XERR "$LINENO" "File '${LOGFILE//*\/}' has no write access."
while read -a X; do
printf -v LAST_LOG "%s\n" "${X[0]}"
done < "$LOGFILE"
printf -v TODAY_DATE "%(%F (%X))T" -1
MSG="Medication taken today, $USER?"
MSG_NO="Take medication, then reload medlog."
if [ "${TODAY_DATE%/*}" == "${LAST_LOG%/*}" ]; then
/usr/bin/notify-send "Medication already taken today."
else
if ! /usr/bin/xmessage -buttons yes,no -center "$MSG"; then
#TODO - Loop a reminder notification every X minutes.
/usr/bin/notify-send "$MSG_NO"
exit 1
else
printf "%(%F (%X))T\n" -1 >> "$LOGFILE"
fi
fi
# vim: noexpandtab colorcolumn=84 tabstop=8 noswapfile nobackup