* USEID3 and USEID3V2 variables are unused now, so remove them. Thanks
[clinton/abcde.git] / abcde
diff --git a/abcde b/abcde
index 4ab5d00..f9ea19f 100755 (executable)
--- a/abcde
+++ b/abcde
@@ -1,6 +1,8 @@
 #!/bin/bash
 # Copyright (c) 1998-2001 Robert Woodcock <rcw@debian.org>
 # Copyright (c) 2003-2006 Jesus Climent <jesus.climent@hispalinux.es>
+# Copyright (c) 2009-2012 Colin Tuckley <colint@debian.org>
+# Copyright (c) 2012      Steve McIntyre <93sam@@debian.org>
 # This code is hereby licensed for public consumption under either the
 # GNU GPL v2 or greater, or Larry Wall's Artistic license - your choice.
 #
@@ -13,7 +15,7 @@
 #
 # $Id$
 
-VERSION='2.4'
+VERSION='2.5.5-UNRELEASED'
 
 usage ()
 {
@@ -43,11 +45,11 @@ echo "-j <#> Number of encoder processes to run at once (localhost)"
 echo "-k     Keep the wav tracks for later use"
 echo "-l     Use low disk space algorithm"
 echo "-L     Use local CDDB storage directory"
-echo "-n     No lookup. Don't query CDDB, just create and use template"
-echo "-N     Noninteractive. Never prompt for anything"
 echo "-m     Modify playlist to include CRLF endings, to comply with some players"
-echo "       WARNING: Deprecated. Use \"cue\" action"
+#echo "       WARNING: Deprecated. Use \"cue\" action"
 #echo "-M     Create a CUE file"
+echo "-n     No lookup. Don't query CDDB, just create and use template"
+echo "-N     Noninteractive. Never prompt for anything"
 echo "-o <type1[,type2]...>"
 echo "       Output file type(s) (vorbis,mp3,flac,spx,mpc,wav,m4a). Defaults to vorbis"
 echo "-p     Pad track numbers with 0's (if less than 10 tracks)"
@@ -56,7 +58,6 @@ echo "-q <level>"
 echo "       Set quality level (high,medium,low)"
 echo "-r <host1[,host2]...>"
 echo "       Also encode on these remote hosts"
-echo "-R     Use local CDDB in recursive mode"
 echo "-s <field>"
 echo "       Show fields from the CDDB info (year,genre)"
 echo "-S <#> Set the CD speed"
@@ -68,7 +69,7 @@ echo "-V     Be a bit more verbose about what is happening behind the scenes"
 echo "-x     Eject CD after all tracks are read"
 echo "-w <comment>"
 echo "       Add a comment to the CD tracks"
-echo "-W <#> Concatenate CDs: -T #01 -w \"CD #\"" 
+echo "-W <#> Concatenate CDs: -T #01 -w \"CD #\""
 echo "-z     Use debug CDROMREADERSYNTAX option (needs cdparanoia)"
 echo ""
 echo "Tracks is a space-delimited list of tracks to grab."
@@ -83,7 +84,7 @@ addstatus ()
 }
 
 # log [level] [message]
-# 
+#
 # log outputs the right message in a common format
 log ()
 {
@@ -96,7 +97,7 @@ log ()
        esac
 }
 
-# Funtions to replace the need of seq, which is too distribution dependant.
+# Functions to replace the need of seq, which is too distribution dependent.
 f_seq_row ()
 {
        i=$1
@@ -118,7 +119,7 @@ f_seq_line ()
                done
                echo
        else
-               log error "syntax error while processing track numbers"
+               log error "syntax error while processing track numbers ($i)"
                exit 1
        fi
 }
@@ -156,7 +157,7 @@ checkstatus ()
 {
        # Take the last line in the status file if there's multiple matches
        PATTERN="^$1(=.*)?$"
-       BLURB=$(egrep $PATTERN "$ABCDETEMPDIR/status" | tail -n 1)
+       BLURB=$(grep -E $PATTERN "$ABCDETEMPDIR/status" | tail -n 1)
 
        if [ -z "$BLURB" ]; then
                # No matches found
@@ -183,7 +184,7 @@ checkwarnings ()
        fi
        # Take the last line in the status file if there's multiple matches
        PATTERN="^$1(:.*)?$"
-       BLURB="$(egrep $PATTERN "$ABCDETEMPDIR/warnings" | tail -n 1)"
+       BLURB="$(grep -E $PATTERN "$ABCDETEMPDIR/warnings" | tail -n 1)"
 
        if [ -z "$BLURB" ]; then
                # negative, we did not have a negative...
@@ -206,7 +207,7 @@ checkerrors ()
        fi
        # Take the last line in the status file if there's multiple matches
        PATTERN="^$1(:.*)?$"
-       BLURB="$(egrep $PATTERN "$ABCDETEMPDIR/errors" | tail -n 1)"
+       BLURB="$(grep -E $PATTERN "$ABCDETEMPDIR/errors" | tail -n 1)"
 
        if [ -z "$BLURB" ]; then
                # negative, we did not have a negative...
@@ -246,6 +247,7 @@ page ()
 # Runs a command, silently if necessary, and updates the status file
 run_command ()
 {
+       vvecho "$@"
        BLURB="$1"
        shift
        # See if this is supposed to be silent
@@ -389,7 +391,7 @@ diffentries ()
                PARSECHOICE2=$(echo $CDDBDIFFCHOICE | cut -d"," -f2 | xargs printf %d 2>/dev/null)
                if [ $PARSECHOICE1 -lt 1 ] || [ $PARSECHOICE1 -gt $CDDBDIFFCHOICES ] || \
                   [ $PARSECHOICE2 -lt 1 ] || [ $PARSECHOICE2 -gt $CDDBDIFFCHOICES ] || \
-                  [ $PARSECHOICE1 -eq $PARSECHOICE2 ]; then 
+                  [ $PARSECHOICE1 -eq $PARSECHOICE2 ]; then
                        echo "Invalid diff range. Please select two comma-separated numbers between 1 and $CDDBDIFFCHOICES" >&2
                else
                        # We parse the 2 choices to diff, store them in temporary files and diff them.
@@ -421,7 +423,7 @@ getcddbinfo()
                TRACKNAME="$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')"
                ;;
        TRACK-INFO)
-               grep ^EXTT$CDDBTRACKNUM= "$CDDBDATA" | cut -f2- -d= | tr -d \[:cntrl:\] | perl -p -e 's/\\n/\n/;'
+               grep ^EXTT$CDDBTRACKNUM= "$CDDBDATA" | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\\n/\n/g'
                ;;
        esac
 }
@@ -489,8 +491,18 @@ makeids ()
        PREGAP=$(($(echo $OFFSETS | cut -f1 -d' ')))
        TOTALTIME=$(( (($LEADOUT + $LEADIN + $PREGAP) / $CDFRAMES) - (($LEADIN + $PREGAP) / $CDFRAMES)))
 
-       printf -v HEXSUM "%08lx" $(( ($CDDBCKSUM % 0xff) << 24 | $TOTALTIME << 8 | $TRACKS))
-       TRACKINFO="${HEXSUM} $((TRACKS)) ${COOKEDOFFSETS} $((($LEADOUT + $LEADIN + $IDMAGICNUM) / $CDFRAMES))"
+       case "$CDDBMETHOD" in
+               cddb)
+                       printf -v DISCID "%08lx" $(( ($CDDBCKSUM % 0xff) << 24 | $TOTALTIME << 8 | $TRACKS))
+                       ;;
+               musicbrainz)
+                       # FIXME: don't assume the first track is 1
+                       echo "dasd: 1 $TRACKS $LEADIN $LEADOUT $OFFSETS "
+                       DISCID=$($MUSICBRAINZ --command calcid --discinfo 1 $TRACKS $LEADIN $LEADOUT $OFFSETS)
+                       ;;
+       esac
+
+       TRACKINFO="${DISCID} $((TRACKS)) ${COOKEDOFFSETS} $((($LEADOUT + $LEADIN + $IDMAGICNUM) / $CDFRAMES))"
 }
 
 do_replaygain()
@@ -520,10 +532,10 @@ do_replaygain()
                                TRACKFILE="$(mungefilename "$TRACKNAME")"
                                ARTISTFILE="$(mungefilename "$TRACKARTIST")"
                                ALBUMFILE="$(mungefilename "$DALBUM")"
-                       GENRE="$(mungegenre "$GENRE")"
-                       YEAR=${CDYEAR:-$CDYEAR}
+                               GENRE="$(mungegenre "$GENRE")"
+                               YEAR=${CDYEAR:-$CDYEAR}
                                gettracknum
-                               if [ "$ONETRACK" = "y" ]; then 
+                               if [ "$ONETRACK" = "y" ]; then
                                        if [ "$VARIOUSARTISTS" = "y" ]; then
                                                OUTPUTFILE="$(eval echo \""$VAONETRACKOUTPUTFORMAT"\")"
                                        else
@@ -541,14 +553,14 @@ do_replaygain()
                        done
                        case "$OUTPUT" in
                                flac)
-                                       run_command replaygain-flac nice $ENCNICE $METAFLAC --add-replay-gain "${OUTPUTFILES[@]}"
+                                       run_command replaygain-flac nice $ENCNICE $METAFLAC $FLACGAINOPTS "${OUTPUTFILES[@]}"
                                        #run_command replaygain-flac true
                                        ;;
                                vorbis|ogg)
-                                       run_command replaygain-vorbis nice $ENCNICE $VORBISGAIN --album "${OUTPUTFILES[@]}"
+                                       run_command replaygain-vorbis nice $ENCNICE $VORBISGAIN $VORBISGAINOPTS "${OUTPUTFILES[@]}"
                                        ;;
                                mp3)
-                                       run_command replaygain-mp3 nice $ENCNICE $MP3GAIN -a -k "${OUTPUTFILES[@]}"
+                                       run_command replaygain-mp3 nice $ENCNICE $MP3GAIN $MP3GAINOPTS "${OUTPUTFILES[@]}"
                                        ;;
                                mpc)
                                        run_command replaygain-mpc nice $ENCNICE $MPPGAIN --auto "${OUTPUTFILES[@]}"
