8515027c7f0f37ce092782c1338dcbaaa3a2bc25
[tpope-extra.git] / bin / away-actions
1 #!/bin/bash
2 # $Id$
3 # -*- sh -*- vim:set ft=sh sw=4 sts=4:
4
5 # Checks several sources of information and updates my away status
6 # appropriately.  Designed to be periodically called by away-tpope.
7
8 PATH="$HOME/bin:/usr/bin:/bin"
9 ssh="ssh -a -x -oBatchmode=yes -oSetupTimeOut=20"
10 ulimit -S -u 96
11
12 br="tpope br"
13
14 if [ -f /tmp/away-actions.pid ]; then
15     oldpid="`cat /tmp/away-actions.pid`"
16     if kill -0 "$oldpid"; then
17         sleep 20
18         if [ -f /tmp/away-actions.pid ]; then
19             newpid="`cat /tmp/away-actions.pid`"
20             [ "$newpid" = "$oldpid" ] && kill "$newpid"
21             sleep 1
22             if [ -f /tmp/away-actions.pid && "x`cat /tmp/away-actions.pid`" = "x$oldpid" ]; then
23                 kill -9 "$oldpid"
24                 rm /tmp/away-actions.pid
25             elif [ -f /tmp/away-actions.pid ]; then
26                 exit 0
27             fi
28         fi
29     else
30         rm /tmp/away-actions.pid
31     fi
32 fi
33
34 on_exit() {
35     rm /tmp/away-actions.pid
36 }
37 trap on_exit EXIT
38
39 echo "$$" >/tmp/away-actions.pid
40
41 #away="`tpope away`"
42 #activity="`tpope activity`"
43 . "$HOME/.away-tpope"
44 information="$status"
45
46 case "$away" in
47     Sleeping|"Away from keyboard"|TV*|"") is_away=0 ;;
48     *[Cc]lass*) is_away=1 ;;
49 esac
50
51 [ "$phone" = absent -a -z "$alive" ] && is_away=1
52
53 find /tmp -name .tpope_lights -maxdepth 1 -mmin 60 -exec rm {} \;
54 if [ "$is_away" = 1 -a ! -f /tmp/.tpope_lights ]; then
55     touch /tmp/.tpope_lights
56     $br -F -F
57     ulimit -S -u 92
58     for host in buster; do
59         ping -c 1 $host >/dev/null && $ssh $host tpope xssc -lock 2>/dev/null &
60     done
61 elif [ "$is_away" = 0 -a -f /tmp/.tpope_lights ]; then
62     $br -n 4
63     rm -f /tmp/.tpope_lights
64     $br -n 4
65     ulimit -S -u 92
66 #    for host in lucille; do
67 #        ping -c 1 $host >/dev/null && $ssh $host tpope unlock
68 #    done
69 fi
70 ulimit -S -u 96
71
72 if [ "$information" = "On desktop" ]; then
73     echo > "$HOME/.caminfo.deskcam"
74     echo "- On desktop" > "$HOME/.caminfo.bedcam"
75     rm -f "$HOME/.plan"
76 else
77     echo "- $information" > "$HOME/.caminfo.deskcam"
78     if [ "$information" = "In bed" ]; then
79         echo "" > "$HOME/.caminfo.bedcam"
80     else
81         echo "- $information" > "$HOME/.caminfo.bedcam"
82     fi
83     #echo "$information" > "$HOME/.status"
84     echo "$information" > "$HOME/.plan"
85 fi
86
87 for host in michael lucille buster oscar; do
88     ping -c 1 $host >/dev/null && scp -q "$HOME/.status" $host: 2>/dev/null
89 done
90
91 ping -c 1 lucille >/dev/null && scp -q "$HOME/.caminfo.deskcam" lucille:.caminfo 2>/dev/null
92 ping -c 1 buster >/dev/null && scp -q "$HOME/.caminfo.bedcam" buster:.caminfo 2>/dev/null
93 rm -f .caminfo.deskcam .caminfo.bedcam
94
95 exit 0