3 # -*- sh -*- vim: ft=sh sw=4 sts=4
5 # Checks several sources of information and updates my away status
6 # appropriately. Designed to be periodically called by cron on marge.
8 PATH=/home/tpope/bin:/bin:/usr/bin
12 afk="Away from keyboard"
15 old_away="`tpope away`"
16 [ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart"
17 cat /dev/null > "$HOME/.away-smart"
19 ssh="ssh -a -x -oBatchmode=yes -oSetupTimeOut=20"
21 if [ -f "/tmp/LCK..`basename $0`" ]; then
22 if [ -d "/tmp/LCK..`basename $0`" ]; then
23 #echo "Locked. Exiting." >&2
26 #echo "Stale lock file found. Removing." >&2
27 rm -f "/tmp/LCK..`basename $0`"
31 trap 'rm -f "/tmp/LCK..`basename $0`"' EXIT
32 echo $$ > "/tmp/LCK..`basename $0`"
35 [ -z "$old_away" -o "$old_away" = "$old_smart" ] && \
41 [ -z "$old_away" -o "$old_away" = "$old_smart" ] && \
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" ] && \
54 if [ "`date +%k`" -ge 2 -a "`date +%k`" -le 9 ]; then
55 #[ -n "$old_away" -a "$HOME/.away" -nt "$HOME/.away-smart" ] || \
63 if [ -n "$old_smart" -a -n "$old_away" ]; then
67 #[ -f "$HOME/.away-smart" ] && tpope away -c
70 now="$(expr 60 \* $(date +%H) + $(date +%M))"
72 today|sed -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'))"
78 *[Cc]lass*) begin="`expr $begin - 10`"
79 end="`expr $begin / 2 + $end / 2`"
82 if [ "$begin" -le "$now" -a "$now" -lt "$end" ]; then
83 echo "scheduled=\"$event\"" > "$HOME/.away-smart"
85 elif [ "$old_away" = "$event" -a "$now" -gt "$max_end" ]; then
86 echo "old_smart=\"$old_away\"" > "$HOME/.away-smart"
90 [ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart"
91 old_sched="$scheduled"
93 for host in abe homer lisa mona; do
94 ping -c 1 $host >/dev/null 2>&1 && livehosts="$livehosts $host"
97 for host in $livehosts; do
98 # True if a non-blanked display is found
99 $ssh $host 'if DISPLAY=:0.0 xscreensaver-command -time >/dev/null 2>&1; then if DISPLAY=:0.0 xscreensaver-command -time 2>/dev/null|grep non-blanked >/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
102 if ! ping -c 1 mona >/dev/null 2>/dev/null; then
105 last_slh="`$ssh mona cat .blue/last_slh 2>/dev/null`"
106 if [ -z "$last_slh" ]; then
108 elif [ "$(expr "$(date +%s)" - "$last_slh")" -lt 600 ]; then
115 if [ -n "$scheduled" ]; then
116 set_custom "$scheduled"
117 elif [ -n "$alive" ]; then
119 elif [ "$phone" = unknown ]; then
121 elif [ "$phone" = present ]; then
127 cat /dev/null > "$HOME/.away-smart"
128 echo "old_smart=\"$smart\"" >>~/.away-smart
129 echo "old_phone=\"$phone\"" >>~/.away-smart
130 echo "old_alive=\"$alive\"" >>~/.away-smart
131 echo "old_sched=\"${scheduled:-$old_sched}\"" >>~/.away-smart