Make sure that ABCDETEMPDIR is quoted so we can deal with spaces in
[clinton/abcde.git] / abcde
diff --git a/abcde b/abcde
index e1123a5..7a6caed 100755 (executable)
--- a/abcde
+++ b/abcde
@@ -13,7 +13,7 @@
 #
 # $Id$
 
-VERSION='2.5.1-UNRELEASED'
+VERSION='2.5.3-UNRELEASED'
 
 usage ()
 {
@@ -43,11 +43,11 @@ echo "-j <#> Number of encoder processes to run at once (localhost)"
 echo "-k     Keep the wav tracks for later use"
 echo "-l     Use low disk space algorithm"
 echo "-L     Use local CDDB storage directory"
-echo "-n     No lookup. Don't query CDDB, just create and use template"
-echo "-N     Noninteractive. Never prompt for anything"
 echo "-m     Modify playlist to include CRLF endings, to comply with some players"
 #echo "       WARNING: Deprecated. Use \"cue\" action"
 #echo "-M     Create a CUE file"
+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). Defaults to vorbis"
 echo "-p     Pad track numbers with 0's (if less than 10 tracks)"
@@ -56,7 +56,6 @@ echo "-q <level>"
 echo "       Set quality level (high,medium,low)"
 echo "-r <host1[,host2]...>"
 echo "       Also encode on these remote hosts"
-echo "-R     Use local CDDB in recursive mode"
 echo "-s <field>"
 echo "       Show fields from the CDDB info (year,genre)"
 echo "-S <#> Set the CD speed"
@@ -118,7 +117,7 @@ f_seq_line ()
                done
                echo
        else
-               log error "syntax error while processing track numbers"
+               log error "syntax error while processing track numbers ($i)"
                exit 1
        fi
 }
@@ -246,6 +245,7 @@ page ()
 # Runs a command, silently if necessary, and updates the status file
 run_command ()
 {
+       vvecho "$@"
        BLURB="$1"
        shift
        # See if this is supposed to be silent
@@ -937,7 +937,7 @@ do_tag ()
                        fi
 
                        #It has to be command file opts for AtomicParsley
-                       run_command tagtrack-$OUTPUT-$1 nice $ENCNICE $ATOMICPARSLEY $ABCDETEMPDIR/track$1.m4a --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --tracknum ${TRACKNUM:-$1} --year "$CDYEAR" --genre "$CDGENRE" --compilation $VARIOUSBOOL --comment "$COMMENTOUTPUT" --output $ATOMICTEMPFILE
+                       run_command tagtrack-$OUTPUT-$1 nice $ENCNICE $ATOMICPARSLEY "$ABCDETEMPDIR/track$1.m4a" --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --tracknum ${TRACKNUM:-$1} --year "$CDYEAR" --genre "$CDGENRE" --compilation $VARIOUSBOOL --comment "$COMMENTOUTPUT" --output $ATOMICTEMPFILE
                        if [ -f $ATOMICTEMPFILE ]; then
                                mv "$ATOMICTEMPFILE" "$ABCDETEMPDIR/track$1.m4a"
                        fi
@@ -1026,7 +1026,7 @@ do_encode ()
                                TEMPARG="PIPE_$MPPENCODER"
                                ;;
                        m4a)
-                               TEMPARG="PIPE_$MPPENCODER"
+                               TEMPARG="PIPE_$AACENCODERSYNTAX"
                                ;;
                esac
                IN="$( eval echo "\$$TEMPARG" )"
@@ -1136,7 +1136,7 @@ do_encode ()
                                fi
                                # Tag the file at encode time, as it can't be done after encoding.
                                if [ "$DOTAG" = "y" ]; then
-                                       RUN_COMMAND nice $EFFECTIVE_NICE $SPEEXENCODER $SPEEXENCODEROPTS --author "$TRACKARTIST" --title "$TRACKNAME" ${COMMENT:+--comment "$COMMENT"} "$IN" "$OUT"
+                                       $RUN_COMMAND nice $EFFECTIVE_NICE $SPEEXENCODER $SPEEXENCODEROPTS --author "$TRACKARTIST" --title "$TRACKNAME" ${COMMENT:+--comment "$COMMENT"} "$IN" "$OUT"
                                else
                                        $RUN_COMMAND nice $EFFECTIVE_NICE $SPEEXENCODER $SPEEXENCODEROPTS "$IN" "$OUT"
                                fi
