r189@frost: data | 2005-10-06 16:38:41 +0300
[clinton/abcde.git] / abcde
diff --git a/abcde b/abcde
index 3cf25af..4c0501d 100755 (executable)
--- a/abcde
+++ b/abcde
@@ -11,7 +11,7 @@
 # Copyright for this work is to expire January 1, 2010, after which it
 # shall be public domain.
 
-VERSION="2.3.99-$Id$"
+VERSION="2.3.99"
 
 usage ()
 {
@@ -22,16 +22,18 @@ echo "-1     Encode the whole CD in a single file"
 echo "-a <action1[,action2]...>"
 echo "       Actions to perform (cddb,read,normalize,encode,tag,move,playlist,clean)"
 #echo "-A     Experimental actions (retag, transcode)"
-echo "-b     Batch mode: enable album normalization and nogap encoding"
+echo "-b     Enable batch normalization"
 echo "-c <file>"
 echo "       Specify a configuration file (overrides system and user config files)"
 echo "-C <discid#>"
 echo "       Specify discid to resume from (only needed if you no longer have the cd)"
 echo "-d <device>"
-echo "       Specify CDROM device to grab"
+echo "       Specify CDROM device to grab (flac uses a single-track flac file)"
 echo "-D     Debugging mode (equivalent to sh -x abcde)"
 echo "-e     Erase encoded track information from status file"
 #echo "-E     Set the encoding information for the tags"
+echo "-f     Force operations that otherwise are considered harmful. Read \"man abcde\""
+echo "-g     Use \"lame --nogap\" for MP3 encoding. Disables low disk and pipes flags"
 echo "-h     This help information"
 #echo "-i    Tag files while encoding, when possible (local only) -NWY-"
 echo "-j <#> Number of encoder processes to run at once (localhost)"
@@ -43,7 +45,7 @@ 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) (vorbis,mp3,flac,spx,mpc). Defaults to vorbis"
+echo "       Output file type(s) (vorbis,mp3,flac,spx,mpc,wav). 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]...>"
@@ -52,8 +54,6 @@ 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"
 echo "-t <#> Start the track numbering at a given number"
 echo "-T <#> Same as -t but modifies tag numbering"
 echo "-v     Show version number and exit"
@@ -62,6 +62,7 @@ echo "-x     Eject CD after all tracks are read"
 echo "-w <comment>"
 echo "       Add a comment to the CD tracks"
 echo "-W <#> Contatenate CDs: -T #01 -w \"CD #\"" 
+echo "-z     Use debug CDROMREADERSYNTAX option (needs cdparanoia)"
 echo ""
 echo "Tracks is a space-delimited list of tracks to grab."
 echo "Ranges specified with hyphens are allowed (i.e., 1-5)."
@@ -144,6 +145,29 @@ checkstatus ()
        fi
 }
 
+# chechwarnings [blurb]
+# Returns "0" if the blurb was found (meaning there was an warning),
+# returns 1 if it wasn't (yes this is a little backwards).
+# Does not print the blurb on stdout.
+# Otherwise, returns "".
+checkwarnings ()
+{
+       if [ -e "$ABCDETEMPDIR/warnings" ]; then :; else
+               return 1
+       fi
+       # Take the last line in the status file if there's multiple matches
+       PATTERN="^$1(:.*)?$"
+       BLURB="$(egrep $PATTERN "$ABCDETEMPDIR/warnings" | tail -n 1)"
+
+       if [ -z "$BLURB" ]; then
+               # negative, we did not have a negative...
+               return 1
+       else
+               # affirmative, we had a negative...
+               return 0
+       fi
+}
+
 # checkerrors [blurb]
 # Returns "0" if the blurb was found (meaning there was an error),
 # returns 1 if it wasn't (yes this is a little backwards).
