Don't discard all previous cddbdata if a sed on cd-text data errors out
[abcde.git] / abcde
diff --git a/abcde b/abcde
index d2030c1..080ed24 100755 (executable)
--- a/abcde
+++ b/abcde
@@ -11,7 +11,7 @@
 # with this program; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
-VERSION='2.7.1-UNRELEASED'
+VERSION='2.7.2-UNRELEASED'
 
 usage ()
 {
@@ -47,7 +47,7 @@ echo "-m     Modify playlist to include CRLF endings, to comply with some player
 echo "-n     No lookup. Don't query CDDB, just create and use template"
 echo "-N     Noninteractive. Never prompt for anything"
 echo "-o <type1[,type2]...>"
-echo "       Output file type(s) (vorbis,mp3,flac,spx,mpc,wav,m4a,opus,wv,ape,mp2,tta). Defaults to vorbis"
+echo "       Output file type(s) (vorbis,mp3,flac,spx,mpc,wav,m4a,opus,mka,wv,ape,mp2,tta). 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]...>"
@@ -781,7 +781,7 @@ local id=""
                "jpop")                  id=146 ;;
                "synthpop")              id=147 ;;
                "rock/pop"|"rock / pop") id=148 ;;
-               *)                       return 1 ;;
+               *)                       id=255 ;;
        esac
 echo ${id}
 return 0
@@ -839,6 +839,16 @@ do_tag ()
                                                ${TPE2:+--TPE2 "$TPE2"} \
                                                "$ABCDETEMPDIR/track$1.$OUTPUT"
                                        ;;
+                               id3tag)
+                                       # FIXME # track numbers in mp3 come with 1/10, so we cannot
+                                       # happily substitute them with $TRACKNUM
+                                       run_command tagtrack-$OUTPUT-$1 nice $ENCNICE \
+                                               $TAGGER $TAGGEROPTS -c "$COMMENTOUTPUT" \
+                                               -A "$DALBUM" -a "$TRACKARTIST" -s "$TRACKNAME" \
+                                               -y "$CDYEAR" -g "$GENREID" \
+                                               -t "${TRACKNUM:-$1}" ${TRACKNUM:+-T "$TRACKS"} \
+                                               "$ABCDETEMPDIR/track$1.$OUTPUT"
+                                       ;;
                                eyed3*)
                                        # FIXME # track numbers in mp3 come with 1/10, so we cannot
                                        # happily substitute them with $TRACKNUM
@@ -926,6 +936,9 @@ do_tag ()
                opus)
                        run_command tagtrack-$OUTPUT-$1 true
                        ;;
+               mka)
+                       run_command tagtrack-$OUTPUT-$1 true
+                       ;;
                flac)
                        (
                        echo ARTIST="$TRACKARTIST"
@@ -1096,6 +1109,9 @@ do_encode ()
                        opus)
                                TEMPARG="PIPE_$OPUSENCODERSYNTAX"
                                ;;
+                       mka)
+                               TEMPARG="PIPE_$MKAENCODERSYNTAX"
+                               ;;
                        flac)
                                TEMPARG="PIPE_$FLACENCODERSYNTAX"
                                ;;
@@ -1133,6 +1149,9 @@ do_encode ()
                                opus)
                                        OUTPUT=$OPUSOUTPUTCONTAINER
                                        ;;
+                               mka)
+                                       OUTPUT=$MKAOUTPUTCONTAINER
+                                       ;;
                                flac)
                                        OUTPUT=$FLACOUTPUTCONTAINER
                                        ;;
@@ -1208,6 +1227,19 @@ do_encode ()
                                        ;;
                                esac
                                ;;
