Readme entry
[clinton/abcde.git] / abcde
diff --git a/abcde b/abcde
index 7cfedd9..99e1cf5 100755 (executable)
--- a/abcde
+++ b/abcde
@@ -1,6 +1,6 @@
 #!/bin/sh
 # Copyright (c) 1998-2001 Robert Woodcock <rcw@debian.org>
-# Copyright (c) 2003-2004 Jesus Climent <jesus.climent@hispalinux.es>
+# Copyright (c) 2003-2005 Jesus Climent <jesus.climent@hispalinux.es>
 # This code is hereby licensed for public consumption under either the
 # GNU GPL v2 or greater, or Larry Wall's Artistic license - your choice.
 #
@@ -11,7 +11,7 @@
 # Copyright for this work is to expire January 1, 2010, after which it
 # shall be public domain.
 
-VERSION="2.3.0"
+VERSION="2.3.99"
 
 usage ()
 {
@@ -30,6 +30,8 @@ echo "       Specify discid to resume from (only needed if you no longer have th
 echo "-d <device>"
 echo "       Specify CDROM device to grab"
 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 "-h     This help information"
 #echo "-i    Tag files while encoding, when possible (local only) -NWY-"
 echo "-j <#> Number of encoder processes to run at once (localhost)"
@@ -41,12 +43,14 @@ 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 "       Output file type(s) (ogg,mp3,flac,spx,mpc). Defaults to ogg"
+echo "       Output file type(s) (vorbis,mp3,flac,spx,mpc). 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 "-R     Add replaygain values to the tag info (only for ogg,flac)"
+echo "-R     Add replaygain values to the tag info (only for vorbis,flac)"
+echo "-s <field>"
+echo "       Show dielfs from the CDDB info (year,genre)"
 echo "-S <#> Set the CD speed"
 #echo "-t    File types to preprocess (wav)"
 #echo "-T    Set postprocessing options"
@@ -60,7 +64,9 @@ echo "       Add a comment to the CD tracks"
 echo "-W <#> Contatenate CDs: -T #01 -w \"CD #\"" 
 echo ""
 echo "Tracks is a space-delimited list of tracks to grab."
-echo "Ranges specified with hyphens are allowed."
+echo "Ranges specified with hyphens are allowed (i.e., 1-5)."
+echo ""
+#echo "Double hyphens are used to concatenate tracks"
 }
 
 # Funtions to replace the need of seq, which is too distribution dependant.
@@ -85,6 +91,31 @@ f_seq_line ()
        echo
 }
 
+# Functions to replace the need of awk {print $1} and {print $NF}
+get_first()
+{
+if [ X"$1" = "X" ]; then
+       for first in `cat`; do
+               break
+       done
+else
+       first=$1
+fi
+echo $first
+}
+
+get_last()
+{
+if [ X"$1" = "X" ]; then
+       for stdin in `cat`; do
+               last=$stdin
+       done
+else
+       for last in $@ ; do :; done
+fi
+echo $last
+}
+
 # checkstatus [blurb]
 # Returns "0" if the blurb was found, returns 1 if it wasn't
 # Puts the blurb content, if available, on stdout.
@@ -120,7 +151,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="$(egrep $PATTERN "$ABCDETEMPDIR/errors" | tail -n 1)"
 
        if [ -z "$BLURB" ]; then
                # negative, we did not have a negative...
@@ -131,6 +162,31 @@ checkerrors ()
        fi
 }
 
+# page [file]
+# Finds the right pager in the system to display a file
+page ()
+{
+       PAGEFILE="$1"
+       # Use the debian sensible-pager wrapper to pick the pager
+       # user has requested via their $PAGER environment variable
+       if [ -x "/usr/bin/sensible-pager" ]; then
+               /usr/bin/sensible-pager "$PAGEFILE"
+       elif [ -x "$PAGER" ]; then
+               # That failed, try to load the preferred editor, starting
+               # with their PAGER variable
+               $PAGER "$PAGEFILE"
+               # If that fails, check for less
+       elif [ -x /usr/bin/less ]; then
+               /usr/bin/less -f "$PAGEFILE"
+               # more should be on all UNIX systems
+       elif [ -x /bin/more ]; then
+               /bin/more "$PAGEFILE"
+       else
+               # No bananas, just cat the thing
+               cat "$PAGEFILE" >&2
+       fi
+}
+
 # run_command [blurb] [command...]
 # Runs a command, silently if necessary, and updates the status file
 run_command ()
@@ -242,40 +298,40 @@ splitvarious ()
        if [ "$VARIOUSARTISTS" = "y" ] && [ ! "$ONETRACK" = "y" ]; then
                case "$VARIOUSARTISTSTYLE" in
                forward)
-                       DTITLEARTIST="$(echo $TRACKNAME | sed 's- / -~-g')"
-                       TRACKARTIST="$(echo $DTITLEARTIST | cut -f1 -d~)"
-                       TRACKNAME="$(echo $DTITLEARTIST | cut -f2 -d~)"
+                       DTITLEARTIST=$(echo "$TRACKNAME" | sed 's- / -~-g')
+                       TRACKARTIST=$(echo "$DTITLEARTIST" | cut -f1 -d~)
+                       TRACKNAME=$(echo "$DTITLEARTIST" | cut -f2 -d~)
                        ;;
                forward-dash)
-                       DTITLEARTIST="$(echo $TRACKNAME | sed 's, - ,~,g')"
-                       TRACKARTIST="$(echo $DTITLEARTIST | cut -f1 -d~)"
-                       TRACKNAME="$(echo $DTITLEARTIST | cut -f2 -d~)"
+                       DTITLEARTIST=$(echo "$TRACKNAME" | sed 's, - ,~,g')
+                       TRACKARTIST=$(echo "$DTITLEARTIST" | cut -f1 -d~)
+                       TRACKNAME=$(echo "$DTITLEARTIST" | cut -f2 -d~)
                        ;;
                reverse)
-                       DTITLEARTIST="$(echo $TRACKNAME | sed 's- / -~-g')"
-                       TRACKARTIST="$(echo $DTITLEARTIST | cut -f2 -d~)"
-                       TRACKNAME="$(echo $DTITLEARTIST | cut -f1 -d~)"
+                       DTITLEARTIST=$(echo "$TRACKNAME" | sed 's- / -~-g')
+                       TRACKARTIST=$(echo "$DTITLEARTIST" | cut -f2 -d~)
+                       TRACKNAME=$(echo "$DTITLEARTIST" | cut -f1 -d~)
                        ;;
                reverse-dash)
-                       DTITLEARTIST="$(echo $TRACKNAME | sed 's, - ,~,g')"
-                       TRACKARTIST="$(echo $DTITLEARTIST | cut -f2 -d~)"
-                       TRACKNAME="$(echo $DTITLEARTIST | cut -f1 -d~)"
+                       DTITLEARTIST=$(echo "$TRACKNAME" | sed 's, - ,~,g')
+                       TRACKARTIST=$(echo "$DTITLEARTIST" | cut -f2 -d~)
+                       TRACKNAME=$(echo "$DTITLEARTIST" | cut -f1 -d~)
                        ;;
                colon)
-                       DTITLEARTIST="$(echo $TRACKNAME | sed 's-: -~-g')"
-                       TRACKARTIST="$(echo $DTITLEARTIST | cut -f1 -d~)"
-                       TRACKNAME="$(echo $DTITLEARTIST | cut -f2 -d~)"
+                       DTITLEARTIST=$(echo "$TRACKNAME" | sed 's-: -~-g')
+                       TRACKARTIST=$(echo "$DTITLEARTIST" | cut -f1 -d~)
+                       TRACKNAME=$(echo "$DTITLEARTIST" | cut -f2 -d~)
                        ;;
                trailing-paren)
-                       DTITLEARTIST="$(echo $TRACKNAME | sed 's,^\(.*\) (\(.*\)),\1~\2,')"
-                       TRACKARTIST="$(echo $DTITLEARTIST | cut -f2 -d~)"
-                       TRACKNAME="$(echo $DTITLEARTIST | cut -f1 -d~)"
+                       DTITLEARTIST=$(echo "$TRACKNAME" | sed 's,^\(.*\) (\(.*\)),\1~\2,')
+                       TRACKARTIST=$(echo "$DTITLEARTIST" | cut -f2 -d~)
+                       TRACKNAME=$(echo "$DTITLEARTIST" | cut -f1 -d~)
                        ;;
                esac
-       elif [ "$ONETRACK" = "y" ]; then
+       elif [ "$VARIOUSARTISTS" = "y" ] && [ "$ONETRACK" = "y" ]; then
                TRACKARTIST="Various"
        else
-               TRACKARTIST=$DARTIST
+               TRACKARTIST="$DARTIST"
        fi
 }
 
@@ -345,7 +401,7 @@ local id=""
                "gangsta")               id=59 ;;
                "top 40")                id=60 ;;
                "christian rap")         id=61 ;;
-               "pop/funk")              id=62 ;;
+               "pop/funk"|"pop / funk") id=62 ;;
                "jungle")                id=63 ;;
                "native american")       id=64 ;;
                "cabaret")               id=65 ;;
@@ -431,6 +487,7 @@ local id=""
                "anime")                 id=145 ;;
                "jpop")                  id=146 ;;
                "synthpop")              id=147 ;;
+               "rock/pop"|"rock / pop") id=148 ;;
                *)                       return 1 ;;
        esac
 echo ${id}
