With debug we dont want to have the CD ejected
[clinton/abcde.git] / abcde
diff --git a/abcde b/abcde
index 0bb6d3b..42f44db 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 ()
 {
@@ -32,6 +32,7 @@ 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 "-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 +44,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]...>"
@@ -62,6 +63,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 +146,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).
@@ -587,14 +612,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 +709,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
@@ -765,6 +789,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"
                                                ;;
@@ -868,13 +893,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 +1018,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 +1102,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 +1122,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 +1153,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])
@@ -1194,13 +1246,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)
@@ -1444,7 +1489,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
@@ -2435,7 +2479,7 @@ 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:z opt ; do
+while getopts 1a:bc:C:d:Defhj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W:z opt ; do
        case "$opt" in
                1) ONETRACK=y ;;
                a) ACTIONS="$OPTARG" ;;
@@ -2449,7 +2493,7 @@ while getopts 1a:bc:C:d:Dehj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W:z opt ; do
                h) usage; exit ;;
                e) ERASEENCODEDSTATUS=y ;;
                E) ENCODING="$OPTARG" ;;
-#              f) FORCECDDBUSELOCAL=y ;;
+               f) FORCE=y ;;
                i) INLINETAG=y ;;
                j) MAXPROCS="$OPTARG" ;;
                k) KEEPWAVS=y ;;
@@ -2487,7 +2531,7 @@ while getopts 1a:bc:C:d:Dehj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W:z opt ; do
                         exit 1
                   fi
                   ;;
-               z) echo "Placeholder for a future autodebug option" ;;
+               z) CDROMREADERSYNTAX=debug ; EJECTCD="n" ;;
                ?) usage; exit ;;
        esac
 done
@@ -2728,6 +2772,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
                        ;;
@@ -3295,6 +3345,16 @@ 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' '|')
+       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!