Some quotes added.
[abcde.git] / abcde
diff --git a/abcde b/abcde
index 24c32fc..5a57333 100755 (executable)
--- a/abcde
+++ b/abcde
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 # Copyright (c) 1998-2001 Robert Woodcock <rcw@debian.org>
 # Copyright (c) 2003-2005 Jesus Climent <jesus.climent@hispalinux.es>
 # This code is hereby licensed for public consumption under either the
 # Copyright (c) 1998-2001 Robert Woodcock <rcw@debian.org>
 # Copyright (c) 2003-2005 Jesus Climent <jesus.climent@hispalinux.es>
 # This code is hereby licensed for public consumption under either the
 #
 # Copyright for this work is to expire January 1, 2010, after which it
 # shall be public domain.
 #
 # Copyright for this work is to expire January 1, 2010, after which it
 # shall be public domain.
+#
+# $Id$
 
 
-VERSION="2.3.99"
+VERSION='2.3.99-$Revision$'
 
 usage ()
 {
 
 usage ()
 {
@@ -20,7 +22,8 @@ echo "Usage: abcde [options] [tracks]"
 echo "Options:"
 echo "-1     Encode the whole CD in a single file"
 echo "-a <action1[,action2]...>"
 echo "Options:"
 echo "-1     Encode the whole CD in a single file"
 echo "-a <action1[,action2]...>"
-echo "       Actions to perform (cddb,read,normalize,encode,tag,move,replaygain,playlist,clean)"
+echo "       Actions to perform:"
+echo "       cddb,read,normalize,encode,tag,move,replaygain,playlist,clean"
 #echo "-A     Experimental actions (retag, transcode)"
 echo "-b     Enable batch normalization"
 #echo "-B     Disable batch replaygain (do file by file)"
 #echo "-A     Experimental actions (retag, transcode)"
 echo "-b     Enable batch normalization"
 #echo "-B     Disable batch replaygain (do file by file)"
@@ -32,7 +35,6 @@ echo "-d <device>"
 echo "       Specify CDROM device to grab (flac uses a single-track flac file)"
 echo "-D     Debugging mode (equivalent to sh -x abcde)"
 echo "-e     Erase encoded track information from status file"
 echo "       Specify CDROM device to grab (flac uses a single-track flac file)"
 echo "-D     Debugging mode (equivalent to sh -x abcde)"
 echo "-e     Erase encoded track information from status file"
-#echo "-E     Set the encoding information for the tags"
 echo "-f     Force operations that otherwise are considered harmful. Read \"man abcde\""
 echo "-g     Use \"lame --nogap\" for MP3 encoding. Disables low disk and pipes flags"
 echo "-h     This help information"
 echo "-f     Force operations that otherwise are considered harmful. Read \"man abcde\""
 echo "-g     Use \"lame --nogap\" for MP3 encoding. Disables low disk and pipes flags"
 echo "-h     This help information"
@@ -46,17 +48,19 @@ echo "-N     Noninteractive. Never prompt for anything"
 echo "-m     Modify playlist to include CRLF endings, to comply with some players"
 echo "-M     Create a CUE file"
 echo "-o <type1[,type2]...>"
 echo "-m     Modify playlist to include CRLF endings, to comply with some players"
 echo "-M     Create a CUE file"
 echo "-o <type1[,type2]...>"
-echo "       Output file type(s) (vorbis,mp3,flac,spx,mpc,wav). Defaults to vorbis"
+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)"
 echo "-P     Use UNIX pipes to read+encode without wav files"
 echo "-r <host1[,host2]...>"
 echo "       Also encode on these remote hosts"
 echo "-p     Pad track numbers with 0's (if less than 10 tracks)"
 echo "-P     Use UNIX pipes to read+encode without wav files"
 echo "-r <host1[,host2]...>"
 echo "       Also encode on these remote hosts"
+echo "-R     Use local CDDB in recursive mode"
 #echo "-R     Add replaygain values to the tag info (only for vorbis,flac,mp3)"
 echo "-s <field>"
 echo "       Show dielfs from the CDDB info (year,genre)"
 echo "-S <#> Set the CD speed"
 echo "-t <#> Start the track numbering at a given number"
 echo "-T <#> Same as -t but modifies tag numbering"
 #echo "-R     Add replaygain values to the tag info (only for vorbis,flac,mp3)"
 echo "-s <field>"
 echo "       Show dielfs from the CDDB info (year,genre)"
 echo "-S <#> Set the CD speed"
 echo "-t <#> Start the track numbering at a given number"
 echo "-T <#> Same as -t but modifies tag numbering"
+echo "-u     Use UNICODE tags and comments"
 echo "-v     Show version number and exit"
 echo "-V     Be a bit more verbose about what is happening behind the scenes"
 echo "-x     Eject CD after all tracks are read"
 echo "-v     Show version number and exit"
 echo "-V     Be a bit more verbose about what is happening behind the scenes"
 echo "-x     Eject CD after all tracks are read"
@@ -84,9 +88,9 @@ log ()
        BLURB="$1"
        shift
        case $BLURB in
        BLURB="$1"
        shift
        case $BLURB in
-               error)   echo "[ERR] abcde: $@" >&2 ;;
-               warning) echo "[WAR] $@" >&2 ;;
-               info)    echo "[INF] $@" ;;
+               error)   echo "[ERROR] abcde: $@" >&2 ;;
+               warning) echo "[WARNING] $@" >&2 ;;
+               info)    echo "[INFO] $@" ;;
        esac
 }
 
        esac
 }
 
@@ -104,7 +108,7 @@ f_seq_row ()
 f_seq_line ()
 {
        i=$1
 f_seq_line ()
 {
        i=$1
-       if echo i | grep -q "[[:digit:]]" ; then
+       if echo $i | grep -q "[[:digit:]]" ; then
                while [ $i -ne `expr $2 + 1` ]
                do
                        printf $i" "
                while [ $i -ne `expr $2 + 1` ]
                do
                        printf $i" "
@@ -330,14 +334,57 @@ relpath ()
        echo $TO
 }
 
        echo $TO
 }
 
-# do_getcddbinfo
-# Finds an specific field from cddbinfo
-# 
-#      TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2 -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')
-#      TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -n 1 | cut -f2 -d= | tr -d \[:cntrl:\])
-#      TRACKNAME="$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')"
+checkexec ()
+{
+       if [ ! "$@" = "" ]; then
+               # Cut off the command-line options we just added in
+               X=$(echo $@ | cut -d' ' -f2)
+               if [ "$(which $X)" = "" ]; then
+                       log error "$X is not in your path." >&2
+                       exit 1
+               elif [ ! -x $(which $X) ]; then
+                       log error "$X is not executable." >&2
+                       exit 1
+               fi
+       fi
+}
+
+# diffentries <filename> <max_value> <entry1>,<entry2>
+diffentries ()
+{
+       FILENAME=$1
+       shift
+       local CDDBDIFFCHOICES=$1
+       shift
+       local CDDBDIFFCHOICE="$@"
+       if [ ! X"$DIFF" = "X" ]; then
+               PARSECHOICE1=$(echo $CDDBDIFFCHOICE | cut -d"," -f1 | xargs printf %d 2>/dev/null)
+               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 
+                       echo "Invalid diff range. Please select two coma-separated numbers between 1 and $CDDBDIFFCHOICES" >&2
+               else
+                       # We parse the 2 choices to diff, store them in temporary files and diff them.
+                       for PARSECHOICE in $(echo $CDDBDIFFCHOICE | tr , \ ); do
+                               do_cddbparse "$ABCDETEMPDIR/$FILENAME.$PARSECHOICE" > "$ABCDETEMPDIR/$FILENAME.parsechoice.$PARSECHOICE"
+                       done
+                       echo "Showing diff between choices $PARSECHOICE1 and $PARSECHOICE2..." > "$ABCDETEMPDIR/$FILENAME.diff"
+                       $DIFF $DIFFOPTS "$ABCDETEMPDIR/$FILENAME.parsechoice.$PARSECHOICE1" "$ABCDETEMPDIR/$FILENAME.parsechoice.$PARSECHOICE2" >> "$ABCDETEMPDIR/$FILENAME.diff"
+                       if [ $(cat "$ABCDETEMPDIR/$FILENAME.diff" | wc -l) -ge 24 ]; then
+                               page "$ABCDETEMPDIR/$FILENAME.diff"
+                       else
+                               cat "$ABCDETEMPDIR/$FILENAME.diff" >&2
+                       fi
+               fi
+       else
+               echo "The diff program was not found in your path. Please choose a number between 0 and $CDDBDIFFCHOICES." >&2
+       fi
+}
 
 
-do_getcddbinfo()
+# getcddbinfo
+# Finds an specific field from cddbinfo
+getcddbinfo()
 {
        case $1 in
        TRACKNAME1)
 {
        case $1 in
        TRACKNAME1)
@@ -349,9 +396,9 @@ do_getcddbinfo()
        esac
 }
 
        esac
 }
 
-# do_gettracknum
+# gettracknum
 # Get the track number we are going to use for different actions
 # Get the track number we are going to use for different actions
-do_gettracknum()
+gettracknum()
 {
        if [ -n "$STARTTRACKNUMBER" ] ; then
                # Get the trackpadding from the current track
 {
        if [ -n "$STARTTRACKNUMBER" ] ; then
                # Get the trackpadding from the current track
@@ -366,42 +413,50 @@ do_replaygain()
 {
        if checkstatus replaygain; then :; else
                run_command "" echo "Adding replygain information..."
 {
        if checkstatus replaygain; then :; else
                run_command "" echo "Adding replygain information..."
-               for OUTPUT in $( echo $OUTPUTTYPE | tr , \ )
+               for TMPOUTPUT in $( echo $OUTPUTTYPE | tr , \ )
                do
                do
-                       case $OUTPUT in
+                       case $TMPOUTPUT in
                                vorbis|ogg)
                                        OUTPUT=$OGGOUTPUTCONTAINER
                                        ;;
                                flac)
                                        OUTPUT=$FLACOUTPUTCONTAINER
                                        ;;
                                vorbis|ogg)
                                        OUTPUT=$OGGOUTPUTCONTAINER
                                        ;;
                                flac)
                                        OUTPUT=$FLACOUTPUTCONTAINER
                                        ;;
+                               *)
+                                       OUTPUT=$TMPOUTPUT
+                                       ;;
                        esac
                        OUTPUTFILES=""
                        esac
                        OUTPUTFILES=""
+                       REPLAYINDEX=0
                        for UTRACKNUM in $TRACKQUEUE
                        do
                                CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
                        for UTRACKNUM in $TRACKQUEUE
                        do
                                CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
