-#!/bin/sh
+#!/bin/bash
# $Id$
-# -*- sh -*- vim: ft=sh sw=4 sts=4
+# -*- sh -*- vim:set ft=sh sw=4 sts=4:
+
+# OBSOLETE. See perl/away-tpope instead.
# Checks several sources of information and updates my away status
# appropriately. Designed to be periodically called by cron on marge.
-PATH=/home/tpope/bin:/bin:/usr/bin
+PATH="$HOME/bin:/bin:/usr/bin"
out="Out"
sleeping="Sleeping"
afk="Away from keyboard"
off="Desktop is off"
+NAME="away-detect"
+
+if [ -f "$HOME/.LCK..$NAME" ]; then
+ if [ -d "/proc/`cat $HOME/.LCK..$NAME`" ]; then
+ echo "Locked. Exiting." >&2
+ exit 1
+ else
+ echo "Stale lock file found. Removing." >&2
+ rm -f $HOME/.LCK..$NAME
+ fi
+fi
+trap 'rm -f "$HOME/.LCK..$NAME"' EXIT
old_away="`tpope away`"
[ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart"
cat /dev/null > "$HOME/.away-smart"
+find "$HOME" -maxdepth 1 -name .activity -mtime +1 -exec rm {} \;
+
ssh="ssh -a -x -oBatchmode=yes -oSetupTimeOut=20"
if [ -f "/tmp/LCK..`basename $0`" ]; then
echo $$ > "/tmp/LCK..`basename $0`"
set_smart() {
- [ -z "$old_away" -o "$old_away" = "$old_smart" ] && \
- tpope away "$*"
+ if [ -z "$old_away" -o "$old_away" = "$old_smart" ] || [ "$phone" = present -a "$old_phone" = absent ]; then
+ tpope away "$*"
+ fi
smart="$*"
}
}
set_out() {
- # Away message is empty or older then our "smart" flag
- #[ -n "$old_away" -a "$HOME/.away" -nt "$HOME/.away-smart" ] || \
[ -z "$old_away" -o "$old_away" = "$old_smart" ] && \
tpope away "$*"
set_smart "$*"
}
set_in() {
- if [ "`date +%k`" -ge 2 -a "`date +%k`" -le 9 ]; then
- #[ -n "$old_away" -a "$HOME/.away" -nt "$HOME/.away-smart" ] || \
+ if [ "`date +%k`" -ge 0 -a "`date +%k`" -le 9 ]; then
set_smart "$sleeping"
else
set_smart "$*"
now="$(expr 60 \* $(date +%H) + $(date +%M))"
-today --category='!school' | sed -e s/^..........//|grep '[0-9][0-9]:[0-9][0-9]-[0-9][0-9]:[0-9][0-9]'| \
+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]'| \
while read times event; do
begin="$(expr 60 \* $(echo $times|sed -e 's/-.*//' -e 's/:/ + /g'))"
end="$(expr 60 \* $(echo $times|sed -e 's/.*-//' -e 's/:/ + /g'))"
- max_end="$end"
+ max_end="`expr $end + 5`"
+ end="`expr 3 \* $begin / 4 + $end / 4`"
+ begin="`expr $begin - 25`"
if [ "$begin" -le "$now" -a "$now" -lt "$end" ]; then
- echo "scheduled=\"$event\"" > "$HOME/.away-smart"
+ echo "scheduled=\"Class: $event\"" >> "$HOME/.away-smart"
break
- elif [ "$old_away" = "$event" -a "$now" -gt "$max_end" ]; then
+ elif [ "$old_away" = "Class: $event" -a "$now" -gt "$max_end" ]; then
echo "old_smart=\"$old_away\"" > "$HOME/.away-smart"
fi
done
-today --category='school' | sed -e s/^..........//|grep '[0-9][0-9]:[0-9][0-9]-[0-9][0-9]:[0-9][0-9]'| \
+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]'| \
while read times event; do
begin="$(expr 60 \* $(echo $times|sed -e 's/-.*//' -e 's/:/ + /g'))"
end="$(expr 60 \* $(echo $times|sed -e 's/.*-//' -e 's/:/ + /g'))"
max_end="$end"
- begin="`expr $begin - 15`"
- end="`expr $begin / 2 + $end / 2`"
if [ "$begin" -le "$now" -a "$now" -lt "$end" ]; then
- echo "scheduled=\"Class ($event)\"" > "$HOME/.away-smart"
+ echo "scheduled=\"$event\"" > "$HOME/.away-smart"
break
elif [ "$old_away" = "$event" -a "$now" -gt "$max_end" ]; then
echo "old_smart=\"$old_away\"" > "$HOME/.away-smart"
[ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart"
old_sched="$scheduled"
-for host in abe homer lisa mona; do
+for host in sarah homer lisa mona; do
ping -c 1 $host >/dev/null 2>&1 && livehosts="$livehosts $host"
done
for host in $livehosts; do
# True if a non-blanked display is found
- $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
+ $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
done
if ! ping -c 1 mona >/dev/null 2>/dev/null; then
echo "old_alive=\"$alive\"" >>~/.away-smart
echo "old_sched=\"${scheduled:-$old_sched}\"" >>~/.away-smart
+# Former away-actions
+
+away="`tpope away`"
+activity="`tpope activity`"
+
+case "$away" in
+ Sleeping|"Away from keyboard"|TV*|"") is_away=0 ;;
+ *[Cc]lass*) is_away=1 ;;
+esac
+
+[ "$phone" = absent ] && is_away=1
+
+if [ "$is_away" = 1 ]; then
+ [ ! -f /tmp/.tpope_lights ] && touch /tmp/.tpope_lights && \
+ $ssh homer br -F -F
+elif [ -f /tmp/.tpope_lights ]; then
+ $ssh homer br -n 5
+ rm -f /tmp/.tpope_lights
+ $ssh homer br -n 5
+fi
+
+if [ "$activity" ]; then
+ information="$activity"
+elif [ "$away" ]; then
+ information="$away"
+else
+ case "$alive" in
+ lisa*|abe*) information="On laptop" ;;
+ sarah*) information="In bed" ;;
+ mona*|homer*) information="On desktop" ;;
+ *) information="Away from keyboard?" ;;
+ esac
+fi
+
+if [ -z "$information" ]; then
+ echo > "$HOME/.caminfo.deskcam"
+ echo "- On desktop" > "$HOME/.caminfo.bedcam"
+ #echo "On desktop" > "$HOME/.status"
+ rm -f "$HOME/.plan"
+else
+ echo "- $information" > "$HOME/.caminfo.deskcam"
+ if [ "$information" = "In bed" ]; then
+ echo "" > "$HOME/.caminfo.bedcam"
+ else
+ echo "- $information" > "$HOME/.caminfo.bedcam"
+ fi
+ #echo "$information" > "$HOME/.status"
+ echo "$information" > "$HOME/.plan"
+fi
+
+for host in bart homer sarah clancy; do
+ ping -c 1 $host >/dev/null && scp -q "$HOME/.status" $host: 2>/dev/null
+done
+
+ping -c 1 homer >/dev/null && scp -q "$HOME/.caminfo.deskcam" homer:.caminfo 2>/dev/null
+ping -c 1 sarah >/dev/null && scp -q "$HOME/.caminfo.bedcam" sarah:.caminfo 2>/dev/null
+rm -f .caminfo.bedcam
+
exit 0