@@ -451,41 +508,52 @@ do_tag ()
        if [ -n "$STARTTRACKNUMBER" -a -n "$STARTTRACKNUMBERTAG" ] ; then
                # Get the trackpadding from the current track
                CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
-               TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - 1 ))
+               TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - $FIRSTTRACK ))
        fi
        for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
        do
        case "$OUTPUT" in
        mp3)
                # id3v2 v0.1.9 claims to have solved the -c bug, so we merge both id3 and id3v2
-      GENREID=$(do_getgenreid "${CDGENRE}")
-
-               # FIXME # track numbers in mp3 come with 1/10, so we cannot happily substitute them with $TRACKNUM
                GENREID=$(do_getgenreid "${CDGENRE}")
 
-               run_command tagtrack-$1 $TAGGER $TAGGEROPTS -c "$COMMENTOUTPUT" \
-                       -A "$DALBUM" -a "$TRACKARTIST" -t "$TRACKNAME" -y "$CDYEAR" \
-                       -g "$GENREID" -T "${TRACKNUM:-$1/$TRACKS}" \
-                       "$ABCDETEMPDIR/track$1.$OUTPUT"
+               case "$ID3SYNTAX" in
+                       id3);;
+                       eyed3)
+                               # FIXME # track numbers in mp3 come with 1/10, so we cannot happily substitute them with $TRACKNUM
+                               run_command tagtrack-$1 $TAGGER $TAGGEROPTS --commen=::"$COMMENTOUTPUT" \
+                                       -A "$DALBUM" -a "$TRACKARTIST" -t "$TRACKNAME" -Y "$CDYEAR" \
+                                       -G "$GENREID" -n "${TRACKNUM:-$1}" "${TRACKNUM:+-N $TRACKS}" \
+                                       "${ENCODING:+--set-encoding=$ENCODING}"
+                                       "$ABCDETEMPDIR/track$1.$OUTPUT"
+                               ;;
+                       *)
+                               # FIXME # track numbers in mp3 come with 1/10, so we cannot happily substitute them with $TRACKNUM
+                               run_command tagtrack-$1 $TAGGER $TAGGEROPTS -c "$COMMENTOUTPUT" \
+                                       -A "$DALBUM" -a "$TRACKARTIST" -t "$TRACKNAME" -y "$CDYEAR" \
+                                       -g "$GENREID" -T "${TRACKNUM:-$1/$TRACKS}" \
+                                       "$ABCDETEMPDIR/track$1.$OUTPUT"
+                               ;;
+               esac
                ;;
-       ogg)
+       vorbis|ogg)
                case "$OGGENCODERSYNTAX" in
                        vorbize|oggenc)
                                # vorbiscomment can't do in-place modification, mv the file first
-                               if [ -f "$ABCDETEMPDIR/track$1.$OUTPUT" -a ! -f "$ABCDETEMPDIR/track$1.uncommented.$OUTPUT" ]; then
-                                       mv "$ABCDETEMPDIR/track$1.$OUTPUT" "$ABCDETEMPDIR/track$1.uncommented.$OUTPUT"
+                               if [ -f "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER" -a ! -f "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER" ]; then
+                                       mv "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER" "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER"
                                fi
                                (
                                # These are from http://www.xiph.org/ogg/vorbis/doc/v-comment.html
-                               echo ARTIST=$TRACKARTIST
+                               echo ARTIST="$TRACKARTIST"
                                echo ALBUM="$DALBUM"
-                               echo TITLE=$TRACKNAME
-                                       if [ -n "$CDYEAR" ]; then
+                               echo TITLE="$TRACKNAME"
+                               if [ -n "$CDYEAR" ]; then
                                        echo DATE="$CDYEAR"
-                                       fi
-                               if [ -n "$CDGENRE" ]; then
+                               fi
+                               if [ -n "$CDGENRE" ]; then
                                        echo GENRE="$CDGENRE"
-                               fi      
+                               fi      
                                echo TRACKNUMBER=${TRACKNUM:-$1}
                                echo CDDB=$CDDBDISCID
                                if [ "$(eval echo ${COMMENT})" != "" ]; then
@@ -494,28 +562,28 @@ do_tag ()
                                                *)   echo COMMENT="$COMMENTOUTPUT";;
                                        esac    
                                fi
-                               ) | run_command tagtrack-$1 $VORBISCOMMENT -w \
-                                       "$ABCDETEMPDIR/track$1.uncommented.$OUTPUT" "$ABCDETEMPDIR/track$1.$OUTPUT"
+                               ) | run_command tagtrack-$1 $VORBISCOMMENT $VORBISCOMMENTOPTS -w \
+                                       "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER" "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER"
                                # Doublecheck that the commented file was created successfully before wiping the original
-                               if [ -f "$ABCDETEMPDIR/track$1.$OUTPUT" ]; then
-                                       rm -f "$ABCDETEMPDIR/track$1.uncommented.$OUTPUT"
+                               if [ -f "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER" ]; then
+                                       rm -f "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER"
                                else
-                                       mv "$ABCDETEMPDIR/track$1.uncommented.$OUTPUT" "$ABCDETEMPDIR/track$1.$OUTPUT"
+                                       mv "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER" "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER"
                                fi
                                ;;
                esac
                ;;
        flac)
-                (
+               (
                echo ARTIST="$TRACKARTIST"
                echo ALBUM="$DALBUM"
                echo TITLE="$TRACKNAME"
-                       if [ -n "$CDYEAR" ]; then
+               if [ -n "$CDYEAR" ]; then
                        echo DATE="$CDYEAR"
-                       fi
-                       if [ -n "$CDGENRE" ]; then
+               fi
+               if [ -n "$CDGENRE" ]; then
                        echo GENRE="$CDGENRE"
-                fi     
+               fi      
                echo TRACKNUMBER=${TRACKNUM:-$1}
                echo CDDB=$CDDBDISCID
                if [ "$(eval echo ${COMMENT})" != "" ]; then
@@ -524,7 +592,7 @@ do_tag ()
                                *)   echo COMMENT="$COMMENTOUTPUT";;
                        esac    
                fi
-                ) | run_command tagtrack-$1 $METAFLAC --no-utf8-convert --import-tags-from=- "$ABCDETEMPDIR/track$1.$OUTPUT"
+               ) | run_command tagtrack-$1 $METAFLAC $METAFLACOPTS --import-tags-from=- "$ABCDETEMPDIR/track$1.$FLACOUTPUTCONTAINER"
                ;;
        spx)
                run_command tagtrack-$1 true
@@ -580,39 +648,92 @@ do_batch_encode ()
 # TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
 do_encode ()
 {
-       IN="$ABCDETEMPDIR/track$1.wav"
-       # We need IN to proceed.
-       if [ -s "$IN" ] ; then
+       if [ "$USEPIPES" = "y" ]; then
+               case "$OUTPUT" in
+                       mp3)
+                               TEMPARG="PIPE_$MP3ENCODERSYNTAX"
+                               ;;
+                       vorbis|ogg)
+                               TEMPARG="PIPE_$OGGENCODERSYNTAX"
+                               ;;
+                       flac)
+                               TEMPARG="PIPE_$FLACENCODERSYNTAX"
+                               ;;
+                       spx)
+                               TEMPARG="PIPE_$SPEEXENCODER"
+                               ;;
+                       mpc)
+                               TEMPARG="PIPE_$MPPENCODER"
+                               ;;
+               esac
+               IN="$( eval echo "\$$TEMPARG" )"
+       else
+               IN="$ABCDETEMPDIR/track$1.wav"
+               case "$OUTPUT" in
+                       mp3)
+                               case "$MP3ENCODERSYNTAX" in
+                                       # FIXME # check if mp3enc needs -if for pipes
+                                       mp3enc)
+                                               FILEARG="-if $IN"
+                                               ;;
+                                       *)
+                                               FILEARG="$IN"
+                                               ;;
+                               esac
+                               ;;
+                       *)
+                               FILEARG="$IN"
+                               ;;
+               esac
+       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 , \ )
                do
-                       OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
-                       run_command '' echo "Encoding track $1 of $TRACKS: $TRACKNAME..."
+                       case "$OUTPUT" in
+                               vorbis|ogg)
+                                       OUT="$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER"
+                                       ;;
+                               flac)
+                                       OUT="$ABCDETEMPDIR/track$1.$FLACOUTPUTCONTAINER"
+                                       ;;
+                               *)
+                                       OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
+                                       ;;
+                       esac
+                       if [ X"$USEPIPES" = "Xy" ]; then
+                               RUN_COMMAND=""
+                       else
+                               run_command '' echo "Encoding track $1 of $TRACKS: $TRACKNAME..."
+                               RUN_COMMAND="run_command encodetrack-$OUTPUT-$1"
+                       fi
                        case "$OUTPUT" in
                        mp3)
                                case "$2" in
                                %local*%)
                                        case "$MP3ENCODERSYNTAX" in