-                               do_getcddbinfo TRACKNAME
+                               getcddbinfo TRACKNAME
                                splitvarious
                                TRACKFILE="$(mungefilename "$TRACKNAME")"
                                ARTISTFILE="$(mungefilename "$TRACKARTIST")"
                                ALBUMFILE="$(mungefilename "$DALBUM")"
                                splitvarious
                                TRACKFILE="$(mungefilename "$TRACKNAME")"
                                ARTISTFILE="$(mungefilename "$TRACKARTIST")"
                                ALBUMFILE="$(mungefilename "$DALBUM")"
-                               do_gettracknum
+                               gettracknum
                                if [ "$VARIOUSARTISTS" = "y" ]; then
                                if [ "$VARIOUSARTISTS" = "y" ]; then
-                                       OUTPUTFILE=$(eval echo $VAOUTPUTFORMAT)
+                                       OUTPUTFILE="$(eval echo "$VAOUTPUTFORMAT")"
                                else
                                else
-                                       OUTPUTFILE=$(eval echo $OUTPUTFORMAT)
+                                       OUTPUTFILE="$(eval echo "$OUTPUTFORMAT")"
                                fi
                                fi
-                               OUTPUTFILES="$OUTPUTDIR/$OUTPUTFILE.$OUTPUT $OUTPUTFILES"
+                               OUTPUTFILES[$REPLAYINDEX]="$OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
+                               (( REPLAYINDEX = $REPLAYINDEX + 1 ))
                        done
                        case "$OUTPUT" in
                                flac)
                        done
                        case "$OUTPUT" in
                                flac)
-                                       run_command replaygain-flac $METAFLAC --add-replay-gain $OUTPUTFILES
+                                       run_command replaygain-flac $METAFLAC --add-replay-gain "${OUTPUTFILES[@]}"
                                        ;;
                                vorbis|ogg)
                                        ;;
                                vorbis|ogg)
-                                       run_command replaygain-vorbis $VORBISGAIN --album $OUTPUTFILES
+                                       run_command replaygain-vorbis $VORBISGAIN --album "${OUTPUTFILES[@]}"
                                        ;;
                                mp3)
                                        ;;
                                mp3)
-                                       run_command replaygain-mp3 $MP3GAIN -a $OUTPUTFILES
+                                       run_command replaygain-mp3 $MP3GAIN -a "${OUTPUTFILES[@]}"
+                                       ;;
+                               mpc)
+                                       run_command replaygain-mpc $MPPGAIN --auto "${OUTPUTFILES[@]}"
                                        ;;
                                *);;
                        esac
                                        ;;
                                *);;
                        esac
@@ -638,7 +693,7 @@ do_tag ()
        # If we want to start the tracks with a given number, we need to modify the
        # TRACKNUM value before evaluation
        if [ -n "$STARTTRACKNUMBERTAG" ] ; then
        # If we want to start the tracks with a given number, we need to modify the
        # TRACKNUM value before evaluation
        if [ -n "$STARTTRACKNUMBERTAG" ] ; then
-               do_gettracknum
+               gettracknum
        fi
        for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
        do
        fi
        for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
        do
@@ -722,7 +777,7 @@ do_tag ()
                                        *)   echo COMMENT="$COMMENTOUTPUT";;
                                esac    
                        fi
                                        *)   echo COMMENT="$COMMENTOUTPUT";;
                                esac    
                        fi
-                       ) | run_command tagtrack-$OUTPUT-$1 $METAFLAC $METAFLACOPTS --import-tags-from=- "$ABCDETEMPDIR/track$1.$FLACOUTPUTCONTAINER"
+                       ) | run_command tagtrack-$OUTPUT-$1 $METAFLAC $METAFLACOPTS ${IMPORTCUESHEET:+--import-cuesheet-from="$ABCDETEMPDIR/$CUEFILE"} --import-tags-from=- "$ABCDETEMPDIR/track$1.$FLACOUTPUTCONTAINER"
                        ;;
                spx)
                        run_command tagtrack-$OUTPUT-$1 true
                        ;;
                spx)
                        run_command tagtrack-$OUTPUT-$1 true
@@ -730,6 +785,9 @@ do_tag ()
                mpc)
                        run_command tagtrack-$OUTPUT-$1 true
                        ;;
                mpc)
                        run_command tagtrack-$OUTPUT-$1 true
                        ;;
+               m4a)
+                       run_command tagtrack-$OUTPUT-$1 true
+                       ;;
                wav)
                        run_command tagtrack-$OUTPUT-$1 true
                        ;;
                wav)
                        run_command tagtrack-$OUTPUT-$1 true
                        ;;
@@ -812,6 +870,9 @@ do_encode ()
                        mpc)
                                TEMPARG="PIPE_$MPPENCODER"
                                ;;
                        mpc)
                                TEMPARG="PIPE_$MPPENCODER"
                                ;;
+                       m4a)
+                               TEMPARG="PIPE_$MPPENCODER"
+                               ;;
                esac
                IN="$( eval echo "\$$TEMPARG" )"
        else
                esac
                IN="$( eval echo "\$$TEMPARG" )"
        else
@@ -836,22 +897,25 @@ do_encode ()
        fi
        # We need IN to proceed, if we are not using pipes.
        if [ -s "$IN" -o X"$USEPIPES" = "Xy" ] ; then
        fi
        # We need IN to proceed, if we are not using pipes.
        if [ -s "$IN" -o X"$USEPIPES" = "Xy" ] ; then
-               for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
+               for TMPOUTPUT in $(echo $OUTPUTTYPE | tr , \ )
                do
                do
-                       if [ "$NOGAP" = "y" ] && checkstatus encodetrack-$OUTPUT-$1 ; then 
-                               continue
-                       fi
-                       case "$OUTPUT" in
+                       case "$TMPOUTPUT" in
                                vorbis|ogg)
                                        OUT="$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER"
                                vorbis|ogg)
                                        OUT="$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER"
+                                       OUTPUT=$OGGOUTPUTCONTAINER
                                        ;;
                                flac)
                                        OUT="$ABCDETEMPDIR/track$1.$FLACOUTPUTCONTAINER"
                                        ;;
                                flac)
                                        OUT="$ABCDETEMPDIR/track$1.$FLACOUTPUTCONTAINER"
+                                       OUTPUT=$FLACOUTPUTCONTAINER
                                        ;;
                                *)
                                        OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
                                        ;;
                                *)
                                        OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
+                                       OUTPUT=$TMPOUTPUT
                                        ;;
                        esac
                                        ;;
                        esac
+                       if [ "$NOGAP" = "y" ] && checkstatus encodetrack-$OUTPUT-$1 ; then 
+                               continue
+                       fi
                        if [ X"$USEPIPES" = "Xy" ]; then
                                RUN_COMMAND=""
                                # We need a way to store the creation of the files when using PIPES
                        if [ X"$USEPIPES" = "Xy" ]; then
                                RUN_COMMAND=""
                                # We need a way to store the creation of the files when using PIPES
@@ -894,7 +958,7 @@ do_encode ()
                                case "$2" in
                                %local*%)
                                        case "$FLACENCODERSYNTAX" in
                                case "$2" in
                                %local*%)
                                        case "$FLACENCODERSYNTAX" in
-                                       flac) $RUN_COMMAND nice $ENCNICE $FLACENCODER $FLACENCODEROPTS -o "$OUT" "$IN" ;; 
+                                       flac) $RUN_COMMAND nice $ENCNICE $FLACENCODER -f $FLACENCODEROPTS -o "$OUT" "$IN" ;; 
                                        esac
                                                ;;
                                        *)
                                        esac
                                                ;;
                                        *)
@@ -928,6 +992,15 @@ do_encode ()
                                ## FIXME ## to the encoder ends up empty.
                                $RUN_COMMAND nice $ENCNICE $MPPENCODER $MPPENCODEROPTS --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --track "$1" --genre "$CDGENRE" --year "$CDYEAR" --comment "$COMMENT" "$IN" "$OUT"
                                ;;
                                ## FIXME ## to the encoder ends up empty.
                                $RUN_COMMAND nice $ENCNICE $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
+                                       $RUN_COMMAND nice $ENCNICE $AACENCODER $AACENCODEROPTS --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --track "$1" --genre "$CDGENRE" --year "$CDYEAR" --comment "$COMMENT" -o "$OUT" "$IN"
+                                       
+                               else
+                                       $RUN_COMMAND nice $ENCNICE $AACENCODER $AACENCODEROPTS -o "$OUT" "$IN"
+                               fi
+                               ;;
                        wav)
                                # In case of wav output we need nothing. Just keep the wavs.
                                ;;
                        wav)
                                # In case of wav output we need nothing. Just keep the wavs.
                                ;;
@@ -1039,7 +1112,7 @@ do_single_gain ()
 
 # do_batch_gain
 # variables used:
 
 # do_batch_gain
 # variables used:
-# MP3GAIN, MP3GAINOPTS, VORBISGAIN, VORBISGAINOPTS
+# MP3GAIN, MP3GAINOPTS, VORBISGAIN, VORBISGAINOPTS, MPPGAIN, MPPGAINOPTS
 # FIXME #
 do_batch_gain ()
 {
 # FIXME #
 do_batch_gain ()
 {
@@ -1121,8 +1194,12 @@ do_normalize ()
 # TRACKNUM, TRACKNAME, TRACKARTIST, DALBUM, OUTPUTFORMAT, CDGENRE, CDYEAR
 do_move ()
 {
 # TRACKNUM, TRACKNAME, TRACKARTIST, DALBUM, OUTPUTFORMAT, CDGENRE, CDYEAR
 do_move ()
 {
-       for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
+       for TMPOUTPUT in $(echo $OUTPUTTYPE | tr , \ )
        do
        do
+               # For now, set OUTPUT as TMPOUTPUT, and then change it once we have
+               # defined the OUTPUTFILE:
+               OUTPUT="$TMPOUTPUT"
+
                # Create ALBUMFILE, ARTISTFILE, TRACKFILE
                # Munge filenames as follows:
                # ' ' -> '_'
                # Create ALBUMFILE, ARTISTFILE, TRACKFILE
                # Munge filenames as follows:
                # ' ' -> '_'
@@ -1134,26 +1211,30 @@ do_move ()
                ARTISTFILE="$(mungefilename "$TRACKARTIST")"
                TRACKFILE="$(mungefilename "$TRACKNAME")"
                GENRE="$(mungegenre "$GENRE")"
                ARTISTFILE="$(mungefilename "$TRACKARTIST")"
                TRACKFILE="$(mungefilename "$TRACKNAME")"
                GENRE="$(mungegenre "$GENRE")"
-               YEAR="$(echo $CDYEAR)"
-               # If we want to start the tracks with a given number, we need to modify the
-               # TRACKNUM value before evaluation
-               do_gettracknum
-               # Supported variables for OUTPUTFORMAT are GENRE, ALBUMFILE, ARTISTFILE,
-               # TRACKFILE, and TRACKNUM.
+               YEAR=${CDYEAR:-$CDYEAR}
+               # If we want to start the tracks with a given number, we need to modify
+               # the TRACKNUM value before evaluation
+               gettracknum
+               # Supported variables for OUTPUTFORMAT are GENRE, YEAR, ALBUMFILE,
+               # ARTISTFILE, TRACKFILE, and TRACKNUM.
                if [ "$VARIOUSARTISTS" = "y" ]; then
                        OUTPUTFILE="$(eval echo "$VAOUTPUTFORMAT")"
                else
                        OUTPUTFILE="$(eval echo "$OUTPUTFORMAT")"
                fi
                if checkerrors "tagtrack-$OUTPUT-$1"; then :; else
                if [ "$VARIOUSARTISTS" = "y" ]; then
                        OUTPUTFILE="$(eval echo "$VAOUTPUTFORMAT")"
                else
                        OUTPUTFILE="$(eval echo "$OUTPUTFORMAT")"
                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
-                       case $OUTPUT in
+                       # Once we know the specific output was successful, we can change
+                       # the OUTPUT to the value containing the container
+                       case $TMPOUTPUT in
                                vorbis|ogg)
                                        OUTPUT=$OGGOUTPUTCONTAINER
                                        ;;
                                flac)
                                        OUTPUT=$FLACOUTPUTCONTAINER
                                        ;;
                                vorbis|ogg)
                                        OUTPUT=$OGGOUTPUTCONTAINER
                                        ;;
                                flac)
                                        OUTPUT=$FLACOUTPUTCONTAINER
                                        ;;
