Badge the upcoming release as 2.7 rather than 2.6.1
[abcde.git] / abcde
diff --git a/abcde b/abcde
index 1cc3320..5b36406 100755 (executable)
--- a/abcde
+++ b/abcde
@@ -13,7 +13,7 @@
 # Copyright for this work is to expire January 1, 2010, after which it
 # shall be public domain.
 
-VERSION='2.6.1-UNRELEASED'
+VERSION='2.7-UNRELEASED'
 
 usage ()
 {
@@ -23,7 +23,7 @@ echo "Options:"
 echo "-1     Encode the whole CD in a single file"
 echo "-a <action1[,action2]...>"
 echo "       Actions to perform:"
-echo "       cddb,read,normalize,encode,tag,move,replaygain,playlist,clean"
+echo "       cddb,read,getalbumart,normalize,encode,tag,move,replaygain,playlist,clean"
 #echo "-A     Experimental actions (retag, transcode)"
 echo "-b     Enable batch normalization"
 echo "-c <file>"
@@ -36,6 +36,7 @@ echo "-D     Debugging mode (equivalent to sh -x abcde)"
 echo "-e     Erase encoded track information from status file"
 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 "-G     Get album art by using the 'getalbumart' action"
 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)"
@@ -86,9 +87,9 @@ log ()
        BLURB="$1"
        shift
        case $BLURB in
-               error)   echo "[ERROR] abcde: $@" >&2 ;;
-               warning) echo "[WARNING] $@" >&2 ;;
-               info)    echo "[INFO] $@" ;;
+               error)   >&2 echo "[ERROR] abcde: $@" >&2 ;;
+               warning) >&2 echo "[WARNING] $@" >&2 ;;
+               info)    >&4 echo "[INFO] $@" ;;
        esac
 }
 
@@ -820,7 +821,6 @@ do_tag ()
                                TPE2="Various"
                        fi
 
-
                        case "$ID3SYNTAX" in
                                id3)
                                        run_command tagtrack-$OUTPUT-$1 nice $ENCNICE \
@@ -863,7 +863,7 @@ do_tag ()
                                                        );;
                                        esac
                                        run_command tagtrack-$OUTPUT-$1 nice $ENCNICE $TAGGER $TAGGEROPTS \
-                                           -A "$DALBUM" \
+                                               -A "$DALBUM" \
                                                -a "$TRACKARTIST" -t "$TRACKNAME" \
                                                -G "$GENREID" -n "${TRACKNUM:-$1}" \
                                                ${TRACKNUM:+-N "$TRACKS"} \
@@ -885,7 +885,7 @@ do_tag ()
                                        fi
                                        (
                                        # These are from
-                                       # http://www.xiph.org/ogg/vorbis/doc/v-comment.html
+                                       # http://www.xiph.org/vorbis/doc/v-comment.html
 
                                        echo ARTIST="$TRACKARTIST"
                                        echo ALBUM="$DALBUM"
@@ -897,6 +897,11 @@ do_tag ()
                                                echo GENRE="$CDGENRE"
                                        fi
                                        echo TRACKNUMBER=${TRACKNUM:-$1}
+                                       # TRACKTOTAL is not in the proposed, minimal list of standard field names from
+                                       # xiph.org: http://www.xiph.org/vorbis/doc/v-comment.html but is in common usage
+                                       # and read by mediainfo, ffprobe, vlc, Aqualung, ogg123, Foobar. And now abcde :)
+                                       # The tag is quietly ignored by  Audacious, MPlayer, mpv, XMMS....
+                                       echo TRACKTOTAL="${TRACKS}"
                                        if [ -n "$DISCNUMBER" ]; then
                                                echo DISCNUMBER="$DISCNUMBER"
                                        fi
@@ -923,7 +928,6 @@ do_tag ()
                opus)
                        run_command tagtrack-$OUTPUT-$1 true
                        ;;
-
                flac)
                        (
                        echo ARTIST="$TRACKARTIST"
@@ -936,6 +940,11 @@ do_tag ()
                                echo GENRE="$CDGENRE"
                        fi
                        echo TRACKNUMBER="${TRACKNUM:-$1}"
+                       # TRACKTOTAL is not in the proposed, minimal list of standard field names from
+                       # xiph.org: http://www.xiph.org/vorbis/doc/v-comment.html but is in common usage
+                       # and read by mediainfo, ffprobe, vlc, Aqualung, ogg123, Foobar. And now abcde :)
+                       # The tag is quietly ignored by  Audacious, MPlayer, mpv, XMMS....
+                       echo TRACKTOTAL="${TRACKS}"
                        if [ -n "$DISCNUMBER" ]; then
                                echo DISCNUMBER="$DISCNUMBER"
                        fi
@@ -946,7 +955,8 @@ do_tag ()
                                        *)   echo COMMENT="$COMMENTOUTPUT";;
                                esac
                        fi
