#!/bin/bash
########################################################################
# filename:	getnews
#
# description:	This script will get the latest copy of the WIA Audio
#		news, VK4_Qnews, AR newsline, ARRL AudioNews, RSGB News,
#               VK5 & VK7 local news and Old Timers Club News.
#
#	     -	This gets run by a cron job, 08:05am on sunday morning.
#               08 05 * * 0 (/home/irlp/custom/getnews all)
#
#	     -	RAOTC is monthly, so cron'ed for a Tuesday download and
#               no big deal if the same file is re-downloaded.
#               05 15 * * 2 (/home/irlp/custom/getnews raotc)
#
#            -  TWIAR is updated on a cycle that doesn't seem to match ours
#               in Aus.  The same edition gets downloaded on Sunday as we had
#               last week, then next Sunday, we skip an edition.. (UTC +11 here)
#               06 05 * * 1 (/home/irlp/custom/getnews twiar)
#
#
# arguments:	'wia, q, arnl, arrl, rsgb, vk5, vk7, twiar, raotc, nzart, all'
#		     on the command line
#
#
# output files:	$NEWS/wianews.mp3
# 		$NEWS/qnews.mp3
# 		$NEWS/arnl.mp3
# 		$NEWS/arrlan.mp3
# 		$NEWS/rain.mp3
# 		$NEWS/rain_report.mp3
#		$NEWS/rsgban.mp3
#		$NEWS/raotc.mp3
#		$NEWS/twiar.mp3
#         $NEWS/vk5.mp3
#         $NEWS/vk7.mp3
#         $NEWS/nzart.mp3
#	if archive="yes" also saves...
#         $archiveloc/wianews_ddmmyyy.mp3 (dmy of download)
#         $archiveloc/vk4_qnews_ddmmyyy.mp3 (dmy of download)
#         $archiveloc/ReportNNNN.mp3 (NNNN edition number)
#         $archiveloc/TWIARNNNN.mp3 (NNNN edition number)
#         $archiveloc/rain-dd_mmmmm_yyyy.mp3
#         $archiveloc/vk5_local-dd_mmmmm_yyyy.mp3
#         $archiveloc/vk7_regional-dd_mmmmm_yyyy.mp3
#         $archiveloc/RSGB-GB2RS-dd_mmmmm_yyyy.mp3
#         $archiveloc/AAN-yyyy-mm-dd.mp3
#         $archiveloc/raotc-yyyy_dd_mmmmm.mp3
#         $archiveloc/nzart_mmmmm_yyyy.mp3
#
#	** for $NEWS and $archiveloc, see config section below **
#
# history:
# 20080704   vk8sj   Rehash of getTWIAR script to download WIA and Q
#                    weekly news from www.wiaq.com as well as TWIAR
# 20090227   vk8sj   Another twist, set to use FTP now to get file
#                    from ftp.wiaq.com - needs YOUR email as passwd.
#                    Falls back to HTTP if FTP fails (dodgy servers)
# 20130428   vk2yld  Rehash for new wia address and remove FTP calls
#                    to streamline operation.  TWIAR disabled as in
#                    recess for a while (permanently??)
# 20130429   vk2yld  added AR Newsline and ARRL Audio News routines
# 20130512   vk2yld  added RSGB download routine & tidyup some mess.
# 20130526   vk2yld  fixed RSGB as podcast page out of step..
# 20130730   vk2yld  fixed RSGB, again!
# 20130822   vk2yld  fixed cleanup routine on arnl as it was leaving
#                    some working files behind
# 20130922   vk2yld  ARRL Audio News filename changed...
# 20130929   vk2yld  AR Newsline file location changed...
# 20150214   vk2yld  Remove TWIAR and TWIARI as both programs are
#                    now defunkt. -=Will be sadly missed=-
# 20150426   vk2yld  ARNL download fails again. fixed
# 20151220   vk2yld  ARNL download fails yet again. Maybe they should
#                    stop stuffing around with it!! RIP Bill. - fixed
# 20160515   vk2yld  ARRL Audio News filename and webpage changed...
# 20160610   vk2yld  Removed last bits of code from TWIAR program
# 20160610   vk2yld  Insert code for rain report, instead of using the
#                    stand-alone downloader.. Now all the news's are
#                    in here.  I will add more as necessary
# 20160623   vk2yld  Insert new code for RSGB Audio News, instead of
#                    4 files, uses podcast from new site.  Old code
#                    now been removed.
# 20160628   vk2yld  move download routines to functions to allow
#                    command line option 'all'
# 20170128   vk2yld  RSGB padded out file with whitespace crashing
#                    script - fixed field select with awk in lieu cut
# 20170226   vk2yld  AR Newsline changed to HTTPS site completely
#                    screwing up the works.  ARNL section had to be
#                    re-written to fix the mess...
# 20170305   vk2yld  AR Newsline changed by adding a banner to the page
#                    for automated download.  File has no ID3 tag to get
#                    the report number, so just fixed the podcast ripper
# 20170319   vk2yld  AR Newsline changed again..  I don't believe this..
#                    Now the filename has changed and presented in a
#                    different code format AGAIN.. Altered error routine
#                    so script continues if errors found.
# 20170326   vk2yld  AR Newsline changed again..  I STILL don't believe this..
#                    Now the filename has shifted from the front page to a
#                    'read more' link and then the audio on this page..
#                    different code format AGAIN...
# 20170409   vk2yld  AR Newsline changed again..  Altered archive routine..
# 20180326   vk2yld  Yep, AR again..  rehashed routines completely again
# 20180408   vk2yld  RSGB routine tidyup for CentOS-5 errors
# 20180420   vk2yld  ARNL again.. endless error loop fixed..
# 20181013   vk2yld  ARNL again.. formatting changed.. fixed..
# 20181118   vk2yld  ARNL again again..  Website gone to HTTPS and
#                    using a self-signed cert plus total page changes
#                    requiring a complete re-hash..
# 20181120   vk2yld  Added function to get RAOTC monthly news program.
# 20181213   vk2yld  Cleanup of logging details
# 20190303   vk2yld  Update RSGB podcast as page has changed again..
# 20190320   vk2yld  change routines to use curl as wget does
#                    not support HTTP2.. crashes on CentOS-6 too..
# 20190423   vk2yld  Guess what...  ARNL again..  Formatting altered yet
#                    again.. changed test to hopefully follow changes.
# 20190818   vk2yld  ARRL this time..  Formatting altered on report page
#                    but index is OK.. changed test to follow change.
# 20190830   vk2yld  Rain report ceased production, removed from download
# 20190901   vk2yld  reintroduced TWIAR download from podcast..
# 20191027   vk2yld  TWIAR download now from static file on twiar.net..
#                    but excluded from 'all' download as usually updated
#                    later than Sunday morning in Aus when 'all' is scheduled.
# 20200301   vk2yld  ARRL format changed.  Index OK.  Test adjusted to suit.
# 20200712   vk2yld  Added routines for NZART download
# 20210103   vk2yld  ARNL again.. formatting changed.. fixed..
# 20211030   vk2yld  RSGB index page changed layout.. fixed..
# 20220515   vk2yld  RSGB index page changed layout again.. fixed again..
# 20220601   vk2yld  Reinstated Rain Report (Classic Rain)
#                     With thanks - Steve K4KSA :-)
# 20230319   vk2yld  Altered ARNL as the index page has changed again...
# 20250512   vk2yld  updated for new RAOTC website layout
# 20260117   vk2yld  block out classic rain as no longer available
# 20260208   vk2yld  Update for ARRL Audio News shifted files again...
# 20260405   vk2yld  Update as ARRL audio news URL changed yet again..
#
################# CONFIGURATION ########################################
#
# Setup some configuration variables..
#
export TERM=vt100                        # Set terminal type for Lynx
NEWS="/home/irlp/audio/custom/news"      # Define where news files stored
archive="yes"                            # Keep an archive? yes/no
archiveloc="/home/news_archive"          # Location of archive folder
#
########################################################################
#  --- Should not need changes below here ---
#
#
process="Start"                          # What we are doing at the moment
# Make sure we are user repeater!!!
   if [ "`/usr/bin/whoami`" != "repeater" ] ; then
     echo "This program must be run as user REPEATER!"
     exit 1
   fi
