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 if ! today --category='holiday' | grep '' >/dev/null; then
73 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]'| \
74 while read times event; do
75 begin="$(expr 60 \* $(echo $times|sed -e 's/-.*//' -e 's/:/ + /g'))"
76 end="$(expr 60 \* $(echo $times|sed -e 's/.*-//' -e 's/:/ + /g'))"
78 begin="`expr $begin - 15`"
79 end="`expr $begin / 2 + $end / 2`"
80 if [ "$begin" -le "$now" -a "$now" -lt "$end" ]; then
81 echo "scheduled=\"Class ($event)\"" > "$HOME/.away-smart"
83 elif [ "$old_away" = "Class ($event)" -a "$now" -gt "$max_end" ]; then
84 echo "old_smart=\"$old_away\"" > "$HOME/.away-smart"
89 today --category='!school' | sed -e s/^..........//|grep '[0-9][0-9]:[0-9][0-9]-[0-9][0-9]:[0-9][0-9]'| \
90 while read times event; do
91 begin="$(expr 60 \* $(echo $times|sed -e 's/-.*//' -e 's/:/ + /g'))"
92 end="$(expr 60 \* $(echo $times|sed -e 's/.*-//' -e 's/:/ + /g'))"
94 if [ "$begin" -le "$now" -a "$now" -lt "$end" ]; then
95 echo "scheduled=\"$event\"" > "$HOME/.away-smart"
97 elif [ "$old_away" = "$event" -a "$now" -gt "$max_end" ]; then
98 echo "old_smart=\"$old_away\"" > "$HOME/.away-smart"
102 [ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart"
103 old_sched="$scheduled"
105 for host in abe homer lisa mona; do
106 ping -c 1 $host >/dev/null 2>&1 && livehosts="$livehosts $host"
109 for host in $livehosts; do
110 # True if a non-blanked display is found
111 $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
114 if ! ping -c 1 mona >/dev/null 2>/dev/null; then
117 last_slh="`$ssh mona cat .blue/last_slh 2>/dev/null`"
118 if [ -z "$last_slh" ]; then
120 elif [ "$(expr "$(date +%s)" - "$last_slh")" -lt 600 ]; then
127 if [ -n "$scheduled" ]; then
128 set_custom "$scheduled"
129 elif [ -n "$alive" ]; then
131 elif [ "$phone" = unknown ]; then
133 elif [ "$phone" = present ]; then
139 cat /dev/null > "$HOME/.away-smart"
140 echo "old_smart=\"$smart\"" >>~/.away-smart
141 echo "old_phone=\"$phone\"" >>~/.away-smart
142 echo "old_alive=\"$alive\"" >>~/.away-smart
143 echo "old_sched=\"${scheduled:-$old_sched}\"" >>~/.away-smart