+                       mka)
+                               case "$MKAENCODERSYNTAX" in
+                               ffmpeg)
+                                       if [ "$DOTAG" = "y" ]; then
+                                               $RUN_COMMAND nice $EFFECTIVE_NICE $MKAENCODER -i "$IN" $MKAENCODEROPTS -metadata artist="$TRACKARTIST" \
+                                               -metadata album="$DALBUM" -metadata title="$TRACKNAME" -metadata track=${TRACKNUM:-$1} -metadata date="$CDYEAR" \
+                                               -metadata genre="$CDGENRE" -metadata comment="$COMMENT" "$OUT"
+                                       else
+                                               $RUN_COMMAND nice $EFFECTIVE_NICE $MKAENCODER -i "$IN" $MKAENCODEROPTS "$OUT"
+                                       fi
+                               ;;
+                               esac
+                               ;;
                        flac)
                                case "$2" in
                                %local*%)
@@ -1575,6 +1607,9 @@ do_move ()
                                opus)
                                        OUTPUT=$OPUSOUTPUTCONTAINER
                                        ;;
+                               mka)
+                                       OUTPUT=$MKAOUTPUTCONTAINER
+                                       ;;
                                flac)
                                        OUTPUT=$FLACOUTPUTCONTAINER
                                        ;;
@@ -1659,6 +1694,9 @@ do_playlist ()
                        opus)
                                OUTPUT=$OPUSOUTPUTCONTAINER
                                ;;
+                       mka)
+                               OUTPUT=$MKAOUTPUTCONTAINER
+                               ;;
                        flac)
                                OUTPUT=$FLACOUTPUTCONTAINER
                                ;;
@@ -2465,11 +2503,12 @@ do_cdtext ()
                TRACKM1=$(($TRACK - 1))
                TITLE="$(grep -E ^Track\ +$TRACK: "$ABCDETEMPDIR/cd-text" | tr -d \\r\\n | sed 's~^Track ..: .~~g;'"s~'$~~g")"
                echo "$TRACK: $TITLE" >> "$ABCDETEMPDIR/cddbchoices"
-               sed "s~^TTITLE${TRACKM1}=.*~TTITLE${TRACKM1}=${TITLE}~" "$ABCDETEMPDIR/cddbread.1" > "$ABCDETEMPDIR/cddbread.new"
-        mv -f "$ABCDETEMPDIR/cddbread.new" "$ABCDETEMPDIR/cddbread.1"
+               sed "s~^TTITLE${TRACKM1}=.*~TTITLE${TRACKM1}=${TITLE}~" "$ABCDETEMPDIR/cddbread.1" > "$ABCDETEMPDIR/cddbread.new" && \
+                       mv -f "$ABCDETEMPDIR/cddbread.new" "$ABCDETEMPDIR/cddbread.1"
        done
-       sed "s~^DTITLE=.*~DTITLE=${ATITLE}~" "$ABCDETEMPDIR/cddbread.1" > "$ABCDETEMPDIR/cddbread.new"
-    mv -f "$ABCDETEMPDIR/cddbread.new" "$ABCDETEMPDIR/cddbread.1"
+       sed "s~^DTITLE=.*~DTITLE=${ATITLE}~" "$ABCDETEMPDIR/cddbread.1" > "$ABCDETEMPDIR/cddbread.new" && \
+               mv -f "$ABCDETEMPDIR/cddbread.new" "$ABCDETEMPDIR/cddbread.1"
+       rm -f "$ABCDETEMPDIR/cddbread.new" # In case a sed error occurred
        echo >> "$ABCDETEMPDIR/cddbchoices"
        echo "cdtext-readcomplete" >> "$ABCDETEMPDIR/status"
 }
@@ -2669,12 +2708,14 @@ do_cddbread ()
                        done
                        echo >> "$ABCDETEMPDIR/cddbchoices"
                        ;;
-               202|403|409|503)
-                       # No match
+               202|403|409|500|503)
+                       # TODO: Explain these error codes a little more accurately:
+                       # http://ftp.freedb.org/pub/freedb/misc/freedb_CDDB_protcoldoc.zip
+                       # No match response:
                        case "$RESPONSECODE" in
                        202) echo "No CDDB match." >> "$ABCDETEMPDIR/cddbchoices" ;;
                        403|409) echo "CDDB entry is corrupt, or the handshake failed." >> "$ABCDETEMPDIR/cddbchoices" ;;