-                                       lame|gogo) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS "$IN" "$OUT" ;;
-                                       bladeenc) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS -quit "$IN" ;;
-                                       l3enc|xingmp3enc) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $MP3ENCODER "$IN" "$OUT" $MP3ENCODEROPTS ;;
-                                       mp3enc) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $MP3ENCODER -if "$IN" -of "$OUT" $MP3ENCODEROPTS ;;
+                                       lame|gogo) $RUN_COMMAND nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS "$IN" "$OUT" ;;
+                                       bladeenc) $RUN_COMMAND nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS -quit "$IN" ;;
+                                       l3enc|xingmp3enc) $RUN_COMMAND nice $ENCNICE $MP3ENCODER "$IN" "$OUT" $MP3ENCODEROPTS ;;
+                                       # FIXME # Relates to the previous FIXME since it might need the "-if" removed.
+                                       mp3enc) $RUN_COMMAND nice $ENCNICE $MP3ENCODER -if "$IN" -of "$OUT" $MP3ENCODEROPTS ;;
                                        esac
                                        ;;
                                *)
-                                       run_command encodetrack-$OUTPUT-$1 nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" >/dev/null 2>&1
+                                       $RUN_COMMAND nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" >/dev/null 2>&1
                                        ;;
                                esac
                                ;;
-                       ogg)
+                       vorbis|ogg)
                                case "$2" in
                                %local*%)
                                        case "$OGGENCODERSYNTAX" in
-                                       vorbize) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $OGGENCODER $OGGENCODEROPTS -w "$OUT" "$IN" ;;
-                                       oggenc) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $OGGENCODER $OGGENCODEROPTS -o "$OUT" "$IN" ;;
+                                       vorbize) $RUN_COMMAND nice $ENCNICE $OGGENCODER $OGGENCODEROPTS -w "$OUT" "$IN" ;;
+                                       oggenc) $RUN_COMMAND nice $ENCNICE $OGGENCODER $OGGENCODEROPTS -o "$OUT" "$IN" ;;
                                        esac
                                        ;;
                                *)
-                                       run_command encodetrack-$OUTPUT-$1 nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" >/dev/null 2>&1
+                                       $RUN_COMMAND nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" >/dev/null 2>&1
                                        ;;
                                esac
                                ;;
@@ -620,14 +741,14 @@ do_encode ()
                                case "$2" in
                                %local*%)
                                        case "$FLACENCODERSYNTAX" in
-                                       flac) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $FLACENCODER  $FLACENCODEROPTS -o "$OUT" "$IN" ;; 
+                                       flac) $RUN_COMMAND nice $ENCNICE $FLACENCODER $FLACENCODEROPTS -o "$OUT" "$IN" ;; 
                                        esac
                                        ;;
        
                                *)
                                        echo -n "DISTMP3:"
                                        echo "$DISTMP3 $DISTMP3OPTS $2 $IN $OUT >/dev/null 2>&1"
-                                       run_command encodetrack-$OUTPUT-$1 nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" > /dev/null 2>&1
+                                       $RUN_COMMAND nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" > /dev/null 2>&1
                                        ;;
                                esac
                                ;;
@@ -641,9 +762,9 @@ do_encode ()
                                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 encodetrack-$OUTPUT-$1 nice $ENCNICE $SPEEXENCODER $SPEEXENCODEROPTS --author "$TRACKARTIST" --title "$TRACKNAME" "$COMMENT" "$IN" "$OUT"
+                                       $RUN_COMMAND nice $ENCNICE $SPEEXENCODER $SPEEXENCODEROPTS --author "$TRACKARTIST" --title "$TRACKNAME" "$COMMENT" "$IN" "$OUT"
                                else
-                                       run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $SPEEXENCODER $SPEEXENCODEROPTS "$IN" "$OUT"
+                                       $RUN_COMMAND nice $ENCNICE $SPEEXENCODER $SPEEXENCODEROPTS "$IN" "$OUT"
                                fi
                                ;;
                        mpc)    
@@ -653,7 +774,7 @@ do_encode ()
                                # errors, so I have not tried it myself.
                                ## FIXME ## Needs some cleanup to determine if an empty tag sent
                                ## FIXME ## to the encoder ends up empty.
-                               run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $MPPENCODER $MPPENCODEROPTS --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --track "$1" --genre "$CDGENRE" --year "$CDYEAR" --comment "$COMMENT" "$IN" "$OUT"
+                               $RUN_COMMAND nice $ENCNICE $MPPENCODER $MPPENCODEROPTS --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --track "$1" --genre "$CDGENRE" --year "$CDYEAR" --comment "$COMMENT" "$IN" "$OUT"
                                ;;
                        esac
                done
@@ -754,9 +875,18 @@ do_encode ()
 #      done
 #}
 
+# do_single_gain
+# variables used:
+# FIXME #
+do_single_gain ()
+{
+:
+}
+
 # do_batch_gain
 # variables used:
 # MP3GAIN, MP3GAINOPTS, VORBISGAIN, VORBISGAINOPTS
+# FIXME #
 do_batch_gain ()
 {
        # The commands here don't go through run_command because they're never supposed to be silenced
@@ -839,6 +969,14 @@ do_move ()
 {
        for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
        do
+               case $OUTPUT in
+                       vorbis|ogg)
+                               OUTPUT=$OGGOUTPUTCONTAINER
+                               ;;
+                       flac)
+                               OUTPUT=$FLACOUTPUTCONTAINER
+                               ;;
+               esac
                # Create ALBUMFILE, ARTISTFILE, TRACKFILE
                # Munge filenames as follows:
                # ' ' -> '_'
@@ -856,7 +994,7 @@ do_move ()
                if [ -n "$STARTTRACKNUMBER" ] ; then
                        # Get the trackpadding from the current track
                        CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
-                       TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - 1 ))
+                       TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - $FIRSTTRACK ))
                else
                        TRACKNUM=${UTRACKNUM}
                fi
@@ -871,7 +1009,33 @@ do_move ()
                OUTPUTFILEDIR=$(dirname "$OUTPUTDIR/$OUTPUTFILE")
                # mkdir -p shouldn't return an error if the directory already exists
                mkdir -p "$OUTPUTFILEDIR"
+               # Silence the Moving output since it overlaps with encoding processes...
+               #run_command '' vecho "Moving track$1.$OUTPUT to $OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
                run_command movetrack-$1 mv "$ABCDETEMPDIR/track$1.$OUTPUT" "$OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
+               # Lets move the cue file
+               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...
+                                       #run_command '' vecho "Copying cue file to its destination directory..."
+                                       if checkstatus onetrack >/dev/null ; then
+                                               case $OUTPUT in
+                                                       # NOTE: Creating a cue file with the 3-char-extension files is to comply with
+                                                       # http://brianvictor.tripod.com/mp3cue.htm#details
+                                                       [a-z0-9][a-z0-9][a-z0-9])
+                                                               run_command movecue-$OUTPUT cp "$ABCDETEMPDIR/$CUEFILE" "$OUTPUTDIR/$OUTPUTFILE.cue"
+                                                               ;;
+                                                       *)
+                                                               run_command movecue-$OUTPUT cp "$ABCDETEMPDIR/$CUEFILE" "$OUTPUTDIR/$OUTPUTFILE.$OUTPUT.cue"
+                                                               ;;
+                                               esac
+                                       else
+                                               run_command movecue-$OUTPUT cp "$ABCDETEMPDIR/$CUEFILE" "$OUTPUTFILEDIR/$CUEFILE"
+                                       fi
+                                       echo movecue-$OUTPUT >> "$ABCDETEMPDIR/status"
+                               fi
+                       fi
+               fi
        done
 }
 
@@ -890,6 +1054,7 @@ do_playlist ()
                ALBUMFILE=$(mungefilename "$DALBUM")
                ARTISTFILE=$(mungefilename "$DARTIST")
                GENRE=$(mungegenre "$GENRE")
+               YEAR=${CDYEAR:-$CDYEAR}
                if [ "$VARIOUSARTISTS" = "y" ] ; then
                        PLAYLISTFILE=$(eval echo $VAPLAYLISTFORMAT)
                else
@@ -932,7 +1097,7 @@ do_playlist ()
                                if [ -n "$STARTTRACKNUMBER" ] ; then
                                        # Get the trackpadding from the current track
                                        CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
-                                       TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - 1 ))
+                                       TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - $FIRSTTRACK ))
                                else
                                        TRACKNUM=${UTRACKNUM}
                                fi
@@ -978,12 +1143,20 @@ do_discid ()
        #elif [ -z "$DISCID" ]; then
        if [ -z "$DISCID" ]; then
                vecho -n "Getting CD track info... "
