#!/bin/sh

# If you use this script, please support National Public Radio.
# Please donate either to your local radio station.  If you haven't a
# local station, consider donating to WDUQ (my old Pittsburgh station,
# www.wduq.org), WHYY (www.whyy.org, Philadelphia, home of Fresh Air)
# or WBEZ (www.wbez.org, Chicago, home of This American Life).  Or
# heck, KOSU (www.kosu.org, Stillwater, OK, the first NPR station I
# listened to).

# The -d argument specifies the date of the program to stream.  For instance, 
# npr-grabber.sh -d yesterday ATC fetches yesterday's All Things Considered.

# -r <path> specifies a different path for saving the file.  By
# default, the .wavs are saved in /storage/multimedia/npr/PROGCODE/.

# -x tells the program to burn a CD

# -1 for fetching the files one at a time, rather than multiple simultaneous downloads

# -n for setting maximum number of mplayers (default: 10)

# -e <regexp> Fetch only those articles with <regexp> in the title

# -m to make mp3's (default)

# -w for no mp3's

# -o to make oggs

# -a means leave old files in place.  By default, we delete them.



if expr match "$*" ".*\\<OTM\\>" >/dev/null
then
  echo "Going to otm-grabber..."
  /home/jesse/bin/otm-grabber.sh "$@"
  exit  
fi

date=`date +%d-%b-%Y`

burncd=false

makemp3=true

makeogg=false

oneatatime=false

directory=/storage/multimedia

maxmplayers=10

delete_old_files=true

script_dir=`dirname $BASH_SOURCE`

while getopts ":e:n:d:r:xm1aow" Option
# Initial declaration.
do
  case $Option in
    e ) regexp="$OPTARG"
    ;;
    d ) date=`date -d "$OPTARG" +%d-%b-%Y`
    ;;
    r ) directory=$OPTARG
    ;;
    x ) burncd=true
    ;;
    n ) maxmplayers="$OPTARG"
    ;;
    1 ) oneatatime=true
    ;;
    m ) makemp3=true
    ;;
    w ) makemp3=false
    ;;
    o ) makeogg=true
    ;;
    a ) delete_old_files=false
  esac
done
shift $(($OPTIND - 1))
# Move argument pointer to next.


##############################
#
# goto_dir (you could use $`date +%Y%m%d`$prgcode)
#
##############################

function goto_dir(){

  mkdir -p "$directory/npr/$prgcode"

  cd $directory/npr/$prgcode

}

##############################
#
# prep_dir
#
##############################

function prep_dir(){
  if $delete_old_files
  then
    #remove any wave files that don't have today's date in them.
    redate=`date -d $date +%Y%m%d`
    oldfiles=`find . -name "*.wav" ! -name "*$redate*"`

    rm -f $oldfiles

    oldfiles=`find . -name "*.mp3" ! -name "*$redate*"`

    rm -f $oldfiles

    if mount | grep -q /mnt/stick
    then 
      rm -f /mnt/stick/npr/$prgcode/*
    fi
  fi  

  rm -f screenlog.0

}

##############################
#
# fetch_smil
#
##############################
  
function fetch_smil(){
  ## www.npr.org uses javascript to serve its sound files.  The javascript 
  ## is found at /include/javascript/jsfuncs.js.  There, you'll find the
  ## definition of getMedia, which suggests the following URL.

  webaddr=http://www.npr.org/dmg/dmg.php\?prgCode\=$prgcode\&showDate\=$date\&segNum\=\&mediaPref\=RM


  wget $webaddr -O npr.smil
  if grep "No data found" npr.smil
  then
    echo Oops!  No data found.
    exit 1
  fi
}


prgcode=$1

goto_dir;
fetch_smil

prep_dir


# Fetch all of the .rm files as .wav files, in the order that they occur 
# in npr.smil
#
# Use ctr as a prefix so that when we burn the CD, we keep the order correct.
ctr=0

grep "<audio[^>]*" npr.smil |
while read i;
do
  ctr=`expr $ctr + 1`
  rtsp=`expr match "$i" '.*\(rtsp://[^\"]*\).*'`
  title=`expr match "$i" '.*title=\"\([^\"]*\).*'`
  artist=`expr match "$i" '.*author=\"\([^\"]*\).*'`

  if [[ -n "$regexp" ]] && ! expr match "$title" ".*$regexp" >/dev/null
  then
    echo "\"$title\" does not match \"$regexp\".  Skip!"
  else
    echo "Fetching $ctr $title ...."


    if $oneatatime
    then
        "$script_dir"/npr-grab-seg.sh "$rtsp" "$prgcode" "$title" "$artist" \
                        "$ctr" "$burncd" "$makemp3" "$makeogg" "1"
    else
        /usr/bin/screen -S "$prgcode$ctr" -d \
                        -m "$script_dir"/npr-grab-seg.sh "$rtsp" \
                        "$prgcode" "$title" "$artist" "$ctr" "$burncd" \
                        "$makemp3" "$makeogg"  "$maxmplayers"
        sleep 1
    fi
  fi
done

if $burncd
then
  "$script_dir"/cd-write-audio.sh
fi