@@ -321,7 +345,7 @@ do_gettracknum()
 do_replaygain()
 {
        if checkstatus replaygain; then :; else
-               run_command "" echo "Adding reply-gain information..."
+               run_command "" echo "Adding replygain information..."
                for OUTPUT in $( echo $OUTPUTTYPE | tr , \ )
                # THE OUTPUT NEEDS TO BE CORRECTED WITH THE CONTAINER?
                do
@@ -587,14 +611,10 @@ return 0
 do_tag ()
 {
        COMMENTOUTPUT="$(eval echo ${COMMENT})"
+       CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
        run_command '' echo "Tagging track $1 of $TRACKS: $TRACKNAME..."
        # If we want to start the tracks with a given number, we need to modify the
        # TRACKNUM value before evaluation
-#      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} - $FIRSTTRACK ))
-#      fi
        if [ -n "$STARTTRACKNUMBERTAG" ] ; then
                do_gettracknum
        fi
@@ -688,6 +708,9 @@ do_tag ()
                mpc)
                        run_command tagtrack-$OUTPUT-$1 true
                        ;;
+               wav)
+                       run_command tagtrack-$OUTPUT-$1 true
+                       ;;
                esac
        done
        if checkerrors "tagtrack-(.{3,6})-$1"; then :; else
@@ -696,40 +719,50 @@ do_tag ()
 
 }
 
-# do_batch_encode
+# do_nogap_encode
 # variables used:
 # OUTPUTTYPE, {FOO}ENCODERSYNTAX, ENCNICE, ENCODER, ENCODEROPTS
-do_batch_encode ()
+do_nogap_encode ()
 {
        # The commands here don't go through run_command because they're never supposed to be silenced
-       echo "Batch encoding tracks: $TRACKQUEUE"
-       OUTPUT=$(echo $OUTPUTTYPE | grep "mp3" )
-       case "$OUTPUT" in
-       mp3)
-               case "$MP3ENCODERSYNTAX" in
-               lame)
-                       (
-                       cd "$ABCDETEMPDIR"
-                       TRACKFILES=
-                       for UTRACKNUM in $TRACKQUEUE
-                       do
-                               TRACKFILES="$TRACKFILES track$UTRACKNUM.wav"
-                       done
-                       nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS `[ "$MP3ENCODER" = "lame" ] && echo "--nogap"` $TRACKFILES
-                       RETURN=$?
-                       if [ "$RETURN" != "0" ]; then
-                               echo "batch-encode: $ENCODER returned code $RETURN" >> errors
-                       else
+       echo "Encoding gapless MP3 tracks: $TRACKQUEUE"
+       for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
+       do
+               case "$OUTPUT" in
+               mp3)
+                       case "$MP3ENCODERSYNTAX" in
+                       lame)
+                               (
+                               cd "$ABCDETEMPDIR"
+                               TRACKFILES=
                                for UTRACKNUM in $TRACKQUEUE
                                do
-                                       run_command encodetrack-$UTRACKNUM true
+                                       TRACKFILES="$TRACKFILES track$UTRACKNUM.wav"
                                done
-                       fi
-                       )
+                               nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS --nogap $TRACKFILES
+                               RETURN=$?
+                               if [ "$RETURN" != "0" ]; then
+                                       echo "nogap-encode: $ENCODER returned code $RETURN" >> errors
+                               else
+                                       for UTRACKNUM in $TRACKQUEUE
+                                       do
+                                               run_command encodetrack-$OUTPUT-$UTRACKNUM true
+                                               #run_command encodetrack-$UTRACKNUM true
+                                       done
+                               fi
+                               )
+                               ;;
+                       esac
                        ;;
                esac
-               ;;
-       esac
+       done            
+       if checkerrors "nogap-encode"; then :; else
+               if [ ! "$KEEPWAVS" = "y" ] ; then
+                       if [ ! "$KEEPWAVS" = "move" ] ; then
+                               rm -f "$IN"
+                       fi
+               fi
+       fi
        # Other encoders fall through to normal encoding as the tracks
        # have not been entered in the status file.
 }
@@ -765,6 +798,7 @@ do_encode ()
                        mp3)
                                case "$MP3ENCODERSYNTAX" in
                                        # FIXME # check if mp3enc needs -if for pipes
+                                       # FIXME # I have not been able to find a working mp3enc binary
                                        mp3enc)
                                                FILEARG="-if $IN"
                                                ;;
@@ -782,6 +816,9 @@ do_encode ()
        if [ -s "$IN" -o X"$USEPIPES" = "Xy" ] ; then
                for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
                do
+                       if [ "$NOGAP" = "y" ] && checkstatus encodetrack-$OUTPUT-$1 ; then 
+                               continue
+                       fi
                        case "$OUTPUT" in
                                vorbis|ogg)
                                        OUT="$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER"
@@ -868,13 +905,19 @@ do_encode ()
                                ## FIXME ## to the encoder ends up empty.
                                $RUN_COMMAND nice $ENCNICE $MPPENCODER $MPPENCODEROPTS --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --track "$1" --genre "$CDGENRE" --year "$CDYEAR" --comment "$COMMENT" "$IN" "$OUT"
                                ;;
