#!/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/OTM/.

# -e <regexp> Fetch only those articles with <regexp> in the title
# (NOT IMPLEMENTED!)

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

date=`date -d "Last Friday" +%m%d%y`

directory=/storage/multimedia

delete_old_files=true

prgcode=OTM

while getopts ":e:d:r:o" Option
# Initial declaration.
do
  case $Option in
    e ) echo -e "Wait for it...\n\n(Not implemented.)\n";
        exit
    ;;
    d ) date=`date -d "$OPTARG" +%m%d%y`
    ;;
    r ) directory=$OPTARG
    ;;
    o ) delete_old_files=false
  esac
done
shift $(($OPTIND - 1))
# Move argument pointer to next.

##############################
#
# goto_dir
#
##############################

function goto_dir(){

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

  cd $directory/npr/$prgcode
}

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

function prep_dir(){
  if $delete_old_files
  then
    #remove all mp3 files
    rm -f mp3/*.mp3 *.html

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

}

##############################
#
# fetch_html
#
##############################
  
function fetch_html(){
  ## 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.

  htmlfile=otm"$date".html
  webaddr="$site/$htmlfile"

  wget $webaddr

# FIX THIS
  if grep "No data found" $htmlfile
  then
    echo Oops!  No data found.
    exit 1
  fi
}

###############################
#
# fetch_mp3s
#
###############################

function fetch_mp3s(){
  regexp="/stream/ram\\.py?file=otm/otm$date.\\.mp3"

  ctr=0

  for i in `grep -o "$regexp" $htmlfile`
  do
    echo $i
    outfile=`expr match $i ".*\\(otm[0-9]*.\\.mp3$\\)"`
    ctr=`expr $ctr + 1`
    echo "Fetching $outfile"
    wget -q -O - "$site$i" | xargs wget -O mp3/$outfile
    datestr=`echo $date | cut -c 1-2`-`echo $date | cut -c 3-4`-`echo $date | cut -c 5-6`
    id3v2 -T "$ctr" -A "OTM $datestr" "mp3/$outfile" 

    if mount | grep -q /mnt/stick
    then 
      mkdir -p /mnt/stick/npr/$prgcode
      rsync -av mp3/$outfile /mnt/stick/npr/$prgcode
    fi

  done
}

site="http://www.onthemedia.org"
goto_dir;

prep_dir;

fetch_html;

fetch_mp3s