-                       ) | run_command tagtrack-$OUTPUT-$1 nice $ENCNICE $METAFLAC $METAFLACOPTS ${IMPORTCUESHEET:+--import-cuesheet-from="$ABCDETEMPDIR/$CUEFILE"} --import-tags-from=- "$ABCDETEMPDIR/track$1.$FLACOUTPUTCONTAINER"
+                       ) | run_command tagtrack-$OUTPUT-$1 nice $ENCNICE $METAFLAC $METAFLACOPTS ${IMPORTCUESHEET:+--import-cuesheet-from="$ABCDETEMPDIR/$CUEFILE"} \
+                       --import-tags-from=- "$ABCDETEMPDIR/track$1.$FLACOUTPUTCONTAINER"
                        ;;
                spx)
                        run_command tagtrack-$OUTPUT-$1 true
@@ -954,32 +964,35 @@ do_tag ()
                mpc)
                        run_command tagtrack-$OUTPUT-$1 true
                        ;;
-        wv)
-            run_command tagtrack-$OUTPUT-$1 true
+               wv)
+                       run_command tagtrack-$OUTPUT-$1 true
                        ;;
-        ape) # This tagging syntax is suitable for Robert Muth's application 'apetag', the Monkey's Audio 
-             # Console port (mac) used for encoding does not have the ability to tag.              Andrew.
-            run_command tagtrack-$OUTPUT-$1 nice $ENCNICE "$APETAG" -i "$ABCDETEMPDIR/track$1.ape" -m overwrite \
-                    -p artist="$TRACKARTIST" -p album="$DALBUM" -p title="$TRACKNAME" -p track=${TRACKNUM:-$1} \
-                    -p year="$CDYEAR" -p genre="$CDGENRE" ${COMMENTOUTPUT:+-p comment="$COMMENTOUTPUT"} 
+               ape)
+                       # This tagging syntax is suitable for Robert Muth's application 'apetag', the Monkey's Audio 
+                       # Console port (mac) used for encoding does not have the ability to tag.
+                       run_command tagtrack-$OUTPUT-$1 nice $ENCNICE "$APETAG" -i "$ABCDETEMPDIR/track$1.ape" -m overwrite \
+                       -p artist="$TRACKARTIST" -p album="$DALBUM" -p title="$TRACKNAME" -p track=${TRACKNUM:-$1} \
+                       -p year="$CDYEAR" -p genre="$CDGENRE" ${COMMENTOUTPUT:+-p comment="$COMMENTOUTPUT"} 
                        ;;
                aac)
                        run_command tagtrack-$OUTPUT-$1 true
-               ;;
-               m4a)
-                 case "$AACENCODERSYNTAX" in
-                   fdkaac) # We will use inline tagging...
-                       run_command tagtrack-$OUTPUT-$1 true
-                       ;;
-                   neroAacEnc) # Tag post encode with neroAacTag...
-                    run_command tagtrack-$OUTPUT-$1 nice $ENCNICE "$NEROAACTAG" "$ABCDETEMPDIR/track$1.m4a" \
-                    -meta:artist="$TRACKARTIST" -meta:album="$DALBUM" -meta:title="$TRACKNAME" -meta:track=${TRACKNUM:-$1} \
-                    -meta:year="$CDYEAR" -meta:genre="$CDGENRE" -meta:comment="$COMMENT"
-                     ;;
-                  faac)
-                    run_command tagtrack-$OUTPUT-$1 true                   
                        ;;
-               esac                    
+               m4a)
+                       case "$AACENCODERSYNTAX" in
+                               fdkaac)
+                                       # We will use inline tagging...
+                                       run_command tagtrack-$OUTPUT-$1 true
+                                       ;;
+                               neroAacEnc)
+                                       # Tag post encode with neroAacTag...
+                                       run_command tagtrack-$OUTPUT-$1 nice $ENCNICE "$NEROAACTAG" "$ABCDETEMPDIR/track$1.m4a" \
+                                       -meta:artist="$TRACKARTIST" -meta:album="$DALBUM" -meta:title="$TRACKNAME" -meta:track=${TRACKNUM:-$1} \
+                                       -meta:year="$CDYEAR" -meta:genre="$CDGENRE" -meta:comment="$COMMENT"
+                                       ;;
+                               faac)
+                                       run_command tagtrack-$OUTPUT-$1 true   
+                                       ;;
+                       esac
                        ;;
                wav)
                        run_command tagtrack-$OUTPUT-$1 true
