#!/bin/sh

cd ~/tmp/radio
# Kill any running realplay processes
kill `ps | grep realplay | awk '{ print $1 }'` 2>/dev/null

# Use vsound to record 61 minutes (3660 seconds) 
#vsound realplay http://www.bbc.co.uk/cgi-bin/worldservice/audiotime.cgi?newshour.txt &
#echo "doing vsound realplay http://cartalk.cars.com/Radio/Show/Audio/200246/RA/all.ram"
rm all.ram
echo ding http://cartalk.cars.com/`lynx --source http://cartalk.cars.com/Radio/Show/ | grep all.ram | perl -e 'while (<>) {/"(.*)"/; print $1}'`  | xargs wget 

echo http://cartalk.cars.com/`lynx --source http://cartalk.cars.com/Radio/Show/ | grep all.ram | perl -e 'while (<>) {/"(.*)"/; print $1}'`  | xargs wget 

b=0;
for a in `cat all.ram` ; do 
  date
  echo $a
  if ((b++)); then echo $b; fi;
  vsound realplay $a &
  VSOUND_PID=$!
  #60s*8 per segment
  echo sleepting 500
  sleep 500
  kill `ps | grep realplay | awk '{ print $1 }'`
  echo killed realplay

  # Wait for vsound to terminate, creating vsound.wav
  wait $VSOUND_PID

  datestamp=`echo \`date +'%Y_%W'\`"_"`
  file=ClickClack_$datestamp_$b.mp3
  echo file is $file
  # Encode the output (vsound.wav) as an mp3
  #nice nice notlame -S -k -v -V 7 vsound.wav BBCNewshour.mp3 
  nice nice notlame -S -k -v -V 7 vsound.wav $file
  rm -f vsound.wav
done