# Make sure we have sourced the environment file
   if [ "$RUN_ENV" != "TRUE" ] ; then
      . /home/irlp/custom/environment
   fi
#
########################################################################
#
### Error Out Of Script
#
error() {
echo
echo "An error has occurred downloading the file!"
writelog "Getnews: ERROR - $process, File download error."
error="YES"
case $1 in
1)	touch $NEWS/wianews.mp3
	touch $NEWS/error-wia ;;

2)	touch $NEWS/qnews.mp3
	touch $NEWS/error-q ;;

3)	touch $NEWS/arnl.mp3
	touch $NEWS/error-arnl ;;

4)	touch $NEWS/arrlan.mp3
	touch $NEWS/error-arrl ;;

5)	touch $NEWS/rain.mp3
	touch $NEWS/error-rain ;;

6)	touch $NEWS/rsgban.mp3
	touch $NEWS/error-rsgb ;;

7)	touch $NEWS/vk5.mp3
	touch $NEWS/error-vk5 ;;

8)	touch $NEWS/vk7.mp3
	touch $NEWS/error-vk7 ;;

9)	touch $NEWS/raotc.mp3
	touch $NEWS/error-raotc ;;

10)	touch $NEWS/twiar.mp3
	touch $NEWS/error-twiar ;;

11)	touch $NEWS/newswest.mp3
	touch $NEWS/error-newswest ;;

