]> git.sthu.org Git - shutils.git/blob - X/tp_alert.sh
vim: Use FSwitch plugin
[shutils.git] / X / tp_alert.sh
1 #!/bin/bash
2 #
3 # Stefan Huber <shuber2@gmail.com>
4 #
5 # ChangeLog:
6 # * 2009-07-24: Initial version
7 # * 2010-10-09: Use more shell commands
8
9
10 # Wait this amount of time until we activate lock
11 INITWAIT="0"
12 # Wait this amount of time after an alert call
13 ALERTPOSTWAIT="2"
14 # Busy wait for polling position sensor
15 BUSYWAIT="0.2"
16 # Tolerance for sensor differences
17 TOLERANCE="5"
18 # Position file
19 POSFILE="/sys/devices/platform/hdaps/position"
20
21
22 #Set the paths
23 export PATH="/bin:/usr/bin"
24 export DISPLAY=":0"
25
26 state=0
27 alert() {
28 amixer -q sset Master 100%,100% unmute
29 echo "Alert $state at time `date`"
30
31 case $state in
32 0)
33 echo "Leave me alone!" | festival --tts
34 state=1
35 ;;
36 1)
37 echo "E-mail sent!" | festival --tts
38 sendemail
39 state=2
40 ;;
41 *)
42 sendemail
43 while isActive; do
44 echo "HELP!" | festival --tts
45 done
46 return
47 ;;
48 esac
49 sleep $ALERTPOSTWAIT
50 }
51
52 sendemail() {
53 /usr/sbin/sendmail $USER <<EOF
54 Subject: tp_alert.sh alarm on `hostname`
55 From: shuber@`hostname`
56 To: shuber@`hostname`
57
58 This is an automatic mail from the tp_alert.sh script:
59 state=$state
60 time=`date`
61 EOF
62 }
63
64 isActive() {
65 if "`qdbus org.freedesktop.ScreenSaver /ScreenSaver GetActive`" \
66 = "true"; then
67 return 0
68 fi
69 return 1
70 }
71
72 getPosition() {
73 pos=`cat $POSFILE`
74 xtilt=${pos%,*}
75 xtilt=${xtilt#(}
76 ytilt=${pos#*,}
77 ytilt=${ytilt%)}
78 }
79
80
81
82
83 if [ ! -e $POSFILE ]; then
84 echo "HDAPS position file not existing!"
85 return -1
86 fi
87
88
89 echo "Launched at time `date`"
90 #Initial wait
91 sleep $INITWAIT
92
93
94
95 #Wait for trigger to activate alertion system
96 while ! isActive; do
97 sleep $BUSYWAIT
98 done
99
100
101 echo "Activated."
102 echo "Activated." | festival --tts
103
104
105 #Get initial positions
106 getPosition
107 initxtilt=$xtilt
108 initytilt=$ytilt
109
110 #Now wait until alertion system is deactivated
111 while isActive; do
112 getPosition
113 diffx=`echo "$xtilt-$initxtilt" | bc`
114 diffy=`echo "$ytilt-$initytilt" | bc`
115 diffx=${diffx#-}
116 diffy=${diffy#-}
117
118 if [[ $diffx -ge $TOLERANCE || $diffy -ge $TOLERANCE ]]; then
119 alert
120 fi
121 sleep $BUSYWAIT
122 done
123
124 echo "Unlocked at time `date`."
125 echo "Unlocked with state $state." | festival --tts
126