#!/bin/bash 

# /usr/local/bin/npr-grab-seg.sh
#
# -----------------------------------------------------------------------------------

if [ "$#" -ne 9 ]
then 
  echo "Parameters: URL, program code, title, artist, segment, burncd, mp3, ogg"
  echo "  This script is called from npr-grabber.sh"
  exit 1
fi

url="$1"
fname=`basename $url`

prgcode="$2"
title="$3"

showAnddate="$4"

# This should be in the form "All Things Considered - Friday, blah blah"
# Split this at "-" for the artist and "album".

artist=`echo $showAnddate | cut -f 1 -d '-'`

album="$prgcode: `echo $showAnddate | cut -f 2 -d '-' |cut -c 2-`"

segment="$5"
burncd="$6"
makemp3="$7"
makeogg="$8"
maxmplayers="$9"

# Long program codes like WESAT and WESUN screw up my mp3 player.
# It doesn't sort correctly.  So I throw away all but the last three
# characters.
short_code=`echo $prgcode | tail -c 4`
outname=$short_code\_`printf %2.2d $segment`\_`echo $fname | cut -d . -f 1`

echo Segment: $segment
echo URL: $url
echo Title: $title

##############################
#
# get_wav
#
##############################
function get_wav(){
  # At present, mplayer fails to detect the end of the realplay file
  # properly.  (Wonder if the Windows Media files would work better?  Maybe
  # I'll check it out later.)  In any case, at the end of the file, it
  # takes a couple of minutes to notice the file had ended -- this means 
  # half an hour or so more to read the 16 or so files from ME or ATC!

  # Alternative: Use vsound to record from realplay, but realplay doesn't
  # halt at the end of the soundfile.  I have no obvious workaround for that.

  # You could use tcpdump to see if packets are still being received and 
  # kill either realplay or mplayer when packets stop, but I don't care
  # much for that solution.  

#  mplayercmd="/usr/local/bin/mplayer -nocache -ao pcm -waveheader -aofile $outname $url"
  mplayercmd="/usr/local/bin/mplayer -nocache -vc null -vo null -ao pcm:waveheader:file=$outname.wav $url"

  while [ `pgrep "\<lame|sox|mplayer\>" | wc -l` -ge "$maxmplayers" ]; do 
    echo "Waiting for mplayer..." 
    sleep 5 
  done 

  $mplayercmd < /dev/null
}

##############################
#
# resample_wav
#
##############################
function resample_wav(){

      ## Check to see if we need to resample.  I *thought* that ME
      ## and ATC always "broadcast" the Realplay files at
      ## frequency 44,100 (whatever that means), but it ain't so.
      ## I got some files at 22,050 recently.  Bad NPR, Bad!

  if $burncd
  then
    file "$outname".wav | grep -L -e "44100" >/dev/null
    if [ $? -eq 1 ]
    then 
      echo  *********Resampling sox....

      while pgrep "\<lame|sox\>" >/dev/null; do 
        echo "Waiting to resample with sox..." 
        sleep 5 
      done 

      sox "$outname".wav -r 44100 new-"$outname" resample 
      mv new-"$outname" "$outname".wav 
    fi
  fi
}

##############################
#
# ogg_it
#
##############################

function ogg_it(){

    mkdir -p "ogg"

    while pgrep "\<lame|sox\>" >/dev/null; do
      echo "Waiting to convert to ogg..."
      sleep 5
    done

    oggenc --resample 16000 --downmix -q 10 -t "$prgcode $segment:$title" -a "$artist" -l "$artist" -o ogg/$outname.ogg $outname.wav
}

##############################
#
# lame_it
#
##############################

function lame_it(){

   mkdir -p "mp3"


    while pgrep "\<lame|sox\>" >/dev/null; do
      echo "Waiting to lame..."
      sleep 5
    done

    lame --resample 22.05 --tt "$title" --tn "$segment" --ta "$artist" --tl "$album" "$outname.wav" "mp3/$outname.mp3" 

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

}

get_wav && resample_wav 

if $makemp3
then
  lame_it
fi

if $makeogg
then
  ogg_it
fi
