r146@frost: data | 2005-08-28 22:24:29 +0300
[clinton/abcde.git] / abcde
diff --git a/abcde b/abcde
index 0cd8a71..714f564 100755 (executable)
--- a/abcde
+++ b/abcde
@@ -31,6 +31,7 @@ echo "-d <device>"
 echo "       Specify CDROM device to grab"
 echo "-D     Debugging mode (equivalent to sh -x abcde)"
 echo "-e     Erase encoded track information from status file"
+#echo "-E     Set the encoding information for the tags"
 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)"
@@ -280,6 +281,70 @@ relpath ()
        echo $TO
 }
 
+# do_getcddbinfo
+# Finds an specific field from cddbinfo
+# 
+#      TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2 -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')
+#      TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -n 1 | cut -f2 -d= | tr -d \[:cntrl:\])
+#      TRACKNAME="$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')"
+
+do_getcddbinfo ()
+{
+       case $1 in
+       TRACKNAME1)
+               TRACKNAME="$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -n 1 | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')"
+               ;;
+       TRACKNAME)
+               TRACKNAME="$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')"
+               ;;
+       esac
+}
+
+# do_gettracknum
+# Get the track number we are going to use for different actions
+do_gettracknum
+{
+       if [ -n "$STARTTRACKNUMBER" ] ; then
+               # Get the trackpadding from the current track
+               CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
+               TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - $FIRSTTRACK ))
+       else
+               TRACKNUM=${UTRACKNUM}
+       fi
+}
+
+do_replaygain
+{
+       run-command "" echo "Adding reply-gain information..."
+       for OUTPUT in $( echo $OUTPUTTYPE | tr , \ )
+       do
+               OUTPUTFILES=""
+               for UTRACKNUM in $TRACKQUEUE
+               do
+                       CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
+                       do_getcddbinfo TRACKNAME
+                       splitvarious
+                       TRACKFILE=$(mungefilename "$TRACKNAME")
+                       ARTISTFILE=$(mungefilename "$TRACKARTIST")
+                       ALBUMFILE=$(mungefilename "$DALBUM")
+                       do_gettrucknum
+                       if [ "$VARIOUSARTISTS" = "y" ]; then
+                               OUTPUTFILE=$(eval echo $VAOUTPUTFORMAT)
+                       else
+                               OUTPUTFILE=$(eval echo $OUTPUTFORMAT)
+                       fi
+                       OUTPUTFILES="$OUTPUTDIR/$OUTPUTFILE.$OUTPUT $OUTPUTFILES"
+               done
+               case "$OUTPUT" in
+                       flac);;
+                       ogg);;
+                       *);;
+               esac
+               echo vorbisgain-$OUTPUT >> "$ABCDETEMPDIR/status"
+       done
+       echo vorbisgain >> "$ABCDETEMPDIR/status"
+}
+
 # This code splits the a Various Artist track name from one of the following
 # forms:
 #
@@ -327,7 +392,7 @@ splitvarious ()
                        TRACKNAME=$(echo "$DTITLEARTIST" | cut -f1 -d~)
                        ;;
                esac
-       elif [ "$ONETRACK" = "y" ]; then
+       elif [ "$VARIOUSARTISTS" = "y" ] && [ "$ONETRACK" = "y" ]; then
                TRACKARTIST="Various"
        else
                TRACKARTIST="$DARTIST"
@@ -400,7 +465,7 @@ local id=""
                "gangsta")               id=59 ;;
                "top 40")                id=60 ;;
                "christian rap")         id=61 ;;
-               "pop/funk")              id=62 ;;
+               "pop/funk"|"pop / funk") id=62 ;;
                "jungle")                id=63 ;;
                "native american")       id=64 ;;
                "cabaret")               id=65 ;;
@@ -486,6 +551,7 @@ local id=""
                "anime")                 id=145 ;;
                "jpop")                  id=146 ;;
                "synthpop")              id=147 ;;
+               "rock/pop"|"rock / pop") id=148 ;;
                *)                       return 1 ;;
        esac
 echo ${id}
@@ -503,10 +569,13 @@ do_tag ()
        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
