#!/bin/sh

cache=""
seconds=""
if [ "$1" = "-seconds" ]; then
	shift
	seconds="$1"
	shift
	seconds="$(( $seconds + 5 ))"
	cache="-cache 32"
elif [ "$1" = "-minutes" ]; then
	shift
	minutes="$1"
	shift
	seconds="$(( $minutes * 60 ))"
	seconds="$(( $seconds + 15 ))"
	cache="-cache 256"
fi

stream="$1"
if [ "$stream" = "cbc1" -o "$stream" = "radio1" ]; then
	stream="mms://wm05.nm.cbc.ca/cbcr1-ottawa"
elif [ "$stream" = "cbc2" -o "$stream" = "radio2" ]; then
	stream="mms://wm05.nm.cbc.ca/cbcr2-toronto"
fi

file=${stream##*/}
if [ "$file" = "" ]; then
	file=stream.out
elif [ "${file##*.}" = "$file" ]; then
	if [ "${stream:0:4}" = "mms:" ]; then
		file="${file}.`date "+%d%b%y_%k%M"`.asf"
	fi
fi
echo "Saving output as '$file'"
cmd="mplayer $cache -dumpstream -dumpfile $file $stream"
echo $cmd

if [ ! "$seconds" = "" ]; then
	( sleep $seconds ; kill $$ ) &
fi
exec $cmd  &>/dev/null 