@@ -989,7 +1002,6 @@ do_tag ()
        if checkerrors "tagtrack-(.{3,6})-$1"; then :; else
                run_command tagtrack-$1 true
        fi
-
 }
 
 # do_nogap_encode
@@ -1124,7 +1136,7 @@ do_encode ()
                                %local*%)
                                        case "$MP3ENCODERSYNTAX" in
                                        lame|toolame|gogo) $RUN_COMMAND nice $EFFECTIVE_NICE $MP3ENCODER $MP3ENCODEROPTS "$IN" "$OUT" ;;
-                                       bladeenc) $RUN_COMMAND nice $EFFECTIVE_NICE $MP3ENCODER $MP3ENCODEROPTS -quit "$IN" ;;
+                                       bladeenc) $RUN_COMMAND nice $EFFECTIVE_NICE $MP3ENCODER $MP3ENCODEROPTS -quit "$IN" "$OUT" ;;
                                        l3enc|xingmp3enc) $RUN_COMMAND nice $EFFECTIVE_NICE $MP3ENCODER "$IN" "$OUT" $MP3ENCODEROPTS ;;
                                        mp3enc) $RUN_COMMAND nice $EFFECTIVE_NICE $MP3ENCODER -if "$IN" -of "$OUT" $MP3ENCODEROPTS ;;
                                        esac
@@ -1932,7 +1944,7 @@ do_discid ()
        if [ -z "$TRACKQUEUE" ]; then
                if [ ! "$STRIPDATATRACKS" = "n" ]; then
                        case "$CDROMREADERSYNTAX" in
-                               cdparanoia|debug)
+                               cdparanoia|libcdio|debug)
                                        if [ "$WEHAVEACD" = "y" ]; then
                                                vecho "Querying the CD for audio tracks..."
                                                CDPARANOIAOUTPUT="$( $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" -Q --verbose 2>&1 )"
@@ -2974,6 +2986,140 @@ do_cddbedit ()
        echo "cddb-edit" >> "$ABCDETEMPDIR/status"
 }
 