@@ -778,15 +790,17 @@ return 0
 # do_tag [tracknumber]
 # id3 tags a filename
 # variables used:
-# TRACKS, TRACKNAME, TRACKARTIST, TAGGER, TAGGEROPTS, VORBISCOMMENT, METAFLAC, ATOMICPARSLEY 
-# COMMENT, DALBUM, DARTIST, CDYEAR, CDGENRE (and temporarily) ID3TAGV
+# TRACKS, TRACKNAME, TRACKARTIST, TAGGER, TAGGEROPTS, VORBISCOMMENT, METAFLAC, ATOMICPARSLEY
+# COMMENT, DALBUM, DARTIST, CDYEAR, CDGENRE
 do_tag ()
 {
        COMMENTOUTPUT="$(eval echo ${COMMENT})"
        if [ -z "$COMMENTOUTPUT" ]; then
                COMMENTOUTPUT="$(getcddbinfo TRACK-INFO)"
        fi
-       CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
+       if [ "$CDDBMETHOD" = "cddb" ]; then
+               CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
+       fi
        run_command '' echo "Tagging track $1 of $TRACKS: $TRACKNAME..."
        # If we want to start the tracks with a given number, we need to modify the
        # TRACKNUM value before evaluation
@@ -814,9 +828,9 @@ do_tag ()
                                                --comment=::"$COMMENTOUTPUT" -A "$DALBUM" \
                                                -a "$TRACKARTIST" -t "$TRACKNAME" -Y "$CDYEAR" \
                                                -G "$GENREID" -n "${TRACKNUM:-$1}" \
-                                               "${TRACKNUM:+-N $TRACKS}" \
-                                               "${ENCODING:+--set-encoding=$ENCODING}" \
-                                               "${TPE2:+--set-user-text-frame=TPE2:$TPE2}" \
+                                               ${TRACKNUM:+-N "$TRACKS"} \
+                                               ${ENCODING:+--set-encoding="$ENCODING"} \
+                                               ${TPE2:+--set-text-frame=TPE2:"$TPE2"} \
                                                "$ABCDETEMPDIR/track$1.$OUTPUT"
                                        ;;
                                # FIXME # Still not activated...
@@ -835,7 +849,7 @@ do_tag ()
                                                -A "$DALBUM" -a "$TRACKARTIST" -t "$TRACKNAME" \
                                                -y "$CDYEAR" -g "$GENREID" \
                                                -T "${TRACKNUM:-$1}/$TRACKS" \
-                                               "${TPE2:+--TPE2 \"$TPE2\"}" \
+                                               ${TPE2:+--TPE2 "$TPE2"} \
                                                "$ABCDETEMPDIR/track$1.$OUTPUT"
                                        ;;
                        esac
@@ -925,7 +939,7 @@ do_tag ()
                        fi
 
                        #It has to be command file opts for AtomicParsley
-                       run_command tagtrack-$OUTPUT-$1 nice $ENCNICE $ATOMICPARSLEY $ABCDETEMPDIR/track$1.m4a --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --tracknum ${TRACKNUM:-$1} --year "$CDYEAR" --genre "$CDGENRE" --compilation $VARIOUSBOOL --comment "$COMMENTOUTPUT" --output $ATOMICTEMPFILE
+                       run_command tagtrack-$OUTPUT-$1 nice $ENCNICE $ATOMICPARSLEY "$ABCDETEMPDIR/track$1.m4a" --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --tracknum ${TRACKNUM:-$1} --year "$CDYEAR" --genre "$CDGENRE" --compilation $VARIOUSBOOL --comment "$COMMENTOUTPUT" --output $ATOMICTEMPFILE
                        if [ -f $ATOMICTEMPFILE ]; then
                                mv "$ATOMICTEMPFILE" "$ABCDETEMPDIR/track$1.m4a"
                        fi
@@ -1014,7 +1028,7 @@ do_encode ()
                                TEMPARG="PIPE_$MPPENCODER"
                                ;;
                        m4a)
-                               TEMPARG="PIPE_$MPPENCODER"
+                               TEMPARG="PIPE_$AACENCODERSYNTAX"
                                ;;
                esac
                IN="$( eval echo "\$$TEMPARG" )"
@@ -1054,7 +1068,7 @@ do_encode ()
                                        ;;
                        esac
                        OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
-                       if [ "$NOGAP" = "y" ] && checkstatus encodetrack-$OUTPUT-$1 ; then 
+                       if [ "$NOGAP" = "y" ] && checkstatus encodetrack-$OUTPUT-$1 ; then
                                continue
                        fi
                        if [ X"$USEPIPES" = "Xy" ]; then
@@ -1105,8 +1119,8 @@ do_encode ()
                                case "$2" in
                                %local*%)
                                        case "$FLACENCODERSYNTAX" in
-                                       flac) $RUN_COMMAND nice $EFFECTIVE_NICE $FLACENCODER -f $FLACENCODEROPTS -o "$OUT" "$IN" ;; 
-                                       esac
+                                       flac) $RUN_COMMAND nice $EFFECTIVE_NICE $FLACENCODER -f $FLACENCODEROPTS -o "$OUT" "$IN" ;;
+                                               esac
                                                ;;
                                        *)
                                                vecho -n "DISTMP3:"
@@ -1121,11 +1135,10 @@ do_encode ()
                                                *=*) ;;
                                                *)   COMMENT="COMMENT=$COMMENT" ;;
                                        esac    
-                                       COMMENT="--comment \"$COMMENT\""
                                fi
-                               # Quick hack to avoid tagging Ogg/Speex, since there is no other way to tag than inline tagging
-                               if [ "$DOTAG" = "y" ]; then
-                                       $RUN_COMMAND nice $EFFECTIVE_NICE $SPEEXENCODER $SPEEXENCODEROPTS --author "$TRACKARTIST" --title "$TRACKNAME" "$COMMENT" "$IN" "$OUT"
+                               # Tag the file at encode time, as it can't be done after encoding.
+                               if [ "$DOTAG" = "y" ]; then
+                                       $RUN_COMMAND nice $EFFECTIVE_NICE $SPEEXENCODER $SPEEXENCODEROPTS --author "$TRACKARTIST" --title "$TRACKNAME" ${COMMENT:+--comment "$COMMENT"} "$IN" "$OUT"
                                else
                                        $RUN_COMMAND nice $EFFECTIVE_NICE $SPEEXENCODER $SPEEXENCODEROPTS "$IN" "$OUT"
                                fi
@@ -1140,8 +1153,8 @@ do_encode ()
                                $RUN_COMMAND nice $EFFECTIVE_NICE $MPPENCODER $MPPENCODEROPTS --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --track "$1" --genre "$CDGENRE" --year "$CDYEAR" --comment "$COMMENT" "$IN" "$OUT"
                                ;;
                        m4a)
-                               # Quick hack to avoid tagging Ogg/Speex, since there is no other way to tag than inline tagging
-                               if [ "$DOTAG" = "y" ]; then
+                               # Tag the file at encode time, as it can't be done after encoding.
+                               if [ "$DOTAG" = "y" ]; then
                                        $RUN_COMMAND nice $EFFECTIVE_NICE $AACENCODER $AACENCODEROPTS --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --track "$1" --genre "$CDGENRE" --year "$CDYEAR" --comment "$COMMENT" -o "$OUT" "$IN"
                                        
                                else
@@ -1196,7 +1209,7 @@ do_encode ()
 #                      esac
 #              done
 #              # Only remove .wav if the encoding succeeded
-#              if checkerrors "preprocess-(.{3,4})-$1"; then 
+#              if checkerrors "preprocess-(.{3,4})-$1"; then
 #                      run_command preprocess-$1 false
 #              else
 #                      run_command preprocess-$1 true
@@ -1234,7 +1247,7 @@ do_encode ()
 #                                      run_command postprocess-$OUTPUT-$1 nice $POSTNICE $SPX_POST $IF $OF ;;
 #                      esac
 #                      # Only remove .wav if the encoding succeeded
-#                      if checkerrors "postprocess-(.{3,4})-$1"; then 
+#                      if checkerrors "postprocess-(.{3,4})-$1"; then
 #                              run_command postprocess-$1 false
 #                      else
 #                              run_command postprocess-$1 true
@@ -1364,19 +1377,19 @@ do_move ()
                gettracknum
                # Supported variables for OUTPUTFORMAT are GENRE, YEAR, ALBUMFILE,
                # ARTISTFILE, TRACKFILE, and TRACKNUM.
-               if [ "$ONETRACK" = "y" ]; then 
+               if [ "$ONETRACK" = "y" ]; then
                        if [ "$VARIOUSARTISTS" = "y" ]; then
                                OUTPUTFILE="$(eval echo \""$VAONETRACKOUTPUTFORMAT"\")"
                        else
                                OUTPUTFILE="$(eval echo \""$ONETRACKOUTPUTFORMAT"\")"
-                       fi
+                       fi
                else    
                        if [ "$VARIOUSARTISTS" = "y" ]; then
                                OUTPUTFILE="$(eval echo \""$VAOUTPUTFORMAT"\")"
                        else
                                OUTPUTFILE="$(eval echo \""$OUTPUTFORMAT"\")"
                        fi
-               fi
+               fi
                if checkerrors "tagtrack-$OUTPUT-$1"; then :; else
                        # Once we know the specific output was successful, we can change
                        # the OUTPUT to the value containing the container
@@ -1417,7 +1430,7 @@ do_move ()
                                        ;;
                        esac
                        # Lets move the cue file
-                       if CUEFILE=$(checkstatus cuefile) >/dev/null ; then 
+                       if CUEFILE=$(checkstatus cuefile) >/dev/null ; then
                                if [ -r "$ABCDETEMPDIR/$CUEFILE" ]; then
                                        if checkstatus movecue-$OUTPUT; then :; else
                                                # Silence the Copying output since it overlaps with encoding processes...