+                               *)
+                                       OUTPUT=$TMPOUTPUT
+                                       ;;
                        esac
                        # Check that the directory for OUTPUTFILE exists, if it doesn't, create it
                        OUTPUTFILEDIR="$(dirname "$OUTPUTDIR/$OUTPUTFILE")"
                        esac
                        # Check that the directory for OUTPUTFILE exists, if it doesn't, create it
                        OUTPUTFILEDIR="$(dirname "$OUTPUTDIR/$OUTPUTFILE")"
@@ -1223,29 +1304,32 @@ do_move ()
 # VARIOUSARTISTS, OUTPUTDIR
 do_playlist ()
 {
 # VARIOUSARTISTS, OUTPUTDIR
 do_playlist ()
 {
-       for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
+       for TMPOUTPUT in $(echo $OUTPUTTYPE | tr , \ )
        do
        do
-               case $OUTPUT in
+               case $TMPOUTPUT in
                        vorbis|ogg)
                                OUTPUT=$OGGOUTPUTCONTAINER
                                ;;
                        flac)
                                OUTPUT=$FLACOUTPUTCONTAINER
                                ;;
                        vorbis|ogg)
                                OUTPUT=$OGGOUTPUTCONTAINER
                                ;;
                        flac)
                                OUTPUT=$FLACOUTPUTCONTAINER
                                ;;
+                       *)
+                               OUTPUT=$TMPOUTPUT
+                               ;;
                esac
                # Create a playlist file for the playlist data to go into.
                # We used to wipe it out if it existed. Now we request permision if interactive.
                for LASTTRACK in $TRACKQUEUE; do :; done
                ALBUMFILE="$(mungefilename "$DALBUM")"
                ARTISTFILE="$(mungefilename "$DARTIST")"
                esac
                # Create a playlist file for the playlist data to go into.
                # We used to wipe it out if it existed. Now we request permision if interactive.
                for LASTTRACK in $TRACKQUEUE; do :; done
                ALBUMFILE="$(mungefilename "$DALBUM")"
                ARTISTFILE="$(mungefilename "$DARTIST")"
-               GENRE=$(mungegenre "$GENRE")
+               GENRE="$(mungegenre "$GENRE")"
                YEAR=${CDYEAR:-$CDYEAR}
                if [ "$VARIOUSARTISTS" = "y" ] ; then
                YEAR=${CDYEAR:-$CDYEAR}
                if [ "$VARIOUSARTISTS" = "y" ] ; then
-                       PLAYLISTFILE=$(eval echo $VAPLAYLISTFORMAT)
+                       PLAYLISTFILE="$(eval echo "$VAPLAYLISTFORMAT")"
                else
                else
-                       PLAYLISTFILE=$(eval echo $PLAYLISTFORMAT)
+                       PLAYLISTFILE="$(eval echo "$PLAYLISTFORMAT")"
                fi
                fi
-               FINALPLAYLISTDIR=$(dirname "$OUTPUTDIR/$PLAYLISTFILE")
+               FINALPLAYLISTDIR="$(dirname "$OUTPUTDIR/$PLAYLISTFILE")"
                mkdir -p "$FINALPLAYLISTDIR"
                if [ -s "$OUTPUTDIR/$PLAYLISTFILE" ]; then
                        echo -n "Erase, Append to, or Keep the existing playlist file? [e/a/k] (e): " >&2
                mkdir -p "$FINALPLAYLISTDIR"
                if [ -s "$OUTPUTDIR/$PLAYLISTFILE" ]; then
                        echo -n "Erase, Append to, or Keep the existing playlist file? [e/a/k] (e): " >&2
@@ -1273,19 +1357,18 @@ do_playlist ()
                        do
                                # Shares some code with do_move since the filenames have to match
                                CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
                        do
                                # Shares some code with do_move since the filenames have to match
                                CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
-                               #TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2 -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')
-                               do_getcddbinfo TRACKNAME
+                               getcddbinfo TRACKNAME
                                splitvarious
                                TRACKFILE="$(mungefilename "$TRACKNAME")"
                                ARTISTFILE="$(mungefilename "$TRACKARTIST")"
                                ALBUMFILE="$(mungefilename "$DALBUM")"
                                # If we want to start the tracks with a given number, we need to modify the
                                # TRACKNUM value before evaluation
                                splitvarious
                                TRACKFILE="$(mungefilename "$TRACKNAME")"
                                ARTISTFILE="$(mungefilename "$TRACKARTIST")"
                                ALBUMFILE="$(mungefilename "$DALBUM")"
                                # If we want to start the tracks with a given number, we need to modify the
                                # TRACKNUM value before evaluation
-                               do_gettracknum
+                               gettracknum
                                if [ "$VARIOUSARTISTS" = "y" ]; then
                                if [ "$VARIOUSARTISTS" = "y" ]; then
-                                       OUTPUTFILE=$(eval echo $VAOUTPUTFORMAT)
+                                       OUTPUTFILE="$(eval echo "$VAOUTPUTFORMAT")"
                                else
                                else
-                                       OUTPUTFILE=$(eval echo $OUTPUTFORMAT)
+                                       OUTPUTFILE="$(eval echo "$OUTPUTFORMAT")"
                                fi
                                if [ "$VARIOUSARTISTS" = "y" ]; then
                                        if [ "$VAPLAYLISTDATAPREFIX" ] ; then
                                fi
                                if [ "$VARIOUSARTISTS" = "y" ]; then
                                        if [ "$VAPLAYLISTDATAPREFIX" ] ; then
@@ -1329,9 +1412,18 @@ do_discid ()
                        disktool -u ${CDROM#/dev/}
                fi
                if [ "$CDROMREADERSYNTAX" = "flac" ] ; then
                        disktool -u ${CDROM#/dev/}
                fi
                if [ "$CDROMREADERSYNTAX" = "flac" ] ; then
-                       TRACKINFO=$($METAFLAC $METAFLACOPTS --export-cuesheet-to=- $CDROM | $CUE2DISCID)
+                       if $METAFLAC $METAFLACOPTS --export-cuesheet-to=- $CDROM > /dev/null 2>&1 ; then
+                               TRACKINFO=$($METAFLAC $METAFLACOPTS --export-cuesheet-to=- $CDROM | $CUE2DISCID)
+                       else
+                               log error "the input flac file does not contain a cuesheet."
+                               exit 1
+                       fi
                else
                else
-                       TRACKINFO=$($CDDISCID $CDROM)
+                       case "$CDDBMETHOD" in
+                               cddb) TRACKINFO=$($CDDISCID $CDROM) ;;
+                               # FIXME # musicbrainz needs a cleanup
+                               musicbrainz) TRACKINFO=$($MUSICBRAINZ -c $CDROM ) ;;
+                       esac
                fi
                # Make sure there's a CD in there by checking cd-discid's return code
                if [ ! "$?" = "0" ]; then
                fi
                # Make sure there's a CD in there by checking cd-discid's return code
                if [ ! "$?" = "0" ]; then
@@ -1349,6 +1441,7 @@ do_discid ()
                        disktool -m ${CDROM#/dev/}
                fi
                WEHAVEACD=y
                        disktool -m ${CDROM#/dev/}
                fi
                WEHAVEACD=y
+               DISCID=$(echo $TRACKINFO | cut -f1 -d' ')
        else
                TRACKINFO=$(cat "$WAVOUTPUTDIR/abcde.$DISCID/discid")
        fi
        else
                TRACKINFO=$(cat "$WAVOUTPUTDIR/abcde.$DISCID/discid")
        fi
@@ -1380,6 +1473,11 @@ do_discid ()
                                                TRACKS="$(echo "$CDPARANOIAOUTPUT" | egrep '^[[:space:]]+[[:digit:]]' | tail -n 1 | get_first | tr -d "." | tr '\n' ' ')"
                                                CDPARANOIAAUDIOTRACKS="$TRACKS"
                                        else
                                                TRACKS="$(echo "$CDPARANOIAOUTPUT" | egrep '^[[: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.
+                                               if [ -f "$ABCDETEMPDIR/cdparanoia-audio-tracks" ]; then
+                                                       echo cdparanoia-audio-tracks=$( cat "$ABCDETEMPDIR/cdparanoia-audio-tracks" ) >> "$ABCDETEMPDIR/status"
+                                                       rm -f "$ABCDETEMPDIR/cdparanoia-audio-tracks"
+                                               fi
                                                if [ -f "$ABCDETEMPDIR/status" ] && TRACKS=$(checkstatus cdparanoia-audio-tracks); then :; else
                                                        TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
                                                fi
                                                if [ -f "$ABCDETEMPDIR/status" ] && TRACKS=$(checkstatus cdparanoia-audio-tracks); then :; else
                                                        TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
                                                fi
@@ -1466,7 +1564,7 @@ do_discid ()
                                exit 1
                        fi
                        rm -rf "$ABCDETEMPDIR" || exit 1
                                exit 1
                        fi
                        rm -rf "$ABCDETEMPDIR" || exit 1
-                       mkdir "$ABCDETEMPDIR"
+                       mkdir -p "$ABCDETEMPDIR"
                        if [ "$?" -gt "0" ]; then
                                # Directory already exists or could not be created
                                echo "abcde: Temp directory $ABCDETEMPDIR could not be created." >&2
                        if [ "$?" -gt "0" ]; then
                                # Directory already exists or could not be created
                                echo "abcde: Temp directory $ABCDETEMPDIR could not be created." >&2
@@ -1488,25 +1586,36 @@ do_discid ()
                fi
        else
                # We are starting from scratch
                fi
        else
                # We are starting from scratch
-               mkdir "$ABCDETEMPDIR"
+               mkdir -p "$ABCDETEMPDIR"
                if [ "$?" -gt "0" ]; then
                        # Directory already exists or could not be created
                        echo "abcde: Temp directory $ABCDETEMPDIR could not be created." >&2
                        exit 1
                fi
                cat /dev/null > "$ABCDETEMPDIR/status"
                if [ "$?" -gt "0" ]; then
                        # Directory already exists or could not be created
                        echo "abcde: Temp directory $ABCDETEMPDIR could not be created." >&2
                        exit 1
                fi
                cat /dev/null > "$ABCDETEMPDIR/status"
+               # Store the abcde version in the status file.
+               echo "abcde-version=$VERSION" >> "$ABCDETEMPDIR/status"
        fi
        if [ X"$MAKECUEFILE" = "Xy" -a X"$WEHAVEACD" = "Xy" ]; then
                if checkstatus cuefile > /dev/null 2>&1 ; then :; else
                        CUEFILE=cue-$(echo "$TRACKINFO" | cut -f1 -d' ').txt
                        vecho "Creating cue file..."
        fi
        if [ X"$MAKECUEFILE" = "Xy" -a X"$WEHAVEACD" = "Xy" ]; then
                if checkstatus cuefile > /dev/null 2>&1 ; then :; else
                        CUEFILE=cue-$(echo "$TRACKINFO" | cut -f1 -d' ').txt
                        vecho "Creating cue file..."
-                       if $CUEREADER $CUEREADEROPTS > "$ABCDETEMPDIR/$CUEFILE"; then
-                               echo cuefile=$CUEFILE >> "$ABCDETEMPDIR/status"
-                       else
-                               log warning "reading the CUE sheet with mkcue is still considered experimental"
-                               log warning "and there was a problem with the CD reading. abcde will continue,"
-                               log warning "but consider reporting the problem to the abcde author"
-                       fi
+                       case $CDROMREADERSYNTAX in
+                               flac)
+                                       if $METAFLAC --export-cuesheet-to=- $CDROM > "$ABCDETEMPDIR/$CUEFILE"; then :; else
+                                               log warning "the input flac file does not contain a cuesheet."
+                                       fi
+                                       ;;
+                               *)
+                                       if $CUEREADER $CUEREADEROPTS > "$ABCDETEMPDIR/$CUEFILE"; then
+                                               echo cuefile=$CUEFILE >> "$ABCDETEMPDIR/status"
+                                       else
+                                               log warning "reading the CUE sheet with mkcue is still considered experimental"
+                                               log warning "and there was a problem with the CD reading. abcde will continue,"
+                                               log warning "but consider reporting the problem to the abcde author"
+                                       fi
+                                       ;;
+                       esac
                fi
        fi
        # If we got the CDPARANOIA status and it is not recorded, save it now
                fi
        fi
        # If we got the CDPARANOIA status and it is not recorded, save it now
@@ -1518,6 +1627,9 @@ do_discid ()
        
        # Create the discid file
        echo "$TRACKINFO" > "$ABCDETEMPDIR/discid"
        
        # Create the discid file
        echo "$TRACKINFO" > "$ABCDETEMPDIR/discid"
+       if checkstatus cddbmethod > /dev/null 2>&1 ; then :; else
+               echo "cddbmethod=$CDDBMETHOD" >> "$ABCDETEMPDIR/status"
+       fi
 }
 
 # do_cleancue
 }
 
 # do_cleancue
@@ -1587,45 +1699,138 @@ do_localcddb ()
 {
        if checkstatus cddb-readcomplete && checkstatus cddb-choice >/dev/null; then :; else
        
 {
        if checkstatus cddb-readcomplete && checkstatus cddb-choice >/dev/null; then :; else
        
-               CDDBLOCALSUCCESS="n"
+               CDDBLOCALSTATUS="notfound"
                CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
                CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
-               CDDBLOCALFILE="${CDDBLOCALDIR}/${CDDBDISCID}"
                USELOCALRESP="y"
                USELOCALRESP="y"
-               
-               # If the user has selected to check a local CDDB repo, we proceed with it
-               if [ -r "${CDDBLOCALFILE}" ]; then
-                       # List out disc title/author and contents
-                       do_cddbparse "${CDDBLOCALFILE}"
-                       echo -n "Locally cached CDDB entry found, use it? [y/n] (y): "
-                       if [ "$INTERACTIVE" = "y" ]; then
-                               read USELOCALRESP
-                               while [ "$USELOCALRESP" != "y" ] && [ "$USELOCALRESP" != "n" ] && [ "$USELOCALRESP" != "" ] ; do
-                                       echo -n 'Invalid selection. Please answer "y" or "n": '
-                                       read USELOCALRESP
-                               done
-                               [ x"$USELOCALRESP" = "x" ] && USELOCALRESP="y"
-                       else
-                               echo "y" >&2
-                       fi
-                       if [ "$USELOCALRESP" = "y" ]; then
-                               #echo "Using local copy of CDDB data"
-                               echo "# DO NOT ERASE THIS LINE! Added by abcde to imitate cddb output" > "$ABCDETEMPDIR/cddbread.1"
-                               cat "${CDDBLOCALFILE}" >> "$ABCDETEMPDIR/cddbread.1"
-                               echo 999 > "$ABCDETEMPDIR/cddbquery" # Assuming 999 isn't used by CDDB
-                               echo cddb-readcomplete >> "$ABCDETEMPDIR/status"
-                               do_cddbparse "${CDDBLOCALFILE}" > "$ABCDETEMPDIR/cddbchoices"
-                               echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
-                               CDDBLOCALSUCCESS="y"
+
+               if [ "$CDDBLOCALRECURSIVE" = "y" ]; then
+                       CDDBLOCALRESULTS="$(find ${CDDBLOCALDIR} -name "${CDDBDISCID}" -type f 2> /dev/null)"
+                       if [ ! "${CDDBLOCALRESULTS}" = "" ]; then
+                               if   (( $(echo "${CDDBLOCALRESULTS}" | wc -l) == 1 )); then
+                                       CDDBLOCALFILE="${CDDBLOCALRESULTS}"
+                                       CDDBLOCALMATCH=single
+                               elif (( $(echo "${CDDBLOCALRESULTS}" | wc -l) > 1 )); then
+                                       CDDBLOCALMATCH=multiple
+                               fi
                        else
                        else
-                               #echo "Not using local copy of CDDB data"
-                               CDDBLOCALSUCCESS="n"
+                               CDDBLOCALMATCH=none
                        fi
                        fi
+               elif [ "$CDDBLOCALMATCH" = "none" ] && [ -r "${CDDBLOCALDIR}/${CDDBDISCID}" ]; then
+                       CDDBLOCALFILE="${CDDBLOCALDIR}/${CDDBDISCID}"
+                       CDDBLOCALMATCH=single
                else
                else
-                       CDDBLOCALSUCCESS="n"
+                       CDDBLOCALMATCH=none
                fi
                fi
+               
+               # If the user has selected to check a local CDDB repo, we proceed with it
+               case $CDDBLOCALMATCH in
+                       multiple)
+                               echo "Processing multiple matching CDDB entries..." > "$ABCDETEMPDIR/cddblocalchoices"
+                               X=0
+                               echo "$CDDBLOCALRESULTS" | while read RESULT ; do
+                                       X=$(expr $X + 1)
+                                       # List out disc title/author and contents
+                                       CDDBLOCALREAD="$ABCDETEMPDIR/cddblocalread.$X"
+                                       cat "$RESULT" > "${CDDBLOCALREAD}"
+                                       {       
+                                               echo -n "#$X: "
+                                               do_cddbparse "${CDDBLOCALREAD}" 
+                                               echo ""
+                                       } >> "$ABCDETEMPDIR/cddblocalchoices"
+                               done
+                               if [ $(cat "$ABCDETEMPDIR/cddblocalchoices" | wc -l) -ge 24 ]; then
+                                       page "$ABCDETEMPDIR/cddblocalchoices"
+                               else
+                                       # It's all going to fit in one page, cat it
+                                       cat "$ABCDETEMPDIR/cddblocalchoices" >&2
+                               fi
+                               CDDBLOCALCHOICES=$( echo "$CDDBLOCALRESULTS" | wc -l )
+                               # Setting the choice to an impossible integer to avoid errors in the numeric comparisons
+                               CDDBLOCALCHOICENUM=-1
+                               if [ "$INTERACTIVE" = "y" ]; then
+                                       while [ $CDDBLOCALCHOICENUM -lt 0 ] || [ $CDDBLOCALCHOICENUM -gt $CDDBLOCALCHOICES ]; do
+                                               echo -n "Locally cached CDDB entries found. Which one would you like to use (0 for none)? [0-$CDDBLOCALCHOICES]: " >&2
+                                               read CDDBLOCALCHOICE
+                                               [ x"$CDDBLOCALCHOICE" = "x" ] && CDDBLOCALCHOICE="1"
+                                               # FIXME # Introduce diff's
+                                               if echo $CDDBLOCALCHOICE | egrep -q "[[:space:]]*[[:digit:]]+,[[:digit:]]+[[:space:]]*" ; then
+                                                       diffentries cddblocalread "$CDDBLOCALCHOICES" "$CDDBLOCALCHOICE"
+                                               elif echo $CDDBLOCALCHOICE | egrep -q "[[:space:]]*[[:digit:]]+[[:space:]]*" ; 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
+                                                               echo "Invalid selection. Please choose a number between 0 and $CDDBLOCALCHOICES." >&2
+                                                       fi
+                                               fi
+                                       done
+                               else
+                                       ### FIXME ###
+                                       echo "Selected ..."
+                                       CDDBLOCALRESP=y
+                               fi
+                               if [ ! "$CDDBLOCALCHOICENUM" = "0" ]; then
+                                       #echo "Using local copy of CDDB data"
+                                       echo "# DO NOT ERASE THIS LINE! Added by abcde to imitate cddb output" > "$ABCDETEMPDIR/cddbread.1"
+                                       cat "$ABCDETEMPDIR/cddblocalread.$CDDBLOCALCHOICENUM" >> "$ABCDETEMPDIR/cddbread.1"
+                                       echo 999 > "$ABCDETEMPDIR/cddbquery" # Assuming 999 isn't used by CDDB
+                                       echo cddb-readcomplete >> "$ABCDETEMPDIR/status"
+                                       do_cddbparse "$ABCDETEMPDIR/cddbread.1" > "$ABCDETEMPDIR/cddbchoices"
+                                       echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
+                                       CDDBLOCALSTATUS="found"
+                               else
+                                       #echo "Not using local copy of CDDB data"
+                                       CDDBLOCALSTATUS="notfound"
+                               fi
+                               ;;
+                       single)
+                               # 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
+                               else
+                                       echo -n "Locally cached CDDB entry found, use it? [y/n] (y): " >&2
+                               fi
+                               if [ "$INTERACTIVE" = "y" ]; then
+                                       read USELOCALRESP
+                                       while [ "$USELOCALRESP" != "y" ] && [ "$USELOCALRESP" != "n" ] && [ "$USELOCALRESP" != "" ] ; do
+                                               echo -n 'Invalid selection. Please answer "y" or "n": ' >&2
+                                               read USELOCALRESP
+                                       done
+                                       [ x"$USELOCALRESP" = "x" ] && USELOCALRESP="y"
+                               else
+                                       echo "y" >&2
+                               fi
+                               if [ "$USELOCALRESP" = "y" ]; then
+                                       #echo "Using local copy of CDDB data"
+                                       echo "# DO NOT ERASE THIS LINE! Added by abcde to imitate cddb output" > "$ABCDETEMPDIR/cddbread.1"
+                                       cat "${CDDBLOCALFILE}" >> "$ABCDETEMPDIR/cddbread.1"
+                                       echo 999 > "$ABCDETEMPDIR/cddbquery" # Assuming 999 isn't used by CDDB
+                                       echo cddb-readcomplete >> "$ABCDETEMPDIR/status"
+                                       do_cddbparse "${CDDBLOCALFILE}" > "$ABCDETEMPDIR/cddbchoices"
+                                       echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
+                                       CDDBLOCALSTATUS="single"
+                               else
+                                       #echo "Not using local copy of CDDB data"
+                                       CDDBLOCALSTATUS="notfound"
+                               fi
+                               ;;
+                       none)
+                               CDDBLOCALSTATUS="notfound"
+                               ;;
+               esac
        fi
 }
 
        fi
 }
 