+# do_getalbumart
+# try to download CD cover
+do_getalbumart()
+{
+       # set variables
+       ALBUMFILE="$(mungefilename "$DALBUM")"
+       ARTISTFILE="$(mungefilename "$DARTIST")"
+       # have we got a musicbrainz mbid or amazon asin?
+       case "$CDDBMETHOD" in
+               musicbrainz)
+                       # try musicbrainz mbid
+                       if [ -s "$ABCDETEMPDIR/mbid.$(checkstatus cddb-choice)" ]; then
+                               MBID=$(cat "$ABCDETEMPDIR/mbid.$(checkstatus cddb-choice)")
+                               vecho "trying to get cover from coverartarchive.orq with musicbrainz mbid $MBID" >&2
+                               ALBUMARTURL="http://coverartarchive.org/release/$MBID/front"
+                               vecho "cover URL: $ALBUMARTURL" >&2
+                               $HTTPGET "$ALBUMARTURL" > "$ABCDETEMPDIR/$ALBUMARTFILE"
+                               if [ $? -ne 0 ]; then
+                                       vecho "could not download cover from musicbrainz" >&2
+                                       # try amazon asin
+                                       if [ -s "$ABCDETEMPDIR/asin.$(checkstatus cddb-choice)" ]; then
+                                               ASIN=$(cat "$ABCDETEMPDIR/asin.$(checkstatus cddb-choice)")
+                                               vecho "trying to get cover from amazon.com with asin $ASIN" >&2
+                                               ALBUMARTURL="http://ec1.images-amazon.com/images/P/$ASIN.01.LZZZZZZZZ.jpg"
+                                               vecho "cover URL: $ALBUMARTURL" >&2
+                                               $HTTPGET "$ALBUMARTURL" > "$ABCDETEMPDIR/$ALBUMARTFILE"
+                                               if [ $? -ne 0 ]; then
+                                                       vecho "could not download cover from amazon" >&2
+                                               else
+                                                       # Check that size is reasonable; sometimes when there is no cover image
+                                                       # on amazon.com a 1x1 pixel gif image will be downloaded instead:
+                                                       FILESIZE=$(wc -c < "$ABCDETEMPDIR/$ALBUMARTFILE")
+                                                       if [ "$FILESIZE" -lt 1024 ]; then
+                                                               rm "$ABCDETEMPDIR/$ALBUMARTFILE"
+                                                               vecho "could not download cover from amazon" >&2
+                                                       fi
+                                               fi
+                                       else
+                                               vecho "no amazon ID found" >&2
+                                       fi
+                               fi
+                       else
+                               vecho "no musicbrainz ID found" >&2
+                       fi
+                       ;;
+       esac
+       # use glyrc
+       if [ ! -s "$ABCDETEMPDIR/$ALBUMARTFILE" ]; then
+               vecho "trying to get cover with glyrc for $ARTISTFILE / $ALBUMFILE" >&2
+               $GLYRC cover --artist "$ARTISTFILE" --album "$ALBUMFILE" --write "$ABCDETEMPDIR/$ALBUMARTFILE" $GLYRCOPTS
+               if [ $? -ne 0 ]; then
+                       vecho "could not download cover with glyrc" >&2
+               else
+                       ALBUMARTURL="glyrc"
+               fi
+       fi
+       if [ "$INTERACTIVE" = "y" ]; then
+               if [ -s "$ABCDETEMPDIR/$ALBUMARTFILE" ]; then
+                       # display properties of coverart when identify is available
+                       if new_checkexec $IDENTIFY; then
+                               $IDENTIFY $IDENTIFYOPTS "$ABCDETEMPDIR/$ALBUMARTFILE" >&2
+                       fi
+                       # display coverart when DISPLAY is set and display command is available
+                       if new_checkexec $DISPLAYCMD && [ "$DISPLAY" != "" ]; then
+                               $DISPLAYCMD $DISPLAYCMDOPTS "$ABCDETEMPDIR/$ALBUMARTFILE" >&2 &
+                       fi
+               else
+                       # nothing downloaded yet
+                       vecho "automatic album art downloading failed" >&2
+               fi
+               # see if the user can find a better album art manually
+               echo -n "Do you want to enter URL or local path for the album art [y/N]? " >&2
+               read YESNO
+               while [ "$YESNO" != "y" ] && [ "$YESNO" != "n" ] && [ "$YESNO" != "Y" ] && \
+                       [ "$YESNO" != "N" ] && [ "$YESNO" != "" ]
+               do
+                       echo -n 'Invalid selection. Please answer "y" or "n": ' >&2
+                       read YESNO
+               done
+               if [ "$YESNO" = "y" ] || [ "$YESNO" = "Y" ]; then
+                       echo -n "Enter URL or local path (ENTER to cancel) :" >&2
+                       read ALBUMARTURL
+                       if [ ! -z "$ALBUMARTURL" ]; then
+                               if [[ ${ALBUMARTURL} =~ (https?|ftp|file)://.* ]]; then
+                                       $HTTPGET "$ALBUMARTURL" > "$ABCDETEMPDIR/$ALBUMARTFILE"
+                                       if [ ! -s "$ABCDETEMPDIR/$ALBUMARTFILE" ]; then
+                                               vecho "unable to download $ALBUMARTURL" >&2
+                                       fi
+                               else # it's a local path
+                                       cp "$ALBUMARTURL" "$ABCDETEMPDIR/$ALBUMARTFILE"
+                                       if [ ! -s "$ABCDETEMPDIR/$ALBUMARTFILE" ]; then
+                                               vecho "unable to copy $ALBUMARTURL to $ABCDETEMPDIR/$ALBUMARTFILE" >&2
+                                       fi
+                               fi
+                       fi
+               fi
+       fi
+       # convert to ALBUMARTTYPE if ImageMagick is available, if not assume correct type
+       if [ -s "$ABCDETEMPDIR/$ALBUMARTFILE" ] && new_checkexec $IDENTIFY; then
+               ALBUMARTURLTYPE=$($IDENTIFY "$ABCDETEMPDIR/$ALBUMARTFILE" | cut -d' ' -f2)
+               if [ "$ALBUMARTURLTYPE" != "$ALBUMARTTYPE" -o "$ALBUMARTALWAYSCONVERT" = "y" ]; then
+                       if new_checkexec $CONVERT; then
+                               mv "$ABCDETEMPDIR/$ALBUMARTFILE" "$ABCDETEMPDIR/$ALBUMARTFILE.tmp"
+                               $CONVERT "$ABCDETEMPDIR/$ALBUMARTFILE.tmp" $CONVERTOPTS "$ABCDETEMPDIR/$ALBUMARTFILE"
+                               rm -f "$ABCDETEMPDIR/$ALBUMARTFILE.tmp"
+                       else
+                               rm -f "$ABCDETEMPDIR/$ALBUMARTFILE"
+                               vecho "sorry, cannot convert $ALBUMARTURLTYPE to $ALBUMARTTYPE without ImageMagick convert" >&2
+                       fi
+               fi
+       fi
+       # copy to target directories
+       if [ -s "$ABCDETEMPDIR/$ALBUMARTFILE" ]; then
+               for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
+               do
+                       # put cover in the same place as the album
+                       if [ "$VARIOUSARTISTS" = "y" ] ; then
+                               ALBUMARTDIR="$(eval echo "$VAOUTPUTFORMAT")"
+                       else
+                               ALBUMARTDIR="$(eval echo "$OUTPUTFORMAT")"
+                       fi
+                       FINALALBUMARTDIR="$(dirname "$OUTPUTDIR/$ALBUMARTDIR")"
+                       vecho "copying cover to target directory $FINALALBUMARTDIR" >&2
+                       mkdir -p "$FINALALBUMARTDIR"
+                       cp "$ABCDETEMPDIR/$ALBUMARTFILE" "$FINALALBUMARTDIR"
+               done
+               rm -f "$ABCDETEMPDIR/$ALBUMARTFILE"
+               echo "get-album-art=$ALBUMARTURL" >> "$ABCDETEMPDIR/status"
+       else
+               log warning "could not get cover"
+               echo "get-album-art=none" >> "$ABCDETEMPDIR/status"
+       fi
+}
+
 # do_cdread [tracknumber]
 # do_cdread onetrack [firsttrack] [lasttrack]
 #
@@ -2991,7 +3137,7 @@ do_cdread ()
                UTRACKNUM=$FIRSTTRACK
                case "$CDROMREADERSYNTAX" in
                        flac) READTRACKNUMS="$FIRSTTRACK.1-$(($LASTTRACK + 1)).0" ;;