@@ -1544,7 +1557,7 @@ do_playlist ()
                ## this will convert the playlist to have CRLF line-endings, if specified
                ## (some hardware players insist on CRLF endings)
                if [ "$DOSPLAYLIST" = "y" ]; then
-                       awk '{substr("\r",""); printf "%s\r\n", $0}' "$OUTPUTDIR/$PLAYLISTFILE" > "$ABCDETEMPDIR/PLAYLISTFILE.tmp"
+                       awk '{sub("\r$",""); printf "%s\r\n", $0}' "$OUTPUTDIR/$PLAYLISTFILE" > "$ABCDETEMPDIR/PLAYLISTFILE.tmp"
 #                      mv -f "$ABCDETEMPDIR/PLAYLISTFILE.tmp" "$OUTPUTDIR/$PLAYLISTFILE"
                        cat "$ABCDETEMPDIR/PLAYLISTFILE.tmp" | sed 's/\//\\/' > "$OUTPUTDIR/$PLAYLISTFILE"
                fi
@@ -1604,7 +1617,7 @@ abcde.cue2discid () {
                        ;;
                PREGAP) PREGAP=$(msf2lba $2)
                        ;;
-               REM)    case "$2" in 
+               REM)    case "$2" in
                        FLAC__lead-out)
                                LEADOUT=$(( $4 / 588 ))
                                ;;
@@ -1658,48 +1671,63 @@ abcde.mkcue () {
 
        echomsf () {
                printf "$1%02i:%02i:%02i\n" $(($2/4500)) $((($2/75)%60)) $(($2%75))
-       } 
+       }
 
        local MODE DISCID TRACKS
-       local i OFFSET LBA 
+       local i OFFSET LBA
+       local CUEWAVFILE
 
+       if [ "$1" = --wholedisc ] ; then
+               MODE=INDEX
+       else
+               MODE=PREGAP
+       fi
 
-    if [ "$1" = --wholedisc ] ; then
-        MODE=INDEX
-    else
-        MODE=PREGAP
-    fi
+       vecho "One track is $ONETRACK"
+       TRACKFILE="$(mungefilename "$TRACKNAME")"
+       ARTISTFILE="$(mungefilename "$TRACKARTIST")"
+       ALBUMFILE="$(mungefilename "$DALBUM")"
+       if [ "$ONETRACK" = "y" ]; then
+               if [ "$VARIOUSARTISTS" = "y" ]; then
+                       CUEWAVFILE="$(eval echo \""$VAONETRACKOUTPUTFORMAT"\" | sed -e 's@^.*/@@').$OUTPUT"
+               else
+                       CUEWAVFILE="$(eval echo \""$ONETRACKOUTPUTFORMAT"\" | sed -e 's@^.*/@@').$OUTPUT"
+               fi
+               vecho "Cue wav file is $CUEWAVFILE"
+       else
+               CUEWAVFILE="dummy.wav"
+       fi
 
-    set -- $TRACKINFO
+       set -- $TRACKINFO
 
-    DISCID=$1
-    TRACKS=$2
-    shift 2
+       DISCID=$1
+       TRACKS=$2
+       shift 2
 
-    echo REM DISCID $DISCID
-    echo FILE \"dummy.wav\" WAVE
+       echo REM DISCID $DISCID
+       echo FILE \""$CUEWAVEFILE"\" WAVE
 
-    if [ $1 -ne 150 ] && [ $MODE = "PREGAP" ] ; then
-        OFFSET=$1
-    else
-        OFFSET=150
-    fi
-
-    i=1
-    while [ $i -le "$TRACKS" ] ; do
-        LBA=$(( $1 - $OFFSET ))
-        printf "  TRACK %02i AUDIO\n" $i
-        if [ $i -eq 1 -a $1 -ne 150 ] ; then
-            if [ $MODE = PREGAP ] ; then
-                echomsf "    PREGAP " $(($OFFSET-150))
-            else
-                echo    "    INDEX 00 00:00:00"
-            fi
+       if [ $1 -ne 150 ] && [ $MODE = "PREGAP" ] ; then
+               OFFSET=$1
+       else
+               OFFSET=150
+       fi
+
+       i=1
+       while [ $i -le "$TRACKS" ] ; do
+               LBA=$(( $1 - $OFFSET ))
+               printf "  TRACK %02i AUDIO\n" $i
+               if [ $i -eq 1 -a $1 -ne 150 ] ; then
+                       if [ $MODE = PREGAP ] ; then
+                               echomsf "    PREGAP " $(($OFFSET-150))
+                       else
+                               echo    "    INDEX 00 00:00:00"
+                       fi
                fi
-        echomsf "    INDEX 01 " $LBA
+               echomsf "    INDEX 01 " $LBA
                i=$(($i+1))
-        shift
-    done
+               shift
+       done
 }
 
 # do_discid
@@ -1715,7 +1743,7 @@ do_discid ()
                vecho -n "Getting CD track info... "
                # In OSX, unmount the disc before a query
                if [ "$OSFLAVOUR" = "OSX" ]; then
-                       disktool -u ${CDROM#/dev/}
+                       diskutil unmount ${CDROM#/dev/}
                fi
                case "$CDROMREADERSYNTAX" in
                        flac)
@@ -1728,7 +1756,7 @@ do_discid ()
                                                        #vecho "Using builtin cue2discid implementation..."
                                                        CUESHEET="$(metaflac $METAFLACOPTS --export-cuesheet-to=- "$CDROM")"
 
-                                                       #TRACKS=$(echo $CUESHEET | egrep "TRACK \+[[:digit:]]\+ \+AUDIO" |wc -l)
+                                                       #TRACKS=$(echo $CUESHEET | grep -E "TRACK \+[[:digit:]]\+ \+AUDIO" |wc -l)
                                                        #TRACKS=0
                                                        OFFSETTIMES=( $(echo "$CUESHEET" | sed -n -e's/\ *INDEX 01\ \+//p' ) )
                                                        TRACKS=${#OFFSETTIMES[@]}
@@ -1739,8 +1767,8 @@ do_discid ()
                                                                #OFFSETS[${#OFFSETS[*]}]=$(( 10#${OFFSETTIME:0:2} * 4500 + 10#${OFFSETTIME:3:2} * 75 + 10#${OFFSETTIME:6:2} ))
                                                        done
 
-                                                       LEADOUT=$(( $(echo "$CUESHEET" | grep lead-out | get_last) / 44100 * 75 ))
-                                                       LEADIN=$(( $(echo "$CUESHEET" | grep lead-in | get_last) / 44100 * 75 ))
+                                                       LEADOUT=$(( $(echo "$CUESHEET" | grep lead-out | get_last) * 75 / 44100 ))
+                                                       LEADIN=$(( $(echo "$CUESHEET" | grep lead-in | get_last) * 75 / 44100 ))
                                                        makeids
                                                ;;
                                                *)
@@ -1760,18 +1788,17 @@ do_discid ()
 #                                      log warning "something went wrong while querying the CD... Maybe a DATA CD?"
 #                              fi
 #
-#                              TRACKS="$(echo "$CDPARANOIAOUTPUT" | egrep '^[[:space:]]+[[:digit:]]' | tail -n 1 | get_first | tr -d "." | tr '\n' ' ')"
+#                              TRACKS="$(echo "$CDPARANOIAOUTPUT" | grep -E '^[[:space:]]+[[:digit:]]' | tail -n 1 | get_first | tr -d "." | tr '\n' ' ')"
 #                              CDPARANOIAAUDIOTRACKS="$TRACKS"
 #
-#                              LEADOUT="$(echo "$CDPARANOIAOUTPUT" | egrep -o '^TOTAL[[:space:]]+([[:digit:]]+)' | get_last)"
-#                              OFFSETS="$(echo "$CDPARANOIAOUTPUT" | sed -n -e's/^ .* \([0-9]\+\) \[.*/\1/p')" 
+#                              LEADOUT="$(echo "$CDPARANOIAOUTPUT" | grep -Eo '^TOTAL[[:space:]]+([[:digit:]]+)' | get_last)"
+#                              OFFSETS="$(echo "$CDPARANOIAOUTPUT" | sed -n -e's/^ .* \([0-9]\+\) \[.*/\1/p')"
 #                              makeids
 #                              ;;
                        *)
                                case "$CDDBMETHOD" in
                                        cddb) TRACKINFO=$($CDDISCID "$CDROM") ;;
-                                       # FIXME # musicbrainz needs a cleanup
-                                       musicbrainz) TRACKINFO=$($MUSICBRAINZ -c "$CDROM" ) ;;
+                                       musicbrainz) TRACKINFO=$($MUSICBRAINZ --command id --device "$CDROM") ;;
                                esac
                                ;;
                esac
@@ -1788,7 +1815,7 @@ do_discid ()
                fi
                # In OSX, remount the disc again
                if [ "$OSFLAVOUR" = "OSX" ]; then
