Assorted away-tpope updates
[tpope-extra.git] / bin / away-detect
1 #!/bin/bash
2 # $Id$
3 # -*- sh -*- vim:set ft=sh sw=4 sts=4:
4
5 # OBSOLETE.  See perl/away-tpope instead.
6
7 # Checks several sources of information and updates my away status
8 # appropriately.  Designed to be periodically called by cron on marge.
9
10 PATH="$HOME/bin:/bin:/usr/bin"
11
12 out="Out"
13 sleeping="Sleeping"
14 afk="Away from keyboard"
15 off="Desktop is off"
16 NAME="away-detect"
17
18 if [ -f "$HOME/.LCK..$NAME" ]; then
19     if [ -d "/proc/`cat $HOME/.LCK..$NAME`" ]; then
20         echo "Locked.  Exiting." >&2
21         exit 1
22     else
23         echo "Stale lock file found.  Removing." >&2
24         rm -f $HOME/.LCK..$NAME
25     fi
26 fi
27 trap 'rm -f "$HOME/.LCK..$NAME"' EXIT
28
29 old_away="`tpope away`"
30 [ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart"
31 cat /dev/null > "$HOME/.away-smart"
32
33 find "$HOME" -maxdepth 1 -name .activity -mtime +1 -exec rm {} \;
34
35 ssh="ssh -a -x -oBatchmode=yes -oSetupTimeOut=20"
36
37 if [ -f "/tmp/LCK..`basename $0`" ]; then
38     if [ -d "/tmp/LCK..`basename $0`" ]; then
39         #echo "Locked.  Exiting." >&2
40         exit 1
41     else
42         #echo "Stale lock file found.  Removing." >&2
43         rm -f "/tmp/LCK..`basename $0`"
44     fi
45 fi
46
47 trap 'rm -f "/tmp/LCK..`basename $0`"' EXIT
48 echo $$ > "/tmp/LCK..`basename $0`"
49
50 set_smart() {
51     if [ -z "$old_away" -o "$old_away" = "$old_smart" ] || [ "$phone" = present -a "$old_phone" = absent ]; then
52         tpope away "$*"
53     fi
54     smart="$*"
55 }
56
57 set_custom() {
58     [ -z "$old_away" -o "$old_away" = "$old_smart" ] && \
59     tpope away "$*"
60 }
61
62 set_out() {
63     [ -z "$old_away" -o "$old_away" = "$old_smart" ] && \
64     tpope away "$*"
65     set_smart "$*"
66 }
67
68 set_in() {
69     if [ "`date +%k`" -ge 0 -a "`date +%k`" -le 9 ]; then
70         set_smart "$sleeping"
71     else
72         set_smart "$*"
73     fi
74 }
75
76 set_active() {
77     if [ -n "$old_smart" -a -n "$old_away" ]; then
78         tpope away -c
79         set_smart ""
80     fi
81     #[ -f "$HOME/.away-smart" ] && tpope away -c
82 }
83
84 now="$(expr 60 \* $(date +%H) + $(date +%M))"
85
86 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]'| \
87 while read times event; do
88     begin="$(expr 60 \* $(echo $times|sed -e 's/-.*//' -e 's/:/ + /g'))"
89     end="$(expr 60 \* $(echo $times|sed -e 's/.*-//' -e 's/:/ + /g'))"
90     max_end="`expr $end + 5`"
91     end="`expr 3 \* $begin / 4 + $end / 4`"
92     begin="`expr $begin - 25`"
93     if [ "$begin" -le "$now" -a "$now" -lt "$end" ]; then
94         echo "scheduled=\"Class: $event\"" >> "$HOME/.away-smart"
95         break
96     elif [ "$old_away" = "Class: $event" -a "$now" -gt "$max_end" ]; then
97         echo "old_smart=\"$old_away\"" > "$HOME/.away-smart"
98     fi
99 done
100
101 today --category='!school'\ '!private' | sed -e s/^..........// -e 's/ \[[^[]*\]$//'|grep '[0-9][0-9]:[0-9][0-9]-[0-9][0-9]:[0-9][0-9]'| \
102 while read times event; do
103     begin="$(expr 60 \* $(echo $times|sed -e 's/-.*//' -e 's/:/ + /g'))"
104     end="$(expr 60 \* $(echo $times|sed -e 's/.*-//' -e 's/:/ + /g'))"
105     max_end="$end"
106     if [ "$begin" -le "$now" -a "$now" -lt "$end" ]; then
107         echo "scheduled=\"$event\"" > "$HOME/.away-smart"
108         break
109     elif [ "$old_away" = "$event" -a "$now" -gt "$max_end" ]; then
110         echo "old_smart=\"$old_away\"" > "$HOME/.away-smart"
111     fi
112 done
113
114 [ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart"
115 old_sched="$scheduled"
116
117 for host in buster maeby lindsay tobias; do
118     ping -c 1 $host >/dev/null 2>&1 && livehosts="$livehosts $host"
119 done
120
121 for host in $livehosts; do
122     # True if a non-blanked display is found
123     $ssh $host 'if pidof xscreensaver >/dev/null && 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|egrep "[0-9]:[0-9][0-9] ssh marge .*(screen.*RR irc|Chat)"|sed -e "s/^  *//"|cut -d" " -f 1`; [ -f "$HOME/.irc.lock" -o -z "$pid" ] || kill $pid; false; fi; else false; fi' && alive=$host
124 done
125
126 if ! ping -c 1 mona >/dev/null 2>/dev/null; then
127     phone=unknown
128 else
129     last_slh="`$ssh mona cat .blue/last_slh 2>/dev/null`"
130     if [ -z "$last_slh" ]; then
131         phone=unknown
132     elif [ "$(expr "$(date +%s)" - "$last_slh")" -lt 600 ]; then
133         phone=present
134     else
135         phone=absent
136     fi
137 fi
138
139 if [ -n "$scheduled" ]; then
140     set_custom "$scheduled"
141 elif [ -n "$alive" ]; then
142     set_active
143 elif [ "$phone" = unknown ]; then
144     set_in "$afk"
145 elif [ "$phone" = present ]; then
146     set_in "$afk"
147 else
148     set_out "$out"
149 fi
150
151 cat /dev/null > "$HOME/.away-smart"
152 echo "old_smart=\"$smart\"" >>~/.away-smart
153 echo "old_phone=\"$phone\"" >>~/.away-smart
154 echo "old_alive=\"$alive\"" >>~/.away-smart
155 echo "old_sched=\"${scheduled:-$old_sched}\"" >>~/.away-smart
156
157 # Former away-actions
158
159 away="`tpope away`"
160 activity="`tpope activity`"
161
162 case "$away" in
163     Sleeping|"Away from keyboard"|TV*|"") is_away=0 ;;
164     *[Cc]lass*) is_away=1 ;;
165 esac
166
167 [ "$phone" = absent ] && is_away=1
168
169 if [ "$is_away" = 1 ]; then
170     [ ! -f /tmp/.tpope_lights ] && touch /tmp/.tpope_lights && \
171         $ssh maeby br -F -F
172 elif [ -f /tmp/.tpope_lights ]; then
173     $ssh maeby br -n 5
174     rm -f /tmp/.tpope_lights
175     $ssh maeby br -n 5
176 fi
177
178 if [ "$activity" ]; then
179     information="$activity"
180 elif [ "$away" ]; then
181     information="$away"
182 else
183     case "$alive" in
184         lindsay*) information="On laptop" ;;
185         buster*) information="In bed" ;;
186         tobias*|lucille*) information="On desktop" ;;
187         *) information="Away from keyboard?" ;;
188     esac
189 fi
190
191 if [ -z "$information" ]; then
192     echo > "$HOME/.caminfo.deskcam"
193     echo "- On desktop" > "$HOME/.caminfo.bedcam"
194     #echo "On desktop" > "$HOME/.status"
195     rm -f "$HOME/.plan"
196 else
197     echo "- $information" > "$HOME/.caminfo.deskcam"
198     if [ "$information" = "In bed" ]; then
199         echo "" > "$HOME/.caminfo.bedcam"
200     else
201         echo "- $information" > "$HOME/.caminfo.bedcam"
202     fi
203     #echo "$information" > "$HOME/.status"
204     echo "$information" > "$HOME/.plan"
205 fi
206
207 for host in gob maeby buster oscar; do
208     ping -c 1 $host >/dev/null && scp -q "$HOME/.status" $host: 2>/dev/null
209 done
210
211 ping -c 1 lucille >/dev/null && scp -q "$HOME/.caminfo.deskcam" lucille:.caminfo 2>/dev/null
212 ping -c 1 buster >/dev/null && scp -q "$HOME/.caminfo.bedcam" buster:.caminfo 2>/dev/null
213 rm -f .caminfo.bedcam
214
215 exit 0