+ if [ $(cat "$ABCDETEMPDIR/cddblocalchoices" | wc -l) -ge 24 ] && [ "$INTERACTIVE" = "y" ]; then
+ page "$ABCDETEMPDIR/cddblocalchoices"
+ else
+ # It's all going to fit in one page, cat it
+ cat "$ABCDETEMPDIR/cddblocalchoices" >&2
+ fi
+ CDDBLOCALCHOICES=$( echo "$CDDBLOCALRESULTS" | wc -l )
+ # Setting the choice to an impossible integer to avoid errors in the numeric comparisons
+ CDDBLOCALCHOICENUM=-1
+ if [ "$INTERACTIVE" = "y" ]; then
+ while [ $CDDBLOCALCHOICENUM -lt 0 ] || [ $CDDBLOCALCHOICENUM -gt $CDDBLOCALCHOICES ]; do
+ echo -n "Locally cached CDDB entries found. Which one would you like to use (0 for none)? [0-$CDDBLOCALCHOICES]: " >&2
+ read CDDBLOCALCHOICE
+ [ x"$CDDBLOCALCHOICE" = "x" ] && CDDBLOCALCHOICE="1"
+ # FIXME # Introduce diff's
+ if echo $CDDBLOCALCHOICE | grep -E "[[:space:]]*[[:digit:]]+,[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
+ diffentries cddblocalread "$CDDBLOCALCHOICES" "$CDDBLOCALCHOICE"
+ elif echo $CDDBLOCALCHOICE | grep -E "[[:space:]]*[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
+ # Make sure we get a valid choice
+ CDDBLOCALCHOICENUM=$(echo $CDDBLOCALCHOICE | xargs printf %d 2>/dev/null)
+ if [ $CDDBLOCALCHOICENUM -lt 0 ] || [ $CDDBLOCALCHOICENUM -gt $CDDBLOCALCHOICES ]; then
+ echo "Invalid selection. Please choose a number between 0 and $CDDBLOCALCHOICES." >&2
+ fi
+ fi
+ done
+ else
+ ### FIXME ###
+ #echo "Selected ..."
+ CDDBLOCALRESP=y
+ CDDBLOCALCHOICENUM=1
+ fi
+ if [ ! "$CDDBLOCALCHOICENUM" = "0" ]; then
+ #echo "Using local copy of CDDB data"
+ echo "# DO NOT ERASE THIS LINE! Added by abcde to imitate cddb output" > "$ABCDETEMPDIR/cddbread.1"
+ cat "$ABCDETEMPDIR/cddblocalread.$CDDBLOCALCHOICENUM" >> "$ABCDETEMPDIR/cddbread.1"
+ echo 999 > "$ABCDETEMPDIR/cddbquery" # Assuming 999 isn't used by CDDB
+ echo cddb-readcomplete >> "$ABCDETEMPDIR/status"
+ do_cddbparse "$ABCDETEMPDIR/cddbread.1" > "$ABCDETEMPDIR/cddbchoices"
+ echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
+ CDDBLOCALSTATUS="found"
+ else
+ #echo "Not using local copy of CDDB data"
+ CDDBLOCALSTATUS="notfound"
+ fi
+ ;;
+ single)
+ # List out disc title/author and contents
+ do_cddbparse "${CDDBLOCALFILE}"
+ #if [ "$CDROMREADERSYNTAX" = "flac" ] ; then
+ # echo -n "Embedded cuesheet entry found, use it [Y/n]? " >&2
+ #else
+ echo -n "Locally cached CDDB entry found, use it [Y/n]? " >&2
+ #fi
+ if [ "$INTERACTIVE" = "y" ]; then
+ read USELOCALRESP
+ while [ "$USELOCALRESP" != "y" ] && [ "$USELOCALRESP" != "n" ] && [ "$USELOCALRESP" != "" ] ; do
+ echo -n 'Invalid selection. Please answer "y" or "n": ' >&2
+ read USELOCALRESP
+ done
+ [ x"$USELOCALRESP" = "x" ] && USELOCALRESP="y"
+ else
+ echo "y" >&2
+ fi
+ if [ "$USELOCALRESP" = "y" ]; then
+ #echo "Using local copy of CDDB data"
+ echo "# DO NOT ERASE THIS LINE! Added by abcde to imitate cddb output" > "$ABCDETEMPDIR/cddbread.1"
+ cat "${CDDBLOCALFILE}" >> "$ABCDETEMPDIR/cddbread.1"
+ echo 999 > "$ABCDETEMPDIR/cddbquery" # Assuming 999 isn't used by CDDB
+ echo cddb-readcomplete >> "$ABCDETEMPDIR/status"
+ do_cddbparse "${CDDBLOCALFILE}" > "$ABCDETEMPDIR/cddbchoices"
+ echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
+ CDDBLOCALSTATUS="single"
+ else
+ #echo "Not using local copy of CDDB data"
+ CDDBLOCALSTATUS="notfound"
+ fi
+ ;;
+ none)
+ CDDBLOCALSTATUS="notfound"
+ ;;
+ esac
+ fi
+}
+
+# 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 ()
+{
+ 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
+ MBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
+ ${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
+ # 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"