#!/bin/bash

# NEWSTYPE is either ARRL or ARN, Always required as parameter 1
#
if [ -z "$1" ]
  then
     echo "No Play type given - ARN or ARRL"
     exit 1
  else
     NEWSTYPE=${1}
     if [ "$NEWSTYPE" != "ARN" ] && [ "$NEWSTYPE" != "ARRL" ]
        then
           echo "Play type must be ARRL or ARN"
           exit 1
     fi
fi

TMPDIR="/tmp"
VOICEDIR="/home/irlp/custom/voice"
MAXPART=`cat /tmp/"$NEWSTYPE"_MAXPART`

# send start announcment
# Add 3 second delay to beginning of text
sleep 3
/usr/local/bin/voicetransmit KD8TUZ_B $VOICEDIR/${NEWSTYPE}start.dvtool

sleep 10

# Start news
PART="1"
while [ "$PART" -le "$MAXPART" ]; do
      echo "Playing file $TMPDIR/$NEWSTYPE.part$PART.dvtool"
      if [ "$PART" -lt  "$MAXPART" ]; then
         # Add Station break message with 1second delay
        /usr/local/bin/voicetransmit KD8TUZ_B $TMPDIR/$NEWSTYPE.part$PART.dvtool
        sleep 1
        /usr/local/bin/voicetransmit KD8TUZ_B $VOICEDIR/${NEWSTYPE}continue.dvtool

        # Break every 2.5 minutes for 20 seconds
         # Note this break has to be long enough for network ID's
         sleep 20
      else
         # Append end message with 2 second delay
        /usr/local/bin/voicetransmit KD8TUZ_B $TMPDIR/$NEWSTYPE.part$PART.dvtool
        sleep 2
        /usr/local/bin/voicetransmit KD8TUZ_B $VOICEDIR/${NEWSTYPE}stop.dvtool
        # Terminate script at end of audio
        #cat $TMPDIR/$NEWSTYPE.part$PART.wav $VOICEDIR/${NEWSTYPE}stop.wav > $TMPDIR/$NEWSTYPE.wav
        LENGTH=`soxi -D $TMPDIR/$NEWSTYPE.wav 2>/dev/null`
         DELAY=${LENGTH/.*}
         sleep $DELAY
      fi
      let "PART += 1"
      if [ "$PART" -lt  "$MAXPART" ]; then
         # Add Station break message with 1 second delay
        /usr/local/bin/voicetransmit KD8TUZ_B $TMPDIR/$NEWSTYPE.part$PART.dvtool
        sleep 1
        /usr/local/bin/voicetransmit KD8TUZ_B $VOICEDIR/${NEWSTYPE}continue.dvtool

        # Break every 2.5 minutes for 20 seconds
         sleep 20
      else
         # Append end message with 2 second delay
        /usr/local/bin/voicetransmit KD8TUZ_B $TMPDIR/$NEWSTYPE.part$PART.dvtool
        sleep 2
        /usr/local/bin/voicetransmit KD8TUZ_B $VOICEDIR/${NEWSTYPE}stop.dvtool
        # Terminate script at end of audio
        cat $TMPDIR/$NEWSTYPE.part$PART.wav $VOICEDIR/${NEWSTYPE}stop.wav > $TMPDIR/$NEWSTYPE.wav
        LENGTH=`soxi -D $TMPDIR/$NEWSTYPE.wav`
         DELAY=${LENGTH/.*}
         sleep $DELAY
      fi
      let "PART += 1"
done

# Remove all files created by playnews
#rm -f $TMPDIR/$NEWSTYPE*

# Done
exit 0