+                       wav)
+                               # In case of wav output we need nothing. Just keep the wavs.
+                               run_command encodetrack-$OUTPUT-$1 true
+                               ;;
                        esac
                done
                # Only remove .wav if the encoding succeeded
                if checkerrors "encodetrack-(.{3,6})-$1"; then :; else
                        run_command encodetrack-$1 true
                        if [ ! "$KEEPWAVS" = "y" ] ; then
-                               rm -f "$IN"
+                               if [ ! "$KEEPWAVS" = "move" ] ; then
+                                       rm -f "$IN"
+                               fi
                        fi
                fi
        else
@@ -987,7 +1030,7 @@ do_batch_gain ()
        do
                MP3FILES="$TRACKFILES track$UTRACKNUM.mp3"
        done
-       # XXX: Hard-coded batch option!
+       # FIXME # Hard-coded batch option!
        $NORMALIZER -b $NORMALIZEROPTS $TRACKFILES
        RETURN=$?
        if [ "$RETURN" != "0" ]; then
@@ -1071,13 +1114,6 @@ do_move ()
                YEAR="$(echo $CDYEAR)"
                # If we want to start the tracks with a given number, we need to modify the
                # TRACKNUM value before evaluation
-       #       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} - $FIRSTTRACK ))
-       #       else
-       #               TRACKNUM=${UTRACKNUM}
-       #       fi
                do_gettracknum
                # Supported variables for OUTPUTFORMAT are GENRE, ALBUMFILE, ARTISTFILE,
                # TRACKFILE, and TRACKNUM.
@@ -1098,9 +1134,29 @@ do_move ()
                        esac
                        # Check that the directory for OUTPUTFILE exists, if it doesn't, create it
                        OUTPUTFILEDIR="$(dirname "$OUTPUTDIR/$OUTPUTFILE")"
-                       # mkdir -p shouldn't return an error if the directory already exists
-                       mkdir -p "$OUTPUTFILEDIR"
-                       run_command movetrack-$1 mv "$ABCDETEMPDIR/track$1.$OUTPUT" "$OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
+                       case $OUTPUT in
+                               wav)
+                                       if [ "$DOCLEAN" != "y" ] && [ "$FORCE" != "y" ]; then
+                                               # FIXME # introduce warnings?
+                                               :
+                                       else
+                                               # mkdir -p shouldn't return an error if the directory already exists
+                                               mkdir -p "$OUTPUTFILEDIR"
+                                               run_command movetrack-$1 mv "$ABCDETEMPDIR/track$1.$OUTPUT" "$OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
+                                               if checkstatus movetrack-output-$OUTPUT; then :; else
+                                                       run_command movetrack-output-$OUTPUT true
+                                               fi
+                                       fi
+                                       ;;
+                               *)
+                                       # mkdir -p shouldn't return an error if the directory already exists
+                                       mkdir -p "$OUTPUTFILEDIR"
+                                       run_command movetrack-$1 mv "$ABCDETEMPDIR/track$1.$OUTPUT" "$OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
+                                       if checkstatus movetrack-output-$OUTPUT; then :; else
+                                               run_command movetrack-output-$OUTPUT true
+                                       fi
+                                       ;;
+                       esac
                        # Lets move the cue file
                        if CUEFILE=$(checkstatus cuefile) >/dev/null ; then 
                                if [ -r "$ABCDETEMPDIR/$CUEFILE" ]; then
@@ -1109,6 +1165,14 @@ do_move ()
                                                #run_command '' vecho "Copying cue file to its destination directory..."
                                                if checkstatus onetrack >/dev/null ; then
                                                        case $OUTPUT in
+                                                               wav)
+                                                                       if [ "$DOCLEAN" != "y" ] && [ "$FORCE" != "y" ]; then
+                                                                               # We dont have the dir, since it was not created before.
+                                                                               :
+                                                                       else
+                                                                               run_command movecue-$OUTPUT cp "$ABCDETEMPDIR/$CUEFILE" "$OUTPUTDIR/$OUTPUTFILE.cue"
+                                                                       fi
+                                                                       ;;
                                                                # 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])