@@ -1555,7 +1555,7 @@ do_playlist ()
                ## this will convert the playlist to have CRLF line-endings, if specified
                ## (some hardware players insist on CRLF endings)
                if [ "$DOSPLAYLIST" = "y" ]; then
-                       awk '{substr("\r",""); printf "%s\r\n", $0}' "$OUTPUTDIR/$PLAYLISTFILE" > "$ABCDETEMPDIR/PLAYLISTFILE.tmp"
+                       awk '{sub("\r$",""); printf "%s\r\n", $0}' "$OUTPUTDIR/$PLAYLISTFILE" > "$ABCDETEMPDIR/PLAYLISTFILE.tmp"
 #                      mv -f "$ABCDETEMPDIR/PLAYLISTFILE.tmp" "$OUTPUTDIR/$PLAYLISTFILE"
                        cat "$ABCDETEMPDIR/PLAYLISTFILE.tmp" | sed 's/\//\\/' > "$OUTPUTDIR/$PLAYLISTFILE"
                fi
@@ -1741,7 +1741,7 @@ do_discid ()
                vecho -n "Getting CD track info... "
                # In OSX, unmount the disc before a query
                if [ "$OSFLAVOUR" = "OSX" ]; then
-                       disktool -u ${CDROM#/dev/}
+                       diskutil unmount ${CDROM#/dev/}
                fi
                case "$CDROMREADERSYNTAX" in
                        flac)
@@ -1813,7 +1813,7 @@ do_discid ()
                fi
                # In OSX, remount the disc again
                if [ "$OSFLAVOUR" = "OSX" ]; then
-                       disktool -m ${CDROM#/dev/}
+                       diskutil mount ${CDROM#/dev/}
                fi
                WEHAVEACD=y
                DISCID=$(echo $TRACKINFO | cut -f1 -d' ')
@@ -2228,6 +2228,58 @@ do_localcddb ()
        fi
 }
 
+# do_cdtext
+# Try to read CD-Text from the drive using icedax / cdda2wav
+do_cdtext ()
+{
+       if new_checkexec icedax; then
+               CDTEXT_READER=icedax
+       elif new_checkexec cdda2wav; then
+               CDTEXT_READER=cdda2wav
+    else
+               # Didn't find either, bail
+               return 0
+       fi
+
+       if [ "$CDROMID" = "" ]; then
+               CDDA2WAVCDROM="$CDROM"
+       else
+               CDDA2WAVCDROM="$CDROMID"
+       fi
+
+       # Do we have CD-Text on the disc (and can the drive read it?)
+       ${CDTEXT_READER} -J -N -D ${CDDA2WAVCDROM} > "$ABCDETEMPDIR/cd-text" 2>&1
+       grep -q '^CD-Text: detected' "$ABCDETEMPDIR/cd-text"
+       ERRORCODE=$?
+       if [ $ERRORCODE -ne 0 ]; then
+               # No CD-Text found, bail
+               return 0
+       fi
+
+       rm -f "$ABCDETEMPDIR/cddbchoices"
+       CDDBCHOICES=1
+       # Make an empty template
+       $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.1"
+       echo -n "Retrieved 1 CD-Text match..." >> "$ABCDETEMPDIR/cddbchoices"
+       echo "done." >> "$ABCDETEMPDIR/cddbchoices"
+       echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
+       echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
+       ATITLE=$(grep -e '^Album title:' "${ABCDETEMPDIR}/cd-text" | cut -c14- )
+       echo "200 none ${ATITLE}" >> "$ABCDETEMPDIR/cddbquery"
+       # List out disc title/author and contents
+       echo ---- ${ATITLE} ---- >> "$ABCDETEMPDIR/cddbchoices"
+       for TRACK in $(f_seq_row 1 $TRACKS)
+       do
+               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 -i "s~^TTITLE${TRACKM1}=.*~TTITLE${TRACKM1}=${TITLE}~" "$ABCDETEMPDIR/cddbread.1"
+       done
+       sed -i "s~^DTITLE=.*~DTITLE=${ATITLE}~" "$ABCDETEMPDIR/cddbread.1"
+       echo >> "$ABCDETEMPDIR/cddbchoices"
+       echo "cdtext-readcomplete" >> "$ABCDETEMPDIR/status"
+}
+
 # do_musicbrainz
 # Work with the musicbrainz WS API, then transform the results here so
 # they look (very) like the results from CDDB. Maybe not the best way
@@ -2242,27 +2294,11 @@ do_musicbrainz ()
                rm -f "$ABCDETEMPDIR/cddbchoices"
                CDDBCHOICES=1 # Overridden by multiple matches
                MBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
-               ${MUSICBRAINZ} --command data --discid "$MBDISCID" --workdir $ABCDETEMPDIR
+               ${MUSICBRAINZ} --command data --discid "$MBDISCID" --workdir "$ABCDETEMPDIR"
 
                # The helper script will write disc matches out to
                # cddbread.*. Count how many we have
-               NUM_RESPONSES=$(echo ${ABCDETEMPDIR}/cddbread.* | wc -w)
-               if [ "$NUM_RESPONSES" -eq 1 ] ; then
-                       # One exact match
-                       echo -n "Retrieved 1 Musicbrainz match..." >> "$ABCDETEMPDIR/cddbchoices"
-                       echo "done." >> "$ABCDETEMPDIR/cddbchoices"
-                       echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
-                       echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
-                       ATITLE=$(grep -e '^DTITLE=' ${ABCDETEMPDIR}/cddbread.1 | cut -c8- )
-                       echo "200 none ${ATITLE}" >> "$ABCDETEMPDIR/cddbquery"
-                       # List out disc title/author and contents
-                       echo ---- ${ATITLE} ---- >> "$ABCDETEMPDIR/cddbchoices"
-                       for TRACK in $(f_seq_row 1 $TRACKS)
-                       do
-                               echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.1" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
-                       done
-                       echo >> "$ABCDETEMPDIR/cddbchoices"
-               elif [ "$NUM_RESPONSES" -eq 0 ] ; then
+               if [ ! -f "${ABCDETEMPDIR}/cddbread.1" ] ; then
                        # No matches. Use the normal cddb template for the user to
                        # fill in
                        echo "No Musicbrainz match." >> "$ABCDETEMPDIR/cddbchoices"
@@ -2279,23 +2315,42 @@ do_musicbrainz ()
                        echo cddb-choice=0 >> "$ABCDETEMPDIR/status"
                        echo 503 > "$ABCDETEMPDIR/cddbquery"
                else
-                       echo "210 Found exact matches, list follows (until terminating .)" > "$ABCDETEMPDIR/cddbquery"
-                       echo "Multiple Musicbrainz matches:" >> "$ABCDETEMPDIR/cddbchoices"
-                       for file in $ABCDETEMPDIR/cddbread.*
-                       do
-                               X=$(echo $file | sed 's/^.*cddbread\.//g')
-                               echo cddb-read-$X-complete >> "$ABCDETEMPDIR/status"
-                               ATITLE=$(grep -e '^DTITLE=' ${ABCDETEMPDIR}/cddbread.$X | cut -c8- )
-                               echo "none ${ATITLE}" >> "$ABCDETEMPDIR/cddbquery"
+                       # We have some matches
+                       NUM_RESPONSES=$(echo "${ABCDETEMPDIR}"/cddbread.* | wc -w)
+                       if [ "$NUM_RESPONSES" -eq 1 ] ; then
+                               # One exact match
+                               echo -n "Retrieved 1 Musicbrainz match..." >> "$ABCDETEMPDIR/cddbchoices"
+                               echo "done." >> "$ABCDETEMPDIR/cddbchoices"
+                               echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
+                               echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
+                               ATITLE=$(grep -e '^DTITLE=' "${ABCDETEMPDIR}/cddbread.1" | cut -c8- )
+                               echo "200 none ${ATITLE}" >> "$ABCDETEMPDIR/cddbquery"
                                # List out disc title/author and contents
-                               echo "#$X: ---- ${ATITLE} ----" >> "$ABCDETEMPDIR/cddbchoices"
+                               echo ---- ${ATITLE} ---- >> "$ABCDETEMPDIR/cddbchoices"
                                for TRACK in $(f_seq_row 1 $TRACKS)
                                do
-                                       echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.$X" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
+                                       echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.1" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
                                done
                                echo >> "$ABCDETEMPDIR/cddbchoices"
-                       done
-                       echo "." >> "$ABCDETEMPDIR/cddbquery"
+                       else
+                               echo "210 Found exact matches, list follows (until terminating .)" > "$ABCDETEMPDIR/cddbquery"
+                               echo "Multiple Musicbrainz matches:" >> "$ABCDETEMPDIR/cddbchoices"
+                               for file in "$ABCDETEMPDIR"/cddbread.*
+                               do
+                                       X=$(echo $file | sed 's/^.*cddbread\.//g')
+                                       echo cddb-read-$X-complete >> "$ABCDETEMPDIR/status"
+                                       ATITLE=$(grep -e '^DTITLE=' "${ABCDETEMPDIR}"/cddbread.$X | cut -c8- )
+                                       echo "none ${ATITLE}" >> "$ABCDETEMPDIR/cddbquery"
+                                       # List out disc title/author and contents
+                                       echo "#$X: ---- ${ATITLE} ----" >> "$ABCDETEMPDIR/cddbchoices"
+                                       for TRACK in $(f_seq_row 1 $TRACKS)
+                                       do
+                                               echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.$X" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
+                                       done
+                                       echo >> "$ABCDETEMPDIR/cddbchoices"
+                               done
+                               echo "." >> "$ABCDETEMPDIR/cddbquery"
+                       fi
                fi
                echo "musicbrainz-readcomplete" >> "$ABCDETEMPDIR/status"
        fi
@@ -2887,7 +2942,7 @@ do_cdread ()
                cdda2wav | icedax)
                        if [ "$OSFLAVOUR" = "OSX" ] ; then
                                # Hei, we have to unmount the device before running anything like cdda2wav/icedax in OSX
-                               disktool -u ${CDROM#/dev/} 0
+                               diskutil unmount ${CDROM#/dev/} 0
                                # Also, in OSX the cdrom device for cdda2wav/icedax changes...
                                CDDA2WAVCDROM="IODVDServices"
                        elif [ "$OSFLAVOUR" = "FBSD" ] ; then
@@ -2974,10 +3029,23 @@ do_cdspeed ()
 
 # vecho [message]
 #
-# vecho outputs a message if EXTRAVERBOSE is selected
+# vecho outputs a message if EXTRAVERBOSE is 1 or more
 vecho ()
 {
-if [ x"$EXTRAVERBOSE" != "x" ]; then
+if [ x"$EXTRAVERBOSE" != "x" ] && [ $EXTRAVERBOSE -gt 0 ] ; then
+       case $1 in
+               warning) shift ; log warning "$@" ;;
+               *) echo "$@" ;;
+       esac
+fi
+}
+
+# vvecho [message]
+#
+# vvecho outputs a message if EXTRAVERBOSE is 2 or more
+vvecho ()
+{
+if [ x"$EXTRAVERBOSE" != "x" ] && [ $EXTRAVERBOSE -gt 1 ] ; then
        case $1 in
                warning) shift ; log warning "$@" ;;
                *) echo "$@" ;;
