#!/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.

# Convert all alpha to lowercase
#   require: null
#   args: $1
#   globals: null
#   output: LC_OUT
lowercase(){
  LC_OUT=$(echo $1 | tr [:upper:] [:lower:])
} # END lowercase

# Convert all alpha to uppercase
#  require: null
#  args: $1
#  globals: null
#  output: UC_OUT
uppercase(){
  UC_OUT=$(echo "$1" | tr [:lower:] [:upper:])
}

# Read array element using indirect reference
#  require: message, stop
#  args: $1, $2
#  globals: null
#  output: RA_OUT or warn & exit
read_array(){
  # check array name does not start with number
  if [[ $1 =~ ^[[:alpha:]] ]]; then
    local TMP=$1[$2]
    RA_OUT=${!TMP}
  else # FALSE check array name
    message "$FUNCNAME FAILED: |$1| must start with an alpha character"
    stop 1
  fi # END check array name
} # END read_array

# User must be root
#  require: message, stop
#  args: $1
#  globals: EUID
#  output: pass or warn`& exit
root_user(){
  # current user
  if [ "$EUID" -ne 0 ]; then
    message "$0 $1 must be run as root user."
    stop 1
  fi # END current user
} # END root_user

# Display message with line spacing before and after
#  require: null
#  args: $1
#  globals: null
#  output: message text
message(){
  echo
  echo "$1"
  echo
} # END message


# Exit or warn & exit
#  require: message
#  args: $1
#  globals: DEBUG
#  output: exit
stop(){
  if [ "$DEBUG" = "yes" ]; then
    message "exit status $1"
  fi # END debug

  # exit with status
  exit $1
} # END stop