12)	touch $NEWS/nzart.mp3
	touch $NEWS/error-nzart ;;

*)	exit 100 ;;
esac

}

########################################################################
#
### Logging ..
#
writelog() {                         # write string to logfile
  MESSAGE="`date '+%b %d %Y %T'` "$@
  if [ -n "$LOGFILE" ]; then
    echo $MESSAGE >> $LOGFILE
  fi
}
echolog() {                          # write string to screen and logfile
  echo $@
  MESSAGE="`date '+%b %d %Y %T'` Getnews: "$@
  if [ -n "$LOGFILE" ]; then
    echo $MESSAGE >> $LOGFILE
  fi
}

########################################################################
#
### Usage message
#
usage() {
  echo
  echo " Usage ....   getnews <bulletin> "
  echo
  echo "         wia - Gets WIA news broadcast"
  echo "     wia_pod - Gets WIA news from Podcast (64k)"
  echo "           q - Gets WIA-Qld news broadcast"
  echo "        arnl - Gets Amateur Radio NewsLine (gets podcast in one piece)"
  echo "        arrl - Gets ARRL Audio News"
  echo "        rsgb - Gets RSGB Audio News (single file)"
#        echo "        rain - Gets Classic Rain report (RAIN had ended)"
  echo "         vk5 - Gets latest VK5 Local news"
  echo "         vk7 - Gets latest VK7 Local news"
  echo "       twiar - Gets latest TWIAR Static Upload"
  echo "   twiar_pod - Gets latest TWIAR from the podcast version"
  echo "       raotc - Gets RAOTC monthly news program"
  echo "        west - Gets latest Newswest program"
  echo "       nzart - Gets latest New Zealand program"
  echo "     tuesday - Gets RAOTC followed by TWIAR (usual tuesday downloads)"
  echo "         all - Get the lot, except RAOTC & TWIAR, one after the other..."
  echo " Exiting now.."
  echo
  exit 1
}

########################################################################
#
### Fetch the latest WIA news programme
#
get_wia() {
  echolog "WIA News file download script active"
# remove old file...
  rm wianews.mp3 error-wia > /dev/null 2>&1
  echo
  process="Get WIA News Programme File"
  if [ "$forcepod" = "YES" ]; then
    echo " Forced podcast download."
    get_wia_podcast || error1
    unset forcepod
   else
    echo "Getting 128K file from - http://www.wiaq.org.au/ftp/wianews_128.mp3"
    echo "  or backup file from - http://www.vk7ax.id.au/wianews/wianews_128.mp3"
    echo "  or fallback to the 64K version - http://www.wiaq.org.au/ftp/wianews_64.mp3"
    echo "  or backup 64K version - http://www.vk7ax.id.au/wianews/wianews_64.mp3"
    echo "  or, if all that fails, try for the podcast then give up."; echo
#    /usr/bin/curl -sSkLO http://www.wiaq.org.au/ftp/wianews_128.mp3 || \
#        /usr/bin/curl -sSkLO http://www.vk7ax.id.au/wianews/wianews_128.mp3 || \
#        /usr/bin/curl -sSkLO http://www.wiaq.org.au/ftp/wianews_64.mp3 || \
#        /usr/bin/curl -sSkLO http://www.vk7ax.id.au/wianews/wianews_64.mp3 || \
#        get_wia_podcast || error 1
    /usr/bin/curl -sSkLO http://www.vk7ax.id.au/wianews/wianews_128.mp3 || \
        /usr/bin/curl -sSkLO http://www.wiaq.org.au/ftp/wianews_64.mp3 || \
        /usr/bin/curl -sSkLO http://www.vk7ax.id.au/wianews/wianews_64.mp3 || \
        get_wia_podcast || error 1
  fi
  if [ "$error" = "YES" ]; then
     unset error
     return 1
  fi
  if [ -f wianews_128.mp3 ]; then
     mv wianews_128.mp3 wianews.mp3
     echolog "Got WIA News @128K     ...   [ COMPLETED ]"
  else
     mv wianews_64.mp3 wianews.mp3
     echolog "Got WIA News @64K      ...   [ COMPLETED ]"
  fi
  if [ "$archive" = "yes" ]; then
    echo "Date data for file - "$DATENOW
    echo "Archiving a copy of downloaded file.."
    if [ -f $archiveloc/WIA/wianews_$DATENOW.mp3 ] ; then
      echolog "ERROR.. - FIle wianews_$DATENOW - Archive Exists."
      return 1
    fi
    cp $NEWS/wianews.mp3 $archiveloc/WIA/wianews_$DATENOW.mp3
    echolog "Completed - File wianews_$DATENOW - Archived."
  fi
}