@@ -3130,6 +3198,7 @@ EYED3=eyeD3
 VORBISCOMMENT=vorbiscomment
 METAFLAC=metaflac
 AACTAG=faac
+ATOMICPARSLEY=AtomicParsley
 
 CDPARANOIA=cdparanoia
 CDDA2WAV=icedax
@@ -3224,8 +3293,8 @@ if [ X$(uname) = "XFreeBSD" ] ; then
 elif [ X$(uname) = "XDarwin" ] ; then
        HTTPGET=curl
        OSFLAVOUR=OSX
-       # We should have disktool in OSX, but let's be sure...
-       NEEDDISKTOOL=y
+       # We should have diskutil in OSX, but let's be sure...
+       NEEDDISKUTIL=y
        CDROMREADERSYNTAX=cddafs
 elif [ X$(uname) = "XOpenBSD" ] ; then
        HTTPGET=wget
@@ -3293,15 +3362,29 @@ if [ "$CDROM" = "" ] ; then
                CDROM=/dev/cdroms/cdrom0
        elif [ -e /dev/cdrom ]; then
                CDROM=/dev/cdrom
+       elif [ -e /dev/sr0 ]; then
+               CDROM=/dev/sr0
        elif [ -e /dev/cd0c ]; then
                CDROM=/dev/cd0c
        elif [ -e /dev/acd0c ]; then
                CDROM=/dev/acd0c
        elif [ -e /dev/disk1 ]; then
                CDROM=/dev/disk1