-                       503) echo "CDDB unavailable." >> "$ABCDETEMPDIR/cddbchoices" ;;
+                       500|503) echo "CDDB unavailable." >> "$ABCDETEMPDIR/cddbchoices" ;;
                        esac
                        $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.0"
                        # List out disc title/author and contents of template
@@ -3180,11 +3221,19 @@ do_getalbumart()
                for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
                do
                        # put cover in the same place as the album
+                       if [ "$ONETRACK" = "y" ] ; then
+                               if [ "$VARIOUSARTISTS" = "y" ] ; then
+                                       ALBUMARTDIR="$(eval echo "$VAONETRACKOUTPUTFORMAT")"
+                               else
+                                       ALBUMARTDIR="$(eval echo "$ONETRACKOUTPUTFORMAT")"
+                       fi
+               else
                        if [ "$VARIOUSARTISTS" = "y" ] ; then
                                ALBUMARTDIR="$(eval echo "$VAOUTPUTFORMAT")"
                        else
                                ALBUMARTDIR="$(eval echo "$OUTPUTFORMAT")"
                        fi
+               fi
                        FINALALBUMARTDIR="$(dirname "$OUTPUTDIR/$ALBUMARTDIR")"
                        vecho "copying cover to target directory $FINALALBUMARTDIR" >&2
                        mkdir -p "$FINALALBUMARTDIR"
@@ -3231,7 +3280,6 @@ do_cdread ()
        if [ "$USEPIPES" = "y" ]; then
                TEMPARG="PIPERIPPER_$CDROMREADERSYNTAX"
                FILEARG="$( eval echo "\$$TEMPARG" )"
-               REDIR=""
                PIPE_MESSAGE="and encoding "
        else
                WAVDATA="$ABCDETEMPDIR/track$UTRACKNUM.wav"
@@ -3244,7 +3292,6 @@ do_cdread ()
                                FILEARG="$WAVDATA"
                                ;;
                esac
-               REDIR=">&2"
        fi
        if [ "$1" = "onetrack" ]; then
                echo "Grabbing ${PIPE_MESSAGE}tracks $UTRACKNUM - $LASTTRACK as one track ..." >&2
@@ -3264,8 +3311,13 @@ do_cdread ()
                        STRIPTRACKNUM=$(expr $UTRACKNUM + 0)
                        nice $READNICE $FLAC -d -f --cue=${READTRACKNUMS:-$STRIPTRACKNUM.1-$(($STRIPTRACKNUM + 1)).0} "$FILEARG" "$CDROM" ;;
                cdparanoia|libcdio)
-                       nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG" $REDIR ;;
-               cdda2wav | icedax)
+                       if [ "$USEPIPES" = "y" ]; then
+                               nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG"
+                       else
+                               nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG" >&2
+                       fi
+                        ;;
+               cdda2wav|icedax)
                        if [ "$OSFLAVOUR" = "OSX" ] ; then
                                # Hei, we have to unmount the device before running anything like cdda2wav/icedax in OSX
                                diskutil unmount ${CDROM#/dev/}
@@ -3280,17 +3332,25 @@ do_cdread ()
                                        CDDA2WAVCDROM="$CDROMID"
                                fi
                        fi
-                       nice $READNICE $CDROMREADER -D $CDDA2WAVCDROM -t ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG" $REDIR ;;
-               dagrab) nice $READNICE $CDROMREADER -d "$CDROM" -f "$FILEARG" -v $UTRACKNUM >&2 ;;
+                       if [ "$USEPIPES" = "y" ]; then
+                               nice $READNICE $CDROMREADER -D $CDDA2WAVCDROM -t ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG"
+                       else
+                               nice $READNICE $CDROMREADER -D $CDDA2WAVCDROM -t ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG" >&2
+                       fi
+               ;;
+               dagrab)
+                                       # I cannot get USEPIPES to work with dagrab so just this:
+                               nice $READNICE $CDROMREADER -d "$CDROM" -f "$FILEARG" -v $UTRACKNUM >&2
+               ;;
                pird)
                        if [ "$USEPIPES" = "y" ]; then
                                nice $READNICE $CDROMREADER -j ${READTRACKNUMS:-$UTRACKNUM} "$CDROM" "$FILEARG"
                        else
                                # Write ripped audio data to stdout and redirect to $FILEARG.