-                       cdparanoia)
+                       cdparanoia|libcdio)
                                #XX FIXME XX
                                # Add a variable to check if tracks are provided in command line and if not, use "0-" to rip the tracks
                                READTRACKNUMS="$FIRSTTRACK-$LASTTRACK" ;;
@@ -3012,13 +3158,10 @@ do_cdread ()
        else
                WAVDATA="$ABCDETEMPDIR/track$UTRACKNUM.wav"
                case "$CDROMREADERSYNTAX" in
-               ## FIXME ## Find the cases for dagrab and flac, to avoid exceptions
+               ## FIXME ## Find the cases for flac, to avoid exceptions
                        flac)
                                FILEARG="--output-name=$WAVDATA"
                                ;;
-                       dagrab)
-                               FILEARG="-f $WAVDATA"
-                               ;;
                        *)
                                FILEARG="$WAVDATA"
                                ;;
@@ -3042,7 +3185,7 @@ do_cdread ()
                        # Avoid problems with math expressions by unpadding the given UTRACKNUM
                        STRIPTRACKNUM=$(expr $UTRACKNUM + 0)
                        nice $READNICE $FLAC -d -f --cue=${READTRACKNUMS:-$STRIPTRACKNUM.1-$(($STRIPTRACKNUM + 1)).0} "$FILEARG" "$CDROM" ;;
-               cdparanoia)
+               cdparanoia|libcdio)
                        nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG" $REDIR ;;
                cdda2wav | icedax)
                        if [ "$OSFLAVOUR" = "OSX" ] ; then
@@ -3060,10 +3203,16 @@ do_cdread ()
                                fi
                        fi
                        nice $READNICE $CDROMREADER -D $CDDA2WAVCDROM -t ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG" $REDIR ;;
-               ## FIXME ## We have an exception for dagrab, since it uses -f
-               ## FIXME ## Shall we just use -f $FILEARG ??
-               dagrab) nice $READNICE $CDROMREADER -d "$CDROM" -v $UTRACKNUM "$FILEARG" $REDIR;;
-               pird) nice $READNICE $CDROMREADER -j ${READTRACKNUMS:-$UTRACKNUM} "$CDROM" "$FILEARG" $REDIR ;;
+               dagrab) 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.
+                               nice $READNICE $CDROMREADER -j ${READTRACKNUMS:-$UTRACKNUM} "$CDROM" "$PIPERIPPER_pird" > "$FILEARG"
+                       fi
+                       ;;
                cddafs)
                        # Find the track's mounted path
                        REALTRACKNUM=$(expr $UTRACKNUM + 0)
@@ -3123,7 +3272,7 @@ vecho ()
 if [ x"$EXTRAVERBOSE" != "x" ] && [ $EXTRAVERBOSE -gt 0 ] ; then
        case $1 in
                warning) shift ; log warning "$@" ;;
-               *) echo "$@" ;;
+               *) >&4 echo "$@" ;;
        esac
 fi
 }
@@ -3136,7 +3285,7 @@ vvecho ()
 if [ x"$EXTRAVERBOSE" != "x" ] && [ $EXTRAVERBOSE -gt 1 ] ; then
        case $1 in
                warning) shift ; log warning "$@" ;;
-               *) echo "$@" ;;
+               *) >&4 echo "$@" ;;
        esac
 fi
 }
