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

# Edit config file control array after input validation
#  require: --conf, message, stop, root_user, lowercase, uppercase, read_array
#  args: $1, $2, $3, $4, $5
#  globals: DEBUG, CONTROL[], CONF_FILE, CONF_HELP
#  output: conf file
conf(){
  if [ "$DEBUG" = "yes" ]; then
    message "START $FUNCNAME - input: ARG1:|$1| ARG2:|$2| ARG3:|$3| ARG:4|$4| ARG:5|$5|"
  fi # END debug

  # require root
  root_user $1

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

    # convert chime name to uppercase, get chime name [0]
    uppercase $2; read_array $UC_OUT 0

    # check chime name [0] non-null/non-zero
    if [ -n "$RA_OUT" ]; then
      CONTROL[0]=$UC_OUT
    else # FALSE check chime name
      message "$FUNCNAME FAILED: |$UC_OUT| must exist in conf file, see --help"
      stop 1
    fi # END check chime name

    # convert play chime to lowercase
    lowercase $3

    # check play chime yes/no
    if [[ "$LC_OUT" = "yes" || "$LC_OUT" = "no" ]]; then
      CONTROL[1]=$LC_OUT
    else # FALSE check play chime
      message "$FUNCNAME FAILED: |$LC_OUT| must be yes/no, see --help"
      stop 1
    fi # END check play chime

    # convert play strike to lowercase
    lowercase $4

    # check play chime yes/no
    if [[ "$LC_OUT" = "yes" || "$LC_OUT" = "no" ]]; then
      CONTROL[2]=$LC_OUT
    else # FALSE check play strike
      message "$FUNCNAME FAILED: |$LC_OUT| must be yes/no, see --help"
      stop 1
    fi # END check play strike

    # check volume is numeric
    if [[ "$5" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
      CONTROL[3]=$5
    else # FALSE check volume is numeric
      message "$FUNCNAME FAILED: |$5| must be numeric, see --help"
      stop 1
    fi # END check volume is numeric

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

  # quote variables to preserve newlines
  # assign tmp as piped conf file contents with stripped control lines
  local TMP="$(printf "$(<$CONF_FILE)" | grep -v 'CONTROL')"
  # expand escape sequences, append control array to tmp, write to conf file
  printf "%b" "$TMP\nCONTROL=(${CONTROL[*]})\n" > "$CONF_FILE"
  # grep to strip comments & blank lines, write to stdout
  message "SAVED $CONF_FILE

list of conf file variables lines:
$(grep -v -e '#\|^$' "$CONF_FILE")"

  if [ "$DEBUG" = "yes" ]; then
    message "END $FUNCNAME - output: conf file"
  fi # END debug

  stop 0
} # END config