-                               # $REDIR can be ignored. Progress is written to stderr by default.
+                               # Progress is written to stderr by default and thus >&2 is not required.
                                nice $READNICE $CDROMREADER -j ${READTRACKNUMS:-$UTRACKNUM} "$CDROM" "$PIPERIPPER_pird" > "$FILEARG"
                        fi
-                       ;;
+               ;;
                cddafs)
                        # Find the track's mounted path
                        REALTRACKNUM=$(expr $UTRACKNUM + 0)
@@ -3298,11 +3358,21 @@ do_cdread ()
                        FILEPATH=$(find "$FILEPATH" | grep "/$REALTRACKNUM ");
                        # If the file exists, copy it
                        if [ -e "$FILEPATH" ] ; then
-                               nice $READNICE $CDROMREADER "$FILEPATH" "$FILEARG" $REDIR
+                               if [ "$USEPIPES" = "y" ]; then
+                                       nice $READNICE $CDROMREADER "$FILEPATH" "$FILEARG"
+                               else
+                               nice $READNICE $CDROMREADER "$FILEPATH" "$FILEARG" >&2
+                       fi
                        else
                                false
                        fi ;;
-               debug) nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" -w $UTRACKNUM-[:1] "$FILEARG" $REDIR ;;
+               debug)
+                       if [ "$USEPIPES" = "y" ]; then
+                               nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" -w $UTRACKNUM-[:1] "$FILEARG"
+                       else
+                               nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" -w $UTRACKNUM-[:1] "$FILEARG" >&2
+                       fi
+               ;;
        esac
        RETURN=$?
        # If we get some error or we get some missing wav
@@ -3448,6 +3518,7 @@ ENCODERSYNTAX=default
 MP3ENCODERSYNTAX=default
 OGGENCODERSYNTAX=default
 OPUSENCODERSYNTAX=default
+MKAENCODERSYNTAX=default
 FLACENCODERSYNTAX=default
 SPEEXENCODERSYNTAX=default
 MPCENCODERSYNTAX=default
@@ -3538,6 +3609,7 @@ ID3=id3
 ID3V2=id3v2
 MID3V2=mid3v2
 EYED3=eyeD3
+ID3TAG=id3tag
 VORBISCOMMENT=vorbiscomment
 METAFLAC=metaflac
 NEROAACTAG=neroAacTag
@@ -3621,6 +3693,7 @@ FHGAACENCOPTS=
 FFMPEGENCOPTS=
 
 ID3OPTS=
+ID3TAGOPTS=
 EYED3OPTS=""
 ATOMICPARSLEYOPTS=
 CDPARANOIAOPTS=
@@ -3683,7 +3756,7 @@ elif [ X$(uname) = "XDarwin" ] ; then
        # We won't find the eject program in OSX, and doing checkexec will fail further below...
        unset EJECT
 elif [ X$(uname) = "XOpenBSD" ] ; then
-       HTTPGET=wget
+       HTTPGET=ftp
        MD5SUM=md5
        OSFLAVOUR=OBSD
 elif [ X$(uname) = "XNetBSD" ] ; then
@@ -4115,6 +4188,7 @@ case "$OUTPUTTYPE" in *:*)
                case "$OUTPUT" in
                        vorbis:*|ogg:*) OGGENCODEROPTSCLI="$( echo $OUTPUT | cut -d: -f2- )" ;;
                        opus:*) OPUSENCODEROPTSCLI="$( echo $OUTPUT | cut -d: -f2- )" ;;
+                       mka:*)  MKAENCODEROPTSCLI="$( 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- )" ;;
@@ -4154,6 +4228,10 @@ do
                        [ "$OPUSENCODERSYNTAX" = "default" ] && OPUSENCODERSYNTAX=opusenc
                        OPUSOUTPUTCONTAINER=opus
                        ;;
