Add support for Musicbrainz using a perl helper script.
[clinton/abcde.git] / abcde
diff --git a/abcde b/abcde
index 4f36556..d585c16 100755 (executable)
--- a/abcde
+++ b/abcde
@@ -1785,8 +1785,7 @@ do_discid ()
                        *)
                                case "$CDDBMETHOD" in
                                        cddb) TRACKINFO=$($CDDISCID "$CDROM") ;;
-                                       # FIXME # musicbrainz needs a cleanup
-                                       musicbrainz) TRACKINFO=$($MUSICBRAINZ -c "$CDROM" ) ;;
+                                       musicbrainz) TRACKINFO=$($MUSICBRAINZ --command id --device "$CDROM") ;;
                                esac
                                ;;
                esac
@@ -2218,15 +2217,76 @@ do_localcddb ()
        fi
 }
 
-do_musicbrainzstat ()
-{
-       :
-}
+# 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
+# to go, but it Works For Me (TM)
 
 do_musicbrainz ()
 {
-# Use MBE_TOCGetCDIndexId on a perl query
-       :
+       if checkstatus musicbrainz-readcomplete; then :; else
+               vecho "Obtaining Musicbrainz results..."
+               # If MB is to be used, interpret the query results and read all
+               # the available entries.
+               rm -f "$ABCDETEMPDIR/cddbchoices"
+               CDDBCHOICES=1 # Overridden by multiple matches
+               ${MUSICBRAINZ} --command data --device "$CDROM" --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
+                       # No matches. Use the normal cddb template for the user to
+                       # fill in
+                       echo "No Musicbrainz match." >> "$ABCDETEMPDIR/cddbchoices"
+                       $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.0"
+                       # List out disc title/author and contents of template
+                       echo ---- Unknown Artist / Unknown Album ---- >> "$ABCDETEMPDIR/cddbchoices"
+                       UNKNOWNDISK=y
+                       for TRACK in $(f_seq_row 1 $TRACKS)
+                       do
+                               echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.0" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
+                       done
+                       echo >> "$ABCDETEMPDIR/cddbchoices"
+                       echo cddb-read-0-complete >> "$ABCDETEMPDIR/status"
+                       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"
+                               # 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
+               echo "musicbrainz-readcomplete" >> "$ABCDETEMPDIR/status"
+       fi
 }
 
 # do_cddbstat
@@ -2972,7 +3032,7 @@ post_encode ()
 # Builtin defaults
 
 # CDDB
-# Defaults to FreeDB, but a python musicbrainz can be used
+# Defaults to FreeDB, but musicbrainz can be used too, via the abcde-musicbrainz-tool script
 CDDBMETHOD=cddb
 CDDBURL="http://freedb.freedb.org/~cddb/cddb.cgi"
 CDDBSUBMIT=freedb-submit@freedb.org
@@ -3065,7 +3125,7 @@ DAGRAB=dagrab
 CDDAFS=cp
 CDDISCID=cd-discid
 CDDBTOOL=cddb-tool
-MUSICBRAINZ=musicbrainz-get-tracks
+MUSICBRAINZ=abcde-musicbrainz-tool
 EJECT=eject
 MD5SUM=md5sum
 DISTMP3=distmp3
@@ -3897,14 +3957,8 @@ if [ "$DOREAD" = "y" ]; then
        vecho "done."
 fi
 
-case "$CDDBMETHOD" in
-       cddb)
-               do_discid # Get ABCDETEMPDIR created and status file initialized
-               ;;
-       musicbrainz)
-               do_musicbrainz id
-               ;;
-esac
+# Get ABCDETEMPDIR created and status file initialized
+do_discid
 
 if [ "$DOCDDB" = "y" ]; then
        # start with a sane default: