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 --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'))"
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"
82 elif [ "$old_away" = "Class ($event)" -a "$now" -gt "$max_end" ]; then
83 echo "old_smart=\"$old_away\"" > "$HOME/.away-smart"
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'))"
92 if [ "$begin" -le "$now" -a "$now" -lt "$end" ]; then
93 echo "scheduled=\"$event\"" > "$HOME/.away-smart"
95 elif [ "$old_away" = "$event" -a "$now" -gt "$max_end" ]; then
96 echo "old_smart=\"$old_away\"" > "$HOME/.away-smart"
100 [ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart"
101 old_sched="$scheduled"
103 for host in abe homer lisa mona; do
104 ping -c 1 $host >/dev/null 2>&1 && livehosts="$livehosts $host"
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
112 if ! ping -c 1 mona >/dev/null 2>/dev/null; then
115 last_slh="`$ssh mona cat .blue/last_slh 2>/dev/null`"
116 if [ -z "$last_slh" ]; then
118 elif [ "$(expr "$(date +%s)" - "$last_slh")" -lt 600 ]; then
125 if [ -n "$scheduled" ]; then
126 set_custom "$scheduled"
127 elif [ -n "$alive" ]; then
129 elif [ "$phone" = unknown ]; then
131 elif [ "$phone" = present ]; then
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