#!/bin/bash
#  1.0   Unknown               Original
#  2.0   24/04/2021  VK2YLD    Updated for Debian-10 SOX version
#
#
#######################################
# MODIFICATIONS TO WAVPLAY SCRIPT TO STOP IT KEYING OVER QSO'S.  
# COSCHECK ONLY WAITS 30 SECONDS, THEN KEYS ANYWAY.
#######################################

#Plays WAV files with check for COS, key, .5 second delay, unkey
#This script can take up to four arguments as wav files without the ".wav"

# Make sure we are user repeater!!!
if [ `/usr/bin/whoami` != "repeater" ] ; then
  echo This program must be run as user REPEATER!
  exit 1
fi

. /home/irlp/custom/environment

# Make sure we have sourced the environment file
if [ "$RUN_ENV" != "TRUE" ] ; then
  echo "You must source the environment file first. Do this by running:"
  echo ". /home/irlp/custom/environment"
  exit 1
fi

if [ "$#" = "0" ] ; then exit 1; fi

#Converts audio to nice congruent audio, without gaps, if there is more
#than one argument in the script.

while [ -n "$1" ];do
  WAVFILES="$WAVFILES $AUDIO/$1.wav"
  shift
done
/usr/bin/sox $WAVFILES $RUN/wavefile.wav

if [ "$OVERRIDE" = "no" ] ; then
  SLEEP="sleep 2"
else
  SLEEP="sleep 1"
fi
echo "OVERRIDE="$OVERRIDE
echo "SLEEP="$SLEEP

$BIN/coscheck
$BIN/coscheck
$BIN/coscheck
$BIN/coscheck
$BIN/coscheck
$BIN/coscheck
$BIN/coscheck
$BIN/coscheck
$BIN/coscheck
$BIN/coscheck
$BIN/key

$SLEEP

$BIN/play $RUN/wavefile.wav >/dev/null 2>&1

$SLEEP

$BIN/unkey

rm -f $RUN/wavefile.wav

exit 0