### Fetch latest WIA news via Podcast
#  This is the last resort, and will result in fetching
#  the last news file that was uploaded..
#
get_wia_podcast() {
  echolog "Attempting to fetch WIA podcast - Activated."
  echo "Download index file from server - http://www.wia.org.au/members/broadcast/wianews"
  process="Get WIA Podcast"
  /usr/bin/lynx -source http://www.wia.org.au/members/broadcast/wianews > wia_dump || error 1
  if [ "$error" = "YES" ]; then
     unset error
     return 1
  fi
  echo " Index Download Completed"
  wia_pod=`grep -i "\.mp3" wia_dump | head -n1 | awk '{print $3}' | cut -d"\"" -f2`
  process="Get WIA Programme File - $wia_pod"
  echo "Fetching $wia_pod from server."
  /usr/bin/curl -sSkLO $wia_pod || error 1
  if [ "$error" = "YES" ]; then
     return 1
  fi
  echo "Done."
  rm -f wia_dump
  file=`echo $wia_pod | cut -d"/" -f5`
  echo "Located "$NEWS"/"$file
  mv $file wianews_64.mp3                           # Podcast is at 64kb
}

########################################################################
#
### Fetch latest Q-News programme
#
get_q() {
  echolog "VK4 Q-News file download script active"
# remove old file...
  rm qnews.mp3 error-q > /dev/null 2>&1
  echo
  process="Get Q-News Programme File"
  echo "Get file from Webserver - http://www.wiaq.org.au/ftp/vk4_qnews_64.mp3"
  echo "or from backup Webserver - http://www.vk7ax.id.au/wianews/vk4_qnews_64.mp3"
#  /usr/bin/curl -sSkLO http://www.wiaq.org.au/ftp/vk4_qnews_64.mp3 || \
#	/usr/bin/curl -sSkLO http://www.vk7ax.id.au/wianews/vk4_qnews_64.mp3 || error 2
  /usr/bin/curl -sSkLO http://www.vk7ax.id.au/wianews/vk4_qnews_64.mp3 || error 2
  if [ "$error" = "YES" ]; then
     unset error
     return 2
  fi
  echolog "Got Q-News    ...              [ COMPLETED ]"
  mv vk4_qnews_64.mp3 qnews.mp3
  #
  if [ "$archive" = "yes" ]; then
    echo "Date data for file - "$DATENOW
    echo "Archiving a copy of downloaded file.."
    if [ -f $archiveloc/VK4/vk4_qnews_$DATENOW.mp3 ] ; then
      echolog "ERROR.. File vk4_qnews_$DATENOW already archived."
      return 2
    fi
    cp $NEWS/qnews.mp3 $archiveloc/VK4/vk4_qnews_$DATENOW.mp3
    echolog "Completed - File vk4_qnews_$DATENOW archived."
  fi
}

########################################################################
#
### Fetch the Amateur Radio Newsline (Podcast)
#
#      echoproducer fallback file stays the same every week
#                      - http://arnewsline7.squarespace.com/s/news.mp3
#                      - https://www.arnewsline.org/s/news.mp3
#      but this file has no ID-Tag or reference to the edition number,
#      and is crap quality (be OK to rebradcast on HF!) at 32kb/s...
#      No good for archiving unless we add a date or something else.
#
get_arnl() {
  echolog "Downloading AR Newsline Programme - Activated."
# remove old file...
  rm -f arnl* error-arnl > /dev/null 2>&1
  echo
  process="Get AR Newsline Index File"
  echo "Download index file from server - https://www.arnewsline.org"
  base_addr="https://www.arnewsline.org"
# get index from website
  /usr/bin/curl -sSkLo ardump $base_addr || error 3
  echo " Index Download Completed"
  if [ "$error" = "YES" ]; then
     unset error
     return 3
  fi
# find file for download
#  line=`grep AUDIO ardump | grep -v script | head -n1 | grep -oE "href=\"\/s\/Re.*\.mp3\"" | cut -d"\"" -f2`
#  line=`grep AUDIO ardump | head -n1 | grep -oE "href=\"\/s\/.*\.mp3\"" | cut -d"\"" -f8`
   line=`grep AUDIO ardump | head -n1 | grep -oE "href=\"\/s\/Re.*\.mp3\"" | cut -d"\"" -f2`
  line="$base_addr$line"

# find actual 'file name' from path
  f=1
  while true ; do
     arfile=`echo $line | cut -d"/" -f$f`
     if [ -z `echo $arfile | grep -i mp3` ]; then
        let f+=1
        if [ $f -gt 30 ]; then
           error 3            # line error maybe??
           echo "ERROR.. Invalid filename in text."
           break
        fi
      else
        break
     fi
  done
  if [ "$error" = "YES" ]; then
     unset error
     return 3
  fi
# arfile now contains filename without path...
  echo "Found :- $line in index text."
  echo "File to download = "$arfile
  echo
  process="Get AR Newsline Programme File - $arfile"
   /usr/bin/curl -sSkLo arnl.mp3 $line || error 3
  echo " Download Completed"
  if [ "$error" = "YES" ]; then
     unset error
     return 3
  fi
  writelog "Getnews: Download AR Newsline - $arfile  ...   [ COMPLETED ]"
  echo "Located "$NEWS"/"$arfile" -"
  echo "  Saved as "$NEWS"/arnl.mp3 for playback."
  rm -f ardump
  archfile=$arfile
  if [ "$archive" = "yes" ]; then
    echo "  Saving copy to archives -   [ Checking ]"
    if [ -f $archiveloc/ARNewsline/$archfile ] ; then
      echolog "ERROR.. - File "$archfile" already archived."
      return 3
    fi
    cp  arnl.mp3 $archiveloc/ARNewsline/$archfile          # Archive the audio program
    echolog "Completed - File "$archfile" - Archived."
  fi
}