@@ -1138,6 +1202,14 @@ do_playlist ()
 {
        for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
        do
+               case $OUTPUT in
+                       vorbis|ogg)
+                               OUTPUT=$OGGOUTPUTCONTAINER
+                               ;;
+                       flac)
+                               OUTPUT=$FLACOUTPUTCONTAINER
+                               ;;
+               esac
                # Create a playlist file for the playlist data to go into.
                # We used to wipe it out if it existed. Now we request permision if interactive.
                for LASTTRACK in $TRACKQUEUE; do :; done
@@ -1186,13 +1258,6 @@ do_playlist ()
                                ALBUMFILE="$(mungefilename "$DALBUM")"
                                # If we want to start the tracks with a given number, we need to modify the
                                # TRACKNUM value before evaluation
-                       #       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} - $FIRSTTRACK ))
-                       #       else
-                       #               TRACKNUM=${UTRACKNUM}
-                       #       fi
                                do_gettracknum
                                if [ "$VARIOUSARTISTS" = "y" ]; then
                                        OUTPUTFILE=$(eval echo $VAOUTPUTFORMAT)
@@ -1240,11 +1305,21 @@ do_discid ()
                if [ "$OSFLAVOUR" = "OSX" ]; then
                        disktool -u ${CDROM#/dev/}
                fi
-               TRACKINFO=$($CDDISCID $CDROM)
+               if [ "$CDROMREADERSYNTAX" = "flac" ] ; then
+                       TRACKINFO=$($METAFLAC $METAFLACOPTS --export-cuesheet-to=- $CDROM | $CUE2DISCID)
+               else
+                       TRACKINFO=$($CDDISCID $CDROM)
+               fi
                # 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
+               if [ ! "$?" = "0" ]; then
+                       if [ "$CDROMREADERSYNTAX" = "flac" ] ; then
+                               echo "abcde error: cuesheet information from the flac file could not be read." >&2
+                               echo "abcde error: Perhaps the flac file does not contain a cuesheet?." >&2
+                               exit 1
+                       else
+                               echo "abcde error: CD could not be read. Perhaps there's no CD in the drive?" >&2
+                               exit 1
+                       fi
                fi
                # In OSX, remount the disc again
                if [ "$OSFLAVOUR" = "OSX" ]; then
@@ -1279,12 +1354,10 @@ do_discid ()
                                                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 | get_first | 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" )
-                                               else
+                                               if [ -f "$ABCDETEMPDIR/status" ] && TRACKS=$(checkstatus cdparanoia-audio-tracks); then :; else
                                                        TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
                                                fi
                                        fi
@@ -1307,7 +1380,7 @@ do_discid ()
                while [ "$X" -ne "$TRACKS" ]
                do
                        X=$(printf "%0.${TRACKNUMPADDING}d" $(expr $X + 1))
-                       TRACKQUEUE=$(echo "$TRACKQUEUE" $X)
+                       TRACKQUEUE=$(echo $TRACKQUEUE $X)
                done
                echo $TRACKQUEUE
        else
@@ -1415,10 +1488,8 @@ do_discid ()
        fi
        # If we got the CDPARANOIA status and it is not recorded, save it now
        if [ -n "$CDPARANOIAAUDIOTRACKS" ]; then
-               if checkstatus cdparanoia-audio-tracks; then :; else
-                       if echo "$CDPARANOIAAUDIOTRACKS" >> "$ABCDETEMPDIR/cdparanoia-audio-tracks"; then
-                               echo "cdparanoia-audio-tracks" >> "$ABCDETEMPDIR/status"
-                       fi
+               if checkstatus cdparanoia-audio-tracks > /dev/null 2>&1; then :; else
+                       echo cdparanoia-audio-tracks=$CDPARANOIAAUDIOTRACKS >> "$ABCDETEMPDIR/status"
                fi
        fi
        
@@ -1430,7 +1501,6 @@ do_discid ()
 # 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
@@ -1516,7 +1586,8 @@ do_localcddb ()
                        fi
                        if [ "$USELOCALRESP" = "y" ]; then
                                #echo "Using local copy of CDDB data"
-                               cp "${CDDBLOCALFILE}" "$ABCDETEMPDIR/cddbread.1"
+                               echo "# DO NOT ERASE THIS LINE! Added by abcde to imitate cddb output" > "$ABCDETEMPDIR/cddbread.1"
+                               cat "${CDDBLOCALFILE}" >> "$ABCDETEMPDIR/cddbread.1"
                                echo 999 > "$ABCDETEMPDIR/cddbquery" # Assuming 999 isn't used by CDDB
                                echo cddb-readcomplete >> "$ABCDETEMPDIR/status"
                                do_cddbparse "${CDDBLOCALFILE}" > "$ABCDETEMPDIR/cddbchoices"
@@ -2039,6 +2110,8 @@ do_cdread ()
                LASTTRACK=$3
                UTRACKNUM=$FIRSTTRACK
                case "$CDROMREADERSYNTAX" in
+                       ### FIXME ### How are we calculating the tracks in flac mode?
+                       flac) READTRACKNUMS="$FIRSTTRACK.1-$(($LASTTRACK + 1)).0" ;;
                        cdparanoia) READTRACKNUMS="$FIRSTTRACK-$LASTTRACK" ;;
                        cdda2wav) READTRACKNUMS="$FIRSTTRACK+$LASTRACK" ;;
                        *) echo "abcde error: $CDROMREADERSYNTAX does not support ONETRACK mode"
