Cleaned up changelog indentation
[clinton/abcde.git] / abcde
diff --git a/abcde b/abcde
index 4f36556..258b728 100755 (executable)
--- a/abcde
+++ b/abcde
@@ -13,7 +13,7 @@
 #
 # $Id$
 
-VERSION='2.4.2'
+VERSION='2.5.1-UNRELEASED'
 
 usage ()
 {
@@ -156,7 +156,7 @@ checkstatus ()
 {
        # Take the last line in the status file if there's multiple matches
        PATTERN="^$1(=.*)?$"
-       BLURB=$(egrep $PATTERN "$ABCDETEMPDIR/status" | tail -n 1)
+       BLURB=$(grep -E $PATTERN "$ABCDETEMPDIR/status" | tail -n 1)
 
        if [ -z "$BLURB" ]; then
                # No matches found
@@ -183,7 +183,7 @@ checkwarnings ()
        fi
        # Take the last line in the status file if there's multiple matches
        PATTERN="^$1(:.*)?$"
-       BLURB="$(egrep $PATTERN "$ABCDETEMPDIR/warnings" | tail -n 1)"
+       BLURB="$(grep -E $PATTERN "$ABCDETEMPDIR/warnings" | tail -n 1)"
 
        if [ -z "$BLURB" ]; then
                # negative, we did not have a negative...
@@ -206,7 +206,7 @@ checkerrors ()
        fi
        # Take the last line in the status file if there's multiple matches
        PATTERN="^$1(:.*)?$"
-       BLURB="$(egrep $PATTERN "$ABCDETEMPDIR/errors" | tail -n 1)"
+       BLURB="$(grep -E $PATTERN "$ABCDETEMPDIR/errors" | tail -n 1)"
 
        if [ -z "$BLURB" ]; then
                # negative, we did not have a negative...
@@ -489,8 +489,18 @@ makeids ()
        PREGAP=$(($(echo $OFFSETS | cut -f1 -d' ')))
        TOTALTIME=$(( (($LEADOUT + $LEADIN + $PREGAP) / $CDFRAMES) - (($LEADIN + $PREGAP) / $CDFRAMES)))
 
-       printf -v HEXSUM "%08lx" $(( ($CDDBCKSUM % 0xff) << 24 | $TOTALTIME << 8 | $TRACKS))
-       TRACKINFO="${HEXSUM} $((TRACKS)) ${COOKEDOFFSETS} $((($LEADOUT + $LEADIN + $IDMAGICNUM) / $CDFRAMES))"
+       case "$CDDBMETHOD" in
+               cddb)
+                       printf -v DISCID "%08lx" $(( ($CDDBCKSUM % 0xff) << 24 | $TOTALTIME << 8 | $TRACKS))
+                       ;;
+               musicbrainz)
+                       # FIXME: don't assume the first track is 1
+                       echo "dasd: 1 $TRACKS $LEADIN $LEADOUT $OFFSETS "
+                       DISCID=$($MUSICBRAINZ --command calcid --discinfo 1 $TRACKS $LEADIN $LEADOUT $OFFSETS)
+                       ;;
+       esac
+
+       TRACKINFO="${DISCID} $((TRACKS)) ${COOKEDOFFSETS} $((($LEADOUT + $LEADIN + $IDMAGICNUM) / $CDFRAMES))"
 }
 
 do_replaygain()
@@ -786,7 +796,9 @@ do_tag ()
        if [ -z "$COMMENTOUTPUT" ]; then
                COMMENTOUTPUT="$(getcddbinfo TRACK-INFO)"
        fi
-       CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
+       if [ "$CDDBMETHOD" = "cddb" ]; then
+               CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
+       fi
        run_command '' echo "Tagging track $1 of $TRACKS: $TRACKNAME..."
        # If we want to start the tracks with a given number, we need to modify the
        # TRACKNUM value before evaluation
@@ -1743,7 +1755,7 @@ do_discid ()
                                                        #vecho "Using builtin cue2discid implementation..."
                                                        CUESHEET="$(metaflac $METAFLACOPTS --export-cuesheet-to=- "$CDROM")"
 