########################################################################
#
### Fetch latest ARRL Audio News
#
get_arrl() {
  echolog "Downloading ARRL Audio News file - Activated."
# remove old file...
  rm -f arrl* error-arrl >/dev/null 2>&1
  user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124.0.0.0"
  echo
  echo "Download index file from server - http://www.blubrry.com/arrlaudionews/"
  process="Get ARRL Audio News Index File"
  /usr/bin/curl -A "$user_agent" -sSkLo aandump http://www.blubrry.com/arrlaudionews/ || error 4
  echo " Index Download Completed"
  if [ "$error" = "YES" ]; then
     unset error
     return 4
  fi
  loop=1; found=0
  while [ $found = 0 ]; do
    check=$(cat aandump | cut -d"<" -f$loop | cut -d ">" -f1)
    check2=$(echo $check | grep mp3)
    if [ ! -z "$check2" ];then found=1;fi
    ((loop++))
    if [ $loop -gt 120 ]; then echo "1 Fuck!"; error 4; break; fi  # lucked out!
  done
  check2=`echo $check2 | sed 's/;/\n/g' | grep "iframe"`
  loop=1; found=0
  while [ $found = 0 ]; do
    check=$(echo $check2 | awk '{print $8}')
    check3=$(echo $check | grep mp3)
    if [ ! -z "$check3" ];then found=1;fi
    ((loop++))
    if [ $loop -gt 50 ]; then echo "2 Fuck!"; break; fi  # lucked out!
  done
  media=$(echo $check3 | cut -d"=" -f3 | sed 's/%3A/:/g' | sed 's/%2F/\//g' | cut -d"&" -f1)
  echo "File to download = "$media
  process="Get ARRL Programme File - $media"
  f=1
  while true ; do
     fname=`echo $media | cut -d"/" -f$f`
     if [ -z `echo $fname | grep -i mp3` ]; then
        let f+=1
      else
        break
     fi
  done
  /usr/bin/curl -A "$user_agent" -sSkLo $fname $media || error 4
  if [ "$error" = "YES" ]; then
     unset error
     return 4
  fi
  echo " Audio Download Completed"
  echolog "Download ARRL Audio News $fname  ...   [ COMPLETED ]"
  echo "Located "$NEWS"/"$fname" -"
  echo "  Saving as "$NEWS"/arrlan.mp3 for playback."
  mv $fname arrlan.mp3
  rm -f aandump
  if [ "$archive" = "yes" ]; then
    echo "  Saving copy to archives -   [ Checking ]"
    if [ -f $archiveloc/AAN/$fname ] ; then
      echolog "ERROR.. - File "$fname" - Archive Exists."
      return 4
    fi
    cp arrlan.mp3 $archiveloc/AAN/$fname          # Archive the audio program
    echolog "Completed - File "$fname" - Archived."
  fi
}

########################################################################
#
### Fetch latest VK5 Local news
#
get_vk5() {
  echolog "Downloading VK5 local Audio file - Activated."
# remove old file...
  rm vk5.mp3 error-vk5 > /dev/null 2>&1
  echo
  process="Get Adelaide Local Programme File"
  echo "Getting file from Webserver - http://northeastradioclub.org.au/broadcast/local.mp3"
  /usr/bin/curl -sSkLo vk5.mp3 http://northeastradioclub.org.au/broadcast/local.mp3 || error 7
  if [ "$error" = "YES" ]; then
     unset error
     return 7
  fi
  echo "Got Adelaide Local News  ...              [ COMPLETED ]"
  writelog "Getnews: Got VK5 Local News Report   ...    [ COMPLETED ]"
  if [ "$archive" = "yes" ]; then
    echo "Date data for file - "$DATENOW
    echo "Archiving a copy of downloaded file.."
    if [ -f $archiveloc/VK5/vk5_local_$DATENOW.mp3 ] ; then
      echolog "ERROR.. - File vk5_local_$DATENOW already archived."
      return 7
    fi
    cp $NEWS/vk5.mp3 $archiveloc/VK5/vk5_local_$DATENOW.mp3
    echolog "Completed - File vk5_local_$DATENOW archived."
  fi
}

