New hostnames
[tpope-extra.git] / bin / away-detect
index e7b5052b7302b11aaa4f166caaabb3e1eec34dbd..91bf430929ea6e1887005ce35895ee5b9ad28e49 100755 (executable)
@@ -1,21 +1,37 @@
-#!/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
@@ -50,7 +66,7 @@ set_out() {
 }
 
 set_in() {
-    if [ "`date +%k`" -ge 2 -a "`date +%k`" -le 9 ]; then
+    if [ "`date +%k`" -ge 0 -a "`date +%k`" -le 9 ]; then
        set_smart "$sleeping"
     else
        set_smart "$*"
@@ -71,21 +87,17 @@ today --category=school | sed -e s/^..........// -e 's/ \[.*\]$//'|grep '[0-9][0
 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 - 15`"
-    echo $begin $now $end $max_end
+    begin="`expr $begin - 25`"
     if [ "$begin" -le "$now" -a "$now" -lt "$end" ]; then
-       echo "scheduled=\"Class ($event)\"" >> "$HOME/.away-smart"
+       echo "scheduled=\"Class: $event\"" >> "$HOME/.away-smart"
        break
-    elif [ "$old_away" = "Class ($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"
-       echo HI MOM
     fi
 done
 
-cat ~/.away-smart
-
 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'))"
@@ -102,13 +114,13 @@ done
 [ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart"
 old_sched="$scheduled"
 
-for host in abe homer lisa mona; do
+for host in buster maeby lindsay tobias; 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 -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
+    $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
@@ -142,4 +154,62 @@ echo "old_phone=\"$phone\"" >>~/.away-smart
 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 maeby br -F -F
+elif [ -f /tmp/.tpope_lights ]; then
+    $ssh maeby br -n 5
+    rm -f /tmp/.tpope_lights
+    $ssh maeby br -n 5
+fi
+
+if [ "$activity" ]; then
+    information="$activity"
+elif [ "$away" ]; then
+    information="$away"
+else
+    case "$alive" in
+       lindsay*) information="On laptop" ;;
+       buster*) information="In bed" ;;
+       tobias*|lucille*) 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 gob maeby buster oscar; do
+    ping -c 1 $host >/dev/null && scp -q "$HOME/.status" $host: 2>/dev/null
+done
+
+ping -c 1 lucille >/dev/null && scp -q "$HOME/.caminfo.deskcam" lucille:.caminfo 2>/dev/null
+ping -c 1 buster >/dev/null && scp -q "$HOME/.caminfo.bedcam" buster:.caminfo 2>/dev/null
+rm -f .caminfo.bedcam
+
 exit 0