#!/bin/bash
#
# Script to download ARRL News or ARN and divide into 2.5 minute segments with breaks.
# Includes voice messages before play, at breaks, and after play.
# This script can be configured for global playback! 
# DO NOT run this on a multi-node connected circuit without consideration. 
# Change MODE to localplay for strictly local node play.
#
# You can run this script from a cron job or from the command line at least
# 15 minutes before the defined run time (TIME value) but it can be scheduled
# anytime within 24 hours prior to the run time.
#
# cron example -
#
# Prime news for play every Tuesday at 8:30PM - actual playtime set by defined 
# comand line TIME parameter. If Playtime is 9PM (2100) 
# This would send pre warnings at 8:50 and 8:55 PM. 
#
# Start a cron job every tuesday at 8:30 PM to run at 9 PM the same day
# and play ARRL news on node 40000, globally
#
# 30 20 * * 2 /etc/asterisk/playnews ARRL 21:00 40000 G &> /dev/null 2>&1
#
# Play ARN news on Thursday at 7PM on node 40000, Start playnews at 6 PM, Play locally
#
# 00 18 * * 4 /etc/asterisk/playnews ARN 19:00 40000 L &> /dev/null 2>&1

# The audio files ARRLstart5, ARRLstart10, ARRLstart, ARRLcontinue, ARRLstop
# and ARNstart, ARNstart10, ARNstart, ARNcontinue, ARNstop
# are supplied but could be customized for your needs. The audio
# files must be in the directory defined by VOICEDIR
#
# ARRLstart10 or ARNstart10   - voice message at ten minutes before start
# ARRLstart5 or ARNstart5     - voice message at five minutes before start
# ARRLstart or ARNstart       - voice message at start of play
# ARRLcontinue or ARNcontinue - voice message at breaks
# ARRLstop or ARNstop         - voice message at end of play
#
# v0.8 update
#
#   - added check for downloaded MP3 filesize. Playnews will not
#     play if size is less than 100K. On holidays ARRL only has
#     small html file that gets downloaded. 
#
# v0.7 updates
#
#   - Added TMPDIR user settable variable
#
# v0.6 updates
#   - Now requires that all parameters be entered
#     except mode which defualts to global
#   - More parameter checking added
#   - Time can be set to "NOW" for immediate start
#
#   DO NOT use the the "NOW" time parameter in a cron !!!

# The following variable needs to be set if different for your install
#
# VOICEDIR - Directory for playnews voice files
# Usually in the same directory as the playnews script

VOICEDIR="/home/irlp/custom/voice"

# TMPDIR - Directory for temporary file storage
# Note if at all possible this should not be on the SD card.
# Use of /tmp or a USB mounted stick is preferred
# Note that the BBB may not have enough memory in /tmp to process

TMPDIR="/tmp" 

# End User defines

if [ ! -f $VOICEDIR/ARNstart ]
  then
    echo "playnews voice files not found - check VOICEDIR in script"
    exit 1
fi

# 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

# Time to start - 24 hour time - required 2nd command line parameter
# Time example 03:19 = 3:19 AM, 22:45 = 10:45 PM 
#
if [ ${2} != "NOW" ] && [[ !  $2 =~ ^[0-9][0-9]:[0-9][0-9]$ ]]
  then
    echo "No Time supplied - Enter 24 hour time to play as 00:00 - (7 PM = 19:00)"
    exit 1
fi
TIME=$2

# Download Newsline or ARRL  and convert to wav
rm -f $TMPDIR/$NEWSTYPE.mp3
echo "Starting Download of $NEWSTYPE Audio News @ $(date +%H:%M:%S)"

if [ $NEWSTYPE == "ARN" ]
  then 
# Timeout set to 15 seconds with 4 retries. Set the timeout longer for slow circuits.
    wget -T 15 --tries=4 -r -O $TMPDIR/$NEWSTYPE.mp3 http://www.arnewsline.org/storage/audio/news.mp3
else
# not ARN so get ARRL
    wget -T 15 --tries=4 -r -O $TMPDIR/$NEWSTYPE.mp3 http://www.arrl.org/files/file/News/Audio%20News/AAN-$(date --date="last friday" +%Y-%m-%d).mp3
fi

if [ -f $TMPDIR/$NEWSTYPE.mp3 ];
 then
 echo "Download complete @ $(date +%H:%M:%S)"
else
 echo "Download failed"
 exit 0
fi

# On holidays ARRL does not put out a news MP3. Instead there is a 
# shorter HTML file. The following routine checks for this and
# exits if there is no valid MP3 file.

filesize=$(wc -c <$TMPDIR/$NEWSTYPE.mp3)
if [ $filesize -lt 100000 ]
 then
    echo "File size too small for play"
    exit 0
fi

echo "Converting from MP3 to individual ulaw format files"

#Convert mp3 to a wav file
#lame -h --decode $TMPDIR/$NEWSTYPE.mp3 $TMPDIR/"$NEWSTYPE"2.wav
ffmpeg -i $TMPDIR/$NEWSTYPE.mp3 -y -ac 1 -ar 48000 
$TMPDIR/"$NEWSTYPE"2.wav

# Get the length of the file in seconds
LENGTH=`soxi -D $TMPDIR/$NEWSTYPE2.wav`

#echo $LENGTH
START=0

# 2.5 minute = 150 seconds
INCREMENT=150
# Calculate number of segments
MAXPART=`echo $LENGTH/$INCREMENT | bc`
let "MAXPART += 1"
PART="1"
echo "$MAXPART" > /tmp/"$NEWSTYPE"_MAXPART

# Divide into 2.5 minute segments
while [ "$PART" -le "$MAXPART" ]; do
      sox --temp $TMPDIR $TMPDIR/$NEWSTYPE2.wav $TMPDIR/$NEWSTYPE.part$PART.wav trim $START $INCREMENT
      echo "Creating $TMPDIR/$NEWSTYPE.part$PART.wav"
      INCREMENT=155
      START=$(($START-5+$INCREMENT))
#     echo "$PART - $START - $INCREMENT  $(($START+$INCREMENT))"
#      let "PART += 1"
      /usr/local/bin/wav2dvtool $TMPDIR/$NEWSTYPE.part$PART.wav CQCQCQ KD8TUZ_B W8RNL__G "AMATEUR RADIO $NEWSTYPE" $TMPDIR/$NEWSTYPE.part$PART.dvtool
      let "PART += 1"
done

exit 0