@@ -2056,7 +2129,10 @@ do_cdread ()
        else
                WAVDATA="$ABCDETEMPDIR/track$UTRACKNUM.wav"
                case "$CDROMREADERSYNTAX" in
-               ## FIXME ## Find the case for dagrab, to avoid exceptions
+               ## FIXME ## Find the cases for dagrab and flac, to avoid exceptions
+                       flac)
+                               FILEARG="--output-name=$WAVDATA"
+                               ;;
                        dagrab)
                                FILEARG="-f $WAVDATA"
                                ;;
@@ -2078,6 +2154,10 @@ do_cdread ()
                fi
        fi
        case "$CDROMREADERSYNTAX" in
+               ### FIXME ### use an exception for flac, since it uses -o
+               ### FIXME ### Shall we just use -o $FILEARG ??
+               flac)
+                       nice $READNICE $FLAC -d --cue=${READTRACKNUMS:-$UTRACKNUM.1-$(($UTRACKNUM + 1)).0} "$FILEARG" "$CDROM" ;;
                cdparanoia) 
                        nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS $CDROM ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG" $REDIR ;;
                cdda2wav)
@@ -2163,6 +2243,17 @@ if [ x"$EXTRAVERBOSE" != "x" ]; then
 fi
 }
 
+# decho [message]
+#
+# decho outputs a debug message if DEBUG is selected
+decho ()
+{
+if [ x"$DEBUG" != "x" ]; then
+       DEBUGECHO=$(echo "$@" | tr -d '[]')
+       echo "[DEBUG] $DEBUGECHO: `eval echo \\$${DEBUGECHO}`"
+fi
+}
+
 # User-redefinable functions
 # Custom filename munging:
 mungefilename ()
@@ -2241,6 +2332,8 @@ VARIOUSARTISTS=n
 VARIOUSARTISTSTYLE=forward
 KEEPWAVS=n
 PADTRACKS=n
+NOGAP=n
+BATCHNORM=n
 
 # If using scsi devices, cdda2wav needs a CDROMID, instead of a device node
 # i.e. CDROMID="1,0,0"
@@ -2252,6 +2345,7 @@ CDPARANOIACDROMBUS="d"
 # program paths - defaults to checking your $PATH
 # mp3
 LAME=lame
+TOOLAME=toolame
 GOGO=gogo
 BLADEENC=bladeenc
 L3ENC=l3enc
@@ -2287,10 +2381,12 @@ VORBISGAIN=vorbisgain
 MKCUE=mkcue
 MKTOC=cdrdao
 DIFF=diff
+CUE2DISCID=cue2discid
 
 # Options for programs called from abcde
 # mp3
 LAMEOPTS=
+TOOLAMEOPTS=
 GOGOOPTS=
 BLADEENCOPTS=
 L3ENCOPTS=
@@ -2411,12 +2507,12 @@ fi
 
 # Parse command line options
 #while getopts 1a:A:bc:C:d:Dehj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W: opt ; do
-while getopts 1a:bc:C:d:Dehj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W: opt ; do
+while getopts 1a:bc:C:d:Defghj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W:z opt ; do
        case "$opt" in
                1) ONETRACK=y ;;
                a) ACTIONS="$OPTARG" ;;
                A) EXPACTIONS="$OPTARG" ;;
-               b) BATCH=y ;;
+               b) BATCHNORM=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" ;;
@@ -2425,7 +2521,8 @@ while getopts 1a:bc:C:d:Dehj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W: opt ; do
                h) usage; exit ;;
                e) ERASEENCODEDSTATUS=y ;;
                E) ENCODING="$OPTARG" ;;