+               # In OSX, unmount the disc before a query
+               if [ "$OSFLAVOUR" = "OSX" ]; then
+                       disktool -u ${CDROM#/dev/}
+               fi
                TRACKINFO=$($CDDISCID $CDROM)
                # Make sure there's a CD in there by checking cd-discid's return code
                if [ "$?" = "1" ]; then
                        echo "abcde error: CD could not be read. Perhaps there's no CD in the drive?" >&2
                        exit 1
                fi
+               # In OSX, remount the disc again
+               if [ "$OSFLAVOUR" = "OSX" ]; then
+                       disktool -m ${CDROM#/dev/}
+               fi
                WEHAVEACD=y
        else
                TRACKINFO=$(cat "$WAVOUTPUTDIR/abcde.$DISCID/discid")
@@ -1008,16 +1181,16 @@ do_discid ()
                                cdparanoia|debug)
                                        if [ "$WEHAVEACD" = "y" ]; then
                                                vecho "Querying the CD for audio tracks..."
-                                               CDPARANOIAOUTPUT="$( $CDROMREADER -d $CDROM -Q --verbose 2>&1 )"
+                                               CDPARANOIAOUTPUT="$( $CDROMREADER -$CDPARANOIACDROMBUS $CDROM -Q --verbose 2>&1 )"
                                                RET=$?
                                                if [ ! "$RET" = "0" ];then
                                                        echo "Warning: Something went wrong while querying the CD... Maybe a DATA CD?"
                                                fi
-                                               TRACKS="$( echo "$CDPARANOIAOUTPUT" | egrep '^[[:space:]]+[[:digit:]]' | tail -n 1 |  awk '{print $1}' | tr -d "." | tr '\n' ' ' )"
+                                               TRACKS="$( echo "$CDPARANOIAOUTPUT" | egrep '^[[:space:]]+[[:digit:]]' | tail -n 1 | get_first | tr -d "." | tr '\n' ' ' )"
                                                CDPARANOIAAUDIOTRACKS="$TRACKS"
                                        else
                                                if [ -f "$ABCDETEMPDIR/status" ] && checkstatus cdparanoia-audio-tracks ; then
-                                                       TRACKS=$( cat $ABCDETEMPDIR/cdparanoia-audio-tracks )
+                                                       TRACKS=$( cat "$ABCDETEMPDIR/cdparanoia-audio-tracks" )
                                                else
                                                        TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
                                                fi
@@ -1028,7 +1201,7 @@ do_discid ()
                else
                        TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
                fi
-               if echo "$TRACKS" | grep [[:digit:]] > /dev/null 2>&1 ;then :;else
+               if echo "$TRACKS" | grep "[[:digit:]]" > /dev/null 2>&1 ;then :;else
                        echo "The disc does not contain any tracks. Giving up..."
                        exit 0
                fi
@@ -1063,8 +1236,6 @@ do_discid ()
                echo Grabbing tracks: "$TRACKQUEUE"
        fi
 
-#      for LASTTRACK in $TRACKQUEUE; do :; done
-
        QUEUEDTRACKS=$(echo $TRACKQUEUE | wc -w | tr -d ' ')
 
        # We have the discid, create a temp directory after it to store all the temp
@@ -1136,10 +1307,17 @@ do_discid ()
                fi
                cat /dev/null > "$ABCDETEMPDIR/status"
        fi
-       if [ X"$CUEFILE" = "Xy" -a X"$WEHAVEACD" = "Xy" ]; then
-               if checkstatus cuefile ; then :; else
-                       $MKCUE $MKCUEOPTS > $ABCDETEMPDIR/cue-$(echo $TRACKINFO | cut -f1 -d' ').txt
-                       echo cuefile >> "$ABCDETEMPDIR/status"
+       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
+                               echo "abcde: reading the CUE sheet with mkcue is still considered experimental"
+                               echo "abcde: and there was a problem with the CD reading. abcde will continue,"
+                               echo "abcde: but consider reporting the problem to the abcde author"
+                       fi
                fi
        fi
        # If we got the CDPARANOIA status and it is not recorded, save it now
@@ -1155,6 +1333,37 @@ do_discid ()
        echo "$TRACKINFO" > "$ABCDETEMPDIR/discid"
 }
 
+# do_cleancue
+# Create a proper CUE file based on the CUE file we created before.
+do_cleancue()
+{
+       # FIXME # we can get the name of the cuefile from the status file
+       if CUEFILE_IN="$ABCDETEMPDIR"/$(checkstatus cuefile); then
+               CUEFILE_OUT=$CUEFILE_IN.out
+               ### FIXME ### checkstatus cddb
+               if [ -e "$CDDBDATA" ]; then
+                       vecho "Adding metadata to the cue file..."
+                       # FIXME It doesn't preserve spaces! Why?
+                       # FIXME parse $track into PERFORMER and TITLE - abcde already has code for this?
+                       n=1
+                       echo "PERFORMER \"$DARTIST\"" >> "$CUEFILE_OUT"
+                       echo "TITLE \"$DALBUM\"" >> "$CUEFILE_OUT"
+                       cat "$CUEFILE_IN" | while read line
+                       do
+                               if echo "$line" | grep -q "INDEX"
+                               then
+                                       eval track="\$TRACK$n"
+                                       n=$(expr $n + 1)
+                                       echo "TITLE \"$track\"" >> "$CUEFILE_OUT"
+                               fi
+                               echo "$line" >> "$CUEFILE_OUT"
+                       done
+                       mv "$CUEFILE_OUT" "$CUEFILE_IN"
+                       echo "cleancuefile" >> "$ABCDETEMPDIR/status"
+               fi
+       fi
+}
+
 # do_cddbparse
 # Parses a CDDB file and outputs the title and the track names.
 # Variables: CDDBFILE
@@ -1166,6 +1375,18 @@ do_cddbparse ()
                vecho "ONETRACK mode selected: displaying only the title of the CD..."
        fi
        echo "---- $(grep DTITLE "${CDDBPARSEFILE}" | cut '-d=' -f2- | tr -d \\r\\n ) ----"
+       if [ X"$SHOWCDDBYEAR" = "Xy" ]; then
+               PARSEDYEAR=$(grep DYEAR "${CDDBPARSEFILE}" | cut '-d=' -f2-)
+               if [ ! X"$PARSEDYEAR" = "X" ]; then
+                       echo "Year: $PARSEDYEAR"
+               fi
+       fi
+       if [ X"$SHOWCDDBGENRE" = "Xy" ]; then
+               PARSEDGENRE=$(grep DGENRE "${CDDBPARSEFILE}" | cut '-d=' -f2-)
+               if [ ! X"$PARSEDGENRE" = "X" ]; then
+                       echo "Genre: $PARSEDGENRE"
+               fi
+       fi
        if [ ! "$ONETRACK" = "y" ]; then
                for TRACK in $(f_seq_row 1 $TRACKS)
                do
@@ -1201,7 +1422,7 @@ do_localcddb ()
                                echo "y" >&2
                        fi
                        if [ "$USELOCALRESP" = "y" ]; then
-                       #echo "Using local copy of CDDB data"
+                               #echo "Using local copy of CDDB data"
                                cp "${CDDBLOCALFILE}" "$ABCDETEMPDIR/cddbread.1"
                                echo 999 > "$ABCDETEMPDIR/cddbquery" # Assuming 999 isn't used by CDDB
                                echo cddb-readcomplete >> "$ABCDETEMPDIR/status"
@@ -1212,7 +1433,6 @@ do_localcddb ()
                                #echo "Not using local copy of CDDB data"
                                CDDBLOCALSUCCESS="n"
                        fi
-                       CDDBLOCALSUCCESS="y"
                else
                        CDDBLOCALSUCCESS="n"
                fi
@@ -1348,7 +1568,7 @@ do_cddbread ()
                        # Multiple exact, (possibly multiple) inexact matches
                        IN=
                        if [ "$RESPONSECODE" = "211" ]; then IN=in; fi
-                       if [ "$(wc -l < $ABCDETEMPDIR/cddbquery | tr -d ' ')" -eq 3 ]; then
+                       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"
@@ -1420,47 +1640,59 @@ do_cddbedit ()
                                else
                                        # The user has a choice to make, display the info in a pager if necessary
                                        if [ $(cat "$ABCDETEMPDIR/cddbchoices" | wc -l) -ge 24 ]; then
-                                               # Use the debian sensible-pager wrapper to pick the pager
-                                               # user has requested via their $PAGER environment variable
-                                               if [ -x "/usr/bin/sensible-pager" ]; then
-                                                       /usr/bin/sensible-pager "$ABCDETEMPDIR/cddbchoices"
-                                               elif [ -x "$PAGER" ]; then
-                                                       # That failed, try to load the preferred editor, starting
-                                                       # with their PAGER variable
-                                                       $PAGER "$ABCDETEMPDIR/cddbchoices"
-                                                       # If that fails, check for less
-                                               elif [ -x /usr/bin/less ]; then
-                                                       /usr/bin/less -f "$ABCDETEMPDIR/cddbchoices"
-                                                       # more should be on all UNIX systems
-                                               elif [ -x /bin/more ]; then
-                                                       /bin/more "$ABCDETEMPDIR/cddbchoices"
-                                               else
-                                                       # No bananas, just cat the thing
-                                                       cat "$ABCDETEMPDIR/cddbchoices" >&2
-                                               fi
+                                               page "$ABCDETEMPDIR/cddbchoices"
                                        else
                                                # It's all going to fit in one page, cat it
                                                cat "$ABCDETEMPDIR/cddbchoices" >&2
                                        fi
                                        
+                                       CDDBCHOICENUM=""
+                                       # Setting the choice to an impossible integer to avoid errors in the numeric comparisons
+                                       CDCHOICENUM=-1
                                        # I'll take CDDB read #3 for $400, Alex
-                                       echo -n "Which entry would you like abcde to use (0 for none)? [0-$CDDBCHOICES]: " >&2
-                                       read CDDBCHOICE
-                                       [ X"$CDDBCHOICE" = "X" ] && CDDBCHOICE=1
-                                       # Make sure we get a valid choice
-                                       CDCHOICENUM=$(echo $CDDBCHOICE | xargs printf %d 2>/dev/null)
-                                       while [ $CDCHOICENUM -lt 0 ] || [ $CDCHOICENUM -gt $CDDBCHOICES ]; do
-                                               echo "Invalid selection. Please choose a number between 1 and $CDDBCHOICES." >&2
-                                               echo -n "Selection [0-$CDDBCHOICES]: " >&2
+                                       while [ $CDCHOICENUM -lt 0 ] || [ $CDCHOICENUM -gt $CDDBCHOICES ]; do
+                                               echo -n "Which entry would you like abcde to use (0 for none)? [0-$CDDBCHOICES]: " >&2
                                                read CDDBCHOICE