-       if [ -n "$STARTTRACKNUMBER" -a -n "$STARTTRACKNUMBERTAG" ] ; then
-               # Get the trackpadding from the current track
-               CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
-               TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - $FIRSTTRACK ))
+#      if [ -n "$STARTTRACKNUMBER" -a -n "$STARTTRACKNUMBERTAG" ] ; then
+#              # Get the trackpadding from the current track
+#              CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
+#              TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - $FIRSTTRACK ))
+#      fi
+       if [ -n "$STARTTRACKNUMBERTAG" ] ; then
+               do_gettracknum
        fi
        for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
        do
@@ -515,11 +584,24 @@ do_tag ()
                # id3v2 v0.1.9 claims to have solved the -c bug, so we merge both id3 and id3v2
                GENREID=$(do_getgenreid "${CDGENRE}")
 
-               # FIXME # track numbers in mp3 come with 1/10, so we cannot happily substitute them with $TRACKNUM
-               run_command tagtrack-$1 $TAGGER $TAGGEROPTS -c "$COMMENTOUTPUT" \
-                       -A "$DALBUM" -a "$TRACKARTIST" -t "$TRACKNAME" -y "$CDYEAR" \
-                       -g "$GENREID" -T "${TRACKNUM:-$1/$TRACKS}" \
-                       "$ABCDETEMPDIR/track$1.$OUTPUT"
+               case "$ID3SYNTAX" in
+                       id3);;
+                       eyed3)
+                               # FIXME # track numbers in mp3 come with 1/10, so we cannot happily substitute them with $TRACKNUM
+                               run_command tagtrack-$1 $TAGGER $TAGGEROPTS --commen=::"$COMMENTOUTPUT" \
+                                       -A "$DALBUM" -a "$TRACKARTIST" -t "$TRACKNAME" -Y "$CDYEAR" \
+                                       -G "$GENREID" -n "${TRACKNUM:-$1}" "${TRACKNUM:+-N $TRACKS}" \
+                                       "${ENCODING:+--set-encoding=$ENCODING}"
+                                       "$ABCDETEMPDIR/track$1.$OUTPUT"
+                               ;;
+                       *)
+                               # FIXME # track numbers in mp3 come with 1/10, so we cannot happily substitute them with $TRACKNUM
+                               run_command tagtrack-$1 $TAGGER $TAGGEROPTS -c "$COMMENTOUTPUT" \
+                                       -A "$DALBUM" -a "$TRACKARTIST" -t "$TRACKNAME" -y "$CDYEAR" \
+                                       -g "$GENREID" -T "${TRACKNUM:-$1/$TRACKS}" \
+                                       "$ABCDETEMPDIR/track$1.$OUTPUT"
+                               ;;
+               esac
                ;;
        vorbis|ogg)
                case "$OGGENCODERSYNTAX" in
@@ -608,7 +690,7 @@ do_batch_encode ()
                        do
                                TRACKFILES="$TRACKFILES track$UTRACKNUM.wav"
                        done
-                       nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS --nogap $TRACKFILES
+                       nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS `[ "$MP3ENCODER" = "lame" ] && echo "--nogap"` $TRACKFILES
                        RETURN=$?
                        if [ "$RETURN" != "0" ]; then
                                echo "batch-encode: $ENCODER returned code $RETURN" >> errors
@@ -651,7 +733,7 @@ do_encode ()
                                TEMPARG="PIPE_$MPPENCODER"
                                ;;
                esac
-               IN=${!TEMPARG}
+               IN="$( eval echo "\$$TEMPARG" )"
        else
                IN="$ABCDETEMPDIR/track$1.wav"
                case "$OUTPUT" in
@@ -976,13 +1058,14 @@ do_move ()
                YEAR=$(echo $CDYEAR)
                # If we want to start the tracks with a given number, we need to modify the
                # TRACKNUM value before evaluation
