+#!/bin/bash
+#
+# Stefan Huber <shuber2@gmail.com>
+#
+# ChangeLog:
+# * 2009-07-24: Initial version
+# * 2010-10-09: Use more shell commands
+
+
+# Wait this amount of time until we activate lock
+INITWAIT="0"
+# Wait this amount of time after an alert call
+ALERTPOSTWAIT="2"
+# Busy wait for polling position sensor
+BUSYWAIT="0.2"
+# Tolerance for sensor differences
+TOLERANCE="5"
+# Position file
+POSFILE="/sys/devices/platform/hdaps/position"
+
+
+#Set the paths
+export PATH="/bin:/usr/bin"
+export DISPLAY=":0"
+
+state=0
+alert() {
+ amixer -q sset Master 100%,100% unmute
+ echo "Alert $state at time `date`"
+
+ case $state in
+ 0)
+ echo "Leave me alone!" | festival --tts
+ state=1
+ ;;
+ 1)
+ echo "E-mail sent!" | festival --tts
+ sendemail
+ state=2
+ ;;
+ *)
+ sendemail
+ while isActive; do
+ echo "HELP!" | festival --tts
+ done
+ return
+ ;;
+ esac
+ sleep $ALERTPOSTWAIT
+}
+
+sendemail() {
+ /usr/sbin/sendmail $USER <<EOF
+Subject: tp_alert.sh alarm on `hostname`
+From: shuber@`hostname`
+To: shuber@`hostname`
+
+This is an automatic mail from the tp_alert.sh script:
+ state=$state
+ time=`date`
+EOF
+}
+
+isActive() {
+ if "`qdbus org.freedesktop.ScreenSaver /ScreenSaver GetActive`" \
+ = "true"; then
+ return 0
+ fi
+ return 1
+}
+
+getPosition() {
+ pos=`cat $POSFILE`
+ xtilt=${pos%,*}
+ xtilt=${xtilt#(}
+ ytilt=${pos#*,}
+ ytilt=${ytilt%)}
+}
+
+
+
+
+if [ ! -e $POSFILE ]; then
+ echo "HDAPS position file not existing!"
+ return -1
+fi
+
+
+echo "Launched at time `date`"
+#Initial wait
+sleep $INITWAIT
+
+
+
+#Wait for trigger to activate alertion system
+while ! isActive; do
+ sleep $BUSYWAIT
+done
+
+
+echo "Activated."
+echo "Activated." | festival --tts
+
+
+#Get initial positions
+getPosition
+initxtilt=$xtilt
+initytilt=$ytilt
+
+#Now wait until alertion system is deactivated
+while isActive; do
+ getPosition
+ diffx=`echo "$xtilt-$initxtilt" | bc`
+ diffy=`echo "$ytilt-$initytilt" | bc`
+ diffx=${diffx#-}
+ diffy=${diffy#-}
+
+ if [[ $diffx -ge $TOLERANCE || $diffy -ge $TOLERANCE ]]; then
+ alert
+ fi
+ sleep $BUSYWAIT
+done
+
+echo "Unlocked at time `date`."
+echo "Unlocked with state $state." | festival --tts
+