-                                               CDCHOICENUM=$(echo $CDDBCHOICE | xargs printf %d 2>/dev/null)
+                                               [ X"$CDDBCHOICE" = "X" ] && CDDBCHOICE=1
+                                               if echo $CDDBCHOICE | egrep -q "[[:digit:]]+,[[:digit:]]+" ; then
+                                                       if [ ! X"$DIFF" = "X" ]; then
+                                                               PARSECHOICE1=$(echo $CDDBCHOICE | cut -d"," -f1)
+                                                               PARSECHOICE2=$(echo $CDDBCHOICE | cut -d"," -f2)
+                                                               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"
+                                                                       done
+                                                                       echo "Showing diff between choices $PARSECHOICE1 and $PARSECHOICE2..." > "$ABCDETEMPDIR/cddbread.diff"
+                                                                       $DIFF $DIFFOPTS "$ABCDETEMPDIR/cddbread.parsechoice.$PARSECHOICE1" "$ABCDETEMPDIR/cddbread.parsechoice.$PARSECHOICE2" >> "$ABCDETEMPDIR/cddbread.diff"
+                                                                       if [ $(cat "$ABCDETEMPDIR/cddbread.diff" | wc -l) -ge 24 ]; then
+                                                                               page "$ABCDETEMPDIR/cddbread.diff"
+                                                                       else
+                                                                               cat "$ABCDETEMPDIR/cddbread.diff" >&2
+                                                                       fi
+                                                               fi
+                                                       else
+                                                               echo "The diff program was not found in your path. Please choose a number between 0 and $CDDBCHOICES." >&2
+                                                       fi
+                                               else
+                                                       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
                                        done
                                        if [ "$CDCHOICENUM" = "0" ]; then
                                                vecho "Creating empty CDDB template..."
                                                UNKNOWNDISK=y
-                                               $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > $ABCDETEMPDIR/cddbread.0
+                                               $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.0"
                                        else
-                                               echo "Selected: #$CDCHOICENUM ($(grep ^DTITLE= $ABCDETEMPDIR/cddbread.$CDCHOICENUM | cut -f2- -d= | tr -d \\r\\n))" >&2
+                                               echo "Selected: #$CDCHOICENUM ($(grep ^DTITLE= "$ABCDETEMPDIR/cddbread.$CDCHOICENUM" | cut -f2- -d= | tr -d \\r\\n))" >&2
                                                do_cddbparse "$ABCDETEMPDIR/cddbread.$CDCHOICENUM"
                                        fi
                                        echo "cddb-choice=$CDCHOICENUM" >> "$ABCDETEMPDIR/status"
@@ -1509,7 +1741,7 @@ do_cddbedit ()
                                CDCHOICENUM=1
                                echo "cddb-choice=$CDCHOICENUM" >> "$ABCDETEMPDIR/status"
                        fi
-                       echo "Selected: #$CDCHOICENUM ($(grep ^DTITLE= $ABCDETEMPDIR/cddbread.$CDCHOICENUM | cut -f2- -d= | tr -d \\r\\n))" >&2
+                       echo "Selected: #$CDCHOICENUM ($(grep ^DTITLE= "$ABCDETEMPDIR/cddbread.$CDCHOICENUM" | cut -f2- -d= | tr -d \\r\\n))" >&2
                fi
        fi
 
@@ -1544,13 +1776,17 @@ do_cddbedit ()
                        if [ -x $(which "${EDITOR%%\ *}") ]; then
                                # That failed, try to load the preferred editor, starting
                                # with their EDITOR variable
-                               eval $(echo "$EDITOR") "$CDDBDATA"
+                               eval $(echo "$EDITOR") \"$CDDBDATA\"
                        fi
                # If that fails, check for a vi
                elif which vi >/dev/null 2>&1; then
                        vi "$CDDBDATA"
+               elif [ -x /usr/bin/vim ]; then
+                       /usr/bin/vim "$CDDBDATA"
                elif [ -x /usr/bin/vi ]; then
                        /usr/bin/vi "$CDDBDATA"
+               elif [ -x /bin/vi ]; then
+                       /bin/vi "$CDDBDATA"
                # nano should be on all (modern, i.e., sarge) debian systems
                elif which nano >/dev/null 2>&1 ; then
                        nano "$CDDBDATA"
@@ -1573,7 +1809,7 @@ 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= | egrep -ci '^(various|soundtrack|varios|sonora|ost)')" != "0" ]; then
                echo "Looks like a Multi-Artist CD" >&2
                VARIOUSARTISTS=y
        else
@@ -1656,7 +1892,7 @@ do_cddbedit ()
        echo "variousartiststyle=$VARIOUSARTISTSTYLE" >> "$ABCDETEMPDIR/status"
 
        if [ "$EDITCDDB" = "y" ] && [ "$UNINTENTIONALLY_ANGER_THE_FREEDB_PEOPLE" = "y" ]; then
-               if [ $CDDBDATAMD5SUM != "" ]  && [ $CDDBDATAMD5SUM != $($MD5SUM "$CDDBDATA" | cut -d " " -f 1) ]; 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) 
@@ -1708,9 +1944,10 @@ do_cdread ()
                # We need the first and last track for cdda2wav
                FIRSTTRACK=$2
                LASTTRACK=$3
+               UTRACKNUM=$FIRSTTRACK
                case "$CDROMREADERSYNTAX" in
-                       cdparanoia) UTRACKNUM="1-" ;;
-                       cdda2wav) UTRACKNUM="1+$LASTRACK" ;;
+                       cdparanoia) READTRACKNUMS="$FIRSTTRACK-$LASTTRACK" ;;
+                       cdda2wav) READTRACKNUMS="$FIRSTTRACK+$LASTRACK" ;;
                        *) echo "abcde error: $CDROMREADERSYNTAX does not support ONETRACK mode"
                           exit 1 ;;
                esac
@@ -1718,31 +1955,37 @@ do_cdread ()
                UTRACKNUM=$1
        fi
        CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
-       WAVDATA="$ABCDETEMPDIR/track$UTRACKNUM.wav"
-       if [ -r "$CDDBDATA" ]; then
-               TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -n 1 | cut -f2 -d= | tr -d \[:cntrl:\])
-               echo "Grabbing track $UTRACKNUM: $TRACKNAME..." >&2
-       else
-               echo "Grabbing track $UTRACKNUM..." >&2
-       fi
        if [ "$USEPIPES" = "y" ]; then
                TEMPARG="PIPE_$CDROMREADERSYNTAX"
-               FILEARG=${!TEMPARG}
+               FILEARG="$( eval echo "\$$TEMPARG" )"
                REDIR=""
+               PIPE_MESSAGE="and encoding "
        else
+               WAVDATA="$ABCDETEMPDIR/track$UTRACKNUM.wav"
                case "$CDROMREADERSYNTAX" in
                ## FIXME ## Find the case for dagrab, to avoid exceptions
                        dagrab)
                                FILEARG="-f $WAVDATA"
                                ;;
                        *)
-                               FILEARG=$WAVDATA
+                               FILEARG="$WAVDATA"
                                ;;
                esac
                REDIR=">&2"
        fi
+       if [ "$1" = "onetrack" ]; 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:\])
+                       echo "Grabbing ${PIPE_MESSAGE}track $UTRACKNUM: $TRACKNAME..." >&2
+               else
+                       echo "Grabbing ${PIPE_MESSAGE}track $UTRACKNUM..." >&2
+               fi
+       fi
        case "$CDROMREADERSYNTAX" in
-               cdparanoia) nice $READNICE $CDROMREADER -d $CDROM $UTRACKNUM $FILEARG $REDIR ;;
+               cdparanoia) 
+                       nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS $CDROM ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG" $REDIR ;;
                cdda2wav)
                        if [ "$OSFLAVOUR" = "OSX" ] ; then
                                # Hei, we have to unmount the device before running anything like cdda2wav in OSX
@@ -1758,11 +2001,11 @@ do_cdread ()
                                        CDDA2WAVCDROM="$CDROMID"
                                fi
                        fi
-                       nice $READNICE $CDROMREADER -D $CDDA2WAVCDROM -t $UTRACKNUM $FILEARG $REDIR
+                       nice $READNICE $CDROMREADER -D $CDDA2WAVCDROM -t ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG" $REDIR
                        ;;
                ## FIXME ## We have an exception for dagrab, since it uses -f
                ## FIXME ## Shall we just use -f $FILEARG ??
-               dagrab) nice $READNICE $CDROMREADER -d $CDROM -v $UTRACKNUM $FILEARG $REDIR
+               dagrab) nice $READNICE $CDROMREADER -d $CDROM -v $UTRACKNUM "$FILEARG" $REDIR
                        ;;
                cddafs)
                        # Find the track's mounted path
@@ -1771,11 +2014,11 @@ do_cdread ()
                        FILEPATH=$(find "$FILEPATH" | grep "/$REALTRACKNUM ");
                        # If the file exists, copy it
                        if [ -e "$FILEPATH" ] ; then
-                               nice $READNICE $CDROMREADER "$FILEPATH" $FILEARG $REDIR
+                               nice $READNICE $CDROMREADER "$FILEPATH" "$FILEARG" $REDIR
                        else
                                false
                        fi ;;