-                                                       #TRACKS=$(echo $CUESHEET | egrep "TRACK \+[[:digit:]]\+ \+AUDIO" |wc -l)
+                                                       #TRACKS=$(echo $CUESHEET | grep -E "TRACK \+[[:digit:]]\+ \+AUDIO" |wc -l)
                                                        #TRACKS=0
                                                        OFFSETTIMES=( $(echo "$CUESHEET" | sed -n -e's/\ *INDEX 01\ \+//p' ) )
                                                        TRACKS=${#OFFSETTIMES[@]}
@@ -1775,18 +1787,17 @@ do_discid ()
 #                                      log warning "something went wrong while querying the CD... Maybe a DATA CD?"
 #                              fi
 #
-#                              TRACKS="$(echo "$CDPARANOIAOUTPUT" | egrep '^[[:space:]]+[[:digit:]]' | tail -n 1 | get_first | tr -d "." | tr '\n' ' ')"
+#                              TRACKS="$(echo "$CDPARANOIAOUTPUT" | grep -E '^[[:space:]]+[[:digit:]]' | tail -n 1 | get_first | tr -d "." | tr '\n' ' ')"
 #                              CDPARANOIAAUDIOTRACKS="$TRACKS"
 #
-#                              LEADOUT="$(echo "$CDPARANOIAOUTPUT" | egrep -o '^TOTAL[[:space:]]+([[:digit:]]+)' | get_last)"
+#                              LEADOUT="$(echo "$CDPARANOIAOUTPUT" | grep -Eo '^TOTAL[[:space:]]+([[:digit:]]+)' | get_last)"
 #                              OFFSETS="$(echo "$CDPARANOIAOUTPUT" | sed -n -e's/^ .* \([0-9]\+\) \[.*/\1/p')"
 #                              makeids
 #                              ;;
                        *)
                                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
@@ -1835,7 +1846,7 @@ do_discid ()
                                                if [ ! "$RET" = "0" ];then
                                                        log warning "something went wrong while querying the CD... Maybe a DATA CD?"
                                                fi
-                                               TRACKS="$(echo "$CDPARANOIAOUTPUT" | egrep '^[[:space:]]+[[:digit:]]' | tail -n 1 | get_first | tr -d "." | tr '\n' ' ')"
+                                               TRACKS="$(echo "$CDPARANOIAOUTPUT" | grep -E '^[[:space:]]+[[:digit:]]' | tail -n 1 | get_first | tr -d "." | tr '\n' ' ')"
                                                CDPARANOIAAUDIOTRACKS="$TRACKS"
                                        else
                                                # Previous versions of abcde would store the tracks on a file, instead of the status record.
@@ -2149,9 +2160,9 @@ do_localcddb ()
                                                read CDDBLOCALCHOICE
                                                [ x"$CDDBLOCALCHOICE" = "x" ] && CDDBLOCALCHOICE="1"
                                                # FIXME # Introduce diff's
-                                               if echo $CDDBLOCALCHOICE | egrep "[[:space:]]*[[:digit:]]+,[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
+                                               if echo $CDDBLOCALCHOICE | grep -E "[[:space:]]*[[:digit:]]+,[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
                                                        diffentries cddblocalread "$CDDBLOCALCHOICES" "$CDDBLOCALCHOICE"
-                                               elif echo $CDDBLOCALCHOICE | egrep "[[:space:]]*[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
+                                               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
@@ -2218,15 +2229,77 @@ 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
+               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"
+               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
@@ -2461,7 +2534,7 @@ do_cddbedit ()
                                                echo -n "Which entry would you like abcde to use (0 for none)? [0-$CDDBCHOICES]: " >&2
                                                read CDDBCHOICE
                                                [ X"$CDDBCHOICE" = "X" ] && CDDBCHOICE=1
-                                               if echo $CDDBCHOICE | egrep "[[:space:]]*[[:digit:]]+,[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
+                                               if echo $CDDBCHOICE | grep -E "[[:space:]]*[[:digit:]]+,[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
                                                        if [ ! X"$DIFF" = "X" ]; then
                                                                PARSECHOICE1=$(echo $CDDBCHOICE | cut -d"," -f1 | xargs printf %d 2>/dev/null)
                                                                PARSECHOICE2=$(echo $CDDBCHOICE | cut -d"," -f2 | xargs printf %d 2>/dev/null)
@@ -2485,7 +2558,7 @@ do_cddbedit ()
                                                        else
                                                                echo "The diff program was not found in your path. Please choose a number between 0 and $CDDBCHOICES." >&2
                                                        fi
-                                               elif echo $CDDBCHOICE | egrep "[[:space:]]*[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
+                                               elif echo $CDDBCHOICE | grep -E "[[:space:]]*[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
                                                        # Make sure we get a valid choice
                                                        CDCHOICENUM=$(echo $CDDBCHOICE | xargs printf %d 2>/dev/null)
                                                        if [ $CDCHOICENUM -lt 0 ] || [ $CDCHOICENUM -gt $CDDBCHOICES ]; then
@@ -2615,7 +2688,7 @@ do_cddbedit ()
 
        # Some heuristics first. Look at Disc Title, and if it starts with
        # "Various", then we'll assume Various Artists
-       if [ "$(grep ^DTITLE= "$CDDBDATA" | cut -f2- -d= | egrep -ci '^(various|soundtrack|varios|sonora|ost)')" != "0" ]; then
+       if [ "$(grep ^DTITLE= "$CDDBDATA" | cut -f2- -d= | grep -Eci '^(various|soundtrack|varios|sonora|ost)')" != "0" ]; then
                echo "Looks like a Multi-Artist CD" >&2
                VARIOUSARTISTS=y
        else
