Added last pieces of code to solve the last bugs in Debian BTS
[abcde.git] / abcde
diff --git a/abcde b/abcde
index 849c9a4..75c382c 100755 (executable)
--- a/abcde
+++ b/abcde
@@ -42,12 +42,12 @@ 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 <#> Set the CD speed"
 #echo "-t    File types to preprocess (wav)"
 #echo "-T    Set postprocessing options"
@@ -61,7 +61,8 @@ 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"
 }
 
@@ -147,7 +148,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...
@@ -493,24 +494,24 @@ do_tag ()
                        -g "$GENREID" -T "${TRACKNUM:-$1/$TRACKS}" \
                        "$ABCDETEMPDIR/track$1.$OUTPUT"
                ;;
-       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 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
@@ -520,18 +521,17 @@ do_tag ()
                                        esac    
                                fi
                                ) | run_command tagtrack-$1 $VORBISCOMMENT $VORBISCOMMENTOPTS -w \
-                                       "$ABCDETEMPDIR/track$1.uncommented.$OUTPUT" "$ABCDETEMPDIR/track$1.$OUTPUT"
+                                       "$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)
-## FIXME ##            [ "$FLACONOGG" = "y" ] && OUTPUT=ogg
                (
                echo ARTIST="$TRACKARTIST"
                echo ALBUM="$DALBUM"
@@ -550,7 +550,7 @@ do_tag ()
                                *)   echo COMMENT="$COMMENTOUTPUT";;
                        esac    
                fi
-               ) | run_command tagtrack-$1 $METAFLAC $METAFLACOPTS --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
@@ -611,7 +611,7 @@ do_encode ()
                        mp3)
                                TEMPARG="PIPE_$MP3ENCODERSYNTAX"
                                ;;
-                       ogg)
+                       vorbis|ogg)
                                TEMPARG="PIPE_$OGGENCODERSYNTAX"
                                ;;
                        flac)
@@ -635,12 +635,12 @@ do_encode ()
                                                FILEARG="-if $IN"
                                                ;;
                                        *)
-                                               FILEARG=$IN
+                                               FILEARG="$IN"
                                                ;;
                                esac
                                ;;
                        *)
-                               FILEARG=$IN
+                               FILEARG="$IN"
                                ;;
                esac
        fi
@@ -648,7 +648,17 @@ do_encode ()
        if [ -s "$IN" -o X"$USEPIPES" = "Xy" ] ; then
                for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
                do
-                       OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
+                       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
@@ -672,7 +682,7 @@ do_encode ()
                                        ;;
                                esac
                                ;;
-                       ogg)
+                       vorbis|ogg)
                                case "$2" in
                                %local*%)
                                        case "$OGGENCODERSYNTAX" in
@@ -689,7 +699,7 @@ do_encode ()
                                case "$2" in
                                %local*%)
                                        case "$FLACENCODERSYNTAX" in
-                                       flac) $RUN_COMMAND nice $ENCNICE $FLACENCODER  $FLACENCODEROPTS -o "$OUT" "$IN" ;; 
+                                       flac) $RUN_COMMAND nice $ENCNICE $FLACENCODER $FLACENCODEROPTS -o "$OUT" "$IN" ;; 
                                        esac
                                        ;;
        
@@ -823,9 +833,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
@@ -908,6 +927,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:
                # ' ' -> '_'
@@ -940,7 +967,31 @@ do_move ()
                OUTPUTFILEDIR=$(dirname "$OUTPUTDIR/$OUTPUTFILE")
                # mkdir -p shouldn't return an error if the directory already exists
                mkdir -p "$OUTPUTFILEDIR"
+               vecho "Moving $ABCDETEMPDIR/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
+                                       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
 }
 
@@ -959,6 +1010,7 @@ do_playlist ()
                ALBUMFILE=$(mungefilename "$DALBUM")
                ARTISTFILE=$(mungefilename "$DARTIST")
                GENRE=$(mungegenre "$GENRE")
+               YEAR=${CDYEAR:-$CDYEAR}
                if [ "$VARIOUSARTISTS" = "y" ] ; then
                        PLAYLISTFILE=$(eval echo $VAPLAYLISTFORMAT)
                else
@@ -1047,12 +1099,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")
@@ -1077,7 +1137,7 @@ 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?"
@@ -1086,7 +1146,7 @@ do_discid ()
                                                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
@@ -1203,10 +1263,12 @@ 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..."
+                       $MKCUE $MKCUEOPTS > "$ABCDETEMPDIR/$CUEFILE"
+                       echo cuefile=$CUEFILE >> "$ABCDETEMPDIR/status"
                fi
        fi
        # If we got the CDPARANOIA status and it is not recorded, save it now
@@ -1222,6 +1284,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
@@ -1414,7 +1507,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"
@@ -1524,9 +1617,9 @@ do_cddbedit ()
                                        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"