-               debug) nice $READNICE $CDROMREADER -d $CDROM -w $UTRACKNUM-[:1] $FILEARG $REDIR
+               debug) nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS $CDROM -w $UTRACKNUM-[:1] "$FILEARG" $REDIR
                        ;;
        esac
        RETURN=$?
@@ -1798,6 +2041,9 @@ do_cdread ()
                        echo readencodetrack-$UTRACKNUM >> "$ABCDETEMPDIR/status"
                else
                        echo readtrack-$UTRACKNUM >> "$ABCDETEMPDIR/status"
+                       if [ "$1" = "onetrack" ]; then
+                               echo onetrack >> "$ABCDETEMPDIR/status"
+                       fi
                fi
        fi
 }
@@ -1827,7 +2073,8 @@ fi
 # Custom filename munging:
 mungefilename ()
 {
-       echo "$@" | sed s,:,\ -,g | tr \ /\* __+ | tr -d \'\"\?\[:cntrl:\]
+       #echo "$@" | sed s,:,\ -,g | tr \ /\* __+ | tr -d \'\"\?\[:cntrl:\]
+       echo "$@" | sed s,:,\ -,g | tr \ / __ | tr -d \'\"\?\[:cntrl:\]
 }
 
 # Custom genre munging:
@@ -1843,6 +2090,13 @@ pre_read ()
 :
 }
 
+# post_read
+# Empty post_read function, to be defined in the configuration file.
+post_read ()
+{
+:
+}
+
 ###############################################################################
 # End of functions
 #
@@ -1854,9 +2108,15 @@ CDDBURL="http://freedb.freedb.org/~cddb/cddb.cgi"
 CDDBSUBMIT=freedb-submit@freedb.org
 CDDBPROTO=5
 HELLOINFO="$(whoami)@$(hostname)"
+CDDBCOPYLOCAL="n"
+CDDBLOCALDIR="$HOME/.cddb"
+CDDBUSELOCAL="n"
+
+# List of fields we parse and show during the CDDB parsing...
+SHOWCDDBFIELDS="year,genre"
+
 INTERACTIVE=y
 CDROMREADERSYNTAX=cdparanoia
-OUTPUTTYPE=ogg
 ENCODERSYNTAX=default
 
 MP3ENCODERSYNTAX=default
@@ -1865,6 +2125,7 @@ FLACENCODERSYNTAX=default
 SPEEXENCODERSYNTAX=default
 MPPENCODERSYNTAX=default
 NORMALIZERSYNTAX=default
+CUEREADERSYNTAX=default
 
 OUTPUTFORMAT='${ARTISTFILE}-${ALBUMFILE}/${TRACKNUM}.${TRACKFILE}'
 # Use the following VAOUTPUTFORMAT to revert to 2.0.x VA format:
@@ -1886,13 +2147,13 @@ VARIOUSARTISTS=n
 VARIOUSARTISTSTYLE=forward
 KEEPWAVS=n
 PADTRACKS=n
-CDDBCOPYLOCAL="n"
-CDDBLOCALDIR="$HOME/.cddb"
-CDDBUSELOCAL="n"
 
 # If using scsi devices, cdda2wav needs a CDROMID, instead of a device node
 # i.e. CDROMID="1,0,0"
 CDROMID=""
+# If we are using the IDE bus, we need CDPARANOIACDROMBUS defined as "d"
+# If we are using the ide-scsi emulation layer, we need to define a "g"
+CDPARANOIACDROMBUS="d"
 
 # program paths - defaults to checking your $PATH
 # mp3
@@ -1914,6 +2175,7 @@ MPPENC=mppenc
 
 ID3=id3
 ID3V2=id3v2
+EYED3=eyeD3
 CDPARANOIA=cdparanoia
 CDDA2WAV=cdda2wav
 DAGRAB=dagrab
@@ -1929,6 +2191,8 @@ NORMALIZE=normalize-audio
 CDSPEED=eject
 VORBISGAIN=vorbisgain
 MKCUE=mkcue
+MKTOC=cdrdao
+DIFF=diff
 
 # Options for programs called from abcde
 # mp3
@@ -1942,7 +2206,7 @@ MP3ENCOPTS=
 VORBIZEOPTS=
 OGGENCOPTS=
 # flac
-FLACOPTS=
+FLACOPTS="-f"
 # speex
 SPEEXENCOPTS=
 # mpc
@@ -1961,6 +2225,9 @@ NORMALIZEOPTS=
 CDSPEEDOPTS="-x"
 CDSPEEDVALUE=
 MKCUEOPTS=
+MKTOCOPTS=""
+VORBISCOMMENTOPTS="-R"
+METAFLACOPTS="--no-utf8-convert"
 
 # Default to one process if -j isn't specified
 MAXPROCS=1
@@ -1968,10 +2235,15 @@ MAXPROCS=1
 # List of actions to perform - by default, run to completion
 ACTIONS=cddb,read,encode,tag,move,clean
 
+# 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
+
 # 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
        HTTPGET=fetch
+       MD5SUM=md5
        NEEDCDROMID=y
        OSFLAVOUR=FBSD
 elif [ X$(uname) = "XDarwin" ] ; then
@@ -1979,9 +2251,13 @@ elif [ X$(uname) = "XDarwin" ] ; then
        OSFLAVOUR=OSX
        # We should have disktool in OSX, but let's be sure...
        NEEDDISKTOOL=y
+       CDROMREADERSYNTAX=cddafs
 elif [ X$(uname) = "XOpenBSD" ] ; then
        HTTPGET=wget
        MD5SUM=md5
+elif [ X$(uname) = "XNetBSD" ] ; then
+       HTTPGET=ftp
+       MD5SUM=md5
 else
        HTTPGET=wget
 fi
@@ -2017,6 +2293,7 @@ if [ "$HTTPGETOPTS" = "" ] ; then
                wget) HTTPGETOPTS="-q -O -";;
                curl) HTTPGETOPTS="-f -s";;
                fetch)HTTPGETOPTS="-q -o -";;
+               ftp)  HTTPGETOPTS="-q -o -";;
                *) echo "abcde warning: HTTPGET in non-standard and HTTPGETOPTS are not defined." >&2 ;;
        esac
 fi
@@ -2039,18 +2316,21 @@ if [ "$CDROM" = "" ] ; then
 fi
 
 # Parse command line options
-#while getopts 1a:bc:C:d:Dhj:klLnNo:pr:S:t:T:vVx opt ; do
-while getopts 1a:A:bc:C:d:Dhj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W: opt ; do
+#while getopts 1a:A:bc:C:d:Dehj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W: opt ; do
+while getopts 1a:bc:C:d:Dehj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W: opt ; do
        case "$opt" in
                1) ONETRACK=y ;;
                a) ACTIONS="$OPTARG" ;;
-               A) EXPACTIONS="$OPTARG";;
+               A) EXPACTIONS="$OPTARG" ;;
                b) BATCH=y ;;
                c) if [ -e "$OPTARG" ] ; then . "$OPTARG" ; else echo "abcde error: config file \"$OPTARG\" cannot be found." >&2 ; exit 1 ; fi ;;
                C) DISCID="${OPTARG#abcde.}" ;;
                d) CDROM="$OPTARG" ;;
                D) set -x ;;
+               e) ERASEENCODEDSTATUS=y ;;
                h) usage; exit ;;
+               e) ERASEENCODEDSTATUS=y ;;
+               E) ENCODING="$OPTARG" ;;
 #              f) FORCECDDBUSELOCAL=y ;;
                i) INLINETAG=y ;;
                j) MAXPROCS="$OPTARG" ;;
@@ -2060,13 +2340,13 @@ while getopts 1a:A:bc:C:d:Dhj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W: opt ; do
                n) CDDBAVAIL=n ;;
                N) INTERACTIVE=n ;;
                m) DOSPLAYLIST=y ;;
-               M) CUEFILE=y ;;
+               M) MAKECUEFILE=y ;;
                o) OUTPUTTYPE="$OPTARG" ;;
                p) PADTRACKS=y ;;
                P) USEPIPES=y ;;
                r) REMOTEHOSTS="$OPTARG" ;;
                R) REPLAYGAIN=y ;;
-               s) STARTTRACKNUMBER="$OPTARG" ;;
+               s) SHOWCDDBFIELDS="$OPTARG" ;;
                S) CDSPEEDVALUE="$OPTARG" ;;
 #              t) PREPROCESSFORMATS="$OPTARG"
 #                 PREPROCESS=y ;;
@@ -2082,7 +2362,13 @@ while getopts 1a:A:bc:C:d:Dhj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W: opt ; do
                V) EXTRAVERBOSE="y" ;;
                x) EJECTCD="y" ;;
                w) COMMENT="$OPTARG" ;;
-               W) STARTTRACKNUMBER="${OPTARG}01" ; STARTTRACKNUMBERTAG="y" ; COMMENT="CD${OPTARG}" ;;
+               W) if echo $OPTARG | grep -q "[[:digit:]]" ; then 
+                    STARTTRACKNUMBER="${OPTARG}01" ; STARTTRACKNUMBERTAG="y" ; COMMENT="CD${OPTARG}"
+                  else
+                    echo "abcde error: argument of -W must be integer"
+                        exit 1
+                  fi
+                  ;;
                ?) usage; exit ;;
        esac
 done
@@ -2093,8 +2379,10 @@ shift $(($OPTIND - 1))
 if [ "$ONETRACK" = "y" ]; then 
        # FIXME # remove check as soon as we find out about the other readers
        case "$CDROMREADERSYNTAX" in