-               if [ -n "$STARTTRACKNUMBER" ] ; then
-                       # Get the trackpadding from the current track
-                       CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
-                       TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - $FIRSTTRACK ))
-               else
-                       TRACKNUM=${UTRACKNUM}
-               fi
+       #       if [ -n "$STARTTRACKNUMBER" ] ; then
+       #               # Get the trackpadding from the current track
+       #               CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
+       #               TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - $FIRSTTRACK ))
+       #       else
+       #               TRACKNUM=${UTRACKNUM}
+       #       fi
+               do_gettracknum
                # Supported variables for OUTPUTFORMAT are GENRE, ALBUMFILE, ARTISTFILE,
                # TRACKFILE, and TRACKNUM.
                if [ "$VARIOUSARTISTS" = "y" ]; then
@@ -1073,19 +1156,22 @@ do_playlist ()
                        do
                                # Shares some code with do_move since the filenames have to match
                                CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
-                               TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2 -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')
+                               #TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2 -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')
+                               do_getcddbinfo TRACKNAME
                                splitvarious
                                TRACKFILE=$(mungefilename "$TRACKNAME")
                                ARTISTFILE=$(mungefilename "$TRACKARTIST")
+                               ALBUMFILE=$(mungefilename "$DALBUM")
                                # If we want to start the tracks with a given number, we need to modify the
                                # TRACKNUM value before evaluation
-                               if [ -n "$STARTTRACKNUMBER" ] ; then
-                                       # Get the trackpadding from the current track
-                                       CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
-                                       TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - $FIRSTTRACK ))
-                               else
-                                       TRACKNUM=${UTRACKNUM}
-                               fi
+                       #       if [ -n "$STARTTRACKNUMBER" ] ; then
+                       #               # Get the trackpadding from the current track
+                       #               CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
+                       #               TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - $FIRSTTRACK ))
+                       #       else
+                       #               TRACKNUM=${UTRACKNUM}
+                       #       fi
+                               do_gettrucknum
                                if [ "$VARIOUSARTISTS" = "y" ]; then
                                        OUTPUTFILE=$(eval echo $VAOUTPUTFORMAT)
                                else
@@ -1877,7 +1963,7 @@ do_cddbedit ()
        echo "variousartiststyle=$VARIOUSARTISTSTYLE" >> "$ABCDETEMPDIR/status"
 
        if [ "$EDITCDDB" = "y" ] && [ "$UNINTENTIONALLY_ANGER_THE_FREEDB_PEOPLE" = "y" ]; then
-               if [ $CDDBDATAMD5SUM != "" ]  && [ $CDDBDATAMD5SUM != $($MD5SUM "$CDDBDATA" | cut -d " " -f 1) ]; 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
                        # (see http://www.freedb.org/src/latest/DBFORMAT) 
@@ -1942,7 +2028,7 @@ do_cdread ()
        CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
        if [ "$USEPIPES" = "y" ]; then
                TEMPARG="PIPE_$CDROMREADERSYNTAX"
-               FILEARG="${!TEMPARG}"
+               FILEARG="$( eval echo "\$$TEMPARG" )"
                REDIR=""
                PIPE_MESSAGE="and encoding "
        else
@@ -1962,7 +2048,8 @@ do_cdread ()
                echo "Grabbing ${PIPE_MESSAGE}tracks $UTRACKNUM - $LASTTRACK as one track ..." >&2
        else
                if [ -r "$CDDBDATA" ]; then
-                       TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -n 1 | cut -f2 -d= | tr -d \[:cntrl:\])
+                       #TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -n 1 | cut -f2 -d= | tr -d \[:cntrl:\])
+                       do_getcddbinfo TRACKNAME
                        echo "Grabbing ${PIPE_MESSAGE}track $UTRACKNUM: $TRACKNAME..." >&2
                else
                        echo "Grabbing ${PIPE_MESSAGE}track $UTRACKNUM..." >&2
@@ -2160,6 +2247,7 @@ MPPENC=mppenc
 
 ID3=id3
 ID3V2=id3v2
+EYED3=eyeD3
 CDPARANOIA=cdparanoia
 CDDA2WAV=cdda2wav
 DAGRAB=dagrab
@@ -2227,6 +2315,7 @@ DEFAULT_OUTPUT_BINARIES=vorbis:oggenc,flac:flac,mp3:lame,mp3:bladeenc,spx:speex
 # Let's use these checkings to determine the OS flavour, which will be used later
 if [ X$(uname) = "XFreeBSD" ] ; then
        HTTPGET=fetch