-                       disktool -m ${CDROM#/dev/}
+                       diskutil mount ${CDROM#/dev/}
                fi
                WEHAVEACD=y
                DISCID=$(echo $TRACKINFO | cut -f1 -d' ')
@@ -1820,7 +1847,7 @@ do_discid ()
                                                if [ ! "$RET" = "0" ];then
                                                        log warning "something went wrong while querying the CD... Maybe a DATA CD?"
                                                fi
-                                               TRACKS="$(echo "$CDPARANOIAOUTPUT" | egrep '^[[:space:]]+[[:digit:]]' | tail -n 1 | get_first | tr -d "." | tr '\n' ' ')"
+                                               TRACKS="$(echo "$CDPARANOIAOUTPUT" | grep -E '^[[:space:]]+[[:digit:]]' | tail -n 1 | get_first | tr -d "." | tr '\n' ' ')"
                                                CDPARANOIAAUDIOTRACKS="$TRACKS"
                                        else
                                                # Previous versions of abcde would store the tracks on a file, instead of the status record.
@@ -1903,7 +1930,7 @@ do_discid ()
                if [ ! -f "$ABCDETEMPDIR/discid" ]; then
                        # Wipe and start fresh
                        echo "abcde: $ABCDETEMPDIR/discid not found. Abcde must remove and recreate" >&2
-                       echo -n "this directory to continue. Continue? [y/n] (n)" >&2
+                       echo -n "this directory to continue. Continue [y/N]? " >&2
                        if [ "$INTERACTIVE" = "y" ]; then
                                read ANSWER
                        else
@@ -1952,7 +1979,7 @@ do_discid ()
                        vecho "Creating cue file..."
                        case $CDROMREADERSYNTAX in
                                flac)
-                                       if $METAFLAC --export-cuesheet-to=- "$CDROM" > "$ABCDETEMPDIR/$CUEFILE"; then 
+                                       if $METAFLAC --export-cuesheet-to=- "$CDROM" > "$ABCDETEMPDIR/$CUEFILE"; then
                                                echo cuefile=$CUEFILE >> "$ABCDETEMPDIR/status"
                                        else
                                                log warning "the input flac file does not contain a cuesheet."
@@ -2011,6 +2038,25 @@ do_cleancue()
                                        eval track="\$TRACK$n"
                                        n=$(expr $n + 1)
                                        echo "    TITLE \"$track\"" >> "$CUEFILE_OUT"
+                               # When making a single-track rip, put the
+                               # actual file name into the file declaration
+                               # in the cue file so that it is usable by
+                               # music players and the like
+                               elif [ "$ONETRACK" = "y" ] &&
+                                               echo "$line" | grep '^FILE "dummy.wav" WAVE' > /dev/null 2>&1 ; then
+
+                                       TRACKFILE="$(mungefilename "$TRACKNAME")"
+                                       ARTISTFILE="$(mungefilename "$TRACKARTIST")"
+                                       ALBUMFILE="$(mungefilename "$DALBUM")"
+
+                                       if [ "$VARIOUSARTISTS" = "y" ]; then
+                                               OUTPUTFILE="$(eval echo \""$VAONETRACKOUTPUTFORMAT"\" | sed -e 's@^.*/@@').$OUTPUT"
+                                       else
+                                               OUTPUTFILE="$(eval echo \""$ONETRACKOUTPUTFORMAT"\" | sed -e 's@^.*/@@').$OUTPUT"
+                                       fi
+
+                                       echo "FILE \"$OUTPUTFILE\" WAVE" >> "$CUEFILE_OUT"
+                                       continue
                                fi
 # FIXME # If the lines above are uncommented, remove the line below.
                                echo "$line" >> "$CUEFILE_OUT"
@@ -2094,7 +2140,7 @@ do_localcddb ()
                                        cat "$RESULT" > "${CDDBLOCALREAD}"
                                        {       
                                                echo -n "#$X: "
-                                               do_cddbparse "${CDDBLOCALREAD}" 
+                                               do_cddbparse "${CDDBLOCALREAD}"
                                                echo ""
                                                ##FIXME## QUICK HACK !!!!
                                                if [ ! "$INTERACTIVE" = "y" ]; then break ; fi
@@ -2115,9 +2161,9 @@ do_localcddb ()
                                                read CDDBLOCALCHOICE
                                                [ x"$CDDBLOCALCHOICE" = "x" ] && CDDBLOCALCHOICE="1"
                                                # FIXME # Introduce diff's
-                                               if echo $CDDBLOCALCHOICE | egrep "[[:space:]]*[[:digit:]]+,[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
+                                               if echo $CDDBLOCALCHOICE | grep -E "[[:space:]]*[[:digit:]]+,[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
                                                        diffentries cddblocalread "$CDDBLOCALCHOICES" "$CDDBLOCALCHOICE"
-                                               elif echo $CDDBLOCALCHOICE | egrep "[[:space:]]*[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
+                                               elif echo $CDDBLOCALCHOICE | grep -E "[[:space:]]*[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
                                                        # Make sure we get a valid choice
                                                        CDDBLOCALCHOICENUM=$(echo $CDDBLOCALCHOICE | xargs printf %d 2>/dev/null)
                                                        if [ $CDDBLOCALCHOICENUM -lt 0 ] || [ $CDDBLOCALCHOICENUM -gt $CDDBLOCALCHOICES ]; then
@@ -2149,9 +2195,9 @@ do_localcddb ()
                                # List out disc title/author and contents
                                do_cddbparse "${CDDBLOCALFILE}"
                                #if [ "$CDROMREADERSYNTAX" = "flac" ] ; then
-                               #       echo -n "Embedded cuesheet entry found, use it? [y/n] (y): " >&2
+                               #       echo -n "Embedded cuesheet entry found, use it [Y/n]? " >&2
                                #else
-                                       echo -n "Locally cached CDDB entry found, use it? [y/n] (y): " >&2
+                                       echo -n "Locally cached CDDB entry found, use it [Y/n]? " >&2
                                #fi
                                if [ "$INTERACTIVE" = "y" ]; then
                                        read USELOCALRESP
@@ -2184,15 +2230,143 @@ do_localcddb ()
        fi
 }
 
-do_musicbrainzstat ()
+# do_cdtext
+# Try to read CD-Text from the drive using icedax / cdda2wav
+do_cdtext ()
 {
-       :
+       if new_checkexec icedax; then
+               CDTEXT_READER=icedax
+       elif new_checkexec cdda2wav; then
+               CDTEXT_READER=cdda2wav
+    else
+               # Didn't find either, bail
+               return 0
+       fi
+
+       if [ "$OSFLAVOUR" = "OSX" ] ; then
+               # Hei, we have to unmount the device before running anything like cdda2wav/icedax in OSX
+               diskutil unmount ${CDROM#/dev/}
+               # Also, in OSX the cdrom device for cdda2wav/icedax changes...
+               CDDA2WAVCDROM="IODVDServices"
+       elif [ "$OSFLAVOUR" = "FBSD" ] ; then
+               CDDA2WAVCDROM="$CDROMID"
+       else
+               if [ "$CDROMID" = "" ]; then
+                       CDDA2WAVCDROM="$CDROM"
+               else
+                       CDDA2WAVCDROM="$CDROMID"
+               fi
+       fi
+
+       # Do we have CD-Text on the disc (and can the drive read it?)
+       ${CDTEXT_READER} -J -N -D ${CDDA2WAVCDROM} > "$ABCDETEMPDIR/cd-text" 2>&1
+       grep -q '^CD-Text: detected' "$ABCDETEMPDIR/cd-text"
+       ERRORCODE=$?
+       if [ $ERRORCODE -ne 0 ]; then
+               # No CD-Text found, bail
+               return 0
+       fi
+
+       rm -f "$ABCDETEMPDIR/cddbchoices"
+       CDDBCHOICES=1
+       # Make an empty template
+       $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.1"
+       echo -n "Retrieved 1 CD-Text match..." >> "$ABCDETEMPDIR/cddbchoices"
+       echo "done." >> "$ABCDETEMPDIR/cddbchoices"
+       echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
+       echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
+       ATITLE=$(grep -e '^Album title:' "${ABCDETEMPDIR}/cd-text" | cut -c14- )
+       echo "200 none ${ATITLE}" >> "$ABCDETEMPDIR/cddbquery"
+       # List out disc title/author and contents
+       echo ---- ${ATITLE} ---- >> "$ABCDETEMPDIR/cddbchoices"
+       for TRACK in $(f_seq_row 1 $TRACKS)
+       do
+               TRACKM1=$(($TRACK - 1))
+               TITLE="$(grep -E ^Track\ +$TRACK: "$ABCDETEMPDIR/cd-text" | tr -d \\r\\n | sed 's~^Track ..: .~~g;'"s~'$~~g")"
+               echo "$TRACK: $TITLE" >> "$ABCDETEMPDIR/cddbchoices"
+               sed "s~^TTITLE${TRACKM1}=.*~TTITLE${TRACKM1}=${TITLE}~" "$ABCDETEMPDIR/cddbread.1" > "$ABCDETEMPDIR/cddbread.new"
+        mv -f "$ABCDETEMPDIR/cddbread.new" "$ABCDETEMPDIR/cddbread.1"
+       done
+       sed "s~^DTITLE=.*~DTITLE=${ATITLE}~" "$ABCDETEMPDIR/cddbread.1" > "$ABCDETEMPDIR/cddbread.new"
+    mv -f "$ABCDETEMPDIR/cddbread.new" "$ABCDETEMPDIR/cddbread.1"
+       echo >> "$ABCDETEMPDIR/cddbchoices"
+       echo "cdtext-readcomplete" >> "$ABCDETEMPDIR/status"
 }
 
+# do_musicbrainz
+# Work with the musicbrainz WS API, then transform the results here so
+# they look (very) like the results from CDDB. Maybe not the best way
+# to go, but it Works For Me (TM)
+
 do_musicbrainz ()
 {
-# Use MBE_TOCGetCDIndexId on a perl query
-       :
+       if checkstatus musicbrainz-readcomplete; then :; else
+               vecho "Obtaining Musicbrainz results..."
+               # If MB is to be used, interpret the query results and read all
+               # the available entries.
+               rm -f "$ABCDETEMPDIR/cddbchoices"
+               CDDBCHOICES=1 # Overridden by multiple matches
+               MBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
+               ${MUSICBRAINZ} --command data --discid "$MBDISCID" --workdir "$ABCDETEMPDIR"
+
+               # The helper script will write disc matches out to
+               # cddbread.*. Count how many we have
+               if [ ! -f "${ABCDETEMPDIR}/cddbread.1" ] ; then
+                       # No matches. Use the normal cddb template for the user to
+                       # fill in
+                       echo "No Musicbrainz match." >> "$ABCDETEMPDIR/cddbchoices"
+                       $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.0"
+                       # List out disc title/author and contents of template
+                       echo ---- Unknown Artist / Unknown Album ---- >> "$ABCDETEMPDIR/cddbchoices"
+                       UNKNOWNDISK=y
+                       for TRACK in $(f_seq_row 1 $TRACKS)
+                       do
+                               echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.0" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
+                       done
+                       echo >> "$ABCDETEMPDIR/cddbchoices"
+                       echo cddb-read-0-complete >> "$ABCDETEMPDIR/status"
+                       echo cddb-choice=0 >> "$ABCDETEMPDIR/status"
+                       echo 503 > "$ABCDETEMPDIR/cddbquery"
+               else
+                       # We have some matches
+                       NUM_RESPONSES=$(echo "${ABCDETEMPDIR}"/cddbread.* | wc -w)
+                       if [ "$NUM_RESPONSES" -eq 1 ] ; then
+                               # One exact match
+                               echo -n "Retrieved 1 Musicbrainz match..." >> "$ABCDETEMPDIR/cddbchoices"
+                               echo "done." >> "$ABCDETEMPDIR/cddbchoices"
+                               echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
+                               echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
+                               ATITLE=$(grep -e '^DTITLE=' "${ABCDETEMPDIR}/cddbread.1" | cut -c8- )
+                               echo "200 none ${ATITLE}" >> "$ABCDETEMPDIR/cddbquery"
+                               # List out disc title/author and contents
+                               echo ---- ${ATITLE} ---- >> "$ABCDETEMPDIR/cddbchoices"
+                               for TRACK in $(f_seq_row 1 $TRACKS)
+                               do
+                                       echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.1" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
+                               done
+                               echo >> "$ABCDETEMPDIR/cddbchoices"
+                       else
+                               echo "210 Found exact matches, list follows (until terminating .)" > "$ABCDETEMPDIR/cddbquery"
+                               echo "Multiple Musicbrainz matches:" >> "$ABCDETEMPDIR/cddbchoices"
+                               for file in "$ABCDETEMPDIR"/cddbread.*
+                               do
+                                       X=$(echo $file | sed 's/^.*cddbread\.//g')
+                                       echo cddb-read-$X-complete >> "$ABCDETEMPDIR/status"
+                                       ATITLE=$(grep -e '^DTITLE=' "${ABCDETEMPDIR}"/cddbread.$X | cut -c8- )
+                                       echo "none ${ATITLE}" >> "$ABCDETEMPDIR/cddbquery"
+                                       # List out disc title/author and contents
+                                       echo "#$X: ---- ${ATITLE} ----" >> "$ABCDETEMPDIR/cddbchoices"
+                                       for TRACK in $(f_seq_row 1 $TRACKS)
+                                       do
+                                               echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.$X" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
+                                       done
+                                       echo >> "$ABCDETEMPDIR/cddbchoices"
+                               done
+                               echo "." >> "$ABCDETEMPDIR/cddbquery"
+                       fi
+               fi
+               echo "musicbrainz-readcomplete" >> "$ABCDETEMPDIR/status"
+       fi
 }
 
 # do_cddbstat
@@ -2215,7 +2389,7 @@ do_cddbstat ()
                                210)    # 210 OK, status information follows (until terminating `.')
                                        rc=0
                                        ;;
-                               501)  # 501 Illegal CDDB protocol level: <n>. 
+                               501)  # 501 Illegal CDDB protocol level: <n>.
                                        CDDBPROTO=`expr $CDDBPROTO - 1`
                                        ;;
                                *)      # Try a cddb query, since freedb2.org doesn't support the stat or ver commands