-               dagrab|cddafs|debug) echo "abcde error: ONETRACK reading is not suported with "$CDROMREADERSYNTAX" yet"
-                                        exit 1 ;;
+               cdparanoia) ;;
+               cdda2wav) ;;
+               *) echo "abcde error: $CDROMREADERSYNTAX does not support ONETRACK mode"
+                  exit 1 ;;
        esac
        if [ "$BATCH" = "y" ]; then
                echo "abcde error: BATCH mode is not compatible with ONETRACK mode"
@@ -2152,6 +2440,14 @@ do
        esac
 done
 
+for SHOWCDDBFIELD in $(echo $SHOWCDDBFIELDS | tr , \ ); do
+       case $SHOWCDDBFIELD in
+               y*|Y*) SHOWCDDBYEAR="y";;
+               g*|G*) SHOWCDDBGENRE="y";;
+               *) ;;
+       esac
+done
+
 # Sanity checks:
 
 # At this point a CDROM has to be defined, so we check it exists.
@@ -2164,12 +2460,12 @@ if [ X"$CDROM" != "X" ] ; then
                                exit 1
                        fi
                fi
-       elif [ ! -e "$CDROM" -a X"$DOREAD" = "y" ]; then
+       elif [ ! -e "$CDROM" -a X"$DOREAD" = "Xy" ]; then
                echo "abcde error: CDROM device cannot be found." >&2
                exit 1
        fi
 # avoid processing if we are not going to hit the CDROM.
-elif [ X"$DOREAD" = "y" ]; then
+elif [ X"$DOREAD" = "Xy" ]; then
        echo "abcde error: CDROM has not been defined or cannot be found" >&2
        exit 1
 fi
@@ -2181,8 +2477,8 @@ fi
 # - lowdisk algorithm
 # - anything else?
 if [ X"$USEPIPES" = "Xy" ]; then
-       if [ $(echo $OUTPUTTYPE | tr , \  | wc -w ) -gt 1 ]; then
-               echo "abcde error: Unix pipes not compatible with multiple outputs"
+       if [ $(echo "$OUTPUTTYPE" | tr , \  | wc -w ) -gt 1 ]; then
+               echo "abcde error: Unix pipes not compatible with multiple outputs" >&2
                exit 1
        fi
        if [ X"$DONORMALIZE" = "Xy" ]; then
@@ -2200,6 +2496,24 @@ if [ X"$USEPIPES" = "Xy" ]; then
        fi
 fi
 
+# Check the encoding format from the ones available in the system, if nothing has been configured in the system.
+if [ X"$OUTPUTTYPE" = "X" ]; then
+       for DEFAULT_OUTPUT in $( echo "$DEFAULT_OUTPUT_BINARIES" | tr , \ ); do
+               DEFAULT_OUTPUT_FORMAT="$(echo $DEFAULT_OUTPUT | cut -d ":" -f 1)"
+               DEFAULT_OUTPUT_BINARY="$(echo $DEFAULT_OUTPUT | cut -d ":" -f 2)"
+               if [ -x $(which $DEFAULT_OUTPUT_BINARY) ] ; then
+                       OUTPUTTYPE=$DEFAULT_OUTPUT_FORMAT
+                       vecho "No default output type defined. Autoselecting $OUTPUTTYPE..." >&2
+                       break
+               fi
+       done
+       if [ X"$OUTPUTTYPE" = "X" ]; then
+               echo "abcde error: no encoder found in the PATH" >&2
+               echo "hits: are all dependencies installed? has the \$PATH been modified?" >&2
+               exit 1
+       fi
+fi
+
 # Decide which CDROM reader we're gonna use
 case "$CDROMREADERSYNTAX" in
        cdparanoia|debug)
@@ -2229,26 +2543,21 @@ case "$NORMALIZERSYNTAX" in
 esac
 
 # Allow -o OUTPUT(1):OPTIONS(1),...,OUTPUT(N):OPTIONS(N) mode of operation
-if echo $OUTPUTTYPE | grep ":" > /dev/null 2>&1 ; then
-       OLDFS="$FS"
-       export FS=","
-       for OUTPUT in "$OUTPUTTYPE"
-       do
+if echo "$OUTPUTTYPE" | grep ":" > /dev/null 2>&1 ; then
+       for OUTPUT in $(echo $OUTPUTTYPE | tr , \ ); do
                case "$OUTPUT" in
-                       ogg:*)  OGGENCODEROPTSCLI="$( echo $OUTPUT | cut -d: -f2- )" ;;
+                       vorbis:*|ogg:*) OGGENCODEROPTSCLI="$( echo $OUTPUT | cut -d: -f2- )" ;;
                        mp3:*)  MP3ENCODEROPTSCLI="$( 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- )" ;;
                esac
        done
-       for OUTPUT in "$OUTPUTTYPE"
-       do
+       for OUTPUT in $(echo $OUTPUTTYPE | tr , \ ); do
                TEMPOUTPUT=$( echo "$OUTPUT" | cut -d: -f1 )
                TEMPOUTPUTTYPE="${TEMPOUTPUTTYPE:+$TEMPOUTPUTTYPE,}$TEMPOUTPUT"
        done
        OUTPUTTYPE="$TEMPOUTPUTTYPE"
-       FS="$OLDFS"
 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
@@ -2257,18 +2566,26 @@ fi
 for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
 do
        case $OUTPUT in
-               ogg)    [ "$OGGENCODERSYNTAX" = "default" ] && OGGENCODERSYNTAX=oggenc
-                               [ "$DOTAG" = "y" ] && NEEDCOMMENTER=y
-                               [ "$REPLAYGAIN" = "y" ] && NEEDVORBISGAIN=y
-                               ;;
-               mp3)    [ "$MP3ENCODERSYNTAX" = "default" ] && MP3ENCODERSYNTAX=lame
-                               [ "$DOTAG" = "y" ] && NEEDTAGGER=y
-                               ;;
-               flac)   [ "$FLACENCODERSYNTAX" = "default" ] && FLACENCODERSYNTAX=flac
-                               [ "$DOTAG" = "y" ] && NEEDMETAFLAC=y
-                               ;;
-               spx)    [ "$SPEEXENCODERSYNTAX" = "default" ] && SPEEXENCODERSYNTAX=speexenc ;;
-               mpc)    [ "$MPPENCODERSYNTAX" = "default" ] && MPPENCODERSYNTAX=mppenc ;;
+               vorbis|ogg)
+                       [ "$OGGENCODERSYNTAX" = "default" ] && OGGENCODERSYNTAX=oggenc
+                       [ "$DOTAG" = "y" ] && NEEDCOMMENTER=y
+                       [ "$REPLAYGAIN" = "y" ] && NEEDVORBISGAIN=y
+                       OGGOUTPUTCONTAINER=ogg
+                       ;;
+               mp3)
+                       [ "$MP3ENCODERSYNTAX" = "default" ] && MP3ENCODERSYNTAX=lame
+                       [ "$DOTAG" = "y" ] && NEEDTAGGER=y
+                       ;;
+               flac)
+                       [ "$FLACENCODERSYNTAX" = "default" ] && FLACENCODERSYNTAX=flac
+                       [ "$DOTAG" = "y" ] && NEEDMETAFLAC=y
+                       ;;
+               spx)
+                       [ "$SPEEXENCODERSYNTAX" = "default" ] && SPEEXENCODERSYNTAX=speexenc
+                       ;;
+               mpc)
+                       [ "$MPPENCODERSYNTAX" = "default" ] && MPPENCODERSYNTAX=mppenc
+                       ;;
                *)      echo "abcde error: Invalid OUTPUTTYPE defined" >&2
                        exit 1
                        ;;
@@ -2315,7 +2632,16 @@ esac
 case "$FLACENCODERSYNTAX" in
        flac)
                FLACENCODEROPTS="${FLACENCODEROPTSCLI:-$FLACOPTS}"
-               FLACENCODER="$FLAC"
+               FLACENCODER="$FLAC"     
+               # FLAC streams can be encapsulated on a Ogg transport layer
+               if echo "$FLACENCODEROPTS" | egrep -q -- "(^| )--ogg($| )" ;then
+                       echo "abcde error: FLAC on an Ogg container is not yet supported" >&2
+                       echo "             due to problem with adding comments to such files" >&2
+                       exit 1
+                       FLACOUTPUTCONTAINER=ogg
+               else
+                       FLACOUTPUTCONTAINER=flac
+               fi
                ;;
 esac
 case "$SPEEXENCODERSYNTAX" in
@@ -2341,6 +2667,24 @@ else
        TAGGEROPTS="$ID3V2OPTS"
 fi
 
+# Options for mkcue
+case "$CUEREADERSYNTAX" in
+       default|mkcue)
+               CUEREADEROPTS="${CDROM}"
+               CUEREADER="$MKCUE"
+               ;;
+esac
+
+# Check if both OGGEOUTPUTCONTAINER and FLACOUTPUTCONTAINER are the same, and differentiante them
+if [ X"$OGGOUTPUTCONTAINER" = "Xogg" ] && [ X"$FLACOUTPUTCONTAINER" = "Xogg" ]; then
+       echo "abcde error: FLAC on an Ogg container is not yet supported" >&2
+       echo "             due to problem with adding comments to such files" >&2
+       exit 1
+       OGGOUTPUTCONTAINER=ogg.ogg
+       FLACOUTPUTCONTAINER=flac.ogg
+       vecho "abcde warning: modified file endings due to conflicting transport layers in Ogg/Vorbis and Ogg/FLAC"
+fi
+
 # Clean up nice options (either use '-n NICELEVEL or -NICELEVEL')
 
 if [ "$ENCNICE" ]; then