########################################################################
#
### Fetch latest VK7 Local news
#
get_vk7() {
  echolog "Downloading VK7 Regional Audio file - Activated."
# remove old file...
  rm vk7.mp3 error-vk7 > /dev/null 2>&1
  echo
  process="Get Tasmania Regional Programme File"
  echo "Getting file from Webserver - http://www.vk7ax.id.au/wianews/VK7Regional.mp3"
  /usr/bin/curl -sSkLo vk7.mp3 http://www.vk7ax.id.au/wianews/VK7Regional.mp3 || error 8
  if [ "$error" = "YES" ]; then
     unset error
     return 8
  fi
  echo "Got Tasmanian Regional News  ...              [ COMPLETED ]"
  writelog "Getnews: Got VK7 Regional News Report   ...     [ COMPLETED ]"
  if [ "$archive" = "yes" ]; then
    echo "Date data for file - "$DATENOW
    echo "Archiving a copy of downloaded file.."
    if [ -f $archiveloc/VK7/vk7_regional_$DATENOW.mp3 ] ; then
      echolog "ERROR.. - File vk7_regional_$DATENOW already archived."
      return 8
    fi
    cp $NEWS/vk7.mp3 $archiveloc/VK7/vk7_regional_$DATENOW.mp3
    echolog "Completed - vk7_regional_$DATENOW archived."
  fi
}

########################################################################
#
### Fetch latest RAIN Report
#
get_rain() {
  echolog "Downloading Classic Rain Report Audio file - Activated."
# remove old file...
  rm rain.mp3 error-rain > /dev/null 2>&1
  echo
  process="Get Rain Report Programme File"
  echo "Getting file from webserver - https://www.therainreport.com/sound/classicrain.mp3"
  /usr/bin/curl -sSkLO http://www.therainreport.com/sound/classicrain.mp3 || error 5
  if [ "$error" = "YES" ]; then
     unset error
     return 5
  fi
  echo "Got Rain Report  ...              [ COMPLETED ]"
  mv classicrain.mp3 rain.mp3
  writelog "Getnews: Got Rain Report   ...    [ COMPLETED ]"
  if [ "$archive" = "yes" ]; then
    echo "Date data for file - "$DATENOW
    echo "Archiving a copy of downloaded file.."
    if [ -f $archiveloc"/Rain/classicrain_"$DATENOW.mp3 ] ; then
      echolog "ERROR.. File classicrain_$DATENOW already archived."
      return 5
    fi
    cp $NEWS/rain.mp3 $archiveloc"/Rain/classicrain_"$DATENOW.mp3
    echolog "Completed - rainreport_$DATENOW archived."
  fi
}

########################################################################
#
### Fetch latest RSGB Audio News (Podcast)
#
get_rsgb() {
  echolog "Downloading RSGB Audio News file - Activated."
# remove old file...
  rm -f rsgb* error-rsgb > /dev/null 2>&1
  echo
  echo "Download index file from server - http://gb2rs.podbean.com"
  process="Get RSGB Audio News Index File"
#  /usr/bin/lynx -source http://gb2rs.podbean.com > rsgb_dump || error 6
  /usr/bin/curl -sSkLo rsgb_dump http://gb2rs.podbean.com || error 6
  if [ "$error" = "YES" ]; then
     unset error
     return 6
  fi
  echo " Index Download Completed"
#  gb2rs=`grep -i "\.mp3" rsgb_dump | head -n2 | tail -n1 | awk '{print $8}' | cut -d"\"" -f2`
  gb2rs=`egrep -io "https.?{,100}mp3" rsgb_dump | head -n1`
  process="Get RSGB Programme File - $gb2rs"
  echo "Downloading - "$gb2rs
  /usr/bin/curl -sSkLo rsgban.mp3 $gb2rs || error 6
  if [ "$error" = "YES" ]; then
    unset error
    return 6
  fi
  echo "Done."
  file=`echo $gb2rs | cut -d"/" -f7`
#  file=`echo $gb2rs | cut -d"/" -f6`
  echo "Located "$NEWS"/"$file" -"
  echo "  Saved as "$NEWS"/rsgban.mp3 for playback."
  rm -f rsgb_dump
  if [ "$archive" = "yes" ]; then
    echo "  Saving copy to archives -   [ Checking ]"
    if [ -f $archiveloc/RSGB/$file ] ; then
      echolog "ERROR.. File "$file" already archived."
      return 6
    fi
    cp rsgban.mp3 $archiveloc/RSGB/$file                   # Archive the audio program
    echolog "Completed - File $file archived."
  fi
}