@@ -2228,10 +2402,10 @@ do_cddbstat ()
                                                        ;;
                                        esac
                                        ;;
-                               esac 
+                               esac
                        done
                        if test $rc -eq 1; then
-                               CDDBAVAIL="n" 
+                               CDDBAVAIL="n"
                        fi
                fi
                echo cddb-statcomplete >> "$ABCDETEMPDIR/status"
@@ -2251,7 +2425,7 @@ do_cddbquery ()
                        ERRORCODE=no_query
                        echo 503 > "$ABCDETEMPDIR/cddbquery"
                # The default CDDBLOCALSTATUS is "notfound"
-               # This part will be triggered if the user CDDB repo does not 
+               # This part will be triggered if the user CDDB repo does not
                # contain the entry, or if we are not trying to use the repo.
                else
                        vecho "Querying the CDDB server..."
@@ -2337,7 +2511,7 @@ do_cddbread ()
                        if [ "$(wc -l < "$ABCDETEMPDIR/cddbquery" | tr -d ' ')" -eq 3 ]; then
                                echo "One ${IN}exact match:" >> "$ABCDETEMPDIR/cddbchoices"
                                tail -n +2 "$ABCDETEMPDIR/cddbquery" | head -n 1 >> "$ABCDETEMPDIR/cddbchoices"
-                               echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
+                                                       echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
                        else
                                echo "Multiple ${IN}exact matches:" >> "$ABCDETEMPDIR/cddbchoices"
                        fi
@@ -2398,7 +2572,7 @@ do_cddbedit ()
                                if [ -n "$CHOICE" ] ; then
                                        case $CDDBCHOICES in
                                                -1) if head -1 "$ABCDETEMPDIR/cddbquery" | grep "^$" > /dev/null 2>&1 ; then
-                                                               log error "CDDB query failed!" 
+                                                               log error "CDDB query failed!"
                                                                exit 1
                                                        else
                                                                cat "$ABCDETEMPDIR/cddbchoices"
@@ -2427,13 +2601,13 @@ do_cddbedit ()
                                                echo -n "Which entry would you like abcde to use (0 for none)? [0-$CDDBCHOICES]: " >&2
                                                read CDDBCHOICE
                                                [ X"$CDDBCHOICE" = "X" ] && CDDBCHOICE=1
-                                               if echo $CDDBCHOICE | egrep "[[:space:]]*[[:digit:]]+,[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
+                                               if echo $CDDBCHOICE | grep -E "[[:space:]]*[[:digit:]]+,[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
                                                        if [ ! X"$DIFF" = "X" ]; then
                                                                PARSECHOICE1=$(echo $CDDBCHOICE | cut -d"," -f1 | xargs printf %d 2>/dev/null)
                                                                PARSECHOICE2=$(echo $CDDBCHOICE | cut -d"," -f2 | xargs printf %d 2>/dev/null)
                                                                if [ $PARSECHOICE1 -lt 1 ] || [ $PARSECHOICE1 -gt $CDDBCHOICES ] || \
                                                                   [ $PARSECHOICE2 -lt 1 ] || [ $PARSECHOICE2 -gt $CDDBCHOICES ] || \
-                                                                  [ $PARSECHOICE1 -eq $PARSECHOICE2 ]; then 
+                                                                  [ $PARSECHOICE1 -eq $PARSECHOICE2 ]; then
                                                                        echo "Invalid diff range. Please select two comma-separated numbers between 1 and $CDDBCHOICES" >&2
                                                                else
                                                                        # We parse the 2 choices to diff, store them in temporary files and diff them.
@@ -2451,7 +2625,7 @@ do_cddbedit ()
                                                        else
                                                                echo "The diff program was not found in your path. Please choose a number between 0 and $CDDBCHOICES." >&2
                                                        fi
-                                               elif echo $CDDBCHOICE | egrep "[[:space:]]*[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
+                                               elif echo $CDDBCHOICE | grep -E "[[:space:]]*[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
                                                        # Make sure we get a valid choice
                                                        CDCHOICENUM=$(echo $CDDBCHOICE | xargs printf %d 2>/dev/null)
                                                        if [ $CDCHOICENUM -lt 0 ] || [ $CDCHOICENUM -gt $CDDBCHOICES ]; then
@@ -2481,7 +2655,7 @@ do_cddbedit ()
                                if [ "$USELOCALRESP" = "y" ]; then :; else
                                        if [ -n "$CHOICE" ] ; then
                                                case $CDDBCHOICES in
-                                                       0) 
+                                                       0)
                                                        UNKNOWNDISK=y
                                                        echo "Selected template."
                                                        ;;
@@ -2581,11 +2755,11 @@ do_cddbedit ()
 
        # Some heuristics first. Look at Disc Title, and if it starts with
        # "Various", then we'll assume Various Artists
-       if [ "$(grep ^DTITLE= "$CDDBDATA" | cut -f2- -d= | egrep -ci '^(various|soundtrack|varios|sonora|ost)')" != "0" ]; then
+       if [ "$(grep ^DTITLE= "$CDDBDATA" | cut -f2- -d= | grep -Eci '^(various|soundtrack|varios|sonora|ost)')" != "0" ]; then
                echo "Looks like a Multi-Artist CD" >&2
                VARIOUSARTISTS=y
        else
-               echo -n "Is the CD multi-artist? [y/n] (n): " >&2
+               echo -n "Is the CD multi-artist [y/N]? " >&2
                if [ "$INTERACTIVE" = "y" ]; then
                        read VARIOUSARTISTS
                else
@@ -2597,7 +2771,7 @@ do_cddbedit ()
                # Set a default
                DEFAULTSTYLE=1
                # Need NUMTRACKS before cddb-tool will return it:
-               NUMTRACKS=$(egrep '^TTITLE[0-9]+=' "$CDDBDATA" | wc -l)
+               NUMTRACKS=$(grep -E '^TTITLE[0-9]+=' "$CDDBDATA" | wc -l)
                if [ "$(grep -c "^TTITLE.*\/" "$CDDBDATA")" -gt "$(expr $NUMTRACKS / 2 )" ]; then
                        # More than 1/2 tracks contain a "/", so guess forward
                        DEFAULTSTYLE=1
@@ -2663,11 +2837,11 @@ do_cddbedit ()
        echo "variousartists=$VARIOUSARTISTS" >> "$ABCDETEMPDIR/status"
        echo "variousartiststyle=$VARIOUSARTISTSTYLE" >> "$ABCDETEMPDIR/status"
 