+do_musicbrainzstat ()
+{
+       :
+}
+
+do_musizbrainz ()
+{
+       :
+}
+
 # do_cddbstat
 do_cddbstat ()
 {
 # do_cddbstat
 do_cddbstat ()
 {
@@ -1671,7 +1876,7 @@ do_cddbquery ()
                if [ "$CDDBAVAIL" = "n" ]; then
                        ERRORCODE=no_query
                        echo 503 > "$ABCDETEMPDIR/cddbquery"
                if [ "$CDDBAVAIL" = "n" ]; then
                        ERRORCODE=no_query
                        echo 503 > "$ABCDETEMPDIR/cddbquery"
-               # The default CDDBLOCALSUCCESS is "n"
+               # The default CDDBLOCALSTATUS is "notfound"
                # 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
                # 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
@@ -1810,7 +2015,7 @@ do_cddbedit ()
        if [ "$INTERACTIVE" = "y" ]; then
                # We should show the CDDB results both when we are not using the local CDDB repo
                # or when we are using it but we could not find a proper match
        if [ "$INTERACTIVE" = "y" ]; then
                # We should show the CDDB results both when we are not using the local CDDB repo
                # or when we are using it but we could not find a proper match
-               if [ "$CDDBUSELOCAL" = "y" ] && [ ! "$CDDBLOCALSUCCESS" = "y" ] || [ ! "$CDDBUSELOCAL" = "y" ]; then
+               if [ "$CDDBUSELOCAL" = "y" ] && [ "$CDDBLOCALSTATUS" = "notfound" ] || [ ! "$CDDBUSELOCAL" = "y" ]; then
                        # Display the $ABCDETEMPDIR/cddbchoices file created above
                        # Pick a pager so that if the tracks overflow the screen the user can still view everything
                        if [ -r "$ABCDETEMPDIR/cddbchoices" ]; then
                        # Display the $ABCDETEMPDIR/cddbchoices file created above
                        # Pick a pager so that if the tracks overflow the screen the user can still view everything
                        if [ -r "$ABCDETEMPDIR/cddbchoices" ]; then
@@ -1818,6 +2023,13 @@ do_cddbedit ()
                                CHOICE=$(checkstatus cddb-choice)
                                if [ -n "$CHOICE" ] ; then
                                        case $CDDBCHOICES in
                                CHOICE=$(checkstatus cddb-choice)
                                if [ -n "$CHOICE" ] ; then
                                        case $CDDBCHOICES in
+                                               -1) if head -1 "$ABCDETEMPDIR/cddbquery" | grep -q "^$" ; then
+                                                               log error "CDDB query failed!" 
+                                                               exit 1
+                                                       else
+                                                               cat "$ABCDETEMPDIR/cddbchoices"
+                                                       fi
+                                                       ;;
                                                1) cat "$ABCDETEMPDIR/cddbchoices" ;;
                                                *)
                                                echo "Selected: #$CHOICE"
                                                1) cat "$ABCDETEMPDIR/cddbchoices" ;;
                                                *)
                                                echo "Selected: #$CHOICE"
@@ -1841,16 +2053,15 @@ 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
                                                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 -q "[[:digit:]]+,[[:digit:]]+" ; then
+                                               if echo $CDDBCHOICE | egrep -q "[[:space:]]*[[:digit:]]+,[[:digit:]]+[[:space:]]*" ; then
                                                        if [ ! X"$DIFF" = "X" ]; then
                                                        if [ ! X"$DIFF" = "X" ]; then
-                                                               PARSECHOICE1=$(echo $CDDBCHOICE | cut -d"," -f1)
-                                                               PARSECHOICE2=$(echo $CDDBCHOICE | cut -d"," -f2)
+                                                               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 
                                                                        echo "Invalid diff range. Please select two coma-separated numbers between 1 and $CDDBCHOICES" >&2
                                                                else
                                                                if [ $PARSECHOICE1 -lt 1 ] || [ $PARSECHOICE1 -gt $CDDBCHOICES ] || \
                                                                   [ $PARSECHOICE2 -lt 1 ] || [ $PARSECHOICE2 -gt $CDDBCHOICES ] || \
                                                                   [ $PARSECHOICE1 -eq $PARSECHOICE2 ]; then 
                                                                        echo "Invalid diff range. Please select two coma-separated numbers between 1 and $CDDBCHOICES" >&2
                                                                else
-
                                                                        # We parse the 2 choices to diff, store them in temporary files and diff them.
                                                                        for PARSECHOICE in $(echo $CDDBCHOICE | tr , \ ); do
                                                                                do_cddbparse "$ABCDETEMPDIR/cddbread.$PARSECHOICE" > "$ABCDETEMPDIR/cddbread.parsechoice.$PARSECHOICE"
                                                                        # We parse the 2 choices to diff, store them in temporary files and diff them.
                                                                        for PARSECHOICE in $(echo $CDDBCHOICE | tr , \ ); do
                                                                                do_cddbparse "$ABCDETEMPDIR/cddbread.$PARSECHOICE" > "$ABCDETEMPDIR/cddbread.parsechoice.$PARSECHOICE"
@@ -1866,10 +2077,10 @@ do_cddbedit ()
                                                        else
                                                                echo "The diff program was not found in your path. Please choose a number between 0 and $CDDBCHOICES." >&2
                                                        fi
                                                        else
                                                                echo "The diff program was not found in your path. Please choose a number between 0 and $CDDBCHOICES." >&2
                                                        fi
-                                               else
+                                               elif echo $CDDBCHOICE | egrep -q "[[:space:]]*[[:digit:]]+[[:space:]]*" ; 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
                                                        if [ $CDCHOICENUM -lt 0 ] || [ $CDCHOICENUM -gt $CDDBCHOICES ]; then
-                                                               # Make sure we get a valid choice
-                                                               CDCHOICENUM=$(echo $CDDBCHOICE | xargs printf %d 2>/dev/null)
                                                                echo "Invalid selection. Please choose a number between 0 and $CDDBCHOICES." >&2
                                                        fi
                                                fi
                                                                echo "Invalid selection. Please choose a number between 0 and $CDDBCHOICES." >&2
                                                        fi
                                                fi
@@ -1917,7 +2128,7 @@ do_cddbedit ()
                        # Show the choice if we are not using the locally stored one
                        # or when the local search failed to find a match.
                        PREVIOUSCHOICE=$(checkstatus cddb-choice)
                        # Show the choice if we are not using the locally stored one
                        # or when the local search failed to find a match.
                        PREVIOUSCHOICE=$(checkstatus cddb-choice)
-                       if [ "$CDDBUSELOCAL" = "y" ] && [ "$CDDBLOCALSUCCESS" = "n" ] || [ ! "$CDDBUSELOCAL" = "y" ]; then
+                       if [ "$CDDBUSELOCAL" = "y" ] && [ "$CDDBLOCALSTATUS" = "notfound" ] || [ ! "$CDDBUSELOCAL" = "y" ]; then
                                #if [ "$PREVIOUSCHOICE" ]; then
                                        cat "$ABCDETEMPDIR/cddbchoices"
                                #fi
                                #if [ "$PREVIOUSCHOICE" ]; then
                                        cat "$ABCDETEMPDIR/cddbchoices"
                                #fi
@@ -2107,10 +2318,12 @@ do_cddbedit ()
                        fi
                fi
        fi
                        fi
                fi
        fi
-       # Make sure the cache directory exists
-       mkdir -p $CDDBLOCALDIR
+       ### FIXME ###
+       # User CDDBLOCALPOLICY to find out if we store the file or not...
        # Cache edited CDDB entry in the user's cddb dir
        # Cache edited CDDB entry in the user's cddb dir
-       if [ "$CDDBCOPYLOCAL" = "y" ] || [ "$COPYCDDBLOCAL" = "Y" ]; then
+       if [ "$CDDBCOPYLOCAL" = "y" ]; then
+               # Make sure the cache directory exists
+               mkdir -p $CDDBLOCALDIR
                cat "$CDDBDATA" | tail -n $(expr $(cat "$CDDBDATA" | wc -l ) - 1 ) > ${CDDBLOCALDIR}/$(echo "$TRACKINFO" | cut -d' ' -f1)
        fi
 
                cat "$CDDBDATA" | tail -n $(expr $(cat "$CDDBDATA" | wc -l ) - 1 ) > ${CDDBLOCALDIR}/$(echo "$TRACKINFO" | cut -d' ' -f1)
        fi
 
@@ -2130,15 +2343,14 @@ do_cdread ()
                # the user said
                # We need the first and last track for cdda2wav
                FIRSTTRACK=$2
                # the user said
                # We need the first and last track for cdda2wav
                FIRSTTRACK=$2
-               LASTTRACK=$3
+               LASTTRACK=$(expr $3 + 0)
                UTRACKNUM=$FIRSTTRACK
                case "$CDROMREADERSYNTAX" in
                UTRACKNUM=$FIRSTTRACK
                case "$CDROMREADERSYNTAX" in
-                       ### FIXME ### How are we calculating the tracks in flac mode?
                        flac) READTRACKNUMS="$FIRSTTRACK.1-$(($LASTTRACK + 1)).0" ;;
                        cdparanoia) READTRACKNUMS="$FIRSTTRACK-$LASTTRACK" ;;
                        flac) READTRACKNUMS="$FIRSTTRACK.1-$(($LASTTRACK + 1)).0" ;;
                        cdparanoia) READTRACKNUMS="$FIRSTTRACK-$LASTTRACK" ;;
-                       cdda2wav) READTRACKNUMS="$FIRSTTRACK+$LASTRACK" ;;
-                       *) log error "$CDROMREADERSYNTAX does not support ONETRACK mode"
-                          exit 1 ;;
+                       cdda2wav) READTRACKNUMS="$FIRSTTRACK+$LASTTRACK" ;;
+                       *) echo "abcde error: $CDROMREADERSYNTAX does not support ONETRACK mode"
+                               exit 1 ;;
                esac
        else
                UTRACKNUM=$1
                esac
        else
                UTRACKNUM=$1
@@ -2169,8 +2381,7 @@ do_cdread ()
                echo "Grabbing ${PIPE_MESSAGE}tracks $UTRACKNUM - $LASTTRACK as one track ..." >&2
        else
                if [ -r "$CDDBDATA" ]; then
                echo "Grabbing ${PIPE_MESSAGE}tracks $UTRACKNUM - $LASTTRACK as one track ..." >&2
        else
                if [ -r "$CDDBDATA" ]; then
-                       #TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -n 1 | cut -f2 -d= | tr -d \[:cntrl:\])
-                       do_getcddbinfo TRACKNAME
+                       getcddbinfo TRACKNAME
                        echo "Grabbing ${PIPE_MESSAGE}track $UTRACKNUM: $TRACKNAME..." >&2
                else
                        echo "Grabbing ${PIPE_MESSAGE}track $UTRACKNUM..." >&2
                        echo "Grabbing ${PIPE_MESSAGE}track $UTRACKNUM: $TRACKNAME..." >&2
                else
                        echo "Grabbing ${PIPE_MESSAGE}track $UTRACKNUM..." >&2
@@ -2180,7 +2391,9 @@ do_cdread ()
                ### FIXME ### use an exception for flac, since it uses -o
                ### FIXME ### Shall we just use -o $FILEARG ??
                flac)
                ### FIXME ### use an exception for flac, since it uses -o
                ### FIXME ### Shall we just use -o $FILEARG ??
                flac)
-                       nice $READNICE $FLAC -d --cue=${READTRACKNUMS:-$UTRACKNUM.1-$(($UTRACKNUM + 1)).0} "$FILEARG" "$CDROM" ;;
+                       # 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) 
                        nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS $CDROM ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG" $REDIR ;;
                cdda2wav)
                cdparanoia) 
                        nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS $CDROM ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG" $REDIR ;;
                cdda2wav)
@@ -2238,9 +2451,9 @@ do_cdread ()
                        echo readencodetrack-$UTRACKNUM >> "$ABCDETEMPDIR/status"
                else
                        echo readtrack-$UTRACKNUM >> "$ABCDETEMPDIR/status"
                        echo readencodetrack-$UTRACKNUM >> "$ABCDETEMPDIR/status"
                else
                        echo readtrack-$UTRACKNUM >> "$ABCDETEMPDIR/status"
-                       if [ "$1" = "onetrack" ]; then
-                               echo onetrack >> "$ABCDETEMPDIR/status"
-                       fi
+               fi
+               if [ "$1" = "onetrack" ]; then
+                       echo onetrack >> "$ABCDETEMPDIR/status"
                fi
        fi
 }
                fi
        fi
 }
@@ -2263,7 +2476,7 @@ vecho ()
 {
 if [ x"$EXTRAVERBOSE" != "x" ]; then
        case $1 in
 {
 if [ x"$EXTRAVERBOSE" != "x" ]; then
        case $1 in
-               warning) log warning "$@" ;;
+               warning) shift ; log warning "$@" ;;
                *) echo "$@" ;;
        esac
 fi
                *) echo "$@" ;;
        esac
 fi
@@ -2319,11 +2532,17 @@ post_read ()
 ###############################################################################
 
 # Builtin defaults
 ###############################################################################
 
 # Builtin defaults
+
+# CDDB
+# Defaults to FreeDB, but a python musicbrainz can be used
+CDDBMETHOD=cddb
 CDDBURL="http://freedb.freedb.org/~cddb/cddb.cgi"
 CDDBSUBMIT=freedb-submit@freedb.org
 CDDBPROTO=5
 HELLOINFO="$(whoami)@$(hostname)"
 CDDBCOPYLOCAL="n"
 CDDBURL="http://freedb.freedb.org/~cddb/cddb.cgi"
 CDDBSUBMIT=freedb-submit@freedb.org
 CDDBPROTO=5
 HELLOINFO="$(whoami)@$(hostname)"
 CDDBCOPYLOCAL="n"
+CDDBLOCALPOLICY="always"
+CDDBLOCALRECURSIVE="y"
 CDDBLOCALDIR="$HOME/.cddb"
 CDDBUSELOCAL="n"
 
 CDDBLOCALDIR="$HOME/.cddb"
 CDDBUSELOCAL="n"
 
@@ -2331,7 +2550,7 @@ CDDBUSELOCAL="n"
 SHOWCDDBFIELDS="year,genre"
 
 INTERACTIVE=y
 SHOWCDDBFIELDS="year,genre"
 
 INTERACTIVE=y
-CDROMREADERSYNTAX=cdparanoia
+#CDROMREADERSYNTAX=cdparanoia
 ENCODERSYNTAX=default
 
 MP3ENCODERSYNTAX=default
 ENCODERSYNTAX=default
 
 MP3ENCODERSYNTAX=default
@@ -2339,6 +2558,7 @@ OGGENCODERSYNTAX=default
 FLACENCODERSYNTAX=default
 SPEEXENCODERSYNTAX=default
 MPPENCODERSYNTAX=default
 FLACENCODERSYNTAX=default
 SPEEXENCODERSYNTAX=default
 MPPENCODERSYNTAX=default
+AACENCODERSYNTAX=default
 NORMALIZERSYNTAX=default
 CUEREADERSYNTAX=default
 
 NORMALIZERSYNTAX=default
 CUEREADERSYNTAX=default
 
@@ -2390,6 +2610,8 @@ FLAC=flac
 SPEEXENC=speexenc
 # mpp (Musepack)
 MPPENC=mppenc
 SPEEXENC=speexenc
 # mpp (Musepack)
 MPPENC=mppenc
+# m4a
+AACENC=faac
 
 ID3=id3
 ID3V2=id3v2
 
 ID3=id3
 ID3V2=id3v2
@@ -2400,6 +2622,7 @@ DAGRAB=dagrab
 CDDAFS=cp
 CDDISCID=cd-discid
 CDDBTOOL=cddb-tool
 CDDAFS=cp
 CDDISCID=cd-discid
 CDDBTOOL=cddb-tool
+MUSICBRAINZ=musicbrainz-get-tracks
 EJECT=eject
 MD5SUM=md5sum
 DISTMP3=distmp3
 EJECT=eject
 MD5SUM=md5sum
 DISTMP3=distmp3
@@ -2409,6 +2632,7 @@ NORMALIZE=normalize-audio
 CDSPEED=eject
 VORBISGAIN=vorbisgain
 MP3GAIN=mp3gain
 CDSPEED=eject
 VORBISGAIN=vorbisgain
 MP3GAIN=mp3gain
+MPPGAIN=replaygain
 MKCUE=mkcue
 MKTOC=cdrdao
 DIFF=diff
 MKCUE=mkcue
 MKTOC=cdrdao
 DIFF=diff
@@ -2427,11 +2651,13 @@ MP3ENCOPTS=
 VORBIZEOPTS=
 OGGENCOPTS=
 # flac
 VORBIZEOPTS=
 OGGENCOPTS=
 # flac
-FLACOPTS="-f"
+FLACOPTS=
 # speex
 SPEEXENCOPTS=
 # mpc
 MPPENCOPTS=
 # speex
 SPEEXENCOPTS=
 # mpc
 MPPENCOPTS=
+# m4a
+AACENCOPTS=
 
 ID3OPTS=
 ID3V2OPTS=
 
 ID3OPTS=
 ID3V2OPTS=
@@ -2457,10 +2683,13 @@ MAXPROCS=1
 # List of actions to perform - by default, run to completion
 ACTIONS=cddb,read,encode,tag,move,clean
 
 # 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
 # List of prefered outputs - by default, run with whatever we have in the path
-# This option is basicaly for Debian package dependencies
 DEFAULT_OUTPUT_BINARIES=vorbis:oggenc,flac:flac,mp3:lame,mp3:bladeenc,spx:speex
 
 DEFAULT_OUTPUT_BINARIES=vorbis:oggenc,flac:flac,mp3:lame,mp3:bladeenc,spx:speex
 
+# List of prefered cdromreaders - by default, run whichever we have in the path
+DEFAULT_CDROMREADERS="cdparanoia cdda2wav"
+
 # Asume fetch if under FreeBSD. curl is used for Mac OS X. wget is used for Linux/OpenBSD/NetBSD.
 # Let's use these checkings to determine the OS flavour, which will be used later
 if [ X$(uname) = "XFreeBSD" ] ; then
 # Asume fetch if under FreeBSD. curl is used for Mac OS X. wget is used for Linux/OpenBSD/NetBSD.
 # Let's use these checkings to determine the OS flavour, which will be used later
 if [ X$(uname) = "XFreeBSD" ] ; then
@@ -2538,8 +2767,8 @@ if [ "$CDROM" = "" ] ; then
 fi
 
 # Parse command line options
 fi
 
 # Parse command line options
-#while getopts 1a:A:bc:C:d:Dehj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W: opt ; do
-while getopts 1a:bBc:C:d:Defghj:klLmMnNo:pPr:s:S:t:T:vVxw:W:z opt ; do
+#while getopts 1a:bc:C:d:Dehj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W: opt ; do
+while getopts 1a:bBc:C:d:Defghj:klLmMnNo:pPr:s:S:t:T:uvVxw:W:z opt ; do
        case "$opt" in
                1) ONETRACK=y ;;
                a) ACTIONS="$OPTARG" ;;
        case "$opt" in
                1) ONETRACK=y ;;
                a) ACTIONS="$OPTARG" ;;
@@ -2547,7 +2776,7 @@ while getopts 1a:bBc:C:d:Defghj:klLmMnNo:pPr:s:S:t:T:vVxw:W:z opt ; do
                b) BATCHNORM=y ;;
                B) NOBATCHREPLAYGAIN=y ;;
                c) if [ -e "$OPTARG" ] ; then . "$OPTARG" ; else log error "config file \"$OPTARG\" cannot be found." ; exit 1 ; fi ;;
                b) BATCHNORM=y ;;
                B) NOBATCHREPLAYGAIN=y ;;
                c) if [ -e "$OPTARG" ] ; then . "$OPTARG" ; else log error "config file \"$OPTARG\" cannot be found." ; exit 1 ; fi ;;
-               C) DISCID="${OPTARG#abcde.}" ;;
+               C) DISCID="$( echo ${OPTARG#abcde.} | tr -d /)" ;;
                d) CDROM="$OPTARG" ;;
                D) set -x ;;
                e) ERASEENCODEDSTATUS=y ;;
                d) CDROM="$OPTARG" ;;
                D) set -x ;;
                e) ERASEENCODEDSTATUS=y ;;
@@ -2572,11 +2801,9 @@ while getopts 1a:bBc:C:d:Defghj:klLmMnNo:pPr:s:S:t:T:vVxw:W:z opt ; do
                R) DOREPLAYGAIN=y ;;
                s) SHOWCDDBFIELDS="$OPTARG" ;;
                S) CDSPEEDVALUE="$OPTARG" ;;
                R) DOREPLAYGAIN=y ;;
                s) SHOWCDDBFIELDS="$OPTARG" ;;
                S) CDSPEEDVALUE="$OPTARG" ;;
-#              t) PREPROCESSFORMATS="$OPTARG"
-#                 PREPROCESS=y ;;
-#              T) POSTPROCESSFORMATS="$OPTARG" ;;
                t) STARTTRACKNUMBER="$OPTARG" ;;
                T) STARTTRACKNUMBER="$OPTARG" ; STARTTRACKNUMBERTAG="y" ;;
                t) STARTTRACKNUMBER="$OPTARG" ;;
                T) STARTTRACKNUMBER="$OPTARG" ; STARTTRACKNUMBERTAG="y" ;;
+               u) CDDBPROTO=6 ;;
                v) 
                   echo "This is abcde v$VERSION."
                   echo "Usage: abcde [options] [tracks]"
                v) 
                   echo "This is abcde v$VERSION."
                   echo "Usage: abcde [options] [tracks]"
@@ -2602,11 +2829,29 @@ shift $(($OPTIND - 1))
 
 # If the user specified a flac file, then switch to special flac mode
 if echo $CDROM | grep -i -q '.flac$'; then
 
 # If the user specified a flac file, then switch to special flac mode
 if echo $CDROM | grep -i -q '.flac$'; then
-       vecho "abcde: switching to flac CDROMREADERSYNTAX..."
+       vecho warning "abcde: switching to flac CDROMREADERSYNTAX..."
        CDROMREADERSYNTAX=flac
        CDROMREADERSYNTAX=flac
-       if [ "$EJECTCD" = "y" ];then
-               vecho "abcde: CDROM flac mode, deactivating EJECTCD..."
-               EJECTCD=n
+       # Added a need on CUE2DISCID until we manage to convert the python script to bash.
+       NEEDCUE2DISCID=y
+       NEEDMETAFLAC=y
+       EJECTCD=n
+fi
+
+# If the user provided a DISCID, disable eject
+if [ -n "$DISCID" ] || [ "$CDROMREADERSYNTAX" = "flac" ]; then EJECTCD=n ; fi
+
+# Check the available cd rippers in the system, from the ones we know.
+if [ "$CDROMREADERSYNTAX" = "" ]; then
+       for DEFAULT_CDROMREADER in $DEFAULT_CDROMREADERS; do
+               if [ -x $( which $DEFAULT_CDROMREADER ) ]; then
+                       CDROMREADERSYNTAX=$DEFAULT_CDROMREADER
+                       break
+               fi
+       done
+       if [ "$CDROMREADERSYNTAX" = "" ]; then
+               log error "no cdreader found in your PATH"
+               log error "hints: are all dependencies installed? has the \$PATH been modified?"
+               exit 1
        fi
 fi
 
        fi
 fi
 
@@ -2832,6 +3077,7 @@ if echo "$OUTPUTTYPE" | grep ":" > /dev/null 2>&1 ; then
                        flac:*) FLACENCODEROPTSCLI="$( echo $OUTPUT | cut -d: -f2- )" ;;
                        spx:*)  SPEEXENCODEROPTSCLI="$( echo $OUTPUT | cut -d: -f2- )" ;;
                        mpc:*)  MPPENCODEROPTSCLI="$( echo $OUTPUT | cut -d: -f2- )" ;;
                        flac:*) FLACENCODEROPTSCLI="$( echo $OUTPUT | cut -d: -f2- )" ;;
                        spx:*)  SPEEXENCODEROPTSCLI="$( echo $OUTPUT | cut -d: -f2- )" ;;
                        mpc:*)  MPPENCODEROPTSCLI="$( echo $OUTPUT | cut -d: -f2- )" ;;
+                       m4a:*)  AACENCODEROPTSCLI="$( echo $OUTPUT | cut -d: -f2- )" ;;
                esac
        done
        for OUTPUT in "$(echo "$OUTPUTTYPE" | tr , \ )"; do
                esac
        done
        for OUTPUT in "$(echo "$OUTPUTTYPE" | tr , \ )"; do
@@ -2841,7 +3087,8 @@ if echo "$OUTPUTTYPE" | grep ":" > /dev/null 2>&1 ; then
        OUTPUTTYPE="$TEMPOUTPUTTYPE"
 fi
 
        OUTPUTTYPE="$TEMPOUTPUTTYPE"
 fi
 
-# If nothing has been specified, use oggenc for oggs and lame for mp3s and flac for flacs and speexenc for speex and mppenc for mpps
+# If nothing has been specified, use oggenc for oggs and lame for mp3s and flac
+# for flacs and speexenc for speex and mppenc for mpps and faac for m4as
 
 # Getting ready for multiple output changes
 for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
 
 # Getting ready for multiple output changes
 for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
@@ -2865,9 +3112,14 @@ do
                        ;;
                spx)
                        [ "$SPEEXENCODERSYNTAX" = "default" ] && SPEEXENCODERSYNTAX=speexenc
                        ;;
                spx)
                        [ "$SPEEXENCODERSYNTAX" = "default" ] && SPEEXENCODERSYNTAX=speexenc
+#                      [ "$DOREPLAYGAIN" = "y" ] &&
                        ;;
                mpc)
                        [ "$MPPENCODERSYNTAX" = "default" ] && MPPENCODERSYNTAX=mppenc
                        ;;
                mpc)
                        [ "$MPPENCODERSYNTAX" = "default" ] && MPPENCODERSYNTAX=mppenc
+                       [ "$DOREPLAYGAIN" = "y" ] && NEEDMPPGAIN=y
+                       ;;
+               m4a)
+                       [ "$AACENCODERSYNTAX" = "default" ] && AACENCODERSYNTAX=faac
                        ;;
                wav)
                        if [ "$KEEPWAVS" = "y" ]; then
                        ;;
                wav)
                        if [ "$KEEPWAVS" = "y" ]; then
@@ -2949,7 +3201,12 @@ case "$MPPENCODERSYNTAX" in
                MPPENCODER="$MPPENC"
                ;;
 esac
                MPPENCODER="$MPPENC"
                ;;
 esac
-               
+case "$AACENCODERSYNTAX" in
+       faac)
+               AACENCODEROPTS="${AACENCODEROPTSCLI:-$AACENCOPTS}"
+               AACENCODER="$AACENC"
+               ;;
+esac
 # and which tagger
 
 if [ "$ID3TAGV" = "1" ]; then
 # and which tagger
 
 if [ "$ID3TAGV" = "1" ]; then
@@ -2974,6 +3231,12 @@ case "$CUEREADERSYNTAX" in
                ;;
 esac
 
                ;;
 esac
 
+# which information retrieval tool are we using?
+case "$CDDBTOOL" in
+       cddb) ;;
+       musicbrainz) ;;
+esac
+
 # Check if both OGGEOUTPUTCONTAINER and FLACOUTPUTCONTAINER are the same, and differentiante them
 if [ X"$OGGOUTPUTCONTAINER" = "Xogg" ] && [ X"$FLACOUTPUTCONTAINER" = "Xogg" ]; then
        log error "FLAC on an Ogg container is not yet supported"
 # Check if both OGGEOUTPUTCONTAINER and FLACOUTPUTCONTAINER are the same, and differentiante them
 if [ X"$OGGOUTPUTCONTAINER" = "Xogg" ] && [ X"$FLACOUTPUTCONTAINER" = "Xogg" ]; then
        log error "FLAC on an Ogg container is not yet supported"
@@ -3007,17 +3270,21 @@ if [ "$EJECTCD" = "y" ]; then
        NEEDEJECT=y
 fi
 if [ ! "$CDDBAVAIL" = "n" ] && [ "$DOCDDB" = "y" ]; then
        NEEDEJECT=y
 fi
 if [ ! "$CDDBAVAIL" = "n" ] && [ "$DOCDDB" = "y" ]; then
-       NEEDHTTPGET=y
+       if [ "$CDDBMETHOD" = "cddb" ]; then
+               NEEDHTTPGET=y
+       elif [ "$CDDBMETHOD" = "musicbrainz" ]; then
+               :
+       fi
 fi
 if [ "$MAKECUEFILE" = "y" ]; then
        NEEDCUEREADER=y
 fi
 
 fi
 if [ "$MAKECUEFILE" = "y" ]; then
        NEEDCUEREADER=y
 fi
 
-if [ X"$CDSPEEDVALUE" != "X" ]; then
+if [ X"$CDSPEEDVALUE" != "X" ] && [ "$DOREAD" = "y"]; then
        case "$CDROMREADERSYNTAX" in
                cdparanoia|debug) CDROMREADEROPTS="$CDPARANOIAOPTS -S $CDSPEEDVALUE" ;;
                ### FIXME ### translate "cue2discid" from python to bash
        case "$CDROMREADERSYNTAX" in
                cdparanoia|debug) CDROMREADEROPTS="$CDPARANOIAOPTS -S $CDSPEEDVALUE" ;;
                ### FIXME ### translate "cue2discid" from python to bash