########################################################################
#
### Fetch latest RAOTC Monthly News
#
get_raotc() {
  echolog "Downloading Old-Timers News file - Activated."
# remove old file...
  rm -f raotc* error-raotc > /dev/null 2>&1
  echo
  baseaddr="http://www.raotc.org.au"
  echo "Download index file from server - $baseaddr/broadcast-recordings"
  process="Get RAOTC monthly News Index File"
  /usr/bin/lynx -source $baseaddr/broadcast-recordings > raotc_dump || error 9
  if [ "$error" = "YES" ]; then
     unset error
     return 9
  fi
  echo " Index Download Completed"
  raotc=`grep -i "\.mp3" raotc_dump | grep $(date +%b) | cut -d"\"" -f6`
  process="Get RAOTC Programme File - $raotc"
  echo; echo "Getting RAOTC Programme File - $raotc"
  /usr/bin/curl -sSkLo raotc.mp3 $raotc || error 9
  if [ "$error" = "YES" ]; then
    unset error
    return 9
  fi
  echo "Done."
  file=`echo $raotc | cut -d"/" -f8`
  echo "Located "$NEWS"/"$file" -"
  echo "  Saved as "$NEWS"/raotc.mp3 for playback."
  rm -f raotc_dump
  if [ "$archive" = "yes" ]; then
    echo "  Saving copy to archives -   [ Checking ]"
    if [ -f $archiveloc/RAOTC/raotc-$file ] ; then
      echolog "Monthly programme raotc-$file already in archive."
      return 9
    fi
    cp raotc.mp3 $archiveloc/RAOTC/raotc-$file          # Archive the audio program
    echolog "Completed - File raotc-$file archived!"
  fi
}

########################################################################
#
### Fetch News West programme
#
get_newswest() {
  echolog "Downloading NewsWest Program - Activated."
# remove old file...
  rm newswest* error-newswest >/dev/null 2>&1
  echo
  process="Get NewsWest Programme File"

  path="http://audio.vk6.net/`date -d "Sunday this week" +%Y%m%d`.newswest.mp3"
  echo "Getting file from Webserver - "$path

  /usr/bin/curl -SskLo newswest.mp3 $path || error 11
  if [ ! -z "`grep "Not Found" newswest.mp3`" ]; then
     echolog "Getnews: Newswest file not on server (404 received)"
     rm newswest.mp3; error 11
  fi
  if [ "$error" = "YES" ]; then
     unset error
     return 11
  fi
  echo "Got NewsWest program file ...              [ COMPLETED ]"
  writelog "Getnews: Got NewsWest Program File ...    [ COMPLETED ]"
  if [ "$archive" = "yes" ]; then
    echo "Date data for file - "$DATENOW
    echo "Archiving a copy of downloaded file.."
    if [ -f $archiveloc/VK6/newswest_$DATENOW.mp3 ] ; then
      echolog "ERROR.. - File newswest_$DATENOW already archived."
      return 11
    fi
    cp $NEWS/newswest.mp3 $archiveloc/VK6/newswest_$DATENOW.mp3
    echolog "Completed - File newswest_$DATENOW archived."
  fi
}

########################################################################
#
### Fetch NZART programme
#
get_nzart() {
  echolog "Downloading NZART Program - Activated."
# remove old file...
  rm nzart* error-nzart >/dev/null 2>&1
  echo
  process="Get NZART Programme File"
  echo "Downloading  index file - https://www.nzart.org.nz/news/broadcast"
  /usr/bin/curl --connect-timeout 15 -sSkLo nzdump https://www.nzart.org.nz/news/broadcast || error 12
  if [ "$error" = "YES" ]; then
     unset error
     return 12
  fi
  path=`grep mp3 nzdump | awk '{ print $2 }' | tail -n1 | cut -d "\"" -f2`
  path="https://www.nzart.org.nz/"$path
  echo "Getting file from Webserver - "$path

  /usr/bin/curl --connect-timeout 15 -SskLo nzart.mp3 $path || error 12
  if [ "$error" = "YES" ]; then
     unset error
     return 12
  fi
  echo "Got NZART program file ...              [ COMPLETED ]"
  writelog "Getnews: Got NZART Program File ...    [ COMPLETED ]"
  rm -f nzdump
  if [ "$archive" = "yes" ]; then
    year=`/bin/date +%Y`
    file_month=`echo $path | cut -d"/" -f7 | cut -d"." -f1`  # returns month of file
    file="NZART_"$year"_"$file_month".mp3"
    echo "Archiving a copy of downloaded file.."
    if [ -f $archiveloc/NZART/$file ] ; then
      echolog "Monthly programme $file already in archive."
      return 12
    fi
    cp $NEWS/nzart.mp3 $archiveloc/NZART/$file
    echolog "Completed - File $file archived."
  fi
}