-       if [ "$EDITCDDB" = "y" ] && [ "$UNINTENTIONALLY_ANGER_THE_FREEDB_PEOPLE" = "y" ]; then
+       if [ "$EDITCDDB" = "y" ] && [ "$CDDBMETHOD" = "cddb" ] && [ "$UNINTENTIONALLY_ANGER_THE_FREEDB_PEOPLE" = "y" ]; then
                if [ "$CDDBDATAMD5SUM" != "" ]  && [ "$CDDBDATAMD5SUM" != "$($MD5SUM "$CDDBDATA" | cut -d " " -f 1)" ]; then
                        # This works but does not have the necessary error checking
                        # yet. If you are familiar with the CDDB spec
-                       # (see http://www.freedb.org/src/latest/DBFORMAT) 
+                       # (see http://www.freedb.org/src/latest/DBFORMAT)
                        # and can create an error-free entry on your own, then put
                        # UNINTENTIONALLY_ANGER_THE_FREEDB_PEOPLE=y in your
                        # abcde.conf to enable it. Put CDDBSUBMIT=email@address in
@@ -2676,7 +2850,7 @@ do_cddbedit ()
 
                        # submit the modified file, if they want
                        if [ "$NOSUBMIT" != "y" ]; then
-                               echo -n "Do you want to submit this entry to $CDDBSUBMIT? [y/n] (n): "
+                               echo -n "Do you want to submit this entry to $CDDBSUBMIT [y/N]? "
                                read YESNO
                                while [ "$YESNO" != "y" ] && [ "$YESNO" != "n" ] && [ "$YESNO" != "Y" ] && \
                                        [ "$YESNO" != "N" ] && [ "$YESNO" != "" ]
@@ -2706,7 +2880,7 @@ do_cddbedit ()
 
 # do_cdread [tracknumber]
 # do_cdread onetrack [firsttrack] [lasttrack]
-# 
+#
 do_cdread ()
 {
        # The commands here don't go through run_command because they're never supposed to be silenced
@@ -2721,7 +2895,7 @@ do_cdread ()
                UTRACKNUM=$FIRSTTRACK
                case "$CDROMREADERSYNTAX" in
                        flac) READTRACKNUMS="$FIRSTTRACK.1-$(($LASTTRACK + 1)).0" ;;
-                       cdparanoia) 
+                       cdparanoia)
                                #XX FIXME XX
                                # Add a variable to check if tracks are provided in command line and if not, use "0-" to rip the tracks
                                READTRACKNUMS="$FIRSTTRACK-$LASTTRACK" ;;
@@ -2771,17 +2945,17 @@ do_cdread ()
                        # Avoid problems wit math expressions by unpadding the given UTRACKNUM
                        STRIPTRACKNUM=$(expr $UTRACKNUM + 0)
                        nice $READNICE $FLAC -d -f --cue=${READTRACKNUMS:-$STRIPTRACKNUM.1-$(($STRIPTRACKNUM + 1)).0} "$FILEARG" "$CDROM" ;;
-               cdparanoia) 
-                   if [ "$REDIR" = "y" ]; then
-                           nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG" >&2
+               cdparanoia)
+                       if [ "$REDIR" = "y" ]; then
+                               nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG" >&2
                        else
-                           nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG"
+                               nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG"
                        fi;;
 
                cdda2wav | icedax)
                        if [ "$OSFLAVOUR" = "OSX" ] ; then
                                # Hei, we have to unmount the device before running anything like cdda2wav/icedax in OSX
-                               disktool -u ${CDROM#/dev/} 0
+                               diskutil unmount ${CDROM#/dev/}
                                # Also, in OSX the cdrom device for cdda2wav/icedax changes...
                                CDDA2WAVCDROM="IODVDServices"
                        elif [ "$OSFLAVOUR" = "FBSD" ] ; then
@@ -2793,15 +2967,15 @@ do_cdread ()
                                        CDDA2WAVCDROM="$CDROMID"
                                fi
                        fi
-                   if [ "$REDIR" = "y" ]; then
+                       if [ "$REDIR" = "y" ]; then
                                nice $READNICE $CDROMREADER -D $CDDA2WAVCDROM -t ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG" >&2
-                   else
+                       else
                                nice $READNICE $CDROMREADER -D $CDDA2WAVCDROM -t ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG"
                        fi ;;
                ## FIXME ## We have an exception for dagrab, since it uses -f
                ## FIXME ## Shall we just use -f $FILEARG ??
-               dagrab) 
-                   if [ "$REDIR" = "y" ]; then
+               dagrab)
+               if [ "$REDIR" = "y" ]; then
                                nice $READNICE $CDROMREADER -d "$CDROM" -v $UTRACKNUM "$FILEARG" >&2
                        else
                                nice $READNICE $CDROMREADER -d "$CDROM" -v $UTRACKNUM "$FILEARG"
@@ -2813,7 +2987,7 @@ do_cdread ()
                        FILEPATH=$(find "$FILEPATH" | grep "/$REALTRACKNUM ");
                        # If the file exists, copy it
                        if [ -e "$FILEPATH" ] ; then
-                       if [ "$REDIR" = "y" ]; then
+                               if [ "$REDIR" = "y" ]; then
                                        nice $READNICE $CDROMREADER "$FILEPATH" "$FILEARG" >&2
                                else
                                        nice $READNICE $CDROMREADER "$FILEPATH" "$FILEARG"
@@ -2822,14 +2996,14 @@ do_cdread ()
                                false
                        fi ;;
                debug)
-                   if [ "$REDIR" = "y" ]; then
+                       if [ "$REDIR" = "y" ]; then
                                nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" -w $UTRACKNUM-[:1] "$FILEARG" >&2
                        else
                                nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" -w $UTRACKNUM-[:1] "$FILEARG"
                        fi ;;
        esac
        RETURN=$?
-       # If we get some error or we get some missing wav 
+       # If we get some error or we get some missing wav
        # (as long as we dont use pipes)
        if [ "$RETURN" != "0" -o \( ! -s "$WAVDATA" -a X"$USEPIPES" != "Xy" \) ]; then
                # Thank goodness errors is only machine-parseable up to the
@@ -2857,7 +3031,7 @@ do_cdread ()
 
 # do_cdspeed
 # No values accepted, only uses env variables
-do_cdspeed () 
+do_cdspeed ()
 {
        if "$CDSPEED" "$CDSPEEDOPTS" "$CDSPEEDVALUE" >/dev/null ; then
                vecho "Setting CD speed to ${CDSPEEDVALUE}x"
@@ -2868,10 +3042,23 @@ do_cdspeed ()
 
 # vecho [message]
 #
-# vecho outputs a message if EXTRAVERBOSE is selected
+# vecho outputs a message if EXTRAVERBOSE is 1 or more
 vecho ()
 {
-if [ x"$EXTRAVERBOSE" != "x" ]; then
+if [ x"$EXTRAVERBOSE" != "x" ] && [ $EXTRAVERBOSE -gt 0 ] ; then
+       case $1 in
+               warning) shift ; log warning "$@" ;;
+               *) echo "$@" ;;
+       esac
+fi
+}
+
+# vvecho [message]
+#
+# vvecho outputs a message if EXTRAVERBOSE is 2 or more
+vvecho ()
+{
+if [ x"$EXTRAVERBOSE" != "x" ] && [ $EXTRAVERBOSE -gt 1 ] ; then
        case $1 in
                warning) shift ; log warning "$@" ;;
                *) echo "$@" ;;
@@ -2938,7 +3125,7 @@ post_encode ()
 # Builtin defaults
 
 # CDDB
-# Defaults to FreeDB, but a python musicbrainz can be used
+# Defaults to FreeDB, but musicbrainz can be used too, via the abcde-musicbrainz-tool script
 CDDBMETHOD=cddb
 CDDBURL="http://freedb.freedb.org/~cddb/cddb.cgi"
 CDDBSUBMIT=freedb-submit@freedb.org
@@ -3024,6 +3211,7 @@ EYED3=eyeD3
 VORBISCOMMENT=vorbiscomment
 METAFLAC=metaflac
 AACTAG=faac
+ATOMICPARSLEY=AtomicParsley
 
 CDPARANOIA=cdparanoia
 CDDA2WAV=icedax
@@ -3031,7 +3219,7 @@ DAGRAB=dagrab
 CDDAFS=cp
 CDDISCID=cd-discid
 CDDBTOOL=cddb-tool
-MUSICBRAINZ=musicbrainz-get-tracks
+MUSICBRAINZ=abcde-musicbrainz-tool
 EJECT=eject
 MD5SUM=md5sum
 DISTMP3=distmp3
@@ -3054,11 +3242,14 @@ BLADEENCOPTS=
 L3ENCOPTS=
 XINGMP3ENCOPTS=
 MP3ENCOPTS=
+MP3GAINOPTS="-a -k"
 # ogg
 VORBIZEOPTS=
 OGGENCOPTS=
+VORBISGAINOPTS="--album"
 # flac
 FLACOPTS=
+FLACGAINOPTS="--add-replay-gain"
 # speex
 SPEEXENCOPTS=
 # mpc
@@ -3090,11 +3281,11 @@ MAXPROCS=1
 # List of actions to perform - by default, run to completion
 ACTIONS=cddb,read,encode,tag,move,clean
 
-# This option is basicaly for Debian package dependencies: 
-# List of prefered outputs - by default, run with whatever we have in the path
+# This option is basically for Debian package dependencies:
+# List of preferred outputs - by default, run with whatever we have in the path
 DEFAULT_OUTPUT_BINARIES=vorbis:oggenc,flac:flac,mp3:toolame,mp3:lame,mp3:bladeenc,spx:speex,m4a:faac
 
-# List of prefered cdromreaders - by default, run whichever we have in the path
+# List of preferred cdromreaders - by default, run whichever we have in the path
 DEFAULT_CDROMREADERS="cdparanoia icedax cdda2wav"
 
 # List of quality levels associated with the encoders:
@@ -3103,7 +3294,7 @@ DEFAULT_QUALITY_LO="oggenc:-q 1,lame:-q 7,speex:--quality 5,m4a:"
 DEFAULT_QUALITY_HI="oggenc:-q 7,lame:--preset standard,speex:--quality 9,m4a:"
 DEFAULT_QUALITY_XHI="oggenc:-q 10,lame:--preset extreme,speex:--quality 10,m4a:"
 
-# Asume fetch if under FreeBSD. curl is used for Mac OS X. wget is used for
+# Assume fetch if under FreeBSD. curl is used for Mac OS X. wget is used for
 # Linux/OpenBSD. ftp is user for NetBSD.
 # Let's use these checkings to determine the OS flavour, which will be used
 # later
@@ -3115,8 +3306,8 @@ if [ X$(uname) = "XFreeBSD" ] ; then
 elif [ X$(uname) = "XDarwin" ] ; then
        HTTPGET=curl
        OSFLAVOUR=OSX
-       # We should have disktool in OSX, but let's be sure...
-       NEEDDISKTOOL=y
+       # We should have diskutil in OSX, but let's be sure...
+       NEEDDISKUTIL=y
        CDROMREADERSYNTAX=cddafs
 elif [ X$(uname) = "XOpenBSD" ] ; then
        HTTPGET=wget
@@ -3135,10 +3326,7 @@ else
 fi
 
 # If CDDBAVAIL is set to n, no CDDB read is done
-# If USEID3 is set to n, no ID3 tagging is done
 CDDBAVAIL=y
-USEID3=y
-USEID3V2=y
 
 # There we go..
 # But before we get into business, let us chop off any GREP environmental
@@ -3182,8 +3370,12 @@ fi
 if [ "$CDROM" = "" ] ; then
        if [ -e /dev/cdroms/cdrom0 ]; then
                CDROM=/dev/cdroms/cdrom0
+       elif [ "$OSFLAVOUR" = "OSX" ] && [[ $(diskutil list) =~ CD_part.*(disk.)$'\n' ]]; then
+               CDROM=/dev/${BASH_REMATCH[1]}
        elif [ -e /dev/cdrom ]; then
                CDROM=/dev/cdrom
+       elif [ -e /dev/sr0 ]; then
+               CDROM=/dev/sr0
        elif [ -e /dev/cd0c ]; then
                CDROM=/dev/cd0c
        elif [ -e /dev/acd0c ]; then
@@ -3193,9 +3385,19 @@ if [ "$CDROM" = "" ] ; then
        fi
 fi
 
+# We used to use EXTRAVERBOSE=y to turn on more debug output. Now
+# that's changed to a number to allow for more control. If
+# EXTRAVERBOSE=y, set it to 1 for backwards compatibility.
+if [ "$EXTRAVERBOSE" = "y" ]; then
+       EXTRAVERBOSE=1
+fi
+if [ "$EXTRAVERBOSE" = "n" ]; then
+       EXTRAVERBOSE=0
+fi
+
 # Parse command line options
 #while getopts 1a:bc:C:d:Dehj:klLmMnNo:pPq:r:Rs:S:t:T:vVxw:W: opt ; do
-while getopts 1a:bBc:C:d:Defghj:klLmMnNo:pPr:s:S:t:T:UvVxX:w:W:z opt ; do
+while getopts 1a:bBc:C:d:Defghj:klLmMnNo:pPq:r:s:S:t:T:UvVxX:w:W:z opt ; do
        case "$opt" in
                1) ONETRACK=y ;;
                a) ACTIONS="$OPTARG" ;;
@@ -3231,23 +3433,23 @@ while getopts 1a:bBc:C:d:Defghj:klLmMnNo:pPr:s:S:t:T:UvVxX:w:W:z opt ; do
                t) STARTTRACKNUMBER="$OPTARG" ;;
                T) STARTTRACKNUMBER="$OPTARG" ; STARTTRACKNUMBERTAG="y" ;;
                U) CDDBPROTO=5 ;;
-               v) 
+               v)
                   echo "This is abcde v$VERSION."
                   echo "Usage: abcde [options] [tracks]"
                   echo "abcde -h for extra help"
                   exit
                   ;;
-               V) EXTRAVERBOSE="y" ;;
+               V) EXTRAVERBOSE=$(($EXTRAVERBOSE + 1)) ;;
                x) EJECTCD="y" ;;
                X) CUE2DISCID="$OPTARG" ;;
                w) COMMENT="$OPTARG" ;;
-               W) if echo $OPTARG | grep "[[:digit:]]" > /dev/null 2>&1 ; then 
-                    STARTTRACKNUMBER="${OPTARG}01"
+               W) if echo $OPTARG | grep "[[:digit:]]" > /dev/null 2>&1 ; then
+                        STARTTRACKNUMBER="${OPTARG}01"
                         STARTTRACKNUMBERTAG="y"
                         COMMENT="CD${OPTARG}"
                         DISCNUMBER="${OPTARG}"
                   else
-                    log error "argument of -W must be integer"
+                        log error "argument of -W must be integer"
                         exit 1
                   fi
                   ;;
@@ -3303,14 +3505,14 @@ if [ "$CDROMREADERSYNTAX" = "" ]; then
 fi
 
 # Decide if we can continue.
-if [ "$ONETRACK" = "y" ]; then 
+if [ "$ONETRACK" = "y" ]; then
        # FIXME # remove check as soon as we find out about the other readers
        case "$CDROMREADERSYNTAX" in
                flac) ;;
                cdparanoia) ;;
                cdda2wav | icedax) ;;
                *) log error "$CDROMREADERSYNTAX does not support ONETRACK mode"
-                  exit 1 ;;
+                       exit 1 ;;
        esac
        if [ "$BATCHNORM" = "y" ]; then
                log warning "BATCHNORM mode is not compatible with ONETRACK mode. Disabling..."
@@ -3330,15 +3532,16 @@ else
                # Range parsing code courtesy of Vincent Ho
                # Cleaned up to use shell built-ins by Charles Steinkuehler
                if [ "${1#*[^0-9-]}" != "$1" ]; then
-                       log error "syntax error while processing track numbers"
-               else
+                       log error "syntax error while processing track numbers ($1)"
+                       exit 1
+               else
                        RSTART=${1%%-*}
                        REND=${1##*-}
                        while [ ${RSTART:=1} -le ${REND:=0} ] ; do
                                TRACKQUEUE="$TRACKQUEUE $RSTART"
                                RSTART=$(( $RSTART + 1 ))
                        done
-               fi
+               fi
                shift
        done
 fi
@@ -3395,12 +3598,12 @@ for SHOWCDDBFIELD in $(echo $SHOWCDDBFIELDS | tr , \ ); do
 done
 
 # At this point a CDROM has to be defined, so we check it exists.
-if [ X"$CDROM" != "X" ] ; then 
+if [ X"$CDROM" != "X" ] ; then
        if ( [ "$CDROMREADERSYNTAX" = "cdda2wav" ] || [ "$CDROMREADERSYNTAX" = "icedax" ] ) && [ "$NEEDCDROMID" = "y" ] ; then
                if [ "$OSFLAVOUR" = "FBSD" ]; then
                        if echo "$CDROMID" | grep "^[0-9],[0-9],[0-9]$" >/dev/null 2>&1 ; then :; else
                                log error "CDROMID not in the right format for $CDROMREADERSYNTAX"
-                               log error "Use \"cdrecord -scanbus\" to obtain a adecuate ID an set CDROMID accordingly"
+                               log error "Use \"cdrecord -scanbus\" to obtain an adequate ID and set CDROMID accordingly"
                                exit 1
                        fi
                fi
@@ -3464,7 +3667,7 @@ if [ X"$LOWDISK" = "Xy" ]; then
 fi
 
 # BATCHNORM pre-tests, before we get into serious problems
-# Not compatible with 
+# Not compatible with
 if [ "$BATCHNORM" = "y" ] && [ "$DONORMALIZE" = "n" ]; then
        vecho warning "Disabling BATCHNORM since we are not normalizing!"
        BATCHNORM=n
@@ -3561,7 +3764,7 @@ do
                        [ "$FLACENCODERSYNTAX" = "default" ] && FLACENCODERSYNTAX=flac
                        [ "$DOTAG" = "y" ] && NEEDMETAFLAC=y
                        [ "$DOREPLAYGAIN" = "y" ] && NEEDMETAFLAC=y
-                       [ "$ONETRACK" = "y" ] && [ "$DOCUE" = "y" ] && NEEDMETAFLAC=y 
+                       [ "$ONETRACK" = "y" ] && [ "$DOCUE" = "y" ] && NEEDMETAFLAC=y
                        ;;
                spx)
                        [ "$SPEEXENCODERSYNTAX" = "default" ] && SPEEXENCODERSYNTAX=speexenc
@@ -3636,7 +3839,7 @@ case "$FLACENCODERSYNTAX" in
                        FLACENCODEROPTS="${FLACENCODEROPTS} --replay-gain"
                fi
                # FLAC streams can be encapsulated on a Ogg transport layer
-               if echo "$FLACENCODEROPTS" | egrep -- "(^| )--ogg($| )" > /dev/null 2>&1 ;then
+               if echo "$FLACENCODEROPTS" | grep -E -- "(^| )--ogg($| )" > /dev/null 2>&1 ;then
                        log error "FLAC on an Ogg container is not yet supported"
                        log error "due to problem with adding comments to such files"
                        exit 1
@@ -3670,8 +3873,10 @@ if [ "$ID3TAGV" = "1" ]; then
        TAGGER="$ID3"
        TAGGEROPTS="$ID3OPTS"
 else
-       TAGGER="$ID3V2"
+       TAGGER="$EYED3"
        TAGGEROPTS="$ID3V2OPTS"
+       ID3SYNTAX=eyed3
+       TAGGEROPTS="--set-encoding=utf16-LE $ID3V2OPTS"
 fi
 
 # NOGAP is specific to lame. Other encoders fail ...
@@ -3717,7 +3922,7 @@ if [ "$DISTMP3NICE" ]; then
 fi
 
 # Don't check for stuff if it's not needed