-#              f) FORCECDDBUSELOCAL=y ;;
+               f) FORCE=y ;;
+               g) NOGAP=y ;;
                i) INLINETAG=y ;;
                j) MAXPROCS="$OPTARG" ;;
                k) KEEPWAVS=y ;;
@@ -2463,24 +2560,40 @@ while getopts 1a:bc:C:d:Dehj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W: opt ; do
                         exit 1
                   fi
                   ;;
+               z) DEBUG=y ; CDROMREADERSYNTAX=debug ; EJECTCD="n" ;;
                ?) usage; exit ;;
        esac
 done
 
 shift $(($OPTIND - 1))
 
+# If the user specified a flac file, then switch to special flac mode
+if echo $CDROM | grep -i -q '.flac$'; then
+       vecho "abcde: switching to flac CDROMREADERSYNTAX..."
+       CDROMREADERSYNTAX=flac
+       if [ "$EJECTCD" = "y" ];then
+               vecho "abcde: CDROM flac mode, deactivating EJECTCD..."
+               EJECTCD=n
+       fi
+fi
+
 # Decide if we can continue.
 if [ "$ONETRACK" = "y" ]; then 
        # FIXME # remove check as soon as we find out about the other readers
        case "$CDROMREADERSYNTAX" in
+               flac) ;;
                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"
-               BATCH=n
+       if [ "$BATCHNORM" = "y" ]; then
+               echo "abcde warning: BATCHNORM mode is not compatible with ONETRACK mode. Disabling..."
+               BATCHNORM=n
+       fi
+       if [ "$NOGAP" = "y" ]; then
+               echo "abcde warning: NOGAP mode is not compatible with ONETRACK mode. Disabling..."
+               NOGAP=n
        fi
        # It does not matter how many tracks we want. In ONETRACK mode we grab them all
        # FIXME # allow ranges of tracks to be selected for onetrack ripping
@@ -2587,6 +2700,14 @@ if [ X"$USEPIPES" = "Xy" ]; then
                # FIXME # Do we need to exit or shall we just disable the mode?
                exit 1
        fi
+       if [ X"$BATCHNORM" = "Xy" ]; then
+               echo "abcde error: Unix pipes not compatible with BATCHNORM encoding"
+               exit 1
+       fi
+       if [ X"$NOGAP" = "Xy" ]; then
+               echo "abcde error: Unix pipes not compatible with NOGAP encoding"
+               exit 1
+       fi
        if [ X"$DOENCODE" = "Xn" ]; then
                vecho "Disabling Unix pipes since we are not encoding!"
                USEPIPES=n
@@ -2597,6 +2718,21 @@ if [ X"$USEPIPES" = "Xy" ]; then
        fi
 fi
 
+# LOWDISK pre-tests, before we get into more problematic stuff
+# Not compatible with anything that needs all the files in the hard disc:
+# - BATCHNORM
+# - NOGAP lame mode
+if [ X"$LOWDISK" = "Xy" ]; then
+       if [ X"$BATCHNORM" = "Xy" ]; then
+               echo "abcde error: Unix pipes not compatible with BATCHNORM encoding"
+               exit 1
+       fi
+       if [ X"$NOGAP" = "Xy" ]; then
+               echo "abcde error: Unix pipes not compatible with NOGAP encoding"
+               exit 1
+       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
@@ -2633,6 +2769,10 @@ case "$CDROMREADERSYNTAX" in
                CDROMREADER="$CDDAFS"
                CDROMREADEROPTS="$CDDAFSOPTS"
                ;;
+       flac)
+               CDROMREADER="$FLAC"
+               CDROMREADEROPTS="$FLACOPTS"
+               ;;
 esac
 
 # There's only one normalize...
@@ -2676,9 +2816,11 @@ do
                mp3)
                        [ "$MP3ENCODERSYNTAX" = "default" ] && MP3ENCODERSYNTAX=lame
                        [ "$DOTAG" = "y" ] && NEEDTAGGER=y
+                       #[ "$NOGAP" = "y" ]
                        ;;
                flac)
                        [ "$FLACENCODERSYNTAX" = "default" ] && FLACENCODERSYNTAX=flac
