3 # -*- sh -*- vim:set ft=sh sw=4 sts=4:
5 # OBSOLETE. See perl/away-tpope instead.
7 # Checks several sources of information and updates my away status
8 # appropriately. Designed to be periodically called by cron on marge.
10 PATH="$HOME/bin:/bin:/usr/bin"
14 afk="Away from keyboard"
18 if [ -f "$HOME/.LCK..$NAME" ]; then
19 if [ -d "/proc/`cat $HOME/.LCK..$NAME`" ]; then
20 echo "Locked. Exiting." >&2
23 echo "Stale lock file found. Removing." >&2
24 rm -f $HOME/.LCK..$NAME
27 trap 'rm -f "$HOME/.LCK..$NAME"' EXIT
29 old_away="`tpope away`"
30 [ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart"
31 cat /dev/null > "$HOME/.away-smart"
33 find "$HOME" -maxdepth 1 -name .activity -mtime +1 -exec rm {} \;
35 ssh="ssh -a -x -oBatchmode=yes -oSetupTimeOut=20"
37 if [ -f "/tmp/LCK..`basename $0`" ]; then
38 if [ -d "/tmp/LCK..`basename $0`" ]; then
39 #echo "Locked. Exiting." >&2
42 #echo "Stale lock file found. Removing." >&2
43 rm -f "/tmp/LCK..`basename $0`"
47 trap 'rm -f "/tmp/LCK..`basename $0`"' EXIT
48 echo $$ > "/tmp/LCK..`basename $0`"
51 if [ -z "$old_away" -o "$old_away" = "$old_smart" ] || [ "$phone" = present -a "$old_phone" = absent ]; then
58 [ -z "$old_away" -o "$old_away" = "$old_smart" ] && \
63 [ -z "$old_away" -o "$old_away" = "$old_smart" ] && \
69 if [ "`date +%k`" -ge 0 -a "`date +%k`" -le 9 ]; then
77 if [ -n "$old_smart" -a -n "$old_away" ]; then
81 #[ -f "$HOME/.away-smart" ] && tpope away -c
84 now="$(expr 60 \* $(date +%H) + $(date +%M))"
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"
96 elif [ "$old_away" = "Class: $event" -a "$now" -gt "$max_end" ]; then
97 echo "old_smart=\"$old_away\"" > "$HOME/.away-smart"
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'))"
106 if [ "$begin" -le "$now" -a "$now" -lt "$end" ]; then
107 echo "scheduled=\"$event\"" > "$HOME/.away-smart"
109 elif [ "$old_away" = "$event" -a "$now" -gt "$max_end" ]; then
110 echo "old_smart=\"$old_away\"" > "$HOME/.away-smart"
114 [ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart"
115 old_sched="$scheduled"
117 for host in sarah homer lisa mona; do
118 ping -c 1 $host >/dev/null 2>&1 && livehosts="$livehosts $host"
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
126 if ! ping -c 1 mona >/dev/null 2>/dev/null; then
129 last_slh="`$ssh mona cat .blue/last_slh 2>/dev/null`"
130 if [ -z "$last_slh" ]; then
132 elif [ "$(expr "$(date +%s)" - "$last_slh")" -lt 600 ]; then
139 if [ -n "$scheduled" ]; then
140 set_custom "$scheduled"
141 elif [ -n "$alive" ]; then
143 elif [ "$phone" = unknown ]; then
145 elif [ "$phone" = present ]; then
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
157 # Former away-actions
160 activity="`tpope activity`"
163 Sleeping|"Away from keyboard"|TV*|"") is_away=0 ;;
164 *[Cc]lass*) is_away=1 ;;
167 [ "$phone" = absent ] && is_away=1
169 if [ "$is_away" = 1 ]; then
170 [ ! -f /tmp/.tpope_lights ] && touch /tmp/.tpope_lights && \
172 elif [ -f /tmp/.tpope_lights ]; then
174 rm -f /tmp/.tpope_lights
178 if [ "$activity" ]; then
179 information="$activity"
180 elif [ "$away" ]; then
184 lisa*|abe*) information="On laptop" ;;
185 sarah*) information="In bed" ;;
186 mona*|homer*) information="On desktop" ;;
187 *) information="Away from keyboard?" ;;
191 if [ -z "$information" ]; then
192 echo > "$HOME/.caminfo.deskcam"
193 echo "- On desktop" > "$HOME/.caminfo.bedcam"
194 #echo "On desktop" > "$HOME/.status"
197 echo "- $information" > "$HOME/.caminfo.deskcam"
198 if [ "$information" = "In bed" ]; then
199 echo "" > "$HOME/.caminfo.bedcam"
201 echo "- $information" > "$HOME/.caminfo.bedcam"
203 #echo "$information" > "$HOME/.status"
204 echo "$information" > "$HOME/.plan"
207 for host in bart homer sarah clancy; do
208 ping -c 1 $host >/dev/null && scp -q "$HOME/.status" $host: 2>/dev/null
211 ping -c 1 homer >/dev/null && scp -q "$HOME/.caminfo.deskcam" homer:.caminfo 2>/dev/null
212 ping -c 1 sarah >/dev/null && scp -q "$HOME/.caminfo.bedcam" sarah:.caminfo 2>/dev/null
213 rm -f .caminfo.bedcam