########################################################################
#
### Fetch TWIAR Programme (Static)        ((DEFUNCT))
#
# Automated file location:-  http://twiar.net/TWIARHAM.mp3
#
#  11/05/2026 - STATIC FILE MISSING - Don't know why, so use podcast
#
#########################
get_twiar() {
  echolog "Downloading TWIAR Static file programme - Activated."
# remove old file...
  rm twiar* error-twiar > /dev/null 2>&1
  echo
  process="Get TWIAR Programme File"
  echo "Getting file from Webserver - https://twiar.net/TWIARHAM.mp3"
  /usr/bin/curl -sSkLo twiar.mp3 https://twiar.net/TWIARHAM.mp3 || error 10
  if [ ! -z "`egrep "^.title.404" twiar.mp3`" ];then error 10; fi
  if [ "$error" = "YES" ]; then
     unset error
     return 10
  fi
  echo "Got TWIAR Static ...              [ COMPLETED ]"
  writelog "Getnews: Got TWIAR Static file ...    [ COMPLETED ]"
  if [ "$archive" = "yes" ]; then
#  Get edition number from ID3 tag in file header data
    edition=`/usr/bin/mpg123 -tn1 twiar.mp3 2>&1 | grep -i artist | cut -d"#" -f2 | sed "s/ //g" | cut -c-4`
    echo "Edition number for file - "$edition
    echo "Archiving a copy of downloaded file.."
    if [ -f $archiveloc/TWIAR/TWIAR_$edition.mp3 ] ; then
      echolog "ERROR.. File TWIAR_$edition already archived."
      return 10
    fi
    cp $NEWS/twiar.mp3 $archiveloc/TWIAR/TWIAR_$edition.mp3
    echolog "Completed - TWIAR_$edition archived."
  fi
}

########################################################################
#
### Fetch TWIAR programme (Podcast)
#
get_twiar_pod() {
  echolog "Downloading TWIAR Podcast - Activated."
# remove old file...
  rm -f twiar* error-twiar > /dev/null 2>&1
  echo
  echo "Download index file from server - https://twiar.net"
  startfile="https://twiar.net/category/podcast"

# get index from website
  /usr/bin/curl -sSkLo twdump $startfile || error10
  echo " Index Download Completed"
  if [ "$error" = "YES" ]; then
     unset error
     return 10
  fi

# find latest file detail page
  line=$(grep this-week twdump | head -n1 | cut -d"\"" -f2)
  edition=$(grep this-week twdump | head -n1 | cut -d"\"" -f2 | cut -d"-" -f8)
  /usr/bin/curl -sSko twdump $line || error 10
  if [ "$error" = "YES" ]; then
     unset error
     return 10
  fi

# find where file is stashed
  file=$(grep Download twdump | cut -d"\"" -f4)
  echo "File to download = "$file
  process="Get TWIAR Programme File - $file"

# Fetch!
  /usr/bin/curl -sSkLo twiar.mp3 $file || error 10
  if [ "$error" = "YES" ]; then
     unset error
     return 10
  fi
  twfile="TWIAR_$edition.mp3"
  echolog "Getnews: Download TWIAR - $twfile  ...   [ COMPLETED ]"
  rm -f twdump

  if [ "$archive" = "yes" ]; then
    echo "  Saving copy to archives -   [ Checking ]"
    if [ -f $archiveloc/TWIAR/$twfile ] ; then
      echolog "ERROR.. - File "$twfile" already archived."
      return 10
    fi
    cp  twiar.mp3 $archiveloc/TWIAR/$twfile            # Archive the audio program
    echolog "Completed - File "$twfile" - Archived."
  fi
}

########################################################################
########################################################################
### End Functions
########################################################################
#
#
# goto news directory
cd $NEWS
DATENOW=`/bin/date +%d_%B_%Y`
# clear; echo; echo
echo; echo

case $1 in
wia)           get_wia ;;
q)             get_q ;;
wia_pod)       forcepod="YES"; get_wia ;;
arnl)          get_arnl ;;
arrl)          get_arrl ;;
#rain)          get_rain ;;
rsgb)          get_rsgb ;;
twiar)         get_twiar_pod ;;
twiar_pod)     get_twiar_pod ;;
vk5)           get_vk5 ;;
vk7)           get_vk7 ;;
nzart)         get_nzart ;;
raotc)         get_raotc ;;
west)          get_newswest ;;
tuesday)       get_raotc
               echo "#######################################################################"
               get_twiar_pod ;;
all)           get_wia
               echo "#######################################################################"
               get_q
               echo "#######################################################################"
               get_arnl
               echo "#######################################################################"
               get_arrl
#               echo "#######################################################################"
#               get_rain
               echo "#######################################################################"
               get_newswest
               echo "#######################################################################"
               get_vk5
               echo "#######################################################################"
               get_vk7
               echo "#######################################################################"
               get_rsgb
               echo "#######################################################################"
               get_nzart ;;
*)             usage ;;
esac


#######################################################################
# Completed...

exit 0
