#!/bin/sh
#
# Usage: e.g.,  setalarm 10:30, or 
#               setalarm  8:05, or 
#               setalarm 18:15, or
#               setalarm 08:05, but NOT
#                          setalarm 8:5
# Use a 24 hr. clock.
#
# First parse $1 into hours and minutes
a=$1

h=${a:0:2}
t=${h:1:1}
if [ $t == ":" ]
then
  h=${a:0:1}
  m=${a:2:2}
else
  m=${a:3:2}
fi

# Time to set the alarm, in minutes.
T1=$(echo "$h $m" | awk '{a=$1*60 + $2; print a}')

# Synchronize system clock to hardware clock, and 
# set hardware clock to local time.
hwclock --systohc --localtime

# Get current time
H=`date +%H`
M=`date +%M`

# Current time - if alarm time is less than current time
# then the midnight barrier must be crossed.
T0=$(echo "$H $M" | awk '{a=$1*60 + $2; print a}')

if [ $T1 -lt $T0 ] 
then
  echo "Need to jump midnight barrier"
# set alarm for midnight
# rtc wakes up on the 55s mark
# 23:60 will actually wake up at 23:59:55
  rtc 23:60
# first turn off the lcd - save a little power.
  echo 0 > /proc/psionw/lcd
# pause and wait for midnight to pass
  sleep 10
#And set the machine to turn off soon, but save
#the original setting.
  O=`cat /proc/psionw/sleep`
  echo 10 > /proc/psionw/sleep
# now set the true alarm.
  rtc $1 
# after wakeup, restore the original sleep setting
  echo $O > /proc/psionw/sleep
else
  rtc $1
fi
# Now play the alarm

echo "Hit any key to stop alarm!"

# read any stray characters...e.g. the power off key
read -t 1 -n 1 B

let A=0
while [ $A -lt 20 ]; do
 if read -t 1 -n 1 B
    then
    echo "  "
    exec echo "Good Morning!"
 else
    let A=A+1
    echo -n " $A "
    cat /root/sounds/PianoMelody3.al > /dev/audio
    sleep 1
 fi
done

echo "  "
echo "Fine, sleep in then, see if I care."

