#!/bin/sh
#check for currently running instance
running=$(ps x | grep "[m]player -slave")
if test -z "$running"; then
	mp3play &
	sleep 2 
	echo pause >> $HOME/tmp/mplayer_fifo
	sleep 1
else
	echo "found old instance, assuming paused"
fi

#get the current time in seconds
t=$(date | awk '{print $4}' | awk 'BEGIN {FS = ":"} {print ($1*60*60+$2*60+$3)}')
#get the time we want the alarm to fire
at=$(echo $1 | awk 'BEGIN {FS = ":"} {print ($1*60*60+$2*60+$3)}')
echo $at minus $t
#compute the delay
delay=$((at-t))
if (($delay<0)); then 
	delay=$((delay+24*60*60))
fi

#now go to sleep for that amount of time
echo sleeping for $delay s or $((delay/60/60)):$((delay/60%60))
echo will wake up at $1
sleep $delay
echo ITS TIME
echo pause >> $HOME/tmp/mplayer_fifo
#wait for user input
read
#stop the music again
if test -z "$running"; then
	echo quit >> $HOME/tmp/mplayer_fifo
else
	echo pause >> $HOME/tmp/mplayer_fifo
fi