+                       [ "$REPLAYGAIN" = "y" ] && NEEDMETAFLAC=y
                        [ "$DOTAG" = "y" ] && NEEDMETAFLAC=y
                        ;;
                spx)
@@ -2687,6 +2829,12 @@ do
                mpc)
                        [ "$MPPENCODERSYNTAX" = "default" ] && MPPENCODERSYNTAX=mppenc
                        ;;
+               wav)
+                       if [ "$KEEPWAVS" = "y" ]; then
+                               vecho "Unsetting the KEEPWAVS option, since the resulting wav files were requested..."
+                       fi
+                       KEEPWAVS=move
+                       ;;
                *)      echo "abcde error: Invalid OUTPUTTYPE defined" >&2
                        exit 1
                        ;;
@@ -2699,6 +2847,10 @@ case "$MP3ENCODERSYNTAX" in
                MP3ENCODEROPTS="${MP3ENCODEROPTSCLI:-$LAMEOPTS}"
                MP3ENCODER="$LAME"
                ;;
+       toolame)
+               MP3ENCODEROPTS="${MP3ENCODEROPTSCLI:-$TOOLAMEOPTS}"
+               MP3ENCODER="$TOOLAME"
+               ;;
        gogo)
                MP3ENCODEROPTS="${MP3ENCODEROPTSCLI:-$GOGOOPTS}"
                MP3ENCODER="$GOGO"
@@ -2768,6 +2920,12 @@ else
        TAGGEROPTS="$ID3V2OPTS"
 fi
 
+# Specific for NOGAP is the use of lame. Another encoder fails...
+if [ "$NOGAP" = "y" ] && [ ! "$MP3ENCODER" = "lame" ]; then
+       "abcde warning: the NOGAP option is specific of lame. Deactivating..."
+       NOGAP=n
+fi
+
 # Options for mkcue
 case "$CUEREADERSYNTAX" in
        default|mkcue)
@@ -2818,6 +2976,8 @@ fi
 if [ X"$CDSPEEDVALUE" != "X" ]; then
        case "$CDROMREADERSYNTAX" in
                cdparanoia|debug) CDROMREADEROPTS="$CDPARANOIAOPTS -S $CDSPEEDVALUE" ;;
+               ### FIXME ### translate "cue2discid" from python to bash
+               flac) NEEDMETAFLAC=y ; NEEDCUE2DISCID=y ;;
                *) NEEDCDSPEED=y ;;
        esac
 fi
@@ -2825,6 +2985,7 @@ fi
 # Rippers with USEPIPE support
 # FIXME # Include here all the rippers we can figure out support pipes
 PIPE_cdparanoia="-"
+PIPE_flac="-c "
 
 # Encoders with USEPIPE support
 # FIXME # Include here all the encoders we can figure out support pipes
@@ -2850,8 +3011,8 @@ if [ "$USEPIPES" = "y" ]; then
        esac
        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
+               echo "read the USEPIPES file from the source tarball to get help." >&2
+               echo "On a Debian system, it is under /usr/share/doc/abcde/USEPIPES.gz" >&2
                exit 1;
        fi
        if [ ! -n "$( eval echo "\$$PIPEENCODERSVARCHECK" )" ] ; then
@@ -2869,7 +3030,8 @@ for X in $CDROMREADER $CDDISCID ${NEEDTAGGER+$TAGGER} $MP3ENCODER \
        ${NEEDCOMMENTER+$VORBISCOMMENT} ${NEEDMETAFLAC+$METAFLAC} \
        ${NEEDNORMALIZER+$NORMALIZER} ${NEEDEJECT+$EJECT} \
        ${NEEDDISKTOOL+disktool} ${NEEDCDSPEED+$CDSPEED} \
-       ${NEEDVORBISGAIN+$VORBISGAIN} ${NEEDCUEREADER+$CUEREADER}
+       ${NEEDVORBISGAIN+$VORBISGAIN} ${NEEDCUEREADER+$CUEREADER} \
+       ${NEEDCUE2DISCID+$CUE2DISCID}
 do
        # Cut off the command-line options we just added in
        X=$(echo $X | cut -d' ' -f2)
@@ -2933,7 +3095,8 @@ fi
 
 if [ X"$CDSPEEDVALUE" != "X" ]; then
        case "$CDROMREADERSYNTAX" in
-               cdparanoia|debug) : ;;
+               cdparanoia|debug) ;;
+               flac) ;;
                *) do_cdspeed ;;
        esac
 fi
