#!/bin/bash
#
# Script to rip a CD with cdda2wav and then encode them  as MP3s 
# with lame with proper names from CDDB from audio.cddb generated by
# cdda2wav.
# Written by Kenny Van Alstyne and Roy Keene
# kvanals@kvanals.org and rkeene@rkeene.org
# Licensed under the GPL Version 2 or Higher
#
#Set whatever bitrate that you want the MP3s to be here.
BITRATE=32
#Set the device for your CD-ROM drive.
DEVICE="0,0,0"
#Set the CDDB Mode for cdda2wav.
CDDB=0
#Set what the cdda2wav binary is called.  You PROBABLY don't need to change
# this unless cdda2wav isn't in your PATH.
CDDA2WAV=cdda2wav
#Set what the lame binary is called.  You PROBABLY don't need to change this
# unless lame isn't in your PATH.
LAME=lame
################################################################
# It's probably better if you don't edit anything below here.  #
################################################################
# Call cdda2wav to rip the tracks
echo Running cdda2wav to rip the tracks.
$CDDA2WAV -B cddb=$CDDB dev=$DEVICE
# Call lame and other things to make the MP3s
echo Encoding the files and naming them something more useful.
disctitle=`grep '^DTITLE=' audio.cddb | cut -f 2 -d = | sed 's@/@_@g'`; mkdir "${disctitle}"; cd "${disctitle}";for tmp in `grep '^TTITLE[0-9]*=' ../audio.cddb | sed 's/^TTITLE\([0-9]*\)=\(.*\)$/\1|\2/;s@ @|@g'`;do trackno=$(echo $[$(echo $tmp | cut -f 1 -d \|)+1] | sed 's/^\([0-9]\)$/0\1/');trackname=`echo $tmp | cut -f 2- -d \| | sed 's@/@_@g;s@|@@g'`;srcwav="../audio_${trackno}.wav";dstmp3="${trackno} - ${trackname}.mp3";$LAME -b $BITRATE "${srcwav}" "${dstmp3}";done
################################################################
# Commented below is the original way of doing things.  It     #
# just renames the wave file.                                  #
################################################################
#echo Renaming the file to be more useful.
#disctitle=`grep '^DTITLE=' audio.cddb | cut -f 2 -d = | sed 's@/@_@g'`; for tmp in `grep '^TTITLE[0-9]*=' audio.cddb | sed 's/^TTITLE\([0-9]*\)=\(.*\)$/\1|\2/;s@ @|@g'`;do trackno=$(echo $[$(echo $tmp | cut -f 1 -d \|)+1] | sed 's/^\([0-9]\)$/0\1/');trackname=`echo $tmp | cut -f 2- -d \| | sed 's@/@_@g;s@|@@g'`;mv audio_${trackno}.wav "${trackno} - ${disctitle} - ${trackname}.wav";done
################################################################
# Commented below is another way of doing things.  It          #
# just makes the MP3s without directories.                     #
################################################################
#echo Encoding the files and naming them something more useful.
#disctitle=`grep '^DTITLE=' audio.cddb | cut -f 2 -d = | sed 's@/@_@g'`; for tmp in `grep '^TTITLE[0-9]*=' audio.cddb | sed 's/^TTITLE\([0-9]*\)=\(.*\)$/\1|\2/;s@ @|@g'`;do trackno=$(echo $[$(echo $tmp | cut -f 1 -d \|)+1] | sed 's/^\([0-9]\)$/0\1/');trackname=`echo $tmp | cut -f 2- -d \| | sed 's@/@_@g;s@|@@g'`;srcwav="audio_${trackno}.wav";dstmp3="${trackno} - ${disctitle} - ${trackname}.mp3";$LAME -b $BITRATE "${srcwav}" "${dstmp3}";done
################################################################
# Let us clean up some stuff before we exit.                   #
################################################################
echo Removing files made by cdda2wav that we do not need.
#Comment the following line if not storing in folder
cd ..
rm -f audio*.inf
rm -f audio.cddb
rm -f audio.cdindex
rm -f audio*.wav
