#!/bin/sh
#wakeup
#starts moosic in the morning!

#Warning! Has to be added in crontab
#with: crontab -e
#0       8       *       *       0-5     /usr/bin/wakeup
#use above line (remove comment!)

PATH="/bin:/usr/bin"
DISPLAY=":0.0"

#default Values:

STEP_TIME=20
EXIT_TIME=600
FILE=/var/start/wakeup
AWAY=/var/start/away

if [ -f /etc/default/wakeup ]
then
	. /etc/default/wakeup
fi

#when there is a parameter, use
#at to specify another time to
#wakeup tomorrow.
if [ "$1" ]
then
	echo "Using at to execute wakeup"
	#first delete all other wakeup calls!
	echo > $FILE
	atrm `atq -q w | awk '{print $1}'`
	echo wakeup | at -q w "$*"
	exit 0
fi

if [ -f $AWAY ]
then
	echo "User away, will not wakeup"
	exit 0
fi

ACTDATE=`date +%Y-%m-%d`
MAYDATE=`cat $FILE`
ATQDATE=`atq -q w | awk '{print $2}'`

if [ "$ACTDATE" = "$MAYDATE" ]
then
	echo "Already executed, will exit"
	exit 0
elif [ "$ACTDATE" = "$ATQDATE" ]
then
	echo "Will be executed with at today"
	exit 0
fi

#Set a flag that wakeup was started
echo -n $ACTDATE > $FILE

#fange mit minimaler Lautst�rke an!
amixer set PCM 0 &> /dev/null

#starting music
moosic play

for i in `seq 1 25`
do
	sleep $STEP_TIME
	amixer set PCM "$i" &> /dev/null
done

sleep $EXIT_TIME

moosic stop