@@ -1575,7 +1668,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
 
@@ -1610,13 +1703,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"
@@ -1774,9 +1871,10 @@ do_cdread ()
                # We need the first and last track for cdda2wav
                FIRSTTRACK=$2
                LASTTRACK=$3
+               UTRACKNUM=$FIRSTTRACK
                case "$CDROMREADERSYNTAX" in
-                       cdparanoia) UTRACKNUM="$FIRSTTRACK-$LASTTRACK" ;;
-                       cdda2wav) UTRACKNUM="$FIRSTTRACK+$LASTRACK" ;;
+                       cdparanoia) READTRACKNUMS="$FIRSTTRACK-$LASTTRACK" ;;
+                       cdda2wav) READTRACKNUMS="$FIRSTTRACK+$LASTRACK" ;;
                        *) echo "abcde error: $CDROMREADERSYNTAX does not support ONETRACK mode"
                           exit 1 ;;
                esac
@@ -1786,7 +1884,7 @@ do_cdread ()
        CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
        if [ "$USEPIPES" = "y" ]; then
                TEMPARG="PIPE_$CDROMREADERSYNTAX"
-               FILEARG=${!TEMPARG}
+               FILEARG="${!TEMPARG}"
                REDIR=""
                PIPE_MESSAGE="and encoding "
        else
@@ -1797,13 +1895,13 @@ do_cdread ()
                                FILEARG="-f $WAVDATA"
                                ;;
                        *)
-                               FILEARG=$WAVDATA
+                               FILEARG="$WAVDATA"
                                ;;
                esac
                REDIR=">&2"
        fi
        if [ "$1" = "onetrack" ]; then
-               echo "Grabbing ${PIPE_MESSAGE}tracks $UTRACKNUM..." >&2
+               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:\])
@@ -1813,7 +1911,8 @@ do_cdread ()
                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
@@ -1829,11 +1928,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
@@ -1842,11 +1941,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=$?
@@ -1869,6 +1968,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
 }
@@ -1915,6 +2017,13 @@ pre_read ()
 :
 }
 
+# post_read
+# Empty post_read function, to be defined in the configuration file.
+post_read ()
+{
+:
+}
+
 ###############################################################################
 # End of functions
 #
@@ -1928,7 +2037,6 @@ CDDBPROTO=5
 HELLOINFO="$(whoami)@$(hostname)"
 INTERACTIVE=y
 CDROMREADERSYNTAX=cdparanoia
-OUTPUTTYPE=ogg
 ENCODERSYNTAX=default
 
 MP3ENCODERSYNTAX=default
@@ -1965,6 +2073,9 @@ 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
@@ -2014,7 +2125,7 @@ MP3ENCOPTS=
 VORBIZEOPTS=
 OGGENCOPTS=
 # flac
-FLACOPTS=
+FLACOPTS="-f"
 # speex
 SPEEXENCOPTS=
 # mpc
@@ -2042,6 +2153,10 @@ 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
@@ -2053,6 +2168,7 @@ 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
@@ -2136,7 +2252,7 @@ while getopts 1a:A:bc:C:d:Dehj: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 ;;
@@ -2175,8 +2291,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"
@@ -2236,6 +2354,24 @@ done
 
 # Sanity checks:
 
+# Check the encoding format, if nothing has been configured in the system, from the ones available 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 which $DEFAULT_OUTPUT_BINARY > /dev/null 2>&1 ; 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
+
 # At this point a CDROM has to be defined, so we check it exists.
 if [ X"$CDROM" != "X" ] ; then 
        if [ "$CDROMREADERSYNTAX" = "cdda2wav" ] && [ "$NEEDCDROMID" = "y" ] ; then
@@ -2263,8 +2399,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
@@ -2311,26 +2447,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
@@ -2339,18 +2470,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
                        ;;
@@ -2398,9 +2537,14 @@ case "$FLACENCODERSYNTAX" in
        flac)
                FLACENCODEROPTS="${FLACENCODEROPTSCLI:-$FLACOPTS}"
                FLACENCODER="$FLAC"     
-               ## FIXME ## FLAC streams can be encapsulated on a Ogg transport layer
+               # FLAC streams can be encapsulated on a Ogg transport layer
                if echo "$FLACENCODEROPTS" | egrep -q -- "(^| )--ogg($| )" ;then
-                       FLACONOGG=y
+                       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
@@ -2427,6 +2571,16 @@ else
        TAGGEROPTS="$ID3V2OPTS"
 fi
 
+# 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
@@ -2480,10 +2634,10 @@ 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)
@@ -2503,7 +2657,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 \
@@ -2657,6 +2810,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
@@ -2825,6 +2981,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
@@ -2833,6 +2995,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.