#! /bin/bash
# Script to interactively function as an alarm making use of
# the user's default cron file. Format for use is
#               alarm <arg>
# where <arg> can be on, off or set.

# Setting pointer variable to cron directory.
  CRON=/mnt/froody/files_linux/applications/cron

# Check input arguments and output error message if none given
  if [ -z ${1} ]; then
    echo ""
    echo "Correct Usage: alarm <OPTION>"
    echo ""
    echo "Where arguments are either off, on or set"
    echo ""
    exit 0
  fi 

# Excerpt for turning the alarm off
  if [ "$1" = off ]; then

  # Flushing crontab list
    crontab -r
 
  # Send Crontab file standard contents to Crond and display
    echo ""
    echo "                  ***** Switching Alarm Off *****"
    echo "                    ***** Crond Resetting *****"
    echo ""

  # Resetting with the user's default crontab file
    crontab $CRON/cron

  # Output Crontab file contents
    crontab -l
    exit 1
  fi 

# Excerpt for turning the alarm on
  if [ "$1" = on ]; then

  # If an alarm setting has already been initialised, use this.
    if [ -f "$CRON/alarm_setting" ]; then

    # Reading in alarm setting from the current config file 
      hour=`grep -i ogg $CRON/alarm_setting | cut --delimiter=' ' -f 2`
      minute=`grep -i ogg $CRON/alarm_setting | cut --delimiter=' ' -f 1`
      echo ""
      echo "                  ***** Switching Alarm On *****"
      echo "           ***** Alarm is currently set for $hour:$minute *****"
      echo ""
      crontab $CRON/alarm_setting
      exit 1
    else
  
    # Echo an error message if the alarm is not currently configured.
      echo ""
      echo "***** Alarm Clock needs initialisation *****"
      echo "Correct Usage: alarm set"
      echo ""
      exit 0
    fi
    exit 1
  fi

# Excerpt for setting the alarm time
  if [ "$1" = set ]; then

  # Read in alarm setting.
    echo "Enter the hour for commencement of really stupid ideas:"
    read hour

  # Checks for non-integer or null input
    test=`echo $hour | grep -v [0-9]`
    while [ -n "$test" ] || [ -z "$hour" ] 
    do
      echo "Enter a valid time smeghead (24hr format):"
      read hour
      test=`echo $hour|grep -v [0-9]`
    done

    echo "and the exact minute to absent-mindedly forget why the alarm was set:"
    read minute

  # Checks for non-integer or null input
    test=`echo $minute | grep -v [0-9]`
    while [ -n "$test" ] || [ -z "$minute" ] 
    do
      echo "Enter a valid value smeghead (0-59 mins):"
      read minute
      test=`echo $minute|grep -v [0-9]`
    done

  # Create Crontab file for the alarm settings
    cat $CRON/cron > $CRON/alarm_setting
    echo "$minute $hour * * * /usr/bin/ogg123 /mnt/music/Rammstein/Sehnsucht/test.ogg & \ 
                   pid=\${!} ; sleep 180 ; kill \$pid" >> \
                   $CRON/alarm_setting

  # Send Crontab file contents to Crond and echo message to screen
    crontab $CRON/alarm_setting
    echo ""
    echo "               ***** Alarm is now set for $hour:$minute *****"
    echo "                ***** Current crontab listing *****"
    echo ""

  # Output Crond Status
    crontab -l
    exit 1
  fi 
