X-Git-Url: http://git.tpope.net/?p=tpope-extra.git;a=blobdiff_plain;f=bin%2Faway-detect;h=91bf430929ea6e1887005ce35895ee5b9ad28e49;hp=9764f75c90d9218950571500f0bf502d5da02d20;hb=4d090af17bfa476efbfddb0257c21d1c13a47b14;hpb=d511f31eef187354e7785ad553f8efc813297fd3 diff --git a/bin/away-detect b/bin/away-detect index 9764f75..91bf430 100755 --- a/bin/away-detect +++ b/bin/away-detect @@ -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 @@ -32,8 +48,9 @@ trap 'rm -f "/tmp/LCK..`basename $0`"' EXIT 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="$*" } @@ -43,16 +60,13 @@ set_custom() { } 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 "$*" @@ -69,24 +83,22 @@ set_active() { now="$(expr 60 \* $(date +%H) + $(date +%M))" -if ! today --category='holiday' | grep '' >/dev/null; then -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]'| \ +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" - begin="`expr $begin - 15`" - end="`expr $begin / 2 + $end / 2`" + 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=\"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" fi done -fi -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'))" @@ -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