CDDB in local mode was broken when selecting not to use the local cddb entry
[clinton/abcde.git] / abcde
diff --git a/abcde b/abcde
index 52808f5..92f996f 100755 (executable)
--- a/abcde
+++ b/abcde
@@ -30,6 +30,7 @@ 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 "-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)"
@@ -85,6 +86,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.
@@ -242,40 +268,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
                TRACKARTIST="Various"
        else
-               TRACKARTIST=$DARTIST
+               TRACKARTIST="$DARTIST"
        fi
 }
 
@@ -451,18 +477,16 @@ 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}")
 
+               # 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}" \
@@ -477,9 +501,9 @@ do_tag ()
                                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
+                               echo TITLE="$TRACKNAME"
                                        if [ -n "$CDYEAR" ]; then
                                        echo DATE="$CDYEAR"
                                        fi
@@ -494,7 +518,7 @@ do_tag ()
                                                *)   echo COMMENT="$COMMENTOUTPUT";;
                                        esac    
                                fi
-                               ) | run_command tagtrack-$1 $VORBISCOMMENT -w \
+                               ) | run_command tagtrack-$1 $VORBISCOMMENT $VORBISCOMMENTOPTS -w \
                                        "$ABCDETEMPDIR/track$1.uncommented.$OUTPUT" "$ABCDETEMPDIR/track$1.$OUTPUT"
                                # Doublecheck that the commented file was created successfully before wiping the original
                                if [ -f "$ABCDETEMPDIR/track$1.$OUTPUT" ]; then
@@ -506,16 +530,17 @@ do_tag ()
                esac
                ;;
        flac)
-                (
+## FIXME ##            [ "$FLACONOGG" = "y" ] && OUTPUT=ogg
+               (
                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 +549,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.$OUTPUT"
                ;;
        spx)
                run_command tagtrack-$1 true
@@ -899,7 +924,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
@@ -975,7 +1000,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
@@ -1056,7 +1081,7 @@ 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 |  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
@@ -1071,7 +1096,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
@@ -1106,8 +1131,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
@@ -1244,7 +1267,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"
@@ -1255,7 +1278,6 @@ do_localcddb ()
                                #echo "Not using local copy of CDDB data"
                                CDDBLOCALSUCCESS="n"
                        fi
-                       CDDBLOCALSUCCESS="y"
                else
                        CDDBLOCALSUCCESS="n"
                fi
@@ -1616,7 +1638,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
@@ -1871,7 +1893,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:
@@ -2005,6 +2028,8 @@ NORMALIZEOPTS=
 CDSPEEDOPTS="-x"
 CDSPEEDVALUE=
 MKCUEOPTS=
+VORBISCOMMENTOPTS="-R"
+METAFLACOPTS="--no-utf8-convert"
 
 # Default to one process if -j isn't specified
 MAXPROCS=1
@@ -2084,7 +2109,7 @@ 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
        case "$opt" in
                1) ONETRACK=y ;;
                a) ACTIONS="$OPTARG" ;;
@@ -2094,7 +2119,9 @@ while getopts 1a:A:bc:C:d:Dhj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W: opt ; do
                C) DISCID="${OPTARG#abcde.}" ;;
                d) CDROM="$OPTARG" ;;
                D) set -x ;;
+               e) ERASEENCODEDSTATUS=y ;;
                h) usage; exit ;;
+               e) ERASEENCODEDSTATUS=y ;;
 #              f) FORCECDDBUSELOCAL=y ;;
                i) INLINETAG=y ;;
                j) MAXPROCS="$OPTARG" ;;
@@ -2126,7 +2153,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
@@ -2137,7 +2170,7 @@ 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"
+               dagrab|cddafs|debug) echo "abcde error: ONETRACK reading is not suported with $CDROMREADERSYNTAX yet"
                                         exit 1 ;;
        esac
        if [ "$BATCH" = "y" ]; then
@@ -2206,14 +2239,13 @@ if [ X"$CDROM" != "X" ] ; then
                                echo "abcde error: CDROMID not in the right format for $CDROMREADERSYNTAX"
                                echo "Use \"cdrecord -scanbus\" to obtain a adecuate ID an set CDROMID accordingly"
                                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
@@ -2359,7 +2391,11 @@ esac
 case "$FLACENCODERSYNTAX" in
        flac)
                FLACENCODEROPTS="${FLACENCODEROPTSCLI:-$FLACOPTS}"
-               FLACENCODER="$FLAC"
+               FLACENCODER="$FLAC"     
+               ## FIXME ## FLAC streams can be encapsulated on a Ogg transport layer
+               if echo "$FLACENCODEROPTS" | egrep -q -- "(^| )--ogg($| )" ;then
+                       FLACONOGG=y
+               fi
                ;;
 esac
 case "$SPEEXENCODERSYNTAX" in
@@ -2398,11 +2434,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 [ "$CUEFILE" = "y" ]; then
+       NEEDMKCUE=y
+fi
 
 if [ X"$CDSPEEDVALUE" != "X" ]; then
        case "$CDROMREADERSYNTAX" in
@@ -2418,6 +2464,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
@@ -2521,6 +2568,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
 
 (
@@ -2539,11 +2596,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
@@ -2665,8 +2720,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
 
@@ -2735,8 +2789,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
@@ -2761,8 +2815,6 @@ do
                        fi
                fi
        fi
-       if [ "$DOTAG" = "y" ]; then
-               if checkstatus encodetrack-$UTRACKNUM; then
                        if checkstatus tagtrack-$UTRACKNUM; then :; else do_tag $UTRACKNUM; fi
                fi
        fi