-               flac) NEEDMETAFLAC=y ; NEEDCUE2DISCID=y ;;
+               flac) NEEDMETAFLAC=y ; NEEDCUE2DISCID=y ; CDSPEEDVALUE="" ;;
                *) NEEDCDSPEED=y ;;
        esac
 fi
                *) NEEDCDSPEED=y ;;
        esac
 fi
@@ -3079,59 +3346,80 @@ for X in $CDROMREADER $CDDISCID ${NEEDTAGGER+$TAGGER} $MP3ENCODER \
        ${NEEDNORMALIZER+$NORMALIZER} ${NEEDEJECT+$EJECT} \
        ${NEEDDISKTOOL+disktool} ${NEEDCDSPEED+$CDSPEED} \
        ${NEEDVORBISGAIN+$VORBISGAIN} ${NEEDMP3GAIN+$MP3GAIN} \
        ${NEEDNORMALIZER+$NORMALIZER} ${NEEDEJECT+$EJECT} \
        ${NEEDDISKTOOL+disktool} ${NEEDCDSPEED+$CDSPEED} \
        ${NEEDVORBISGAIN+$VORBISGAIN} ${NEEDMP3GAIN+$MP3GAIN} \
-       ${NEEDCUEREADER+$CUEREADER} ${NEEDCUE2DISCID+$CUE2DISCID}
+       ${NEEDMPPGAIN+$MPPGAIN} ${NEEDCUEREADER+$CUEREADER} \
+       ${NEEDCUE2DISCID+$CUE2DISCID}
 do
 do
-       # Cut off the command-line options we just added in
-       X=$(echo $X | cut -d' ' -f2)
-       if [ "$(which $X)" = "" ]; then
-               log error "$X is not in your path." >&2
-               exit 1
-       elif [ ! -x $(which $X) ]; then
-               log error "$X is not executable." >&2
-               exit 1
-       fi
+       checkexec "$X"
 done
 
 done
 
-CDROMREADER="$CDROMREADER $CDROMREADEROPTS"
-CDDBTOOL="$CDDBTOOL $CDDBTOOLOPTS"
-HTTPGET="$HTTPGET $HTTPGETOPTS"
-
 # And last but not least, check if we can diff between files
 if [ -x $(which $DIFF) ]; then :; else
        vecho warning "Disabling diff since we cannot find it in the \$PATH..."
        DIFF=""
 fi
 
 # And last but not least, check if we can diff between files
 if [ -x $(which $DIFF) ]; then :; else
        vecho warning "Disabling diff since we cannot find it in the \$PATH..."
        DIFF=""
 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 
+#                      checkexec ${NEEDCUE2DISCID+$CUE2DISCID}
+#              fi
+#              ;;
+#esac
+
+CDROMREADER="$CDROMREADER $CDROMREADEROPTS"
+CDDBTOOL="$CDDBTOOL $CDDBTOOLOPTS"
+HTTPGET="$HTTPGET $HTTPGETOPTS"
+
 # Here it used to say:
 # One thousand lines in, we can start doing stuff with things
 # Here it used to say:
 # One thousand lines in, we can start doing stuff with things
-# Well, right now we are at line 2736 ;)
+# Well, right now we are at line 3306 ;)
 
 # Export needed things so they can be read in this subshell
 export CDDBTOOL ABCDETEMPDIR TRACKQUEUE LOWDISK EJECTCD EJECT EJECTOPTS
 export CDROM CDDBDATA REMOTEHOSTS MAXPROCS HTTPGET MD5SUM
 
 
 # Export needed things so they can be read in this subshell
 export CDDBTOOL ABCDETEMPDIR TRACKQUEUE LOWDISK EJECTCD EJECT EJECTOPTS
 export CDROM CDDBDATA REMOTEHOSTS MAXPROCS HTTPGET MD5SUM
 
-# User-definable function to set some things. Use it for
-#  - closing the CD tray with eject -t
-#  - set the CD speed value with eject -x
-vecho -n "Executing customizable pre-read function... "
+if [ "$DOREAD" = "y" ]; then
+       # User-definable function to set some things. Use it for
+       #  - closing the CD tray with eject -t
+       #  - 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 funtion. Close the CD with it.
 
 
-vecho "done."
+       vecho "done."
+fi
 
 
-do_discid # Get ABCDETEMPDIR created and status file initialized
+case "$CDDBMETHOD" in
+       cddb)
+               do_discid # Get ABCDETEMPDIR created and status file initialized
+               ;;
+       musicbrainz)
+               do_musicbrainz id
+               ;;
+esac
 
 if [ "$DOCDDB" = "y" ]; then
 
 if [ "$DOCDDB" = "y" ]; then
+       # start with a sane default:
+       CDDBLOCALSTATUS=notfound
        if [ $CDDBUSELOCAL = "y" ]; then
                do_localcddb
        fi
        if checkstatus cddb-choice > /dev/null; then
                :
        else 
        if [ $CDDBUSELOCAL = "y" ]; then
                do_localcddb
        fi
        if checkstatus cddb-choice > /dev/null; then
                :
        else 
-               if [ ! "$CDDBLOCALSUCCESS" = "y" ] ; then
-                       do_cddbstat
-                       do_cddbquery
-                       do_cddbread
+               if [ "$CDDBLOCALSTATUS" = "notfound" ] ; then
+                       case "$CDDBMETHOD" in
+                               cddb)
+                                       do_cddbstat
+                                       do_cddbquery
+                                       do_cddbread
+                                       ;;
+                               musicbrainz)
+                                       do_musicbrainz
+                                       ;;
+                       esac
                fi
        fi
        do_cddbedit
                fi
        fi
        do_cddbedit
@@ -3159,6 +3447,15 @@ if [ -f "$ABCDETEMPDIR/status" ] && [ X"$ERASEENCODEDSTATUS" = "Xy" ]; then
                | grep -v ^encode-output > "$ABCDETEMPDIR/status"
 fi
 
                | grep -v ^encode-output > "$ABCDETEMPDIR/status"
 fi
 
+if checkstatus onetrack ; then ONETRACK=y ; fi
+
+if [ "$ONETRACK" = "y" ]; then 
+       # Reuse the CUEFILE in case we created it in a previous run
+       if CUEFILE=$(checkstatus cuefile); then
+               IMPORTCUESHEET=y
+       fi
+fi
+
 # Create playlist if needed (backgroundable) and start reading in tracks
 
 (
 # Create playlist if needed (backgroundable) and start reading in tracks
 
 (
@@ -3178,10 +3475,12 @@ fi
 
 if [ "$ONETRACK" = "y" ]; then 
        TRACKS="$FIRSTTRACK"
 
 if [ "$ONETRACK" = "y" ]; then 
        TRACKS="$FIRSTTRACK"
-       if checkstatus readtrack-$FIRSTTRACK; then :; else
-               if [ "$USEPIPES" = "y" ]; then
+       if [ "$USEPIPES" = "y" ]; then
+               if checkstatus readencodetrack-$FIRSTTRACK; then :; else
                        do_cdread onetrack $FIRSTTRACK $LASTTRACK | do_encode $FIRSTTRACK %local0% > /dev/null 2>&1
                        do_cdread onetrack $FIRSTTRACK $LASTTRACK | do_encode $FIRSTTRACK %local0% > /dev/null 2>&1
-               else
+               fi
+       else
+               if checkstatus readtrack-$FIRSTTRACK; then :; else
                        do_cdread onetrack $FIRSTTRACK $LASTTRACK
                fi
        fi
                        do_cdread onetrack $FIRSTTRACK $LASTTRACK
                fi
        fi
@@ -3209,6 +3508,11 @@ else
                if [ "$NOGAP" = "y" ] || [ "$BATCHNORM" = "y" ]; then
                    :
                else
                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.
+                       if [ "$MAXPROCS" = "1" ] && [ ! "$DOREAD" = "y" ]; then
+                               echo "encode-output=loud" >> "$ABCDETEMPDIR/status"
+                       fi
                        echo NEXTTRACK # Get the encoder machine churning again
                        if [ "$DOREAD" = "y" ]; then
                                if [ "$LOWDISK" = "y" ] && [ "$DOENCODE" = "y" ]; then
                        echo NEXTTRACK # Get the encoder machine churning again
                        if [ "$DOREAD" = "y" ]; then
                                if [ "$LOWDISK" = "y" ] && [ "$DOENCODE" = "y" ]; then
@@ -3384,11 +3688,9 @@ do
                        TRACKNUM="$FIRSTTRACK"
                        splitvarious
                else
                        TRACKNUM="$FIRSTTRACK"
                        splitvarious
                else
-#                      TRACKNUM=$(printf %0.${TRACKNUMPADDING}d $(expr ${UTRACKNUM} + 0))
                        TRACKNUM=$UTRACKNUM
                        CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
                        TRACKNUM=$UTRACKNUM
                        CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
-                       #TRACKNAME="$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')"
-                       do_getcddbinfo TRACKNAME
+                       getcddbinfo TRACKNAME
                        splitvarious
                fi
        fi
                        splitvarious
                fi
        fi
@@ -3462,6 +3764,7 @@ wait
 if [ "$DOREPLAYGAIN" = "y" ]; then
        do_replaygain
 fi
 if [ "$DOREPLAYGAIN" = "y" ]; then
        do_replaygain
 fi
+
 # Check to see if run_command logged any errors
 if [ -f "$ABCDETEMPDIR/errors" ]; then
        log error "The following commands failed to run:"
 # Check to see if run_command logged any errors
 if [ -f "$ABCDETEMPDIR/errors" ]; then
        log error "The following commands failed to run:"
@@ -3477,11 +3780,11 @@ fi
 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' '|')
 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' '|')
-       decho [ENCODED_FORMATS]
-       decho [MOVED_FORMATS]
        if [ "$ENCODED_FORMATS" != "$MOVED_FORMATS" ]; then
        if [ "$ENCODED_FORMATS" != "$MOVED_FORMATS" ]; then
-               log warning "Not all encoded formats have been requested to be moved."
-               log warning "Use \"-a clean -f -C $DISCID\" to force the removal of the remaining data."
+               log warning "The encoded formats does not match with the moved ones"
+               log warning "Formats encoded: $( echo $ENCODED_FORMATS | tr "|" " " )"
+               log warning "Formats moved: $( echo $MOVED_FORMATS | tr "|" " " )"
+               log warning "Use \"abcde -a clean -f -C $DISCID\" to force the removal of the remaining data."
                DOCLEAN=n
        fi
 fi
                DOCLEAN=n
        fi
 fi