#!/bin/sh
# This tiny script converts  OGG Files to MP3 in any subdirectory of given one
# usage: o2m </absolute_path/to_where/the_files/are>
# 2005 by Nicolai Beuermannn
# GPL licensed

# for valid options you should consult lame's manual page.
LAME_OPTS="--preset standard"
if [ -z "$1" ] 
	then  echo "usage: ogg2mp3 <pathname>"
else 
	# testing for necessary tools
	if [ ! -x `which ogg123` ] && [ ! -x `which lame` ]; then
	echo "Please install 'ogg123' and 'lame'"
	fi;
	
	# to handle spaces in pathnames
	IFS=$'\n'
	
	# where we go
	WORKDIR=$1
	
	# find directories to work on
	DIRS=`find $WORKDIR -type d`
	
	# conversion works on all directories 
	for verz in $DIRS
	do
		cd $verz
		for i in *.ogg
		do 
			DATEINAME=`basename "$i" .ogg`
			if [ ! -e $DATEINAME".mp3" ]; then
			IFS=$' '
			ogg123 -d wav -f - "$DATEINAME"".ogg" | lame $LAME_OPTS - "$DATEINAME"".mp3"
			IFS=$'\n'
			fi;
		done
		cd ..
	done;
	read -p "Push <return> to finish"
	fi;
IFS=$'\n'

exit 0
