#!/bin/sh

# Play audio script for ClockChimes

# Copyright 2018 - Stu Miller - Colorado, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Manually play chime & strike after input validation
#  require: --test, message, stop, run
#  args: $1, $2, $3
#  globals: DEBUG, TEST_HELP
#  output: LOOP, MINUTE, TEST
test(){
  if [ "$DEBUG" = "yes" ]; then
    message "START $FUNCNAME - input: ARG1:|$1| ARG2:|$2| ARG3:|$3|"
  fi # END debug

  # check for required arguments
  if [ $# -eq 3 ]; then

    # check hour is integer
    if [ "$2" -eq "$2" ] 2>/dev/null; then

      # check hour -le 12
      if [ $2 -le 12 ]; then
        LOOP=$2

      # check hour -le 24
      elif [ $2 -le 24 ]; then
        LOOP=$(($2-12))

      else # FALSE hour -le 12 or 24
        message "$FUNCNAME FAILED: |$2| must be an integer <= 24, see --help"
        stop 1
      fi # END hour -le 12 or 24

    else # FALSE hour is integer
      message "$FUNCNAME FAILED: |$2| must be an integer <= 24, see --help"
      stop 1
    fi # END hour is integer

    # check minute is integer
    if [ "$3" -eq "$3" ] 2>/dev/null; then
      # check minute -le 45
      if [ $3 -le 45 ]; then
        MINUTE=$3
        TEST="yes"
      else # FALSE minute -le 45
        message "$FUNCNAME FAILED: |$3| must be an integer <= 45, see --help"
        stop 1
      fi # END minute -le 45

    else # FALSE minute is integer
      message "$FUNCNAME FAILED: |$3| must be an integer <= 45, see --help"
      stop 1
    fi # END minute is integer

  else # FALSE check for required arguments
    message "$TEST_HELP"
    stop 1
  fi # END check for required arguments

  # process chimes, strike & play
  run

  if [ "$DEBUG" = "yes" ]; then
    message "END $FUNCNAME - output: LOOP:|$LOOP| MINUTE:|$MINUTE| TEST:|$TEST|"
  fi # END debug
} # END test