@@ -2354,11 +2698,21 @@ if [ "$DISTMP3NICE" ]; then
 fi
 
 # Don't check for stuff if it's not needed
-if [ "$REMOTEHOSTS" ]; then NEEDDISTMP3=y; fi
-[ "$DONORMALIZE" = "y" ] && NEEDNORMALIZER=y
-[ "$EJECTCD" = "y" ] && NEEDEJECT=y
-[ ! "$CDDBAVAIL" = "n" ] && [ "$DOCDDB" = "y" ] && NEEDHTTPGET=y
-[ "$CUEFILE" = "y" ] && NEEDMKCUE=y
+if [ "$REMOTEHOSTS" ]; then 
+       NEEDDISTMP3=y
+fi
+if [ "$DONORMALIZE" = "y" ]; then
+       NEEDNORMALIZER=y
+fi
+if [ "$EJECTCD" = "y" ]; then
+       NEEDEJECT=y
+fi
+if [ ! "$CDDBAVAIL" = "n" ] && [ "$DOCDDB" = "y" ]; then
+       NEEDHTTPGET=y
+fi
+if [ "$MAKECUEFILE" = "y" ]; then
+       NEEDCUEREADER=y
+fi
 
 if [ X"$CDSPEEDVALUE" != "X" ]; then
        case "$CDROMREADERSYNTAX" in
@@ -2374,6 +2728,7 @@ PIPE_cdparanoia="-"
 # Encoders with USEPIPE support
 # FIXME # Include here all the encoders we can figure out support pipes
 PIPE_lame="-"
+PIPE_bladeenc="-"
 PIPE_oggenc="-"
 
 # Figure out if we can use pipes with the ripper/encoder combination
@@ -2383,22 +2738,22 @@ if [ "$USEPIPES" = "y" ]; then
        case "$OUTPUT" in
                mp3)
                        PIPEENCODERSVARCHECK="PIPE_$MP3ENCODERSYNTAX" ;;
-               ogg)
+               vorbis|ogg)
                        PIPEENCODERSVARCHECK="PIPE_$OGGENCODERSYNTAX" ;;
                flac)
-                       PIPEENCODERSVARCHECK="PIPE_$FLACENCODERSYTNAX" ;;
+                       PIPEENCODERSVARCHECK="PIPE_$FLACENCODERSYNTAX" ;;
                spx)
                        PIPEENCODERSVARCHECK="PIPE_$SPEEXENCODER" ;;
                mpc)
                        PIPEENCODERSVARCHECK="PIPE_$MPPENCODER" ;;
        esac
-       if [ ! -n "${!PIPERIPPERSVARCHECK}" ] ; then
+       if [ ! -n "$( eval echo "\$$PIPERIPPERSVARCHECK" )" ] ; then
                echo "abcde error: no support for pipes with given ripper" >&2
                echo "read the USEPIPES file from the source tarball to help" >&2
                echo "on a Debian system, read /usr/share/doc/abcde/USEPIPES.gz" >&2
                exit 1;
        fi
-       if [ ! -n "${!PIPEENCODERSVARCHECK}" ] ; then
+       if [ ! -n "$( eval echo "\$$PIPEENCODERSVARCHECK" )" ] ; then
                echo "abcde error: no support for pipes with given encoder" >&2
                echo "read the USEPIPES file from the source tarball to help" >&2
                echo "on a Debian system, read /usr/share/doc/abcde/USEPIPES.gz" >&2
@@ -2406,7 +2761,6 @@ if [ "$USEPIPES" = "y" ]; then
        fi
 fi
 
-
 # Make sure a buncha things exist
 for X in $CDROMREADER $CDDISCID ${NEEDTAGGER+$TAGGER} $MP3ENCODER \
        $OGGENCODER $FLACENCODER $SPEEXENCODER $MPPENCODER \
@@ -2414,7 +2768,7 @@ for X in $CDROMREADER $CDDISCID ${NEEDTAGGER+$TAGGER} $MP3ENCODER \
        ${NEEDCOMMENTER+$VORBISCOMMENT} ${NEEDMETAFLAC+$METAFLAC} \
        ${NEEDNORMALIZER+$NORMALIZER} ${NEEDEJECT+$EJECT} \
        ${NEEDDISKTOOL+disktool} ${NEEDCDSPEED+$CDSPEED} \
-       ${NEEDVORBISGAIN+$VORBISGAIN} ${NEEDMKCUE+$MKCUE}
+       ${NEEDVORBISGAIN+$VORBISGAIN} ${NEEDCUEREADER+$CUEREADER}
 do
        # Cut off the command-line options we just added in
        X=$(echo $X | cut -d' ' -f2)
@@ -2431,9 +2785,15 @@ 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 "[WAR] Disabling diff since we cannot find it in the \$PATH..."
+       DIFF=""
+fi
+
 # Here it used to say:
 # One thousand lines in, we can start doing stuff with things
-# Well, right now we are at line 2157 ;)
+# Well, right now we are at line 2736 ;)
 
 # Export needed things so they can be read in this subshell
 export CDDBTOOL ABCDETEMPDIR TRACKQUEUE LOWDISK EJECTCD EJECT EJECTOPTS
@@ -2477,6 +2837,16 @@ if [ X"$CDSPEEDVALUE" != "X" ]; then
        esac
 fi
 
+# Define the first and last track, since we might need them later in several places
+FIRSTTRACK=$( get_first $TRACKQUEUE )
+LASTTRACK=$( get_last $TRACKQUEUE )
+
+if [ -f "$ABCDETEMPDIR/status" ] && [ X"$ERASEENCODEDSTATUS" = "Xy" ]; then
+       mv "$ABCDETEMPDIR/status" "$ABCDETEMPDIR/status.old"
+       grep -v ^encodetracklocation- < "$ABCDETEMPDIR/status.old" \
+               | grep -v ^encode-output > "$ABCDETEMPDIR/status"
+fi
+
 # Create playlist if needed (backgroundable) and start reading in tracks
 
 (
@@ -2495,11 +2865,9 @@ if [ "$LOWDISK" = "y" ] || [ "$ONETRACK" = "y" ]; then
 fi
 
 if [ "$ONETRACK" = "y" ]; then 
-       FIRSTTRACK=$( echo $TRACKQUEUE | awk '{print $1}' )
        TRACKS="$FIRSTTRACK"
-       for UTRACKNUM in $TRACKQUEUE; do :;done
        if checkstatus readtrack-$FIRSTTRACK; then :; else
-               do_cdread onetrack $FIRSTTRACK $UTRACKNUM
+               do_cdread onetrack $FIRSTTRACK $LASTTRACK
        fi
 else
        for UTRACKNUM in $TRACKQUEUE
@@ -2507,7 +2875,8 @@ else
                if [ "$DOREAD" = "y" ]; then
                        if [ "$USEPIPES" = "y" ]; then
                                if checkstatus readencodetrack-$UTRACKNUM; then :; else
-                                       do_cdread $UTRACKNUM | do_encode $UTRACKNUM %local0%
+                                       # Read, pipe, shut up!
+                                       do_cdread $UTRACKNUM | do_encode $UTRACKNUM %local0% > /dev/null 2>&1
                                fi
                        else
                                if checkstatus readtrack-$UTRACKNUM; then :; else
@@ -2551,6 +2920,9 @@ if [ "$BATCH" = "y" ] || [ "$ONETRACK" = "y" ]; then
        echo NEXTTRACK
 fi
 
+# Execute the user-defined post_read funtion 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
@@ -2620,8 +2992,7 @@ fi
 
 # If we are using ONETRACK, we can proceed with the normal encoding using just the $FIRSTTRACK as TRACKQUEUE
 if [ "$ONETRACK" = "y" ] ; then
-       FIRSTTRACK=$( echo $TRACKQUEUE | awk '{print $1}')
-       TRACKQUEUE=$FIRSTTRACK
+       TRACKQUEUE="$FIRSTTRACK"
        TRACKS="$FIRSTTRACK"
 fi
 
@@ -2690,8 +3061,8 @@ do
                else
 #                      TRACKNUM=$(printf %0.${TRACKNUMPADDING}d $(expr ${UTRACKNUM} + 0))
                        TRACKNUM=$UTRACKNUM
-                       CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
-                       TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2 -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')
+                       CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
+                       TRACKNAME="$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')"
                        splitvarious
                fi
        fi
@@ -2720,6 +3091,12 @@ do
                if checkstatus encodetrack-$UTRACKNUM; then
                        if checkstatus tagtrack-$UTRACKNUM; then :; else do_tag $UTRACKNUM; fi
                fi
+               # Lets tag the cue file
+               if checkstatus cleancuefile >/dev/null; then :; else
+                       if checkstatus cuefile >/dev/null ; then 
+                               do_cleancue
+                       fi
+               fi
        fi
        if [ "$DOMOVE" = "y" ]; then
                if checkstatus tagtrack-$UTRACKNUM; then
@@ -2728,6 +3105,8 @@ do
        fi
        ) &
 done
+
+
 # Go through it again and make sure there's no distmp3 stragglers, otherwise
 # we'll delete the files they're working on
 ## FIXME ## Check also for files which are encoded using PIPEs.