20110728

wecker / alarm clock


### BEGIN INIT INFO
# Provides:          wecker
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start wecker at boot time
# Description:       Enable service provided by wecker.
### END INIT INFO

#!/bin/sh

PLAYER="/usr/bin/mpg123"
AMIXER="/usr/bin/amixer"
PLAYER_OPTIONS="--random --index --control --list"
# put outside of encrypted home dirs etc. because you want it to start before logging in
PLAYLIST="/var/wecker-muzaq/wecker.playlist" 
SLEEPTIME="5"
# don't run after this hour
TIME_THRESHOLD="22" 
SET_VOL_MASTER="$AMIXER set 'Master',0 75% > /dev/null"
SET_VOL_HEADPHONEJACK="$AMIXER set 'Headphone',0 100% > /dev/null"
SET_VOL_SPEAKERS="$AMIXER set 'Master',0 100% > /dev/null"
SET_VOLUME="$SET_VOL_MASTER; $SET_VOL_HEADPHONEJACK; $SET_VOL_SPEAKERS"
UNMUTE_MASTER="$AMIXER set 'Master',0 unmute > /dev/null"
UNMUTE_HEADPHONEJACK="$AMIXER set 'Headphone',0 unmute > /dev/null"
UNMUTE_SPEAKERS="$AMIXER set 'Speaker',0 unmute > /dev/null"
UNMUTE="$UNMUTE_MASTER; $UNMUTE_HEADPHONEJACK; $UNMUTE_SPEAKERS"
LOGFILE="/var/log/messages"
KILL="/bin/kill -9"
# attached keyboard
SNOOZE_DEV="/dev/input/event3" 
# use script from http://www.thelinuxdaily.com/2010/05/grab-raw-keyboard-input-from-event-device-node-devinputevent/
# to determine device no.

# uncomment echo "EVENT=$EVENT" below to see what the different codes are 
SNOOZE_KEY="0039" # spacebar
STOP_KEY="000e" # backspace

if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root!" 1>&2
   exit 1
fi

# got this sub from somewhere; what exactly does this do and how?
# can't use keyword 'function' b/c it's not recognised by /bin/sh
getkey () 
{
 KEYPRESS=$(dd if=$SNOOZE_DEV bs=16 count=1 2>/dev/null | od -x | grep -v 0000020)
 #echo $KEYPRESS
 echo $KEYPRESS > /tmp/event.tmp
 EVENT=$(cut -b 39-42 /tmp/event.tmp)
 rm /tmp/event.tmp
}

case "$1" in
  start)
    TIME=$(date +%k)
    if [ $TIME -lt $TIME_THRESHOLD ]; then
     $UNMUTE
     $SET_VOLUME 
 $PLAYER $PLAYER_OPTIONS $PLAYLIST &
 if [ $? -eq 0 ]; then
  echo "$(date) started wecker" >> $LOGFILE
 fi
 while true; do
  getkey
  #echo "EVENT=$EVENT"
  case $EVENT in
   $SNOOZE_KEY)
    $KILL $(pidof $PLAYER)
    echo "$(date) snoozing wecker for $SLEEPTIME" >> $LOGFILE
    sleep $SLEEPTIME
    $PLAYER $PLAYER_OPTIONS $PLAYLIST &
    if [ $? -eq 0 ]; then
     echo "$(date) restarted wecker after having snoozed $SLEEPTIME" >> $LOGFILE
    fi
    ;;
   $STOP_KEY)
    $KILL $(pidof $PLAYER)
    if [ $? -eq 0 ]; then
     echo "$(date) stopped wecker" >> $LOGFILE
    fi
    exit 1
    ;;
  esac

 done
    fi
    ;;
  stop)
    echo -n "Stopping wecker init script"
    $KILL $(pidof $PLAYER)
    if [ $? -eq 0 ]; then
  echo "stopped wecker" >> $LOGFILE
 fi
    ;;
  *)
    echo "Usage: /etc/init.d/wecker {start|stop}"
    exit 1
    ;;
esac

exit 0

Keine Kommentare:

Kommentar veröffentlichen