@@ -3149,9 +3298,9 @@ decho ()
 if [ x"$DEBUG" != "x" ]; then
        if echo $1 | grep "^\[" > /dev/null 2>&1 ; then
                DEBUGECHO=$(echo "$@" | tr -d '[]')
-               echo "[DEBUG] $DEBUGECHO: `eval echo \\$${DEBUGECHO}`"
+               echo >&4 "[DEBUG] $DEBUGECHO: `eval echo \\$${DEBUGECHO}`"
        else
-               echo "[DEBUG] $1"
+               echo >&4 "[DEBUG] $1"
        fi
 fi
 }
@@ -3300,6 +3449,7 @@ METAFLAC=metaflac
 NEROAACTAG=neroAacTag
 
 CDPARANOIA=cdparanoia
+CD_PARANOIA=cd-paranoia
 CDDA2WAV=icedax
 DAGRAB=dagrab
 CDDAFS=cp
@@ -3324,6 +3474,10 @@ MKCUE=mkcue
 MKTOC=cdrdao
 DIFF=diff
 CUE2DISCID=builtin
+GLYRC=glyrc
+IDENTIFY=identify
+CONVERT=convert
+DISPLAYCMD=display
 
 # Options for programs called from abcde
 # mp3
@@ -3355,16 +3509,19 @@ MPCENCOPTS=
 WVENCOPTS=
 # ape
 # Monkey's Audio Console (mac) chokes without a mode setting
-# so we set one here.                                 Andrew.
+# so we set one here.
 APENCOPTS='-c4000'
 # m4a
 FAACENCOPTS=
 NEROACENCOPTS=
-FDKAACENCOPTS=
+# fdkaac chokes without either a bitrate or bitrate-mode specified so 
+# we set bitrate here.
+FDKAACENCOPTS='--bitrate 192k'
 
 ID3OPTS=
 EYED3OPTS=""
 CDPARANOIAOPTS=
+PIRDOPTS=
 CDDA2WAVOPTS=
 DAGRABOPTS=
 CDDAFSOPTS="-f"
@@ -3379,6 +3536,15 @@ MKTOCOPTS=""
 VORBISCOMMENTOPTS="-R"
 METAFLACOPTS="--no-utf8-convert"
 DIFFOPTS=
+GLYRCOPTS=
+IDENTIFYOPTS=
+CONVERTOPTS=
+DISPLAYCMDOPTS="-resize 512x512 -title abcde_album_art"
+
+# Defaults for album art downloads
+ALBUMARTFILE="cover.jpg"
+ALBUMARTTYPE="JPEG"
+ALBUMARTALWAYSCONVERT="n"
 
 # Default to one process if -j isn't specified
 MAXPROCS=1
@@ -3391,7 +3557,10 @@ ACTIONS=cddb,read,encode,tag,move,clean
 DEFAULT_OUTPUT_BINARIES=vorbis:oggenc,flac:flac,mp3:toolame,mp3:lame,mp3:bladeenc,spx:speex,m4a:faac:opus
 
 # List of preferred cdromreaders - by default, run whichever we have in the path
-DEFAULT_CDROMREADERS="cdparanoia icedax cdda2wav pird"
+DEFAULT_CDROMREADERS="cdparanoia icedax cdda2wav libcdio pird"
+
+# fd for when `vecho` and `log info` called with redirected stdout
+exec 4>&1
 
 # Assume fetch if under FreeBSD. curl is used for Mac OS X. wget is used for
 # Linux/OpenBSD. ftp is user for NetBSD.
@@ -3471,7 +3640,7 @@ fi
 if [ "$CDROM" = "" ] ; then
        if [ -e /dev/cdroms/cdrom0 ]; then
                CDROM=/dev/cdroms/cdrom0
-       elif [ "$OSFLAVOUR" = "OSX" ] && [[ $(diskutil list) =~ CD_part.*(disk.)$'\n' ]]; then
+       elif [ "$OSFLAVOUR" = "OSX" ] && [[ $(diskutil list) =~ CD_part[^/]*(disk.)$'\n' ]]; then
                CDROM=/dev/${BASH_REMATCH[1]}
        elif [ -e /dev/cdrom ]; then
                CDROM=/dev/cdrom
@@ -3499,7 +3668,7 @@ case "$EXTRAVERBOSE" in
 esac
 
 # Parse command line options
-while getopts 1a:bc:C:d:Defghj:klLmMnNo:pPr:s:S:t:T:UvVxX:w:W:z opt ; do
+while getopts 1a:bc:C:d:DefgGhj:klLmMnNo:pPr:s:S:t:T:UvVxX:w:W:z opt ; do
        case "$opt" in
                1) ONETRACK=y ;;
                a) ACTIONS="$OPTARG" ;;