+       elif [ "$OSFLAVOUR" = "OSX" ] && [[ $(diskutil list) =~ CD_part.*(disk.)$'\n' ]]; then
+               CDROM=/dev/${BASH_REMATCH[1]}
        fi
 fi
 
+# We used to use EXTRAVERBOSE=y to turn on more debug output. Now
+# that's changed to a number to allow for more control. If
+# EXTRAVERBOSE=y, set it to 1 for backwards compatibility.
+if [ "$EXTRAVERBOSE" = "y" ]; then
+       EXTRAVERBOSE=1
+fi
+if [ "$EXTRAVERBOSE" = "n" ]; then
+       EXTRAVERBOSE=0
+fi
+
 # Parse command line options
 #while getopts 1a:bc:C:d:Dehj:klLmMnNo:pPq:r:Rs:S:t:T:vVxw:W: opt ; do
 while getopts 1a:bBc:C:d:Defghj:klLmMnNo:pPq:r:s:S:t:T:UvVxX:w:W:z opt ; do
@@ -3346,7 +3429,7 @@ while getopts 1a:bBc:C:d:Defghj:klLmMnNo:pPq:r:s:S:t:T:UvVxX:w:W:z opt ; do
                   echo "abcde -h for extra help"
                   exit
                   ;;
-               V) EXTRAVERBOSE="y" ;;
+               V) EXTRAVERBOSE=$(($EXTRAVERBOSE + 1)) ;;
                x) EJECTCD="y" ;;
                X) CUE2DISCID="$OPTARG" ;;
                w) COMMENT="$OPTARG" ;;