+               mka)
+                       [ "$MKAENCODERSYNTAX" = "default" ] && MKAENCODERSYNTAX=ffmpeg
+                       MKAOUTPUTCONTAINER=mka
+                       ;;
                mp3)
                        [ "$MP3ENCODERSYNTAX" = "default" ] && MP3ENCODERSYNTAX=lame
                        [ "$DOTAG" = "y" ] && NEEDTAGGER=y
@@ -4262,6 +4340,12 @@ case "$OPUSENCODERSYNTAX" in
                OPUSENCODER="$OPUSENC"
                ;;
 esac
+case "$MKAENCODERSYNTAX" in
+       ffmpeg)
+               MKAENCODEROPTS="${MKAENCODEROPTSCLI:-$FFMPEGENCOPTS}"
+               MKAENCODER="$FFMPEG"
+               ;;
+esac
 case "$FLACENCODERSYNTAX" in
        flac)
                FLACENCODEROPTS="${FLACENCODEROPTSCLI:-$FLACOPTS}"
@@ -4407,6 +4491,11 @@ case "$ID3TAGV" in
                ID3SYNTAX=id3v2
                TAGGEROPTS="$ID3V2OPTS"
                ;;
+       id3tag)
+               TAGGER="$ID3TAG"
+               ID3SYNTAX=id3tag
+               TAGGEROPTS="$ID3TAGOPTS"
+               ;;
        id3v2.4)
                TAGGER="$EYED3"
                # Note that eyeD3 is set to tag in utf-16 (below). This causes
@@ -4555,6 +4644,8 @@ if [ "$USEPIPES" = "y" ]; then
                        PIPEENCODERSVARCHECK="PIPE_$OGGENCODERSYNTAX" ;;
                opus)
                        PIPEENCODERSVARCHECK="PIPE_$OPUSENCODERSYNTAX" ;;
+               mka)
+                       PIPEENCODERSVARCHECK="PIPE_$MKAENCODERSYNTAX" ;;
                flac)
                        PIPEENCODERSVARCHECK="PIPE_$FLACENCODERSYNTAX" ;;
                spx)
@@ -4588,7 +4679,7 @@ fi
 
 # Make sure a buncha things exist
 for X in $CDROMREADER $CDDISCID ${NEEDTAGGER+$TAGGER} $MP3ENCODER \
-       $OGGENCODER $OPUSENCODER $FLACENCODER $SPEEXENCODER $MPCENCODER \
+       $OGGENCODER $OPUSENCODER $MKAENCODER $FLACENCODER $SPEEXENCODER $MPCENCODER \
        $AACENCODER $WVENCODER $CDDBTOOL $APENCODER $MP2ENCODER $TTAENCODER \
        ${NEEDHTTPGET+$HTTPGET} ${NEEDDISTMP3+$DISTMP3} \
        ${NEEDCOMMENTER+$VORBISCOMMENT} ${NEEDMETAFLAC+$METAFLAC} \
@@ -4873,24 +4964,25 @@ fi
 #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" ] && [ "$BATCHNORM" = "y" ]; then
-                       if checkstatus normalizetrack-$LASTTRACK; then :; else do_batch_normalize; fi
-                       if checkerrors batch-normalize; then exit 1; fi
-               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
+       if [ "$GOAHEAD" != "NO" ]; then
+               for LASTTRACK in $TRACKQUEUE; do :; done
+               if checkstatus readtrack-$LASTTRACK; then
+                       if [ "$DONORMALIZE" = "y" ] && [ "$BATCHNORM" = "y" ]; then
+                               if checkstatus normalizetrack-$LASTTRACK; then :; else do_batch_normalize; fi
+                               if checkerrors batch-normalize; then exit 1; fi
+                       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
+                               fi
+                               if checkstatus encodetrack-$LASTTRACK; then :; else do_nogap_encode; fi
+                               if checkerrors nogap-encode; then exit 1; fi
                        fi
-                       if checkstatus encodetrack-$LASTTRACK; then :; else do_nogap_encode; fi
-                       if checkerrors nogap-encode; then exit 1; fi
                fi
        fi
 fi