@@ -3514,6 +3683,7 @@ while getopts 1a:bc:C:d:Defghj:klLmMnNo:pPr:s:S:t:T:UvVxX:w:W:z opt ; do
                E) ENCODING="$OPTARG" ;;
                f) FORCE=y ;;
                g) NOGAP=y ;;
+               G) GETALBUMART=y ;;
                i) INLINETAG=y ;;
                j) MAXPROCS="$OPTARG" ;;
                k) KEEPWAVS=y ;;
@@ -3609,7 +3779,7 @@ if [ "$ONETRACK" = "y" ]; then
        # FIXME # remove check as soon as we find out about the other readers
        case "$CDROMREADERSYNTAX" in
                flac) ;;
-               cdparanoia) ;;
+               cdparanoia | libcdio) ;;
                cdda2wav | icedax) ;;
                pird) ;;
                *) log error "$CDROMREADERSYNTAX does not support ONETRACK mode"
@@ -3682,6 +3852,7 @@ do
                replaygain) DOCDDB=y; DOREAD=y; DOENCODE=y; DOTAG=y; DOMOVE=y; DOREPLAYGAIN=y;;
                playlist) DOCDDB=y; DOPLAYLIST=y;;
                clean) DOCLEAN=y;;
+               getalbumart) GETALBUMART=y;;
        esac
 done
 
@@ -3798,6 +3969,12 @@ case "$CDROMREADERSYNTAX" in
                CDROMREADER="$CDPARANOIA"
                CDROMREADEROPTS="$CDPARANOIAOPTS"
                ;;
+       libcdio)
+               # GNU's libcdio package will use cd-paranoia but I believe will be happy with
+               # the standard cdparanoia options. If I am wrong this will need to be fixed :).
+               CDROMREADER="$CD_PARANOIA"
+               CDROMREADEROPTS="$CDPARANOIAOPTS"
+               ;;
        cdda2wav | icedax)
                CDROMREADER="$CDDA2WAV"
                CDROMREADEROPTS="$CDDA2WAVOPTS"
@@ -3901,11 +4078,11 @@ do
                        [ "$DOTAG" = "y" ] && NEEDAPETAG=y
                        ;;
                aac)
-                   [ "$AACENCODERSYNTAX" = "default" ] && AACENCODERSYNTAX=faac
+                       [ "$AACENCODERSYNTAX" = "default" ] && AACENCODERSYNTAX=faac
                        [ "$DOTAG" = "n" ]
                # Neither Faac nor AtomicParsley can tag the .aac files which are used for faac
                # compiled without libmp4v2... Andrew.
-                       ;;      
+                       ;;
                m4a)
                        [ "$AACENCODERSYNTAX" = "default" ] && AACENCODERSYNTAX=faac
                        [ "$DOTAG" = "y" ] && CHECKFAACBUILD=y
@@ -4015,7 +4192,7 @@ esac
 case "$AACENCODERSYNTAX" in
                # Some elaborate 'if' work to keep backward compatability for those
                # who don't realise that AACENCOPTS does not work with abcde from
-               # version 2.6.1 onwards.
+               # version 2.7 onwards.
        faac)
                if [ -z "$FAACENCOPTS" ] && [ -n "$AACENCOPTS" ]; then
                        AACENCODEROPTS="${AACENCODEROPTSCLI:-$AACENCOPTS}"
@@ -4148,7 +4325,7 @@ fi
 
 if [ X"$CDSPEEDVALUE" != "X" ] && [ "$DOREAD" = "y" ]; then
        case "$CDROMREADERSYNTAX" in
-               cdparanoia|debug) CDROMREADEROPTS="$CDPARANOIAOPTS -S $CDSPEEDVALUE" ;;
+               cdparanoia|libcdio|debug) CDROMREADEROPTS="$CDPARANOIAOPTS -S $CDSPEEDVALUE" ;;
                pird) CDROMREADEROPTS="$PIRDOPTS -s $CDSPEEDVALUE" ;;
                ### FIXME ### translate "cue2discid" from python to bash
                flac) NEEDMETAFLAC=y ; NEEDCUE2DISCID=y ; CDSPEEDVALUE="" ;;
@@ -4156,10 +4333,16 @@ if [ X"$CDSPEEDVALUE" != "X" ] && [ "$DOREAD" = "y" ]; then
        esac
 fi
 
+if [ "$GETALBUMART" = "y" ]; then
+       NEEDHTTPGET="y"
+       NEEDGLYRC="y"
+fi
+
 ###USEPIPESSUPPORT###
 
 # Rippers with USEPIPE support
 PIPERIPPER_cdparanoia="-"
+PIPERIPPER_libcdio="-"
 # Note that by default in abcde cdda2wav redirects to icedax.        
 PIPERIPPER_cdda2wav="-"
 PIPERIPPER_debug="-"