@@ -3439,7 +3522,8 @@ else
                # Range parsing code courtesy of Vincent Ho
                # Cleaned up to use shell built-ins by Charles Steinkuehler
                if [ "${1#*[^0-9-]}" != "$1" ]; then
-                       log error "syntax error while processing track numbers"
+                       log error "syntax error while processing track numbers ($1)"
+                       exit 1
                else
                        RSTART=${1%%-*}
                        REND=${1##*-}
@@ -3907,12 +3991,12 @@ fi
 # Make sure a buncha things exist
 for X in $CDROMREADER $CDDISCID ${NEEDTAGGER+$TAGGER} $MP3ENCODER \
        $OGGENCODER $FLACENCODER $SPEEXENCODER $MPPENCODER \
-       $AACENCODER \
+       $AACENCODER $CDDBTOOL \
        ${NEEDATOMICPARSLEY+$ATOMICPARSLEY} \
        ${NEEDHTTPGET+$HTTPGET} ${NEEDDISTMP3+$DISTMP3} \
        ${NEEDCOMMENTER+$VORBISCOMMENT} ${NEEDMETAFLAC+$METAFLAC} \
        ${NEEDNORMALIZER+$NORMALIZER} ${NEEDEJECT+$EJECT} \
-       ${NEEDDISKTOOL+disktool} ${NEEDCDSPEED+$CDSPEED} \
+       ${NEEDDISKUTIL+diskutil} ${NEEDCDSPEED+$CDSPEED} \
        ${NEEDVORBISGAIN+$VORBISGAIN} ${NEEDMP3GAIN+$MP3GAIN} \
        ${NEEDMPPGAIN+$MPPGAIN} ${NEEDCUEREADER+$CUEREADER} \
        ${NEEDCUE2DISCID+$CUE2DISCID}
@@ -3993,6 +4077,13 @@ if [ "$DOCDDB" = "y" ]; then
                                        ;;
                        esac
                fi
+               CHOICE=$(checkstatus cddb-choice)
+               if [ $CHOICE = 0 ] ; then
+                       # We don't have any information at all; try to fall back
+                       # to CD-Text for basic information
+                       vecho "No CDDB information found, trying cdtext from the CD"
+                       do_cdtext
+               fi
        fi
        do_cddbedit
 
@@ -4133,7 +4224,7 @@ if [ "$EJECTCD" = "y" ] && [ -x $(which $EJECT) ]; then
                cd="$(echo $CDROM | sed -e 's=.*/==;s=[a-h]$==;')"
                $eject $EJECTOPTS $cd
        elif [ X"$(uname)" = X"Darwin" ] ; then
-               disktool -e ${CDROM#/dev/} 0
+               diskutil eject ${CDROM#/dev/} 0
        else
                $EJECT $EJECTOPTS "$CDROM"
        fi