@@ -2631,7 +2704,7 @@ do_cddbedit ()
                # Set a default
                DEFAULTSTYLE=1
                # Need NUMTRACKS before cddb-tool will return it:
-               NUMTRACKS=$(egrep '^TTITLE[0-9]+=' "$CDDBDATA" | wc -l)
+               NUMTRACKS=$(grep -E '^TTITLE[0-9]+=' "$CDDBDATA" | wc -l)
                if [ "$(grep -c "^TTITLE.*\/" "$CDDBDATA")" -gt "$(expr $NUMTRACKS / 2 )" ]; then
                        # More than 1/2 tracks contain a "/", so guess forward
                        DEFAULTSTYLE=1
@@ -2697,7 +2770,7 @@ do_cddbedit ()
        echo "variousartists=$VARIOUSARTISTS" >> "$ABCDETEMPDIR/status"
        echo "variousartiststyle=$VARIOUSARTISTSTYLE" >> "$ABCDETEMPDIR/status"
 
-       if [ "$EDITCDDB" = "y" ] && [ "$UNINTENTIONALLY_ANGER_THE_FREEDB_PEOPLE" = "y" ]; then
+       if [ "$EDITCDDB" = "y" ] && [ "$CDDBMETHOD" = "cddb" ] && [ "$UNINTENTIONALLY_ANGER_THE_FREEDB_PEOPLE" = "y" ]; then
                if [ "$CDDBDATAMD5SUM" != "" ]  && [ "$CDDBDATAMD5SUM" != "$($MD5SUM "$CDDBDATA" | cut -d " " -f 1)" ]; then
                        # This works but does not have the necessary error checking
                        # yet. If you are familiar with the CDDB spec
@@ -2972,7 +3045,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 +3138,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
@@ -3232,7 +3305,7 @@ 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:pPr:s:S:t:T:UvVxX:w:W:z opt ; do
+while getopts 1a:bBc:C:d:Defghj:klLmMnNo:pPq:r:s:S:t:T:UvVxX:w:W:z opt ; do
        case "$opt" in
                1) ONETRACK=y ;;
                a) ACTIONS="$OPTARG" ;;
@@ -3673,7 +3746,7 @@ case "$FLACENCODERSYNTAX" in
                        FLACENCODEROPTS="${FLACENCODEROPTS} --replay-gain"
                fi
                # FLAC streams can be encapsulated on a Ogg transport layer
-               if echo "$FLACENCODEROPTS" | egrep -- "(^| )--ogg($| )" > /dev/null 2>&1 ;then
+               if echo "$FLACENCODEROPTS" | grep -E -- "(^| )--ogg($| )" > /dev/null 2>&1 ;then
                        log error "FLAC on an Ogg container is not yet supported"
                        log error "due to problem with adding comments to such files"
                        exit 1
@@ -3867,7 +3940,7 @@ fi
 ## Now that we have metaflac, check if we need cue2discid
 #case $CDROMREADERSYNTAX in
 #      flac)
-#              TRACKINFO=$($METAFLAC --show-tag=CDDB $CDROM | cut -d"=" -f2 | egrep "[a-f0-9]{8}")
+#              TRACKINFO=$($METAFLAC --show-tag=CDDB $CDROM | cut -d"=" -f2 | grep -E "[a-f0-9]{8}")
 #              if [ "$TRACKINFO" = "" ]; then
 #                      checkexec ${NEEDCUE2DISCID+$CUE2DISCID}
 #              fi
@@ -3897,14 +3970,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:
@@ -4290,8 +4357,8 @@ if [ "$KEEPWAVS" = "y" ];then
 fi
 # Check if we have moved all the formats we had previously encoded, if we are not using the FORCE.
 if [ "$DOCLEAN" = "y" ] && [ ! "$FORCE" = "y" ]; then
-       ENCODED_FORMATS=$(egrep "^encodetrack-(.{3,6})-(.{1,2})$" "$ABCDETEMPDIR/status" | cut -d"-" -f2 | sort -u | tr '\n' '|')
-       MOVED_FORMATS=$(egrep "^movetrack-output-(.{3,6})$" "$ABCDETEMPDIR/status" | cut -d"-" -f3 | sort -u | tr '\n' '|')
+       ENCODED_FORMATS=$(grep -E "^encodetrack-(.{3,6})-(.{1,2})$" "$ABCDETEMPDIR/status" | cut -d"-" -f2 | sort -u | tr '\n' '|')
+       MOVED_FORMATS=$(grep -E "^movetrack-output-(.{3,6})$" "$ABCDETEMPDIR/status" | cut -d"-" -f3 | sort -u | tr '\n' '|')
        if [ "$ENCODED_FORMATS" != "$MOVED_FORMATS" ]; then
                log warning "The encoded formats does not match with the moved ones"
                log warning "Formats encoded: $( echo $ENCODED_FORMATS | tr "|" " " )"