-if [ "$REMOTEHOSTS" ]; then 
+if [ "$REMOTEHOSTS" ]; then
        NEEDDISTMP3=y
 fi
 if [ "$DONORMALIZE" = "y" ]; then
@@ -3798,12 +4003,12 @@ fi
 # Make sure a buncha things exist
 for X in $CDROMREADER $CDDISCID ${NEEDTAGGER+$TAGGER} $MP3ENCODER \
        $OGGENCODER $FLACENCODER $SPEEXENCODER $MPPENCODER \
-       $AACENCODER \
+       $AACENCODER $CDDBTOOL \
        ${NEEDATOMICPARSLEY+$ATOMICPARSLEY} \
        ${NEEDHTTPGET+$HTTPGET} ${NEEDDISTMP3+$DISTMP3} \
        ${NEEDCOMMENTER+$VORBISCOMMENT} ${NEEDMETAFLAC+$METAFLAC} \
        ${NEEDNORMALIZER+$NORMALIZER} ${NEEDEJECT+$EJECT} \
-       ${NEEDDISKTOOL+disktool} ${NEEDCDSPEED+$CDSPEED} \
+       ${NEEDDISKUTIL+diskutil} ${NEEDCDSPEED+$CDSPEED} \
        ${NEEDVORBISGAIN+$VORBISGAIN} ${NEEDMP3GAIN+$MP3GAIN} \
        ${NEEDMPPGAIN+$MPPGAIN} ${NEEDCUEREADER+$CUEREADER} \
        ${NEEDCUE2DISCID+$CUE2DISCID}
@@ -3830,8 +4035,8 @@ fi
 ## Now that we have metaflac, check if we need cue2discid
 #case $CDROMREADERSYNTAX in
 #      flac)
-#              TRACKINFO=$($METAFLAC --show-tag=CDDB $CDROM | cut -d"=" -f2 | egrep "[a-f0-9]{8}")
-#              if [ "$TRACKINFO" = "" ]; then 
+#              TRACKINFO=$($METAFLAC --show-tag=CDDB $CDROM | cut -d"=" -f2 | grep -E "[a-f0-9]{8}")
+#              if [ "$TRACKINFO" = "" ]; then
 #                      checkexec ${NEEDCUE2DISCID+$CUE2DISCID}
 #              fi
 #              ;;
@@ -3855,19 +4060,13 @@ if [ "$DOREAD" = "y" ]; then
        #  - set the CD speed value with eject -x
        vecho -n "Executing customizable pre-read function... "
 
-       pre_read # Execute the user-defined pre-read funtion. Close the CD with it.
+       pre_read # Execute the user-defined pre-read function. Close the CD with it.
 
        vecho "done."
 fi
 
-case "$CDDBMETHOD" in
-       cddb)
-               do_discid # Get ABCDETEMPDIR created and status file initialized
-               ;;
-       musicbrainz)
-               do_musicbrainz id
-               ;;
-esac
+# Get ABCDETEMPDIR created and status file initialized
+do_discid
 
 if [ "$DOCDDB" = "y" ]; then
        # start with a sane default:
@@ -3877,7 +4076,7 @@ if [ "$DOCDDB" = "y" ]; then
        fi
        if checkstatus cddb-choice > /dev/null; then
                :
-       else 
+       else
                if [ "$CDDBLOCALSTATUS" = "notfound" ] ; then
                        case "$CDDBMETHOD" in
                                cddb)
@@ -3890,6 +4089,13 @@ if [ "$DOCDDB" = "y" ]; then
                                        ;;
                        esac
                fi
+               CHOICE=$(checkstatus cddb-choice)
+               if [ "$CHOICE" = 0 ] ; then
+                       # We don't have any information at all; try to fall back
+                       # to CD-Text for basic information
+                       vecho "No CDDB information found, trying cdtext from the CD"
+                       do_cdtext
+               fi
        fi
        do_cddbedit
 
@@ -3918,7 +4124,7 @@ fi
 
 if checkstatus onetrack ; then ONETRACK=y ; fi
 
-if [ "$ONETRACK" = "y" ]; then 
+if [ "$ONETRACK" = "y" ]; then
        # Reuse the CUEFILE in case we created it (now or in a previous run)
        if CUEFILE=$(checkstatus cuefile); then
                IMPORTCUESHEET=y
@@ -3942,7 +4148,7 @@ if [ "$LOWDISK" = "y" ] || [ "$ONETRACK" = "y" ]; then
        echo "encode-output=loud" >> "$ABCDETEMPDIR/status"
 fi
 
-if [ "$ONETRACK" = "y" ]; then 
+if [ "$ONETRACK" = "y" ]; then
        TRACKS="$FIRSTTRACK"
        if [ "$USEPIPES" = "y" ]; then
                if checkstatus readencodetrack-$FIRSTTRACK; then :; else
@@ -3975,7 +4181,7 @@ else
                        fi
                fi
                if [ "$NOGAP" = "y" ] || [ "$BATCHNORM" = "y" ]; then
-                   :
+                       :
                else
                        # If we are not reading, set the encode output to loud already, so
                        # that we can see the output of the first track.
@@ -4009,17 +4215,17 @@ if [ "$NOGAP" = "y" ] || [ "$BATCHNORM" = "y" ] || [ "$ONETRACK" = "y" ]; then
        echo NEXTTRACK
 fi
 
-# Execute the user-defined post_read funtion before ejecting CD
+# Execute the user-defined post_read function before ejecting CD
 post_read
 
 # We are now finished with the cdrom - it can be safely ejected. Note that
 # abcde will not have completed yet.
 if [ "$EJECTCD" = "y" ] && [ -x $(which $EJECT) ]; then
-       # We check if the disk we are processing is actually the disk inside the 
+       # We check if the disk we are processing is actually the disk inside the
        # CD tray. If not, we do not eject the CD, since it might be so that the
        # user ejected it manually.
        #CURRENTTRACKINFO=$($CDDISCID $CDROM)
-       #if if [ "$?" != "1" ] && [ "$CURRENTTRACKINFO" = "$TRACKINFO" ] ; then 
+       #if if [ "$?" != "1" ] && [ "$CURRENTTRACKINFO" = "$TRACKINFO" ] ; then
        # More FreeBSD bits.
        if [ X"$(uname)" = X"FreeBSD" ] ; then
                # FreeBSD eject uses the EJECT environment variable to name the CDROM
@@ -4030,7 +4236,7 @@ if [ "$EJECTCD" = "y" ] && [ -x $(which $EJECT) ]; then
                cd="$(echo $CDROM | sed -e 's=.*/==;s=[a-h]$==;')"
                $eject $EJECTOPTS $cd
        elif [ X"$(uname)" = X"Darwin" ] ; then
-               disktool -e ${CDROM#/dev/} 0
+               diskutil eject ${CDROM#/dev/} 0
        else
                $EJECT $EJECTOPTS "$CDROM"
        fi
@@ -4039,7 +4245,7 @@ fi
 
 ) | (
 
-## Do we need to pre-process 
+## Do we need to pre-process
 #if [ x"$PREPROCESS" = "x" ] ; then
 #      cat
 #else
@@ -4054,7 +4260,7 @@ fi
 #                      # all locations are working, wait and try again later
 #                      if [ ! $PREPROCEED ]; then sleep 3; fi
 #              done
-#              ( do_preprocess $PRETRACKNUM 
+#              ( do_preprocess $PRETRACKNUM
 #              echo "NEXTTRACK"
 #              ) &
 #      done
@@ -4150,11 +4356,11 @@ do
        fi
        # Don't proceed with the rest of the loop if we can't encode
        if [ "$ABORT" ]; then continue; fi
-       ## FIXME ## Add here 
+       ## FIXME ## Add here
        ## run_command tagtrack-$OUTPUT-$1 $METAFLAC $METAFLACOPTS ${IMPORTCUESHEET:+--import-cuesheet-from="$ABCDETEMPDIR/$CUEFILE"} --import-tags-from=- "$ABCDETEMPDIR/track$1.$FLACOUTPUTCONTAINER"
        # Set TRACKNUM, TRACKNAME
        if [ -e "$CDDBDATA" ]; then
-               if [ "$ONETRACK" = "y" ]; then 
+               if [ "$ONETRACK" = "y" ]; then
                        TRACKNAME="$DALBUM"
                        TRACKNUM="$FIRSTTRACK"
                        splitvarious
@@ -4192,7 +4398,7 @@ do
                fi
                # Lets tag the cue file
                if checkstatus cleancuefile >/dev/null; then :; else
-                       if checkstatus cuefile >/dev/null ; then 
+                       if checkstatus cuefile >/dev/null ; then
                                do_cleancue
                        fi
                fi
@@ -4253,8 +4459,8 @@ if [ "$KEEPWAVS" = "y" ];then
 fi
 # Check if we have moved all the formats we had previously encoded, if we are not using the FORCE.
 if [ "$DOCLEAN" = "y" ] && [ ! "$FORCE" = "y" ]; then
-       ENCODED_FORMATS=$(egrep "^encodetrack-(.{3,6})-(.{1,2})$" "$ABCDETEMPDIR/status" | cut -d"-" -f2 | sort -u | tr '\n' '|')
-       MOVED_FORMATS=$(egrep "^movetrack-output-(.{3,6})$" "$ABCDETEMPDIR/status" | cut -d"-" -f3 | sort -u | tr '\n' '|')
+       ENCODED_FORMATS=$(grep -E "^encodetrack-(.{3,6})-(.{1,2})$" "$ABCDETEMPDIR/status" | cut -d"-" -f2 | sort -u | tr '\n' '|')
+       MOVED_FORMATS=$(grep -E "^movetrack-output-(.{3,6})$" "$ABCDETEMPDIR/status" | cut -d"-" -f3 | sort -u | tr '\n' '|')
        if [ "$ENCODED_FORMATS" != "$MOVED_FORMATS" ]; then
                log warning "The encoded formats does not match with the moved ones"
                log warning "Formats encoded: $( echo $ENCODED_FORMATS | tr "|" " " )"