Reversed school and email fields in faculty
[tpope-extra.git] / bin / away-detect
1 #!/bin/sh
2 # $Id$
3 # -*- sh -*- vim: ft=sh sw=4 sts=4
4
5 # Checks several sources of information and updates my away status
6 # appropriately.  Designed to be periodically called by cron on marge.
7
8 PATH=/home/tpope/bin:/bin:/usr/bin
9
10 out="Out"
11 sleeping="Sleeping"
12 afk="Away from keyboard"
13 off="Desktop is off"
14
15 old_away="`tpope away`"
16 [ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart"
17 cat /dev/null > "$HOME/.away-smart"
18
19 ssh="ssh -a -x -oBatchmode=yes -oSetupTimeOut=20"
20
21 if [ -f "/tmp/LCK..`basename $0`" ]; then
22     if [ -d "/tmp/LCK..`basename $0`" ]; then
23         #echo "Locked.  Exiting." >&2
24         exit 1
25     else
26         #echo "Stale lock file found.  Removing." >&2
27         rm -f "/tmp/LCK..`basename $0`"
28     fi
29 fi
30
31 trap 'rm -f "/tmp/LCK..`basename $0`"' EXIT
32 echo $$ > "/tmp/LCK..`basename $0`"
33
34 set_smart() {
35     [ -z "$old_away" -o "$old_away" = "$old_smart" ] && \
36     tpope away "$*"
37     smart="$*"
38 }
39
40 set_custom() {
41     [ -z "$old_away" -o "$old_away" = "$old_smart" ] && \
42     tpope away "$*"
43 }
44
45 set_out() {
46     # Away message is empty or older then our "smart" flag
47     #[ -n "$old_away" -a "$HOME/.away" -nt "$HOME/.away-smart" ] || \
48     [ -z "$old_away" -o "$old_away" = "$old_smart" ] && \
49     tpope away "$*"
50     set_smart "$*"
51 }
52
53 set_in() {
54     if [ "`date +%k`" -ge 2 -a "`date +%k`" -le 9 ]; then
55         #[ -n "$old_away" -a "$HOME/.away" -nt "$HOME/.away-smart" ] || \
56         set_smart "$sleeping"
57     else
58         set_smart "$*"
59     fi
60 }
61
62 set_active() {
63     if [ -n "$old_smart" -a -n "$old_away" ]; then
64         tpope away -c
65         set_smart ""
66     fi
67     #[ -f "$HOME/.away-smart" ] && tpope away -c
68 }
69
70 now="$(expr 60 \* $(date +%H) + $(date +%M))"
71
72 today --category='school' | sed -e s/^..........// -e 's/ \[.*\]$//'|grep '[0-9][0-9]:[0-9][0-9]-[0-9][0-9]:[0-9][0-9]'| \
73 while read times event; do
74     begin="$(expr 60 \* $(echo $times|sed -e 's/-.*//' -e 's/:/ + /g'))"
75     end="$(expr 60 \* $(echo $times|sed -e 's/.*-//' -e 's/:/ + /g'))"
76     max_end="$end"
77     begin="`expr $begin - 15`"
78     end="`expr $begin / 2 + $end / 2`"
79     if [ "$begin" -le "$now" -a "$now" -lt "$end" ]; then
80         echo "scheduled=\"Class ($event)\"" > "$HOME/.away-smart"
81         break
82     elif [ "$old_away" = "Class ($event)" -a "$now" -gt "$max_end" ]; then
83         echo "old_smart=\"$old_away\"" > "$HOME/.away-smart"
84     fi
85 done
86
87 today --category='!school' | sed -e s/^..........//|grep '[0-9][0-9]:[0-9][0-9]-[0-9][0-9]:[0-9][0-9]'| \
88 while read times event; do
89     begin="$(expr 60 \* $(echo $times|sed -e 's/-.*//' -e 's/:/ + /g'))"
90     end="$(expr 60 \* $(echo $times|sed -e 's/.*-//' -e 's/:/ + /g'))"
91     max_end="$end"
92     if [ "$begin" -le "$now" -a "$now" -lt "$end" ]; then
93         echo "scheduled=\"$event\"" > "$HOME/.away-smart"
94         break
95     elif [ "$old_away" = "$event" -a "$now" -gt "$max_end" ]; then
96         echo "old_smart=\"$old_away\"" > "$HOME/.away-smart"
97     fi
98 done
99
100 [ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart"
101 old_sched="$scheduled"
102
103 for host in abe homer lisa mona; do
104     ping -c 1 $host >/dev/null 2>&1 && livehosts="$livehosts $host"
105 done
106
107 for host in $livehosts; do
108     # True if a non-blanked display is found
109     $ssh $host 'if DISPLAY=:0.0 xscreensaver-command -version >/dev/null 2>&1; then if DISPLAY=:0.0 xscreensaver-command -time 2>&1 |egrep "non-blanked|no saver status" >/dev/null; then true; else pid=`ps ax|grep "[0-9]:[0-9][0-9] ssh .*exec screen.*RR irc"|sed -e "s/^  *//"|cut -d" " -f 1`; [ -f "$HOME/.irc.lock" -o -z "$pid" ] || kill $pid; false; fi; else false; fi' && alive=$host
110 done
111
112 if ! ping -c 1 mona >/dev/null 2>/dev/null; then
113     phone=unknown
114 else
115     last_slh="`$ssh mona cat .blue/last_slh 2>/dev/null`"
116     if [ -z "$last_slh" ]; then
117         phone=unknown
118     elif [ "$(expr "$(date +%s)" - "$last_slh")" -lt 600 ]; then
119         phone=present
120     else
121         phone=absent
122     fi
123 fi
124
125 if [ -n "$scheduled" ]; then
126     set_custom "$scheduled"
127 elif [ -n "$alive" ]; then
128     set_active
129 elif [ "$phone" = unknown ]; then
130     set_in "$afk"
131 elif [ "$phone" = present ]; then
132     set_in "$afk"
133 else
134     set_out "$out"
135 fi
136
137 cat /dev/null > "$HOME/.away-smart"
138 echo "old_smart=\"$smart\"" >>~/.away-smart
139 echo "old_phone=\"$phone\"" >>~/.away-smart
140 echo "old_alive=\"$alive\"" >>~/.away-smart
141 echo "old_sched=\"${scheduled:-$old_sched}\"" >>~/.away-smart
142
143 exit 0