Locking and miscellaneous changes
[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 if [ -f /tmp/away-actions.pid ]; then
13     oldpid="`cat /tmp/away-actions.pid`"
14     if kill -0 "$oldpid"; then
15         sleep 20
16         if [ -f /tmp/away-actions.pid ]; then
17             newpid="`cat /tmp/away-actions.pid`"
18             [ "$newpid" = "$oldpid" ] && kill "$newpid"
19             sleep 1
20             if [ -f /tmp/away-actions.pid && "`cat /tmp/away-actions.pid`" = "$oldpid" ]; then
21                 kill -9 "$oldpid"
22                 rm /tmp/away-actions.pid
23             elif [ -f /tmp/away-actions.pid ]; then
24                 exit 0
25             fi
26         fi
27     else
28         rm /tmp/away-actions.pid
29     fi
30 fi
31
32 on_exit() {
33     rm /tmp/away-actions.pid
34 }
35 trap on_exit EXIT
36
37 echo "$$" >/tmp/away-actions.pid
38
39 #away="`tpope away`"
40 #activity="`tpope activity`"
41 . "$HOME/.away-tpope"
42 information="$status"
43
44 case "$away" in
45     Sleeping|"Away from keyboard"|TV*|"") is_away=0 ;;
46     *[Cc]lass*) is_away=1 ;;
47 esac
48
49 [ "$phone" = absent -a -z "$alive" ] && is_away=1
50
51 find /tmp -name .tpope_lights -maxdepth 1 -mmin 60 -exec rm {} \;
52 if [ "$is_away" = 1 -a ! -f /tmp/.tpope_lights ]; then
53     touch /tmp/.tpope_lights
54     $ssh homer br -F -F
55     ulimit -S -u 92
56     for host in sarah homer; do
57         ping -c 1 $host >/dev/null && $ssh $host tpope xssc -lock 2>/dev/null &
58     done
59 elif [ "$is_away" = 0 -a -f /tmp/.tpope_lights ]; then
60     $ssh homer br -n 1
61     rm -f /tmp/.tpope_lights
62     $ssh homer br -n 1
63     ulimit -S -u 92
64     for host in lisa; do
65         ping -c 1 $host >/dev/null && $ssh $host tpope unlock
66     done
67 fi
68 ulimit -S -u 96
69
70 if [ "$information" = "On desktop" ]; then
71     echo > "$HOME/.caminfo.deskcam"
72     echo "- On desktop" > "$HOME/.caminfo.bedcam"
73     rm -f "$HOME/.plan"
74 else
75     echo "- $information" > "$HOME/.caminfo.deskcam"
76     if [ "$information" = "In bed" ]; then
77         echo "" > "$HOME/.caminfo.bedcam"
78     else
79         echo "- $information" > "$HOME/.caminfo.bedcam"
80     fi
81     #echo "$information" > "$HOME/.status"
82     echo "$information" > "$HOME/.plan"
83 fi
84
85 for host in bart homer sarah clancy; do
86     ping -c 1 $host >/dev/null && scp -q "$HOME/.status" $host: 2>/dev/null
87 done
88
89 ping -c 1 homer >/dev/null && scp -q "$HOME/.caminfo.deskcam" homer:.caminfo 2>/dev/null
90 ping -c 1 sarah >/dev/null && scp -q "$HOME/.caminfo.bedcam" sarah:.caminfo 2>/dev/null
91 rm -f .caminfo.deskcam .caminfo.bedcam
92
93 exit 0