if [[ $# -ne 3 ]]; then
echo -e "wrong number of arguments.\n" \
"Usage: $0 <station> <dump method> <out directory>\n" \
"Example: $0 oberhausen mplayer ." >&2
exit 1
fi
if [[ $1 != "muelheim" && $1 != "oberhausen" ]]; then
echo "no known radio station given... exiting now." >&2
exit 1
fi
if [[ $2 == "vlc" ]]; then
cmd="cvlc %s --sout=file://%s vlc://quit"
elif [[ $2 == "mplayer" ]]; then
cmd="mplayer -dumpstream %s -dumpfile %s"
else
echo "no dump method given... exiting now." >&2
exit 1
fi
if [[ $3 == "" ]]; then
echo "no dump directory given... exiting now." >&2
exit 1
fi
echo "current directory is $(pwd)..."
pidfile="/tmp/radio$1_$2.pid"
if [ -f $pidfile ]; then
echo "pidfile already exists. exiting..."
exit
fi
echo "my pid is $$. logging to pidfile..."
echo $$ > $pidfile
stationurl="http://edge.live.mp3.mdn.newmedia.nacamar.net/radio$1/livestream.mp3"
quit=0
trap "quit=1" SIGTERM SIGHUP SIGINT
while [ $quit -ne 1 ]; do
clocknow=`date +%Y%m%d%H%M%S`
outfile="$3/$1_$2_$clocknow.mp3"
echo "dump to $outfile..."
dumpcmd="$(printf "$cmd" $stationurl $outfile)&"
echo "\$ ${dumpcmd}"
eval ${dumpcmd}
pid=$!
wait
[ $quit -ne 1 ] && sleep 1
done
echo "kill last process $pid..."
kill $pid
echo "removing pidfile..."
rm $pidfile
echo "exiting..."