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

# Play chime & strike variables
#  require: message
#  args: null
# globals: DEBUG, CHIME, STRIKE, TEST
#  output: sox audio
play_chimestrike(){
  if [ "$DEBUG" = "yes" ]; then
    message "START $FUNCNAME - input: CHIME:|$CHIME| STRIKE:|$STRIKE| TEST:|$TEST|"
  fi # END debug

  # check chimestrike not null
  if [ "$CHIME$STRIKE" != "" ]; then
    # determine user based on 1st local display
    # last - reads /var/log/wtmp file
    # grep - looks for ":0" followed by "still logged in"
    # awk - uses 1st line and 1st field, ignores remaining lines
    local USER=$(last | grep -E :0.*"still logged in" | awk 'NR=1{print $1}')

    # play as default root user or user currently logged in
    # for play commands, see: man sox
    # functionaly equivalent to: sox -q $CHIME$STRIKE -d
    if [[ "$USER" = "" || "$USER" = "root" ]]; then
      # play as root
      play -q $CHIME$STRIKE

    else # FALSE play as root
      # display password notice
      if [ "$TEST" = "yes" ]; then
        message "NOTE: requires logged in user password if NOT running as root"
      fi # END display password notice
      # play as user currently logged in
      su -l $USER -c "play -q $CHIME$STRIKE"
    fi # END play as root or user
  fi # END chimestrike not null

  if [ "$DEBUG" = "yes" ]; then
    message "END $FUNCNAME - output: sox audio"
  fi # END debug
} # END play_chimestrike

# Build strike variable
#  require: message, read_array
#  args: null
#  globals: DEBUG, CONTROL[], MINUTE, LOOP, PRE_SOUND
#  output: STRIKE
set_strike(){
  if [ "$DEBUG" = "yes" ]; then
    message "START "$FUNCNAME" - input: MINUTE:|$MINUTE| LOOP:|$LOOP|"
  fi # END debug

  # check play strike
  if [ "${CONTROL[2]}" = "yes" ]; then

    # check min 00
    if [[ "$MINUTE" = "00" || "$MINUTE" = "0" ]]; then

      # set strike to play, minus last
      while [ "$LOOP" -gt "1" ]; do
        # concatenate pre & strike
        read_array ${CONTROL[0]} 4
        STRIKE=$STRIKE" "$PRE_SOUND$RA_OUT
        # decrement loop counter
        LOOP=$(($LOOP-1))
      done # END strike loop

      # concatenate last pre & strike
      read_array ${CONTROL[0]} 5
      STRIKE=$STRIKE" "$PRE_SOUND$RA_OUT
    fi # END min 00
  fi # END play strike

  if [ "$DEBUG" = "yes" ]; then
    message "END $FUNCNAME - output: STRIKE:|$STRIKE|"
  fi # END debug
} # END set_strikes

# Assign chime variable
#  require: message, lowercase, read_array
#  args: null
#  globals: DEBUG, CONTROL[], PRE_SOUND, MINUTE
#  output: PRE_SOUND, CHIME
set_chime(){
  if [ "$DEBUG" = "yes" ]; then
    message "START $FUNCNAME - input: null"
  fi # END debug

  # append sound file directory 
  lowercase ${CONTROL[0]}
  PRE_SOUND=$PRE_SOUND$LC_OUT"/"

  # check build chime
  if [ "${CONTROL[1]}" = "yes" ]; then

    # check minute on the hour
    if [[ "$MINUTE" = "00" || "$MINUTE" = "0" ]]; then
      read_array ${CONTROL[0]} 0
      CHIME=$PRE_SOUND$RA_OUT

    # check minute on quarter hour
    elif [ "$MINUTE" = "15" ]; then
      read_array ${CONTROL[0]} 1
      CHIME=$PRE_SOUND$RA_OUT

    # check minute on the half hour
    elif [ "$MINUTE" = "30" ]; then
      read_array ${CONTROL[0]} 2
      CHIME=$PRE_SOUND$RA_OUT

    # check minute on the three quarter hour
    elif [ "$MINUTE" = "45" ]; then
      read_array ${CONTROL[0]} 3
      CHIME=$PRE_SOUND$RA_OUT
    fi # END minute on the hour, quarter, half hour, thre quarter hour
  fi # END build chime

  if [ "$DEBUG" = "yes" ]; then
    message "END $FUNCNAME - output: CHIME:|$CHIME|"
  fi # END debug
} # END set_chime

# Run core chime, strike and play functions
#  require: --run, message, set_chime, set_strike, play_chimestrike
#  args: null
#  globals: DEBUG
#  output: null
run(){
  if [ "$DEBUG" = "yes" ]; then
    message "START "$FUNCNAME" - input: null"
  fi # END debug

  set_chime
  set_strike
  play_chimestrike

  if [ "$DEBUG" = "yes" ]; then
    message "END "$FUNCNAME" - output: null"
  fi # END debug
} # END run