+       MD5SUM=md5
        NEEDCDROMID=y
        OSFLAVOUR=FBSD
 elif [ X$(uname) = "XDarwin" ] ; then
@@ -2238,6 +2327,9 @@ elif [ X$(uname) = "XDarwin" ] ; then
 elif [ X$(uname) = "XOpenBSD" ] ; then
        HTTPGET=wget
        MD5SUM=md5
+elif [ X$(uname) = "XNetBSD" ] ; then
+       HTTPGET=ftp
+       MD5SUM=md5
 else
        HTTPGET=wget
 fi
@@ -2273,6 +2365,7 @@ if [ "$HTTPGETOPTS" = "" ] ; then
                wget) HTTPGETOPTS="-q -O -";;
                curl) HTTPGETOPTS="-f -s";;
                fetch)HTTPGETOPTS="-q -o -";;
+               ftp)  HTTPGETOPTS="-q -o -";;
                *) echo "abcde warning: HTTPGET in non-standard and HTTPGETOPTS are not defined." >&2 ;;
        esac
 fi
@@ -2300,7 +2393,7 @@ while getopts 1a:bc:C:d:Dehj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W: opt ; do
        case "$opt" in
                1) ONETRACK=y ;;
                a) ACTIONS="$OPTARG" ;;
-               A) EXPACTIONS="$OPTARG";;
+               A) EXPACTIONS="$OPTARG" ;;
                b) BATCH=y ;;
                c) if [ -e "$OPTARG" ] ; then . "$OPTARG" ; else echo "abcde error: config file \"$OPTARG\" cannot be found." >&2 ; exit 1 ; fi ;;
                C) DISCID="${OPTARG#abcde.}" ;;
@@ -2309,6 +2402,7 @@ while getopts 1a:bc:C:d:Dehj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W: opt ; do
                e) ERASEENCODEDSTATUS=y ;;
                h) usage; exit ;;
                e) ERASEENCODEDSTATUS=y ;;
+               E) ENCODING="$OPTARG" ;;
 #              f) FORCECDDBUSELOCAL=y ;;
                i) INLINETAG=y ;;
                j) MAXPROCS="$OPTARG" ;;
@@ -2725,13 +2819,13 @@ if [ "$USEPIPES" = "y" ]; then
                mpc)
                        PIPEENCODERSVARCHECK="PIPE_$MPPENCODER" ;;
        esac
-       if [ ! -n "${!PIPERIPPERSVARCHECK}" ] ; then
+       if [ ! -n "$( eval echo "\$$PIPERIPPERSVARCHECK" )" ] ; then
                echo "abcde error: no support for pipes with given ripper" >&2
                echo "read the USEPIPES file from the source tarball to help" >&2
                echo "on a Debian system, read /usr/share/doc/abcde/USEPIPES.gz" >&2
                exit 1;
        fi
-       if [ ! -n "${!PIPEENCODERSVARCHECK}" ] ; then
+       if [ ! -n "$( eval echo "\$$PIPEENCODERSVARCHECK" )" ] ; then
                echo "abcde error: no support for pipes with given encoder" >&2
                echo "read the USEPIPES file from the source tarball to help" >&2
                echo "on a Debian system, read /usr/share/doc/abcde/USEPIPES.gz" >&2
@@ -3040,7 +3134,8 @@ do
 #                      TRACKNUM=$(printf %0.${TRACKNUMPADDING}d $(expr ${UTRACKNUM} + 0))
                        TRACKNUM=$UTRACKNUM
                        CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
-                       TRACKNAME="$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')"
+                       #TRACKNAME="$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')"
+                       do_getcddbinfo TRACKNAME
                        splitvarious
                fi
        fi
@@ -3111,6 +3206,9 @@ if [ "$DOENCODE" = "y" -a "$USEPIPES" != "y" ]; then
 fi
 # If the above didn't catch the stragglers, this will
 wait
+if checkstatus replaygain; then :; else
+       do_replaygain
+fi
 # Check to see if run_command logged any errors
 if [ -f "$ABCDETEMPDIR/errors" ]; then
        echo "The following commands failed to run:"