@@ -4169,7 +4352,7 @@ PIPERIPPER_pird="-"
 # Encoders with USEPIPE support
 PIPE_mp3enc="-sti"
 PIPE_lame="-"
-PIPE_bladeenc="-"
+PIPE_bladeenc="stdin"
 PIPE_oggenc="-"
 PIPE_opusenc="-"
 PIPE_flac="-"
@@ -4211,16 +4394,14 @@ if [ "$USEPIPES" = "y" ]; then
        if [ "$( eval echo "\$$PIPERIPPERSVARCHECK" )" = "$" ] || \
           [ "$( eval echo "\$$PIPERIPPERSVARCHECK" )" = "" ] ; then
                log error "no support for pipes with given ripper"
-               log error "read the USEPIPES file from the source tarball to get help."
-               log error "On a Debian system, it is under /usr/share/doc/abcde/USEPIPES.gz"
+               log error "read the FAQ file from the source tarball to get help."
                exit 1;
        fi
        decho "PIPEENCODERSVARCHECK: $( eval echo "\$$PIPEENCODERSVARCHECK" )"
        if [ "$( eval echo "\$$PIPEENCODERSVARCHECK" )" = "$" ] || \
           [ "$( eval echo "\$$PIPEENCODERSVARCHECK" )" = "" ] ; then
                log error "no support for pipes with given encoder"
-               log error "read the USEPIPES file from the source tarball to help"
-               log error "on a Debian system, read /usr/share/doc/abcde/USEPIPES.gz"
+               log error "read the FAQ file from the source tarball to get help"
                exit 1;
        fi
 fi
@@ -4236,7 +4417,8 @@ for X in $CDROMREADER $CDDISCID ${NEEDTAGGER+$TAGGER} $MP3ENCODER \
        ${NEEDVORBISGAIN+$VORBISGAIN} ${NEEDMP3GAIN+$MP3GAIN} \
        ${NEEDMPCGAIN+$MPCGAIN} ${NEEDCUEREADER+$CUEREADER} \
        ${NEEDWVGAIN+WVGAIN} ${NEEDAPETAG+$APETAG} \
-       ${NEEDCUE2DISCID+$CUE2DISCID} ${NEEDNEROAACTAG+$NEROAACTAG}
+       ${NEEDCUE2DISCID+$CUE2DISCID} ${NEEDNEROAACTAG+$NEROAACTAG} \
+       ${NEEDGLYRC+$GLYRC}
 do
        checkexec "$X"
 done
@@ -4245,16 +4427,17 @@ done
 # for the mp4 container and no tagging capability. (AtomicParsley also cannot tag
 # these files). The resulting files are actually ADTS streams which belong in an 
 # aac container and with some version sniffing this is all cleaned up below. If
-# faac is compiled with libmp4v2 inline tagging occurs with faac.         Andrew. 
+# faac is compiled with libmp4v2 inline tagging occurs with faac.
 
+# FIXME: Should this be in this location? (Better incorporated into the checks above.)
 if [ "$CHECKFAACBUILD" = "y" ] && [ "$AACENCODERSYNTAX" = "faac" ] ; then
-  if faac --help 2>&1 | grep -q -F 'MP4 support unavailable.'; then 
-   echo "WARNING: Your copy of Faac does not have mp4 support"
-   echo "WARNING: Encoding untagged files to aac..."
-   OUTPUTTYPE=aac
-  else
-   echo "Using Faac to Tag AAC Tracks..."
-  fi
+       if faac --help 2>&1 | grep -q -F 'MP4 support unavailable.'; then 
+               echo "WARNING: Your copy of Faac does not have mp4 support"
+               echo "WARNING: Encoding untagged files to aac..."
+               OUTPUTTYPE=aac
+       else
+               echo "Using Faac to Tag AAC Tracks..."
+       fi
 fi
 
 # And last but not least, check if we can diff between files. We do not abort,
@@ -4338,7 +4521,7 @@ fi
 
 if [ X"$CDSPEEDVALUE" != "X" ]; then
        case "$CDROMREADERSYNTAX" in
-               cdparanoia|debug) ;;
+               cdparanoia|libcdio|debug) ;;
                pird) ;;
                flac) ;;
                *) do_cdspeed ;;
@@ -4375,6 +4558,10 @@ if [ ! "$ONETRACK" = "y" ]; then
        fi
 fi
 
+if [ "$GETALBUMART" = "y" ]; then
+       do_getalbumart
+fi
+
 # For the lowdisk option, only one program is running at once so the encoder
 # can be unsilenced right away.
 if [ "$LOWDISK" = "y" ] || [ "$ONETRACK" = "y" ]; then