@@ -2991,7 +3154,7 @@ else
                                fi
                        fi
                fi
-               if [ "$BATCH" = "y" ]; then
+               if [ "$NOGAP" = "y" ] || [ "$BATCHNORM" = "y" ]; then
                    :
                else
                        echo NEXTTRACK # Get the encoder machine churning again
@@ -3017,7 +3180,7 @@ if [ "$MAXPROCS" = "1" ]; then
 fi
 
 # All tracks read, start encoding.
-if [ "$BATCH" = "y" ] || [ "$ONETRACK" = "y" ]; then
+if [ "$NOGAP" = "y" ] || [ "$BATCHNORM" = "y" ] || [ "$ONETRACK" = "y" ]; then
        echo NEXTTRACK
 fi
 
@@ -3074,19 +3237,29 @@ fi
 #
 #) | (
 
-# In batch mode, we want all tracks to be read first.
-if [ "$BATCH" = "y" ]; then
+# In BATCHNORM and/or NOGAP modes, we want all tracks to be read first.
+#BACK
+if [ "$BATCHNORM" = "y" ] || [ "$NOGAP" = "y" ]; then
        read GOAHEAD # For blocking - will contain either "NO" or "NEXTTRACK"
        if [ "$GOAHEAD" = "NO" ]; then break; fi
        for LASTTRACK in $TRACKQUEUE; do :; done
        if checkstatus readtrack-$LASTTRACK; then
-               if [ "$DONORMALIZE" = "y" ]; then
+               if [ "$DONORMALIZE" = "y" ] && [ "$BATCHNORM" = "y" ]; then
                        if checkstatus normalizetrack-$LASTTRACK; then :; else do_batch_normalize; fi
-                       if checkerrors batch-normalize; then exit; fi
+                       if checkerrors batch-normalize; then exit 1; fi
                fi
-               if [ "$DOENCODE" = "y" ]; then
-                       if checkstatus encodetrack-$LASTTRACK; then :; else do_batch_encode; fi
-                       if checkerrors batch-encode; then exit; fi
+               if [ "$DOENCODE" = "y" ] && [ "$NOGAP" = "y" ]; then
+                       if [ "$DONORMALIZE" = "y" ]; then
+                               for UTRACKNUM in $TRACKQUEUE
+                               do
+                                       if checkstatus readtrack-$UTRACKNUM; then
+                                               if checkstatus normalizetrack-$UTRACKNUM; then :; else do_normalize $UTRACKNUM; fi
+                                       fi
+                               done
+                       else
+                               if checkstatus encodetrack-$LASTTRACK; then :; else do_nogap_encode; fi
+                               if checkerrors nogap-encode; then exit 1; fi
+                       fi
                fi
        fi
 fi
@@ -3235,7 +3408,7 @@ if [ "$DOENCODE" = "y" -a "$USEPIPES" != "y" ]; then
 fi
 # If the above didn't catch the stragglers, this will
 wait
-if [ "$DOREPLAYGAIN" = "y" ]; then
+if [ "$REPLAYGAIN" = "y" ]; then
        do_replaygain
 fi
 # Check to see if run_command logged any errors
@@ -3249,6 +3422,18 @@ if [ "$KEEPWAVS" = "y" ];then
        # Don't clean up
        DOCLEAN=n
 fi
+# Check if we have moved all the formats we had previously encoded, if we are not using the FORCE.
+if [ "$DOCLEAN" = "y" ] && [ ! "$FORCE" = "y" ]; then
+       ENCODED_FORMATS=$(egrep "^encodetrack-(.{3,6})-(.{1,2})$" "$ABCDETEMPDIR/status" | cut -d"-" -f2 | sort -u | tr '\n' '|')
+       MOVED_FORMATS=$(egrep "^movetrack-output-(.{3,6})$" "$ABCDETEMPDIR/status" | cut -d"-" -f3 | sort -u | tr '\n' '|')
+       decho [ENCODED_FORMATS]
+       decho [MOVED_FORMATS]
+       if [ "$ENCODED_FORMATS" != "$MOVED_FORMATS" ]; then
+               echo "Not all encoded formats have been requested to be moved."
+               echo "Use \"-a clean -f -C $DISCID\" to force the removal of the remaining data."
+               DOCLEAN=n
+       fi
+fi
 if [ "$DOCLEAN" = "y" ]; then
        # Wipe all the evidence
        # Gimme gimme gimme some more time!