Add -O / OVERRIDEALBUMARTDOWNLOAD option
[abcde.git] / abcde
1 #!/bin/bash
2 # Copyright (c) 1998-2001 Robert Woodcock <rcw@debian.org>
3 # Copyright (c) 2003-2006 Jesus Climent <jesus.climent@hispalinux.es>
4 # Copyright (c) 2009-2012 Colin Tuckley <colint@debian.org>
5 # Copyright (c) 2012-2019 Steve McIntyre <93sam@@debian.org>
6 # Copyright (c) 2015-2017 Andrew Strong <andrew.david.strong@gmail.com>
7 # This code is hereby licensed for public consumption under either the
8 # GNU GPL v2 or greater, or Larry Wall's Artistic license - your choice.
9 #
10 # You should have received a copy of the GNU General Public License along
11 # with this program; if not, write to the Free Software Foundation, Inc.,
12 # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13
14 VERSION='2.9.4-DEV'
15
16 usage ()
17 {
18 echo "This is abcde v$VERSION."
19 echo "Usage: abcde [options] [tracks]"
20 echo "Options:"
21 echo "-1     Encode the whole CD in a single file"
22 echo "-a <action1[,action2]...>"
23 echo "       Actions to perform, comma separated:"
24 echo "       (cddb,read,getalbumart,embedalbumart,normalize,encode,"
25 echo "       tag,move,replaygain,playlist,clean)"
26 #echo "-A     Experimental actions (retag, transcode)"
27 echo "-b     Enable batch normalization"
28 echo "-B     Embed albumart (this also activates getalbumart)"
29 echo "-c <file>"
30 echo "       Specify a configuration file (overrides system and user config files)"
31 echo "-C <discid#>"
32 echo "       Specify discid to resume from (only needed if you no longer have the cd)"
33 echo "-d <device>"
34 echo "       Specify CDROM device to grab (flac uses a single-track flac file)"
35 echo "-D     Debugging mode (equivalent to sh -x abcde)"
36 echo "-e     Erase encoded track information from status file"
37 echo "-f     Force operations that otherwise are considered harmful. Read \"man abcde\""
38 echo "-g     Use \"lame --nogap\" for MP3 encoding. Disables low disk and pipes flags"
39 echo "-G     Get album art by using the 'getalbumart' action"
40 echo "-h     This help information"
41 #echo "-i    Tag files while encoding, when possible (local only) -NWY-"
42 echo "-j <#> Number of encoder processes to run at once (localhost)"
43 echo "-k     Keep the wav tracks for later use"
44 echo "-l     Use low disk space algorithm"
45 echo "-L     Use local CDDB storage directory"
46 echo "-m     Modify playlist to include CRLF endings, to comply with some players"
47 #echo "       WARNING: Deprecated. Use \"cue\" action"
48 #echo "-M     Create a CUE file"
49 echo "-n     No lookup. Don't query CDDB, just create and use template"
50 echo "-N     Noninteractive. Never prompt for anything"
51 echo "-o <type1[,type2]...>"
52 echo "       Output file type(s), comma separated"
53 echo "       (vorbis,mp3,flac,spx,mpc,wav,m4a,opus,mka,wv,ape,mp2,tta,aiff)."
54 echo "       Defaults to vorbis"
55 echo "-O     Allow Override of album art lookup"
56 echo "-p     Pad track numbers with 0's (if less than 10 tracks)"
57 echo "-P     Use UNIX pipes to read+encode without wav files"
58 echo "-Q     CD lookup method(s), comma separated"
59 echo "       (musicbrainz,cddb,cdtext). Defaults to musicbrainz".
60 echo "-r <host1[,host2]...>"
61 echo "       Also encode on these remote hosts"
62 echo "-s <field>"
63 echo "       Show fields from the CDDB info (year,genre)"
64 echo "-S <#> Set the CD speed"
65 echo "-t <#> Start the track numbering at a given number"
66 echo "-T <#> Same as -t but modifies tag numbering"
67 echo "-U     Do NOT use UNICODE (UTF8) tags and comments"
68 echo "-v     Show version number and exit"
69 echo "-V     Be a bit more verbose about what is happening behind the scenes"
70 echo "-x     Eject CD after all tracks are read"
71 echo "-w <comment>"
72 echo "       Add a comment to the CD tracks"
73 echo "-W <#> Concatenate CDs: -T #01 -w \"CD #\""
74 echo "-z     Use debug CDROMREADERSYNTAX option (needs cdparanoia)"
75 echo ""
76 echo "Tracks is a space-delimited list of tracks to grab."
77 echo "Ranges specified with hyphens are allowed (i.e., 1-5)."
78 echo ""
79 #echo "Double hyphens are used to concatenate tracks"
80 }
81
82 addstatus ()
83 {
84         echo "$@" >> "${ABCDETEMPDIR}/status"
85 }
86
87 # log [level] [message]
88 #
89 # log outputs the right message in a common format
90 log ()
91 {
92         BLURB="$1"
93         shift
94         case $BLURB in
95                 error)   >&2 echo "[ERROR] abcde: $@" >&2 ;;
96                 warning) >&2 echo "[WARNING] $@" >&2 ;;
97                 info)    >&4 echo "[INFO] $@" ;;
98         esac
99 }
100
101 # Functions to replace the need of seq, which is too distribution dependent.
102 f_seq_row ()
103 {
104         i=$1
105         while [ $i -le $2 ]
106         do
107                 echo $i
108                 i=$(( $i + 1 ))
109         done
110 }
111
112 f_seq_line ()
113 {
114         i=$1
115         if echo "$i" | grep "[[:digit:]]" > /dev/null 2>&1 ; then
116                 while [ $i -le $2 ]
117                 do
118                         printf "%d " "$i"
119                         i=$(( $i + 1 ))
120                 done
121                 echo
122         else
123                 log error "syntax error while processing track numbers ($i)"
124                 exit 1
125         fi
126 }
127
128 # Functions to replace the need of awk {print $1} and {print $NF}
129 get_first()
130 {
131         if [ X"$1" = "X" ]; then
132                 for first in $(cat); do
133                         break
134                 done
135         else
136                 first=$1
137         fi
138         echo "$first"
139 }
140
141 get_last()
142 {
143         if [ X"$1" = "X" ]; then
144                 for stdin in $(cat); do
145                         last=$stdin
146                 done
147         else
148                 for last in $@ ; do :; done
149         fi
150         echo "$last"
151 }
152
153 # checkstatus [blurb]
154 # Returns "0" if the blurb was found, returns 1 if it wasn't
155 # Puts the blurb content, if available, on stdout.
156 # Otherwise, returns "".
157 checkstatus ()
158 {
159         # Take the last line in the status file if there's multiple matches
160         PATTERN="^$1(=.*)?$"
161         BLURB=$(grep -E "$PATTERN" "${ABCDETEMPDIR}/status" | tail -n 1)
162
163         if [ -z "$BLURB" ]; then
164                 # No matches found
165                 return 1
166         else
167                 # Matches found
168                 # See if there's a = in it
169                 if ( echo "$BLURB" | grep -q '=' ); then
170                         echo "$BLURB" | cut -f2- -d=
171                 fi
172                 return 0
173         fi
174 }
175
176 # checkwarnings [blurb]
177 # Returns "0" if the blurb was found (meaning there was an warning),
178 # returns 1 if it wasn't (yes this is a little backwards).
179 # Does not print the blurb on stdout.
180 # Otherwise, returns "".
181 checkwarnings ()
182 {
183         if [ -e "${ABCDETEMPDIR}/warnings" ]; then :; else
184                 return 1
185         fi
186         # Take the last line in the status file if there's multiple matches
187         PATTERN="^$1(:.*)?$"
188         BLURB=$(grep -E "$PATTERN" "${ABCDETEMPDIR}/warnings" | tail -n 1)
189
190         if [ -z "$BLURB" ]; then
191                 # negative, we did not have a negative...
192                 return 1
193         else
194                 # affirmative, we had a negative...
195                 return 0
196         fi
197 }
198
199 # checkerrors [blurb]
200 # Returns "0" if the blurb was found (meaning there was an error),
201 # returns 1 if it wasn't (yes this is a little backwards).
202 # Does not print the blurb on stdout.
203 # Otherwise, returns "".
204 checkerrors ()
205 {
206         if [ -e "${ABCDETEMPDIR}/errors" ]; then :; else
207                 return 1
208         fi
209         # Take the last line in the status file if there's multiple matches
210         PATTERN="^$1(:.*)?$"
211         BLURB=$(grep -E "$PATTERN" "${ABCDETEMPDIR}/errors" | tail -n 1)
212
213         if [ -z "$BLURB" ]; then
214                 # negative, we did not have a negative...
215                 return 1
216         else
217                 # affirmative, we had a negative...
218                 return 0
219         fi
220 }
221
222 # page [file]
223 # Display a file. If it's too long to fit the current terminal size,
224 # find the right pager in the system and use it
225 page ()
226 {
227         PAGEFILE="$1"
228         local NUM_LINES=$(wc -l < "$PAGEFILE")
229         local LINES=""
230
231         # Check the terminal length every time we're called - the user
232         # might have resized since last time we were here
233         # First, try tput
234         if new_checkexec tput; then
235                 LINES=$(tput lines)
236         fi
237
238         # If tput didn't work, 24 will do...
239         if [ -z "$LINES" ]; then
240                 LINES=24
241         fi
242
243         # Is the text long enough to need a pager?
244         if [ "$NUM_LINES" -ge $LINES ]; then
245                 # Yes!
246                 # Use the debian sensible-pager wrapper to pick the pager user
247                 # has requested via their $PAGER environment variable
248                 if [ -x "/usr/bin/sensible-pager" ]; then
249                         /usr/bin/sensible-pager "$PAGEFILE"
250                 elif [ -x "$PAGER" ]; then
251                         # That failed, try to load the preferred pager, starting
252                         # with their PAGER variable
253                         $PAGER "$PAGEFILE"
254                         # If that fails, check for less
255                 elif [ -x /usr/bin/less ]; then
256                         /usr/bin/less -f "$PAGEFILE"
257                         # more should be on all UNIX systems
258                 elif [ -x /bin/more ]; then
259                         /bin/more "$PAGEFILE"
260                 else
261                         # No bananas, just cat the thing
262                         cat "$PAGEFILE" >&2
263                 fi
264         else
265                 # No, just cat the thing
266                 cat "$PAGEFILE" >&2
267         fi
268 }
269
270 # run_command [blurb] [command...]
271 # Runs a command, silently if necessary, and updates the status file
272 run_command ()
273 {
274         vvecho "$@"
275         BLURB="$1"
276         shift
277         # See if this is supposed to be silent
278         if [ "$(checkstatus encode-output)" = "loud" ]; then
279                 "$@" >&2
280                 RETURN=$?
281         else
282                 # Special case for SMP, since
283                 # encoder output is never displayed, don't mute echos
284                 if [ -z "$BLURB" ] && [ "$MAXPROCS" != "1" ]; then
285                         "$@" >&2
286                         RETURN=$?
287                 else
288                         "$@" >/dev/null 2>&1
289                         RETURN=$?
290                 fi
291         fi
292         case "$1" in
293         normalize|normalize-audio)
294                 if [ "$RETURN" = "2" ]; then
295                         # File was already normalized.
296                         RETURN=0
297                 fi
298                 ;;
299         esac
300         if [ "$RETURN" != "0" ]; then
301                 # Put an error in the errors file. For various reasons we
302                 # can't capture a copy of the program's output but we can
303                 # log what we attempted to execute and the error code
304                 # returned by the program.
305                 if [ "$BLURB" ]; then
306                         TWEAK="$BLURB: "
307                 fi
308                 echo "${TWEAK}returned code $RETURN: $@" >> "${ABCDETEMPDIR}/errors"
309                 return $RETURN # Do not pass go, do not update the status file
310         fi
311         if [ "$BLURB" ]; then
312                 echo "$BLURB" >> "${ABCDETEMPDIR}/status"
313         fi
314 }
315
316 # relpath() and slash() are Copyright (c) 1999 Stuart Ballard and
317 # distributed under the terms of the GNU GPL v2 or later, at your option
318
319 # Function to determine if a word contains a slash.
320 slash ()
321 {
322         case "$1" in
323         */*) return 0;;
324         *) return 1;;
325         esac
326 }
327
328 # Function to give the relative path from one file to another.
329 # Usage: relpath fromfile tofile
330 # eg relpath music/Artist/Album.m3u music/Artist/Album/Song.mp3
331 # (the result would be Album/Song.mp3)
332 # Output is relative path to $2 from $1 on stdout
333
334 # This code has the following restrictions:
335 # Multiple ////s are not collapsed into single /s, with strange effects.
336 # Absolute paths and ../s are handled wrong in FR (but they work in TO)
337 # If FR is a directory it must have a trailing /
338
339 relpath ()
340 {
341         FR="$1"
342         TO="$2"
343
344         case "$TO" in
345         /*) ;; # No processing is needed for absolute paths
346         *)
347                 # Loop through common prefixes, ignoring them.
348                 while slash "$FR" && [ "$(echo "$FR" | cut -d/ -f1)" = "$(echo "$TO" | cut -d/ -f1)" ]
349                 do
350                         FR="$(echo "$FR" | cut -d/ -f2-)"
351                         TO="$(echo "$TO" | cut -d/ -f2-)"
352                 done
353                 # Loop through directory portions left in FR, adding appropriate ../s.
354                 while slash "$FR"
355                 do
356                         FR="$(echo "$FR" | cut -d/ -f2-)"
357                         TO="../$TO"
358                 done
359                 ;;
360         esac
361
362         echo "$TO"
363 }
364
365 new_checkexec ()
366 {
367         if [ ! "$@" = "" ]; then
368                 # Cut off any command-line option we added in
369                 X=$(echo "$@" | cut -d' ' -f2)
370                 WHICH=$(which "$X" 2>/dev/null)
371                 if [ -z "$WHICH" ]; then
372                         return 1
373                 elif [ ! -x "$WHICH" ]; then
374                         return 2
375                 fi
376         fi
377         return 0
378 }
379
380 checkexec ()
381 {
382         if [ ! "$@" = "" ]; then
383                 # Cut off any command-line option we added in
384                 X=$(echo "$@" | cut -d' ' -f2)
385                 # Test for built-in abcde.function
386                 [ "$X" != "${X#abcde.}" ] && type "$X" >/dev/null 2>&1 && return
387                 WHICH=$(which "$X" 2>/dev/null)
388                 if [ -z "$WHICH" ]; then
389                         log error "$X is not in your path." >&2
390                         log info  "Define the full path to the executable if it exists on your system." >&2
391                         if [ -e /etc/debian_version ] ; then
392                                 case $X in
393                                         oggenc)
394                                                 MISSING_PACKAGE=vorbis-tools
395                                                 ;;
396                                         lame|flac|cd-discid|eject|mkcue|icedax|glyrc)
397                                                 MISSING_PACKAGE=$X
398                                                 ;;
399                                 esac
400                                 log info "Hint: sudo apt-get install $MISSING_PACKAGE" >&2
401                         fi
402                         exit 1
403                 elif [ ! -x "$WHICH" ]; then
404                         log error "$X is not executable." >&2
405                         exit 1
406                 fi
407         fi
408 }
409
410 # diffentries <filename> <max_value> <entry1>,<entry2>
411 # max_value: the range of entries goes from 1 to <max_value>
412 diffentries ()
413 {
414         FILENAME=$1
415         shift
416         local CDDBDIFFCHOICES=$1
417         shift
418         local CDDBDIFFCHOICE="$@"
419         if [ ! X"$DIFF" = "X" ]; then
420                 PARSECHOICE1=$(echo "$CDDBDIFFCHOICE" | cut -d"," -f1 | xargs printf %d 2>/dev/null)
421                 PARSECHOICE2=$(echo "$CDDBDIFFCHOICE" | cut -d"," -f2 | xargs printf %d 2>/dev/null)
422                 if [ "$PARSECHOICE1" -lt 1 ] || [ "$PARSECHOICE1" -gt "$CDDBDIFFCHOICES" ] || \
423                    [ "$PARSECHOICE2" -lt 1 ] || [ "$PARSECHOICE2" -gt "$CDDBDIFFCHOICES" ] || \
424                    [ "$PARSECHOICE1" -eq "$PARSECHOICE2" ]; then
425                         echo "Invalid diff range." >&2
426                         echo "Please select two comma-separated numbers between 1 and $CDDBDIFFCHOICES" >&2
427                 else
428                         # We parse the 2 choices to diff, store them in temporary files and diff them.
429                         for PARSECHOICE in $(echo "$CDDBDIFFCHOICE" | tr , \ ); do
430                                 do_cddbparse "${ABCDETEMPDIR}/$FILENAME.$PARSECHOICE" > "${ABCDETEMPDIR}/$FILENAME.parsechoice.$PARSECHOICE"
431                         done
432                         echo "Showing diff between choices $PARSECHOICE1 and $PARSECHOICE2..." > "${ABCDETEMPDIR}/$FILENAME.diff"
433                         $DIFF $DIFFOPTS "${ABCDETEMPDIR}/$FILENAME.parsechoice.$PARSECHOICE1" \
434                                   "${ABCDETEMPDIR}/$FILENAME.parsechoice.$PARSECHOICE2" >> "${ABCDETEMPDIR}/$FILENAME.diff"
435                         page "${ABCDETEMPDIR}/$FILENAME.diff"
436                 fi
437         else
438                 echo "The diff program was not found in your path." >&2
439                 echo "Please choose a number between 0 and $CDDBDIFFCHOICES." >&2
440         fi
441 }
442
443 # getcddbinfo
444 # Finds an specific field from cddbinfo
445 getcddbinfo()
446 {
447         case $1 in
448         TRACKNAME1)
449                 TRACKNAME="$(grep -a "^TTITLE$CDDBTRACKNUM=" "$CDDBDATA" | head -n 1 | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')"
450                 ;;
451         TRACKNAME)
452                 TRACKNAME="$(grep -a "^TTITLE$CDDBTRACKNUM=" "$CDDBDATA" | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')"
453                 ;;
454         TRACK-INFO)
455                 grep -a "^EXTT$CDDBTRACKNUM=" "$CDDBDATA" | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\\n/\n/g'
456                 ;;
457         esac
458 }
459
460 # gettracknum
461 # Get the track number we are going to use for different actions
462 gettracknum()
463 {
464         if [ -n "$STARTTRACKNUMBER" ] ; then
465                 # Get the trackpadding from the current track, also trim whitespace for MacOSX
466                 CURRENTTRACKPADDING=$(echo -n "$UTRACKNUM" | wc -c | tr -d ' ')
467                 REALTRACKNUM=$(expr $UTRACKNUM + 0) # Unpad
468                 TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $((${REALTRACKNUM} + ${STARTTRACKNUMBER} - ${FIRSTTRACK})))
469         else
470                 TRACKNUM=${UTRACKNUM}
471         fi
472 }
473
474 # makeids
475 #
476 # Calculate cddb disc ids without requiring specialized helper programs.
477 # largely copied from cd-discid and musicbrainz examples.  some of the steps
478 # don't make sense, but they're necessary to match the ids generated by other
479 # programs.
480 #
481 ## FIXME ## Right now, we get 2 frames more than with cue2discid ??
482 # data@petit:~$ sh /tmp/cue2discid /home/data/tmp/flac/01.Roisin_Murphy--Ruby_Blue.flac
483 # processing offsetimes 00:00:00 04:47:10 08:20:37 11:46:46 17:45:36 21:41:57 27:32:21 32:03:73 35:39:28 38:27:33 43:50:38 44:42:34
484 # 980b4b0c 12 150 21685 37687 53146 80061 97782 124071 144448 160603 173208 197438 201334 2895
485 # data@petit:~$ metaflac --export-cuesheet-to=- /home/data/tmp/flac/01.Roisin_Murphy--Ruby_Blue.flac| python /home/data/sources/abcde/trunk/examples/cue2discid
486 # 980b4b0c 12 150 21685 37687 53146 80061 97782 124071 144448 160603 173208 197438 201334 2893
487 #
488 # Variables: OFFSETS, TRACKS, LEADOUT, [LEADIN]
489 makeids ()
490 {
491         if [ X"$LEADOUT" = "X" ]; then
492                 log warning "Error trying to calculate disc ids without lead-out information."
493                 exit 1
494         fi
495
496         # default to a two second lead-in
497         IDMAGICNUM=150
498         LEADIN=${LEADIN:=150}
499
500         # number of cdframes per second
501         CDFRAMES=75
502
503         # reset cddb checksum for cddb disc-id calululation
504         CDDBCKSUM=0
505
506         COOKEDOFFSETS=""
507         for OFFSET in $OFFSETS
508         do
509                 COOKEDOFFSETS="${COOKEDOFFSETS} $(($OFFSET + $LEADIN))"
510
511                 OFFSETTIME=$(( ($OFFSET + $LEADIN) / $CDFRAMES  ))
512                 while [ $OFFSETTIME -gt 0 ]; do
513                         CDDBCKSUM=$(($CDDBCKSUM + $OFFSETTIME % 10))
514                         OFFSETTIME=$(($OFFSETTIME / 10))
515                 done
516
517         done
518
519         COOKEDOFFSETS="${COOKEDOFFSETS:1}"  # eat the leading space
520
521         PREGAP=$(echo $OFFSETS | cut -f1 -d' ')
522         TOTALTIME=$(( (($LEADOUT + $LEADIN + $PREGAP) / $CDFRAMES) - (($LEADIN + $PREGAP) / $CDFRAMES)))
523
524         vvecho "makeids: PREGAP $PREGAP, LEADIN $LEADIN, LEADOUT $LEADOUT"
525
526         # Calculate both the cddb discid *and* the musicbrainz discid
527         # now. We'll use the cddb discid for reference in most cases
528         # for consistency, but we also have the musicbrainz discid for
529         # when we need it
530         printf -v CDDBDISCID "%08lx" $(( ($CDDBCKSUM % 0xff) * 16777216 | $TOTALTIME * 256 | $TRACKS))
531         CDDBTRACKINFO="${CDDBDISCID} $((TRACKS)) ${COOKEDOFFSETS} $((($LEADOUT + $LEADIN + $IDMAGICNUM) / $CDFRAMES))"
532
533         case $CDDBMETHOD in
534                 *musicbrainz*)
535                         # FIXME: don't assume the first track is 1
536                         MBDISCID=$($MUSICBRAINZ --command calcid --discinfo 1 $TRACKS $LEADIN $(($PREGAP + $LEADOUT)) $OFFSETS)
537                         error=$?
538                         if [ $error != 0 ]; then
539                                 log error "$MUSICBRAINZ failed to run; ABORT"
540                                 exit $error
541                         fi
542                         MBTRACKINFO="${MBDISCID} $((TRACKS)) ${COOKEDOFFSETS} $((($LEADOUT + $LEADIN + $IDMAGICNUM) / $CDFRAMES))"
543                         ;;
544         esac
545 }
546
547 do_replaygain()
548 {
549         if checkstatus replaygain; then :; else
550                 run_command "" echo "Adding replaygain information..."
551                 for TMPOUTPUT in $( echo "$OUTPUTTYPE" | tr , \ )
552                 do
553                         case $TMPOUTPUT in
554                                 vorbis|ogg)
555                                         OUTPUT=$OGGOUTPUTCONTAINER
556                                         ;;
557                                 opus)
558                                         OUTPUT=$OPUSOUTPUTCONTAINER
559                                         ;;
560                                 flac)
561                                         OUTPUT=$FLACOUTPUTCONTAINER
562                                         ;;
563                                 *)
564                                         OUTPUT=$TMPOUTPUT
565                                         ;;
566                         esac
567                         OUTPUTFILES=""
568                         REPLAYINDEX=0
569                         for UTRACKNUM in $TRACKQUEUE
570                         do
571                                 CDDBTRACKNUM=$(expr $UTRACKNUM - 1) # Unpad
572                                 getcddbinfo TRACKNAME
573                                 splitvarious
574                                 TRACKFILE="$(mungetrackname "$TRACKNAME")"
575                                 ARTISTFILE="$(mungeartistname "$TRACKARTIST")"
576                                 ALBUMFILE="$(mungealbumname "$DALBUM")"
577                                 GENRE="$(mungegenre "$GENRE")"
578                                 YEAR=${CDYEAR:-$CDYEAR}
579                                 gettracknum
580                                 if [ "$ONETRACK" = "y" ]; then
581                                         if [ "$VARIOUSARTISTS" = "y" ]; then
582                                                 OUTPUTFILE="$(eval echo \""$VAONETRACKOUTPUTFORMAT"\")"
583                                         else
584                                                 OUTPUTFILE="$(eval echo \""$ONETRACKOUTPUTFORMAT"\")"
585                                         fi
586                                 else
587                                         if [ "$VARIOUSARTISTS" = "y" ]; then
588                                                 OUTPUTFILE="$(eval echo \""$VAOUTPUTFORMAT"\")"
589                                         else
590                                                 OUTPUTFILE="$(eval echo \""$OUTPUTFORMAT"\")"
591                                         fi
592                                 fi
593                                 OUTPUTFILES[$REPLAYINDEX]="$OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
594                                 (( REPLAYINDEX = $REPLAYINDEX + 1 ))
595                         done
596                         case "$OUTPUT" in
597                                 flac)
598                                         run_command "replaygain-flac" nice $ENCNICE $METAFLAC $FLACGAINOPTS "${OUTPUTFILES[@]}"
599                                         ;;
600                                 vorbis|ogg)
601                                         run_command "replaygain-vorbis" nice $ENCNICE $VORBISGAIN $VORBISGAINOPTS "${OUTPUTFILES[@]}"
602                                         ;;
603                                 mp3)
604                                         run_command "replaygain-mp3" nice $ENCNICE $MP3GAIN $MP3GAINOPTS "${OUTPUTFILES[@]}"
605                                         ;;
606                                 mpc)
607                                         run_command "replaygain-mpc" nice $ENCNICE $MPCGAIN "${OUTPUTFILES[@]}"
608                                         ;;
609                                 wv)
610                                         run_command "replaygain-wv" nice $ENCNICE $WVGAIN $WVGAINOPTS "${OUTPUTFILES[@]}"
611                                         ;;
612                                 *);;
613                         esac
614                 done
615                 if checkerrors "replaygain-.{3,6}"; then :; else
616                         run_command "replaygain" true
617                 fi
618         fi
619 }
620
621 # This code splits the a Various Artist track name from one of the following
622 # forms:
623 #
624 #  forward:        Artist / Track
625 #  forward-dash:   Artist - Track
626 #  reverse:        Track / Artist
627 #  reverse-dash:   Track - Artist
628 #  colon:          Artist: Track
629 #  trailing-paren: Artist (Track)
630 #
631 # variables used:
632 # VARIOUSARTISTS, VARIOUSARTISTSTYLE, TRACKNAME, TRACKARTIST
633 splitvarious ()
634 {
635         if [ "$VARIOUSARTISTS" = "y" ] && [ ! "$ONETRACK" = "y" ]; then
636                 case "$VARIOUSARTISTSTYLE" in
637                 forward)
638                         DTITLEARTIST="$(echo "$TRACKNAME" | sed 's- / -~-g')"
639                         TRACKARTIST="$(echo "$DTITLEARTIST" | cut -f1 -d~)"
640                         TRACKNAME="$(echo "$DTITLEARTIST" | cut -f2 -d~)"
641                         ;;
642                 forward-dash)
643                         DTITLEARTIST="$(echo "$TRACKNAME" | sed 's, - ,~,g')"
644                         TRACKARTIST="$(echo "$DTITLEARTIST" | cut -f1 -d~)"
645                         TRACKNAME="$(echo "$DTITLEARTIST" | cut -f2 -d~)"
646                         ;;
647                 reverse)
648                         DTITLEARTIST="$(echo "$TRACKNAME" | sed 's- / -~-g')"
649                         TRACKARTIST="$(echo "$DTITLEARTIST" | cut -f2 -d~)"
650                         TRACKNAME="$(echo "$DTITLEARTIST" | cut -f1 -d~)"
651                         ;;
652                 reverse-dash)
653                         DTITLEARTIST="$(echo "$TRACKNAME" | sed 's, - ,~,g')"
654                         TRACKARTIST="$(echo "$DTITLEARTIST" | cut -f2 -d~)"
655                         TRACKNAME="$(echo "$DTITLEARTIST" | cut -f1 -d~)"
656                         ;;
657                 colon)
658                         DTITLEARTIST="$(echo "$TRACKNAME" | sed 's-: -~-g')"
659                         TRACKARTIST="$(echo "$DTITLEARTIST" | cut -f1 -d~)"
660                         TRACKNAME="$(echo "$DTITLEARTIST" | cut -f2 -d~)"
661                         ;;
662                 trailing-paren)
663                         DTITLEARTIST="$(echo "$TRACKNAME" | sed 's,^\(.*\) (\(.*\)),\1~\2,')"
664                         TRACKARTIST="$(echo "$DTITLEARTIST" | cut -f2 -d~)"
665                         TRACKNAME="$(echo "$DTITLEARTIST" | cut -f1 -d~)"
666                         ;;
667                 esac
668         elif [ "$VARIOUSARTISTS" = "y" ] && [ "$ONETRACK" = "y" ]; then
669                 TRACKARTIST="Various"
670         else
671                 TRACKARTIST="$DARTIST"
672         fi
673 }
674
675 do_getgenreid () {
676         local genre=$(echo "${@}" | tr 'A-Z' 'a-z')
677         local id=""
678         case ${genre} in
679                 "blues")                 id=0 ;;
680                 "classic rock")          id=1 ;;
681                 "country")               id=2 ;;
682                 "dance")                 id=3 ;;
683                 "disco")                 id=4 ;;
684                 "funk")                  id=5 ;;
685                 "grunge")                id=6 ;;
686                 "hip-hop")               id=7 ;;
687                 "jazz")                  id=8 ;;
688                 "metal")                 id=9 ;;
689                 "new age")               id=10 ;;
690                 "oldies")                id=11 ;;
691                 "other")                 id=12 ;;
692                 "pop")                   id=13 ;;
693                 "r&b")                   id=14 ;;
694                 "rap")                   id=15 ;;
695                 "reggae")                id=16 ;;
696                 "rock")                  id=17 ;;
697                 "techno")                id=18 ;;
698                 "industrial")            id=19 ;;
699                 "alternative")           id=20 ;;
700                 "ska")                   id=21 ;;
701                 "death metal")           id=22 ;;
702                 "pranks")                id=23 ;;
703                 "soundtrack")            id=24 ;;
704                 "euro-techno")           id=25 ;;
705                 "ambient")               id=26 ;;
706                 "trip-hop")              id=27 ;;
707                 "vocal")                 id=28 ;;
708                 "jazz+funk")             id=29 ;;
709                 "fusion")                id=30 ;;
710                 "trance")                id=31 ;;
711                 "classical")             id=32 ;;
712                 "instrumental")          id=33 ;;
713                 "acid")                  id=34 ;;
714                 "house")                 id=35 ;;
715                 "game")                  id=36 ;;
716                 "sound clip")            id=37 ;;
717                 "gospel")                id=38 ;;
718                 "noise")                 id=39 ;;
719                 "alt. rock")             id=40 ;;
720                 "bass")                  id=41 ;;
721                 "soul")                  id=42 ;;
722                 "punk")                  id=43 ;;
723                 "space")                 id=44 ;;
724                 "meditative")            id=45 ;;
725                 "instrum. pop")          id=46 ;;
726                 "instrum. rock")         id=47 ;;
727                 "ethnic")                id=48 ;;
728                 "gothic")                id=49 ;;
729                 "darkwave")              id=50 ;;
730                 "techno-indust.")        id=51 ;;
731                 "electronic")            id=52 ;;
732                 "pop-folk")              id=53 ;;
733                 "eurodance")             id=54 ;;
734                 "dream")                 id=55 ;;
735                 "southern rock")         id=56 ;;
736                 "comedy")                id=57 ;;
737                 "cult")                  id=58 ;;
738                 "gangsta")               id=59 ;;
739                 "top 40")                id=60 ;;
740                 "christian rap")         id=61 ;;
741                 "pop/funk"|"pop / funk") id=62 ;;
742                 "jungle")                id=63 ;;
743                 "native american")       id=64 ;;
744                 "cabaret")               id=65 ;;
745                 "new wave")              id=66 ;;
746                 "psychadelic")           id=67 ;;
747                 "rave")                  id=68 ;;
748                 "showtunes")             id=69 ;;
749                 "trailer")               id=70 ;;
750                 "lo-fi")                 id=71 ;;
751                 "tribal")                id=72 ;;
752                 "acid punk")             id=73 ;;
753                 "acid jazz")             id=74 ;;
754                 "polka")                 id=75 ;;
755                 "retro")                 id=76 ;;
756                 "musical")               id=77 ;;
757                 "rock & roll")           id=78 ;;
758                 "hard rock")             id=79 ;;
759                 "folk")                  id=80 ;;
760                 "folk/rock")             id=81 ;;
761                 "national folk")         id=82 ;;
762                 "swing")                 id=83 ;;
763                 "fusion")                id=84 ;;
764                 "bebob")                 id=85 ;;
765                 "latin")                 id=86 ;;
766                 "revival")               id=87 ;;
767                 "celtic")                id=88 ;;
768                 "bluegrass")             id=89 ;;
769                 "avantgarde")            id=90 ;;
770                 "gothic rock")           id=91 ;;
771                 "progress. rock")        id=92 ;;
772                 "psychadel. rock")       id=93 ;;
773                 "symphonic rock")        id=94 ;;
774                 "slow rock")             id=95 ;;
775                 "big band")              id=96 ;;
776                 "chorus")                id=97 ;;
777                 "easy listening")        id=98 ;;
778                 "acoustic")              id=99 ;;
779                 "humour")                id=100 ;;
780                 "speech")                id=101 ;;
781                 "chanson")               id=102 ;;
782                 "opera")                 id=103 ;;
783                 "chamber music")         id=104 ;;
784                 "sonata")                id=105 ;;
785                 "symphony")              id=106 ;;
786                 "booty bass")            id=107 ;;
787                 "primus")                id=108 ;;
788                 "porn groove")           id=109 ;;
789                 "satire")                id=110 ;;
790                 "slow jam")              id=111 ;;
791                 "club")                  id=112 ;;
792                 "tango")                 id=113 ;;
793                 "samba")                 id=114 ;;
794                 "folklore")              id=115 ;;
795                 "ballad")                id=116 ;;
796                 "power ballad")          id=117 ;;
797                 "rhythmic soul")         id=118 ;;
798                 "freestyle")             id=119 ;;
799                 "duet")                  id=120 ;;
800                 "punk rock")             id=121 ;;
801                 "drum solo")             id=122 ;;
802                 "a capella")             id=123 ;;
803                 "euro-house")            id=124 ;;
804                 "dance hall")            id=125 ;;
805                 "goa")                   id=126 ;;
806                 "drum & bass")           id=127 ;;
807                 "club-house")            id=128 ;;
808                 "hardcore")              id=129 ;;
809                 "terror")                id=130 ;;
810                 "indie")                 id=131 ;;
811                 "britpop")               id=132 ;;
812                 "negerpunk")             id=133 ;;
813                 "polsk punk")            id=134 ;;
814                 "beat")                  id=135 ;;
815                 "christian gangsta rap") id=136 ;;
816                 "heavy metal")           id=137 ;;
817                 "black metal")           id=138 ;;
818                 "crossover")             id=139 ;;
819                 "contemporary christian")id=140 ;;
820                 "christian rock")        id=141 ;;
821                 "merengue")              id=142 ;;
822                 "salsa")                 id=143 ;;
823                 "thrash metal")          id=144 ;;
824                 "anime")                 id=145 ;;
825                 "jpop")                  id=146 ;;
826                 "synthpop")              id=147 ;;
827                 "rock/pop"|"rock / pop") id=148 ;;
828                 *)                       id=255 ;;
829         esac
830 echo ${id}
831 return 0
832 }
833
834 # do_tag [tracknumber]
835 # id3 tags a filename
836 # variables used:
837 # TRACKS, TRACKNAME, TRACKARTIST, TAGGER, TAGGEROPTS, VORBISCOMMENT, METAFLAC,
838 # COMMENT, DALBUM, DARTIST, CDYEAR, CDGENRE
839 do_tag ()
840 {
841         COMMENTOUTPUT="$(eval echo "${COMMENT}")"
842         if [ -z "$COMMENTOUTPUT" ]; then
843                 COMMENTOUTPUT="$(getcddbinfo TRACK-INFO)"
844         fi
845         CDDBDISCID=$(echo "$CDDBTRACKINFO" | cut -d' ' -f1)
846         run_command "" echo "Tagging track $1 of $TRACKS: $TRACKNAME..."
847         # If we want to start the tracks with a given number, we need to modify the
848         # TRACKNUM value before evaluation
849         if [ -n "$STARTTRACKNUMBERTAG" ] ; then
850                 gettracknum
851         fi
852         for OUTPUT in $(echo "$OUTPUTTYPE" | tr , \ )
853         do
854                 case "$OUTPUT" in
855                 mp3)
856                         # id3v2 v0.1.9 claims to have solved the -c bug, so we merge both id3 and id3v2
857                         GENREID=$(do_getgenreid "${CDGENRE}")
858                         # Set TPE2 in case we have a Various Artists rip.
859                         TPE2=""
860                         if [ "$VARIOUSARTISTS" = "y" ]; then
861                                 TPE2="Various"
862                         fi
863
864                         case "$ID3SYNTAX" in
865                                 id3)
866                                         run_command "tagtrack-$OUTPUT-$1" nice $ENCNICE \
867                                                 $TAGGER $TAGGEROPTS -c "$COMMENTOUTPUT" \
868                                                 -A "$DALBUM" -a "$TRACKARTIST" -t "$TRACKNAME" \
869                                                 -y "$CDYEAR" -g "$GENREID" \
870                                                 -T "${TRACKNUM:-$1}" \
871                                                 "${ABCDETEMPDIR}/track$1.$OUTPUT"
872                                         ;;
873                                 id3v2)
874                                         # FIXME # track numbers in mp3 come with 1/10, so we cannot
875                                         # happily substitute them with $TRACKNUM
876                                         run_command "tagtrack-$OUTPUT-$1" nice $ENCNICE \
877                                                 $TAGGER $TAGGEROPTS -c "$COMMENTOUTPUT" \
878                                                 -A "$DALBUM" -a "$TRACKARTIST" -t "$TRACKNAME" \
879                                                 -y "$CDYEAR" -g "$GENREID" \
880                                                 -T "${TRACKNUM:-$1}/$TRACKS" \
881                                                 ${TPE2:+--TPE2 "$TPE2"} \
882                                                 "${ABCDETEMPDIR}/track$1.$OUTPUT"
883                                         ;;
884                                 id3tag)
885                                         # FIXME # track numbers in mp3 come with 1/10, so we cannot
886                                         # happily substitute them with $TRACKNUM
887                                         run_command "tagtrack-$OUTPUT-$1" nice $ENCNICE \
888                                                 $TAGGER $TAGGEROPTS -c "$COMMENTOUTPUT" \
889                                                 -A "$DALBUM" -a "$TRACKARTIST" -s "$TRACKNAME" \
890                                                 -y "$CDYEAR" -g "$GENREID" \
891                                                 -t "${TRACKNUM:-$1}" ${TRACKNUM:+-T "$TRACKS"} \
892                                                 "${ABCDETEMPDIR}/track$1.$OUTPUT"
893                                         ;;
894                                 eyed3*)
895                                         # FIXME # track numbers in mp3 come with 1/10, so we cannot
896                                         # happily substitute them with $TRACKNUM
897                                         case "$ID3SYNTAX" in
898                                                 eyed3_06) addopts=( \
899                                                         ${ENCODING:+--set-encoding="$ENCODING"} \
900                                                         ${TPE2:+--set-text-frame=TPE2:"$TPE2"} \
901                                         # We set 'recording-date' so the date tag will show 
902                                         # in Audacious, vlc and friends...          Andrew.
903                                                         ${CDYEAR:+--set-text-frame="TDRC:$CDYEAR"} \
904                                                         ${COMMENTOUTPUT:+--comment=::"$COMMENTOUTPUT"} \
905                                                         );;
906                                                 *) addopts=( \
907                                                         ${ENCODING:+--encoding="$ENCODING"} \
908                                                         ${TPE2:+--text-frame=TPE2:"$TPE2"} \
909                                         # We set 'recording-date' so the date tag will show 
910                                         # in Audacious, vlc and friends...           Andrew.
911                                                         ${CDYEAR:+--text-frame="TDRC:$CDYEAR"} \
912                                                         ${COMMENTOUTPUT:+--comment "$COMMENTOUTPUT"} \
913                                                         );;
914                                         esac
915                                         run_command "tagtrack-$OUTPUT-$1" nice $ENCNICE $TAGGER $TAGGEROPTS \
916                                                 -A "$DALBUM" \
917                                                 -a "$TRACKARTIST" -t "$TRACKNAME" \
918                                                 -G "$GENREID" -n "${TRACKNUM:-$1}" \
919                                                 ${TRACKNUM:+-N "$TRACKS"} \
920                                                 "${addopts[@]}" \
921                                                 "${ABCDETEMPDIR}/track$1.$OUTPUT"
922                                         ;;
923                                 *)
924                                         log error "Internal error: ID3SYNTAX has an illegal value"
925                                         exit 1
926                                         ;;
927                         esac
928                         ;;
929                 vorbis|ogg)
930                         case "$OGGENCODERSYNTAX" in
931                                 vorbize|oggenc)
932                                         # vorbiscomment can't do in-place modification, mv the file first
933                                         if [ -f "${ABCDETEMPDIR}/track$1.$OGGOUTPUTCONTAINER" ] && \
934                                                    [ ! -f "${ABCDETEMPDIR}/track$1.uncommented.$OGGOUTPUTCONTAINER" ]; then
935                                                 mv "${ABCDETEMPDIR}/track$1.$OGGOUTPUTCONTAINER" \
936                                                    "${ABCDETEMPDIR}/track$1.uncommented.$OGGOUTPUTCONTAINER"
937                                         fi
938                                         (
939                                         # These are from
940                                         # https://www.xiph.org/vorbis/doc/v-comment.html
941
942                                         echo ARTIST="$TRACKARTIST"
943                                         echo ALBUM="$DALBUM"
944                                         echo TITLE="$TRACKNAME"
945                                         if [ -n "$CDYEAR" ]; then
946                                                 echo DATE="$CDYEAR"
947                                         fi
948                                         if [ -n "$CDGENRE" ]; then
949                                                 echo GENRE="$CDGENRE"
950                                         fi
951                                         echo TRACKNUMBER="${TRACKNUM:-$1}"
952                                         # TRACKTOTAL is not in the proposed, minimal list of standard field names from
953                                         # xiph.org: https://www.xiph.org/vorbis/doc/v-comment.html but is in common usage
954                                         # and read by mediainfo, ffprobe, vlc, Aqualung, ogg123, Foobar. And now abcde :)
955                                         # The tag is quietly ignored by  Audacious, MPlayer, mpv, XMMS....
956                                         echo TRACKTOTAL="${TRACKS}"
957                                         if [ -n "$DISCNUMBER" ]; then
958                                                 echo DISCNUMBER="$DISCNUMBER"
959                                         fi
960                                         echo CDDB="${CDDBDISCID}"
961                                         if [ "$(eval echo ${COMMENT})" != "" ]; then
962                                                 case "$COMMENTOUTPUT" in
963                                                         *=*) echo "$COMMENTOUTPUT";;
964                                                         *)   echo COMMENT="$COMMENTOUTPUT";;
965                                                 esac
966                                         fi
967                                         ) | run_command "tagtrack-$OUTPUT-$1" nice $ENCNICE \
968                                                 $VORBISCOMMENT $VORBISCOMMENTOPTS -w \
969                                                 "${ABCDETEMPDIR}/track$1.uncommented.$OGGOUTPUTCONTAINER" \
970                                                 "${ABCDETEMPDIR}/track$1.$OGGOUTPUTCONTAINER"
971                                         # Doublecheck that the commented file was created
972                                         # successfully before wiping the original
973                                         if [ -f "${ABCDETEMPDIR}/track$1.$OGGOUTPUTCONTAINER" ]; then
974                                                 rm -f "${ABCDETEMPDIR}/track$1.uncommented.$OGGOUTPUTCONTAINER"
975                                         else
976                                                 mv "${ABCDETEMPDIR}/track$1.uncommented.$OGGOUTPUTCONTAINER" \
977                                                    "${ABCDETEMPDIR}/track$1.$OGGOUTPUTCONTAINER"
978                                         fi
979                                         ;;
980                         esac
981                         ;;
982                 opus)
983                         run_command "tagtrack-$OUTPUT-$1" true
984                         ;;
985                 mka)
986                         run_command "tagtrack-$OUTPUT-$1" true
987                         ;;
988                 aiff)
989                         run_command "tagtrack-$OUTPUT-$1" true
990                         ;;
991                 flac)
992                         (
993                         echo ARTIST="$TRACKARTIST"
994                         echo ALBUM="$DALBUM"
995                         echo TITLE="$TRACKNAME"
996                         if [ -n "$CDYEAR" ]; then
997                                 echo DATE="$CDYEAR"
998                         fi
999                         if [ -n "$CDGENRE" ]; then
1000                                 echo GENRE="$CDGENRE"
1001                         fi
1002                         echo TRACKNUMBER="${TRACKNUM:-$1}"
1003                         # TRACKTOTAL is not in the proposed, minimal list of standard field names from
1004                         # xiph.org: https://www.xiph.org/vorbis/doc/v-comment.html but is in common usage
1005                         # and read by mediainfo, ffprobe, vlc, Aqualung, ogg123, Foobar. And now abcde :)
1006                         # The tag is quietly ignored by  Audacious, MPlayer, mpv, XMMS....
1007                         echo TRACKTOTAL="${TRACKS}"
1008                         if [ -n "$DISCNUMBER" ]; then
1009                                 echo DISCNUMBER="$DISCNUMBER"
1010                         fi
1011                         echo CDDB="${CDDBDISCID}"
1012                         if [ -n "$(eval echo "${COMMENT}")" ]; then
1013                                 case "$COMMENTOUTPUT" in
1014                                         *=*) echo "$COMMENTOUTPUT";;
1015                                         *)   echo COMMENT="$COMMENTOUTPUT";;
1016                                 esac
1017                         fi
1018                         ) | run_command "tagtrack-$OUTPUT-$1" nice $ENCNICE $METAFLAC $METAFLACOPTS \
1019                                                         ${IMPORTCUESHEET:+--import-cuesheet-from="${ABCDETEMPDIR}/$CUEFILE"} \
1020                                                         --import-tags-from=- "${ABCDETEMPDIR}/track$1.$FLACOUTPUTCONTAINER"
1021                         ;;
1022                 spx)
1023                         run_command "tagtrack-$OUTPUT-$1" true
1024                         ;;
1025                 mpc)
1026                         run_command "tagtrack-$OUTPUT-$1" true
1027                         ;;
1028                 wv)
1029                         run_command "tagtrack-$OUTPUT-$1" true
1030                         ;;
1031                 ape)
1032                         # This tagging syntax is suitable for Robert Muth's application 'apetag', the Monkey's Audio 
1033                         # Console port (mac) used for encoding does not have the ability to tag.
1034                         run_command "tagtrack-$OUTPUT-$1" nice $ENCNICE "$APETAG" -i "${ABCDETEMPDIR}/track$1.ape" -m overwrite \
1035                                                 -p artist="$TRACKARTIST" -p album="$DALBUM" -p title="$TRACKNAME" -p track="${TRACKNUM:-$1}" \
1036                                                 -p year="$CDYEAR" -p genre="$CDGENRE" ${COMMENTOUTPUT:+-p comment="$COMMENTOUTPUT"} 
1037                         ;;
1038                 mp2)
1039                         # Using Mutagen's mid3v2 for tagging with id3v2.4.0. Interesting enough vlc, MPlayer and XMMS ignore
1040                         # these tags but they are read by Audacious, Xine, Aqualung, mediainfo, ffplay, ffprobe. FFmpeg does
1041                         # not currently tag mp2 audio so twolame and FFmpeg will both use mid3v2...
1042                         run_command "tagtrack-$OUTPUT-$1" nice $ENCNICE "$MID3V2" --verbose -A "$DALBUM" \
1043                                                 -a "$TRACKARTIST" -t "$TRACKNAME" -y "$CDYEAR" -g "$CDGENRE" \
1044                                                 -T "${TRACKNUM:-$1}/$TRACKS" ${TPE2:+--TPE2 "$TPE2"} \
1045                                                 ${COMMENTOUTPUT:+--comment="$COMMENTOUTPUT"} \
1046                                                 "${ABCDETEMPDIR}/track$1.mp2"
1047                         ;;
1048                 aac)
1049                         run_command "tagtrack-$OUTPUT-$1" true
1050                         ;;
1051                 m4a)
1052                         case "$AACENCODERSYNTAX" in
1053                                 fdkaac)
1054                                         # We will use inline tagging...
1055                                         run_command "tagtrack-$OUTPUT-$1" true
1056                                         ;;
1057                                 neroAacEnc)
1058                                         # Tag post encode with neroAacTag...
1059                                         run_command "tagtrack-$OUTPUT-$1" nice $ENCNICE "$NEROAACTAG" \
1060                                                                 "${ABCDETEMPDIR}/track$1.m4a" \
1061                                                                 -meta:artist="$TRACKARTIST" -meta:album="$DALBUM" \
1062                                                                 -meta:title="$TRACKNAME" -meta:track="${TRACKNUM:-$1}" \
1063                                                                 -meta:year="$CDYEAR" -meta:genre="$CDGENRE" -meta:comment="$COMMENT"
1064                                         ;;
1065                                 faac)
1066                                         run_command "tagtrack-$OUTPUT-$1" true
1067                                         ;;
1068                                 qaac)
1069                                         run_command "tagtrack-$OUTPUT-$1" true
1070                                         ;;
1071                                 fhgaacenc)
1072                                         # Tag post encode with AtomicParsley. Note that previous problems with seg fault when using
1073                                         # 'overWrite' cannot be reproduced with newer versions: https://bitbucket.org/wez/atomicparsley
1074                                         run_command "tagtrack-$OUTPUT-$1" nice $ENCNICE "$ATOMICPARSLEY" \
1075                                                                 "${ABCDETEMPDIR}/track$1.m4a" --artist="$TRACKARTIST" --album="$DALBUM" \
1076                                                                 --title="$TRACKNAME" --tracknum="${TRACKNUM:-$1}" \
1077                                                                 --year="$CDYEAR" --genre="$CDGENRE" --comment="$COMMENT" \
1078                                                                 $ATOMICPARSLEYOPTS --overWrite
1079                                         ;;
1080                                 ffmpeg)
1081                                         run_command "tagtrack-$OUTPUT-$1" true
1082                                 ;;
1083                         esac
1084                         ;;
1085                 tta)
1086                         # We use mid3v2 tagging for True Audio:
1087                         run_command "tagtrack-$OUTPUT-$1" nice $ENCNICE "$MID3V2" --verbose -A "$DALBUM" \
1088                                                 -a "$TRACKARTIST" -t "$TRACKNAME" -y "$CDYEAR" -g "$CDGENRE" \
1089                                                 -T "${TRACKNUM:-$1}/$TRACKS" ${TPE2:+--TPE2 "$TPE2"} \
1090                                                 ${COMMENTOUTPUT:+--comment="$COMMENTOUTPUT"} "${ABCDETEMPDIR}/track$1.tta"
1091                         ;;
1092                 wav)
1093                         run_command "tagtrack-$OUTPUT-$1" true
1094                         ;;
1095                 esac
1096         done
1097         if checkerrors "tagtrack-(.{3,6})-$1"; then :; else
1098                 run_command "tagtrack-$1" true
1099         fi
1100 }
1101
1102 # do_nogap_encode
1103 # variables used:
1104 # OUTPUTTYPE, {FOO}ENCODERSYNTAX, ENCNICE, ENCODER, ENCODEROPTS
1105 do_nogap_encode ()
1106 {
1107         # The commands here don't go through run_command because they're never
1108         # supposed to be silenced
1109         echo "Encoding gapless MP3 tracks: $TRACKQUEUE"
1110         for OUTPUT in $(echo "$OUTPUTTYPE" | tr , \ )
1111         do
1112                 case "$OUTPUT" in
1113                 mp3)
1114                         case "$MP3ENCODERSYNTAX" in
1115                         lame)
1116                                 (
1117                                         cd "${ABCDETEMPDIR}"
1118                                         TRACKFILES=
1119                                         for THISTRACKNUM in $TRACKQUEUE
1120                                         do
1121                                                 TRACKFILES="$TRACKFILES track$THISTRACKNUM.wav"
1122                                         done
1123                                         nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS --nogap $TRACKFILES
1124                                         RETURN=$?
1125                                         if [ "$RETURN" != "0" ]; then
1126                                                 echo "nogap-encode: $MP3ENCODER returned code $RETURN" >> "${ABCDETEMPDIR}/errors"
1127                                         else
1128                                                 for THISTRACKNUM in $TRACKQUEUE
1129                                                 do
1130                                                         run_command "encodetrack-$OUTPUT-$THISTRACKNUM" true
1131                                                 done
1132                                         fi
1133                                 )
1134                                 ;;
1135                         esac
1136                         ;;
1137                 esac
1138         done
1139         if checkerrors "nogap-encode"; then :; else
1140                 if [ ! "$KEEPWAVS" = "y" ] ; then
1141                         if [ ! "$KEEPWAVS" = "move" ] ; then
1142                                 rm -f "$IN"
1143                         fi
1144                 fi
1145         fi
1146         # Other encoders fall through to normal encoding as the tracks have not
1147         # been entered in the status file.
1148 }
1149
1150 # do_encode [tracknumber] [hostname]
1151 # If no hostname is specified, encode locally
1152 # variables used:
1153 # TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, 
1154 # DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
1155 do_encode ()
1156 {
1157         if [ "$USEPIPES" = "y" ]; then
1158                 case "$OUTPUT" in
1159                         mp3)
1160                                 TEMPARG="PIPE_$MP3ENCODERSYNTAX"
1161                                 ;;
1162                         vorbis|ogg)
1163                                 TEMPARG="PIPE_$OGGENCODERSYNTAX"
1164                                 ;;
1165                         opus)
1166                                 TEMPARG="PIPE_$OPUSENCODERSYNTAX"
1167                                 ;;
1168                         mka)
1169                                 TEMPARG="PIPE_$MKAENCODERSYNTAX"
1170                                 ;;
1171                         aiff)
1172                                 TEMPARG="PIPE_$AIFFENCODERSYNTAX"
1173                                 ;;
1174                         flac)
1175                                 TEMPARG="PIPE_$FLACENCODERSYNTAX"
1176                                 ;;
1177                         spx)
1178                                 TEMPARG="PIPE_$SPEEXENCODER"
1179                                 ;;
1180                         mpc)
1181                                 TEMPARG="PIPE_$MPCENCODER"
1182                                 ;;
1183                         wv)
1184                                 TEMPARG="PIPE_$WVENCODERSYNTAX"
1185                                 ;;
1186                         tta)
1187                                 TEMPARG="PIPE_$TTAENCODERSYNTAX"
1188                                 ;;
1189                         aac)
1190                                 TEMPARG="PIPE_$AACENCODERSYNTAX"
1191                                 ;;
1192                         m4a)
1193                                 TEMPARG="PIPE_$AACENCODERSYNTAX"
1194                                 ;;
1195                 esac
1196                 IN="$( eval echo "\$$TEMPARG" )"
1197         else
1198                 IN="${ABCDETEMPDIR}/track$1.wav"
1199         fi
1200         # We need IN to proceed, if we are not using pipes.
1201         if [ -s "$IN" ] || [ X"$USEPIPES" = "Xy" ] ; then
1202                 for TMPOUTPUT in $(echo "$OUTPUTTYPE" | tr , \ )
1203                 do
1204                         case "$TMPOUTPUT" in
1205                                 vorbis|ogg)
1206                                         OUTPUT=$OGGOUTPUTCONTAINER
1207                                         ;;
1208                                 opus)
1209                                         OUTPUT=$OPUSOUTPUTCONTAINER
1210                                         ;;
1211                                 mka)
1212                                         OUTPUT=$MKAOUTPUTCONTAINER
1213                                         ;;
1214                                 aiff)
1215                                         OUTPUT=$AIFFOUTPUTCONTAINER
1216                                         ;;
1217                                 flac)
1218                                         OUTPUT=$FLACOUTPUTCONTAINER
1219                                         ;;
1220                                 *)
1221                                         OUTPUT=$TMPOUTPUT
1222                                         ;;
1223                         esac
1224                         OUT="${ABCDETEMPDIR}/track$1.$OUTPUT"
1225                         if [ "$NOGAP" = "y" ] && checkstatus "encodetrack-$OUTPUT-$1" ; then
1226                                 continue
1227                         fi
1228                         if [ X"$USEPIPES" = "Xy" ]; then
1229                                 RUN_COMMAND=""
1230                                 # We need a way to store the creation of the files when using PIPES
1231                                 RUN_COMMAND_PIPES="run_command encodetrack-$OUTPUT-$1 true"
1232                                 # When piping it does not make sense to have a higher nice for
1233                                 # reading than for encoding, since it will be hold by the
1234                                 # encoding process. Setting an effective nice, to calm down a
1235                                 # bit the reading process.
1236                                 EFFECTIVE_NICE=$READNICE
1237                         else
1238                                 run_command "" echo "Encoding track $1 of $TRACKS: $TRACKNAME..."
1239                                 RUN_COMMAND="run_command encodetrack-$OUTPUT-$1"
1240                                 EFFECTIVE_NICE=$ENCNICE
1241                         fi
1242                         case "$OUTPUT" in
1243                         mp3)
1244                                 case "$2" in
1245                                 %local*%)
1246                                         case "$MP3ENCODERSYNTAX" in
1247                                         lame|gogo) $RUN_COMMAND nice $EFFECTIVE_NICE $MP3ENCODER $MP3ENCODEROPTS "$IN" "$OUT" ;;
1248                                         bladeenc) $RUN_COMMAND nice $EFFECTIVE_NICE $MP3ENCODER $MP3ENCODEROPTS -quit "$IN" "$OUT" ;;
1249                                         l3enc|xingmp3enc) $RUN_COMMAND nice $EFFECTIVE_NICE $MP3ENCODER "$IN" "$OUT" $MP3ENCODEROPTS ;;
1250                                         mp3enc) $RUN_COMMAND nice $EFFECTIVE_NICE $MP3ENCODER -if "$IN" -of "$OUT" $MP3ENCODEROPTS ;;
1251                                         esac
1252                                         ;;
1253                                 *)
1254                                         $RUN_COMMAND nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" >/dev/null 2>&1
1255                                         ;;
1256                                 esac
1257                                 ;;
1258                         vorbis|ogg)
1259                                 case "$2" in
1260                                 %local*%)
1261                                         case "$OGGENCODERSYNTAX" in
1262                                         vorbize) $RUN_COMMAND nice $EFFECTIVE_NICE $OGGENCODER $OGGENCODEROPTS -w "$OUT" "$IN" ;;
1263                                         oggenc) $RUN_COMMAND nice $EFFECTIVE_NICE $OGGENCODER $OGGENCODEROPTS -o "$OUT" "$IN" ;;
1264                                         esac
1265                                         ;;
1266                                 *)
1267                                         $RUN_COMMAND nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" >/dev/null 2>&1
1268                                         ;;
1269                                 esac
1270                                 ;;
1271                         opus)
1272                                 case "$2" in
1273                                 %local*%)
1274                                         case "$OPUSENCODERSYNTAX" in
1275                                         opusenc)
1276                                         # Tag the file at encode time, as it can't be done after encoding.
1277                                                 if [ "$DOTAG" = "y" ]; then
1278                                                         $RUN_COMMAND nice $EFFECTIVE_NICE $OPUSENCODER $OPUSENCODEROPTS \
1279                                                                                  --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" \
1280                                                                                  --genre "$CDGENRE" --date "$CDYEAR" --comment TRACKNUMBER="$1" \
1281                                                                                  ${COMMENT:+--comment COMMENT="$COMMENT"} "$IN" "$OUT"
1282                                                 else
1283                                                         $RUN_COMMAND nice $EFFECTIVE_NICE $OPUSENCODER $OPUSENCODEROPTS "$IN" "$OUT"
1284                                                 fi
1285                                         ;;
1286                                         esac
1287                                         ;;
1288                                 *)
1289                                         $RUN_COMMAND nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" >/dev/null 2>&1
1290                                         ;;
1291                                 esac
1292                                 ;;
1293                         mka)
1294                                 case "$MKAENCODERSYNTAX" in
1295                                 ffmpeg)
1296                                         if [ "$DOTAG" = "y" ]; then
1297                                                 $RUN_COMMAND nice $EFFECTIVE_NICE $MKAENCODER -i "$IN" $MKAENCODEROPTS \
1298                                                                          -metadata artist="$TRACKARTIST" -metadata album="$DALBUM" \
1299                                                                          -metadata title="$TRACKNAME" -metadata track="${TRACKNUM:-$1}" \
1300                                                                          -metadata date="$CDYEAR" -metadata genre="$CDGENRE" \
1301                                                                          -metadata comment="$COMMENT" "$OUT"
1302                                         else
1303                                                 $RUN_COMMAND nice $EFFECTIVE_NICE $MKAENCODER -i "$IN" $MKAENCODEROPTS "$OUT"
1304                                         fi
1305                                 ;;
1306                                 esac
1307                                 ;;
1308                         aiff)
1309                                 case "$AIFFENCODERSYNTAX" in
1310                                 ffmpeg)
1311                                         if [ "$DOTAG" = "y" ]; then
1312                                                 $RUN_COMMAND nice $EFFECTIVE_NICE $AIFFENCODER -i "$IN" $AIFFENCODEROPTS \
1313                                                                          -metadata artist="$TRACKARTIST" -metadata album="$DALBUM" \
1314                                                                          -metadata title="$TRACKNAME" -metadata track="${TRACKNUM:-$1}" \
1315                                                                          -metadata date="$CDYEAR" -metadata genre="$CDGENRE" \
1316                                                                          -metadata comment="$COMMENT" "$OUT"
1317                                         else
1318                                                 $RUN_COMMAND nice $EFFECTIVE_NICE $AIFFENCODER -i "$IN" $AIFFENCODEROPTS "$OUT"
1319                                         fi
1320                                 ;;
1321                                 esac
1322                                 ;;
1323                         flac)
1324                                 case "$2" in
1325                                 %local*%)
1326                                         case "$FLACENCODERSYNTAX" in
1327                                         flac) $RUN_COMMAND nice $EFFECTIVE_NICE $FLACENCODER -f $FLACENCODEROPTS -o "$OUT" "$IN" ;;
1328                                                 esac
1329                                                 ;;
1330                                         *)
1331                                                 vecho -n "DISTMP3:"
1332                                                 vecho "$DISTMP3 $DISTMP3OPTS $2 $IN $OUT >/dev/null 2>&1"
1333                                                 $RUN_COMMAND nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" > /dev/null 2>&1
1334                                         ;;
1335                                 esac
1336                                 ;;
1337                         spx)
1338                                 if [ -n "$(eval echo "${COMMENT}")" ]; then
1339                                         case "$COMMENT" in
1340                                                 *=*) ;;
1341                                                 *)   COMMENT="COMMENT=$COMMENT" ;;
1342                                         esac
1343                                 fi
1344                                 # Tag the file at encode time, as it can't be done after encoding.
1345                                 if [ "$DOTAG" = "y" ]; then
1346                                         $RUN_COMMAND nice $EFFECTIVE_NICE $SPEEXENCODER $SPEEXENCODEROPTS \
1347                                                                  --author "$TRACKARTIST" --title "$TRACKNAME" \
1348                                                                  ${COMMENT:+--comment "$COMMENT"} "$IN" "$OUT"
1349                                 else
1350                                         $RUN_COMMAND nice $EFFECTIVE_NICE $SPEEXENCODER $SPEEXENCODEROPTS "$IN" "$OUT"
1351                                 fi
1352                                 ;;
1353                         mpc)
1354                                 # Tag the file inline at encode time.
1355                                 if [ "$DOTAG" = "y" ]; then
1356                                         $RUN_COMMAND nice $EFFECTIVE_NICE $MPCENCODER $MPCENCODEROPTS \
1357                                                                  --artist "$TRACKARTIST" --album "$DALBUM" \
1358                                                                  --title "$TRACKNAME" --track "$1" --genre "$CDGENRE" \
1359                                                                  --year "$CDYEAR" ${COMMENT:+--comment "$COMMENT"} "$IN" "$OUT"
1360                                 else
1361                                         $RUN_COMMAND nice $EFFECTIVE_NICE $MPCENCODER $MPCENCODEROPTS "$IN" "$OUT"
1362                                 fi
1363                                 ;;
1364                         tta)
1365                                 case "$TTAENCODERSYNTAX" in
1366                                 # tta is the newer version with a small syntax change...
1367                                         tta)
1368                                         $RUN_COMMAND nice $EFFECTIVE_NICE $TTAENCODER -e $TTAENCODEROPTS "$IN" "$OUT"
1369                                         ;;
1370                                         ttaenc)
1371                                         $RUN_COMMAND nice $EFFECTIVE_NICE $TTAENCODER -e $TTAENCODEROPTS "$IN" -o "$OUT"
1372                                         ;;
1373                                 esac
1374                                 ;;
1375                         wv)
1376                         case "$WVENCODERSYNTAX" in
1377                                 wavpack)
1378                                         if [ "$DOTAG" = "y" ]; then
1379                                                 $RUN_COMMAND nice $EFFECTIVE_NICE $WVENCODER $WVENCODEROPTS \
1380                                                                          -w Artist="$TRACKARTIST" -w Album="$DALBUM" \
1381                                                                          -w Title="$TRACKNAME" -w Track="$1" -w Genre="$CDGENRE" \
1382                                                                          -w Year="$CDYEAR" ${COMMENT:+-w Comment="$COMMENT"} "$IN" -o "$OUT"
1383                                         else
1384                                                 $RUN_COMMAND nice $EFFECTIVE_NICE $WVENCODER $WVENCODEROPTS "$IN" -o "$OUT"
1385                                         fi
1386                                 ;;
1387                                 ffmpeg)
1388                                         if [ "$DOTAG" = "y" ]; then
1389                                                 $RUN_COMMAND nice $EFFECTIVE_NICE $WVENCODER -i "$IN" $WVENCODEROPTS \
1390                                                                          -metadata artist="$TRACKARTIST" \
1391                                                                          -metadata album="$DALBUM" -metadata title="$TRACKNAME" \
1392                                                                          -metadata track="${TRACKNUM:-$1}" -metadata date="$CDYEAR" \
1393                                                                          -metadata genre="$CDGENRE" -metadata comment="$COMMENT" "$OUT" 
1394                                         else 
1395                                                 $RUN_COMMAND nice $EFFECTIVE_NICE $WVENCODER -i "$IN" $WVENCODEROPTS "$OUT"
1396                                         fi
1397                                 ;;
1398                         esac
1399                                 ;;
1400                         ape)
1401                                 $RUN_COMMAND nice $EFFECTIVE_NICE $APENCODER "$IN" "$OUT" $APENCODEROPTS
1402                                 ;;
1403                         mp2)
1404                                 case "$MP2ENCODERSYNTAX" in
1405                                         twolame)
1406                                                 $RUN_COMMAND nice $EFFECTIVE_NICE $MP2ENCODER $MP2ENCODEROPTS "$IN" "$OUT"
1407                                                 ;;
1408                                         ffmpeg)
1409                                                 $RUN_COMMAND nice $EFFECTIVE_NICE $MP2ENCODER -i "$IN" $MP2ENCODEROPTS "$OUT" 
1410                                                 ;;
1411                                 esac
1412                                 ;;
1413                         aac)
1414                                 # aac container is only used to catch faac encoded files where faac 
1415                                 # is compiled without mp4 support (with libmp4v2).
1416                                 $RUN_COMMAND nice $EFFECTIVE_NICE $AACENCODER $AACENCODEROPTS -o "$OUT" "$IN"
1417                                 ;;
1418                         m4a)
1419                                 case "$AACENCODERSYNTAX" in
1420                                         faac)
1421                                                 if [ "$DOTAG" = "y" ]; then
1422                                                         $RUN_COMMAND nice $EFFECTIVE_NICE $AACENCODER $AACENCODEROPTS  \
1423                                                                                  --artist "$TRACKARTIST" --album "$DALBUM" \
1424                                                                                  --title "$TRACKNAME" --track "${TRACKNUM:-$1}" \
1425                                                                                  --year "$CDYEAR" --genre "$CDGENRE" --comment "$COMMENT" -o "$OUT" "$IN"
1426                                                 else 
1427                                                         $RUN_COMMAND nice $EFFECTIVE_NICE $AACENCODER $AACENCODEROPTS -o "$OUT" "$IN"   
1428                                                 fi
1429                                                 ;;
1430                                         neroAacEnc)
1431                                                 $RUN_COMMAND nice $EFFECTIVE_NICE $AACENCODER $AACENCODEROPTS -if "$IN" -of "$OUT"
1432                                                 ;;
1433                                         fdkaac)      
1434                                                 if [ "$DOTAG" = "y" ]; then
1435                                                         $RUN_COMMAND nice $EFFECTIVE_NICE $AACENCODER $AACENCODEROPTS \
1436                                                                                  --artist "$TRACKARTIST" --album "$DALBUM" \
1437                                                                                  --title "$TRACKNAME" --track "$1" --genre "$CDGENRE" \
1438                                                                                  --date "$CDYEAR" --comment "$COMMENT" "$IN" -o "$OUT"
1439                                                 else
1440                                                         $RUN_COMMAND nice $EFFECTIVE_NICE $AACENCODER $AACENCODEROPTS "$IN" -o "$OUT"
1441                                                 fi
1442                                                 ;;
1443                                         qaac)
1444                                                 if [ "$DOTAG" = "y" ]; then
1445                                                         $RUN_COMMAND nice $EFFECTIVE_NICE $WINE $AACENCODER $AACENCODEROPTS \
1446                                                                                  --artist "$TRACKARTIST" --album "$DALBUM" \
1447                                                                                  --title "$TRACKNAME" --track "${TRACKNUM:-$1}" \
1448                                                                                  --date "$CDYEAR" --genre "$CDGENRE" --comment "$COMMENT" -o "$OUT" "$IN"
1449                                                 else 
1450                                                         $RUN_COMMAND nice $EFFECTIVE_NICE $WINE $AACENCODER $AACENCODEROPTS -o "$OUT" "$IN"
1451                                                 fi
1452                                                 ;;
1453                                         fhgaacenc)
1454                                                 $RUN_COMMAND nice $EFFECTIVE_NICE $WINE $AACENCODER $AACENCODEROPTS "$IN" "$OUT"
1455                                                 ;;
1456                                         ffmpeg)
1457                                                 if [ "$DOTAG" = "y" ]; then
1458                                                         $RUN_COMMAND nice $EFFECTIVE_NICE $AACENCODER -i "$IN" \
1459                                                                                  $AACENCODEROPTS -metadata artist="$TRACKARTIST" \
1460                                                                                  -metadata album="$DALBUM" -metadata title="$TRACKNAME" \
1461                                                                                  -metadata track="${TRACKNUM:-$1}" -metadata date="$CDYEAR" \
1462                                                                                  -metadata genre="$CDGENRE" -metadata comment="$COMMENT" "$OUT" 
1463                                                 else 
1464                                                         $RUN_COMMAND nice $EFFECTIVE_NICE $AACENCODER -i "$IN"  $AACENCODEROPTS "$OUT"
1465                                                 fi
1466                                                 ;;
1467                                         esac
1468                                 ;;
1469                         wav)
1470                                 # In case of wav output we need nothing. Just keep the wavs.
1471                                 # But we need the following to allow full logging and subsequent 
1472                                 # successful cleaning of ${ABCDETEMPDIR}.
1473                                 echo "encodetrack-$OUTPUT-$UTRACKNUM" >> "${ABCDETEMPDIR}/status"
1474                                 ;;
1475                         esac
1476                         $RUN_COMMAND_PIPES
1477                 done
1478                 # Only remove .wav if the encoding succeeded
1479                 if checkerrors "encodetrack-(.{3,6})-$1"; then :; else
1480                         run_command "encodetrack-$1" true
1481                         if [ ! "$KEEPWAVS" = "y" ] ; then
1482                                 if [ ! "$KEEPWAVS" = "move" ] ; then
1483                                         rm -f "$IN"
1484                                 fi
1485                         fi
1486                 fi
1487         else
1488                 run_command "" echo "HEH! The file we were about to encode disappeared:"
1489                 run_command "" echo ">> $IN"
1490                 run_command "encodetrack-$1" false
1491         fi
1492 }
1493
1494 # do_preprocess [tracknumber]
1495 # variables used:
1496 # TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX,
1497 # OUTPUTTYPE, ENCODEROPTS, DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
1498 #do_preprocess ()
1499 #{
1500 #       IN="${ABCDETEMPDIR}/track$1.wav"
1501 #       # We need IN to proceed.
1502 #       if [ -s "$IN" ] ; then
1503 #               for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
1504 #               do
1505 #                       #OUT="${ABCDETEMPDIR}/track$1.$OUTPUT"
1506 #                       run_command "" echo "Pre-processing track $1 of $TRACKS..."
1507 #                       case "$POSTPROCESSFORMAT" in
1508 #                       all|wav*)
1509 #                               run_command "preprocess-$OUTPUT-$1" nice $PRENICE $WAV_PRE $IF $OF ;;
1510 #                       mp3)
1511 #                               run_command "preprocess-$OUTPUT-$1" nice $PRENICE $MP3_PRE $IF $OF ;;
1512 #                       ogg)
1513 #                               run_command "preprocess-$OUTPUT-$1" nice $PRENICE $OGG_PRE $IF $OF ;;
1514 #                       flac)
1515 #                               run_command "preprocess-$OUTPUT-$1" nice $PRENICE $FLAC_PRE $IF $OF ;;
1516 #                       spx)
1517 #                               run_command "preprocess-$OUTPUT-$1" nice $PRENICE $SPX_PRE $IF $OF ;;
1518 #                       esac
1519 #               done
1520 #               # Only remove .wav if the encoding succeeded
1521 #               if checkerrors "preprocess-(.{3,4})-$1"; then
1522 #                       run_command "preprocess-$1" false
1523 #               else
1524 #                       run_command "preprocess-$1" true
1525 #               fi
1526 #       else
1527 #               if [ "$(checkstatus encode-output)" = "loud" ]; then
1528 #                       echo "HEH! The file we were about to pre-process disappeared:"
1529 #                       echo ">> $IN"
1530 #               fi
1531 #               run_command "preprocess-$1" false
1532 #       fi
1533 #}
1534
1535
1536 # do_postprocess [tracknumber]
1537 # variables used:
1538 # TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, 
1539 # DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
1540 #do_postprocess ()
1541 #{
1542 #       for POSTPROCESSFORMAT in $(echo $POSTPROCESSFORMATS | tr , \ )
1543 #       do
1544 #               IN="${ABCDETEMPDIR}/track$1.$POSTPROCESSFORMAT"
1545 #               # We need IN to proceed.
1546 #               if [ -s "$IN" ] ; then
1547 #                       #OUT="${ABCDETEMPDIR}/track$1.$OUTPUT"
1548 #                       run_command "" echo "Post-processing track $1 of $TRACKS..."
1549 #                       case "$POSTPROCESSFORMAT" in
1550 #                               mp3)
1551 #                                       run_command "postprocess-$OUTPUT-$1" nice $POSTNICE $MP3_POST $IF $OF ;;
1552 #                               ogg)
1553 #                                       run_command "postprocess-$OUTPUT-$1" nice $POSTNICE $OGG_POST $IF $OF ;;
1554 #                               flac)
1555 #                                       run_command "postprocess-$OUTPUT-$1" nice $POSTNICE $FLAC_POST $IF $OF ;;
1556 #                               spx)
1557 #                                       run_command "postprocess-$OUTPUT-$1" nice $POSTNICE $SPX_POST $IF $OF ;;
1558 #                       esac
1559 #                       # Only remove .wav if the encoding succeeded
1560 #                       if checkerrors "postprocess-(.{3,4})-$1"; then
1561 #                               run_command "postprocess-$1" false
1562 #                       else
1563 #                               run_command "postprocess-$1" true
1564 #                       fi
1565 #               else
1566 #                       if [ "$(checkstatus encode-output)" = "loud" ]; then
1567 #                               echo "HEH! The file we were about to post-process disappeared:"
1568 #                               echo ">> $IN"
1569 #                       fi
1570 #                       run_command "postprocess-$1" false
1571 #               fi
1572 #       done
1573 #}
1574
1575 # do_single_gain
1576 # variables used:
1577 # FIXME #
1578 do_single_gain ()
1579 {
1580 :
1581 }
1582
1583 # do_batch_gain
1584 # variables used:
1585 # MP3GAIN, MP3GAINOPTS, VORBISGAIN, VORBISGAINOPTS, MPCGAIN
1586 # FIXME #
1587 do_batch_gain ()
1588 {
1589         # The commands here don't go through run_command because they're never supposed to be silenced
1590         echo "Batch analizing gain in tracks: $TRACKQUEUE"
1591         (
1592         cd "${ABCDETEMPDIR}"
1593         BLURB=
1594         TRACKFILES=
1595         for UTRACKNUM in $TRACKQUEUE
1596         do
1597                 TRACKFILES="$TRACKFILES track$UTRACKNUM.mp3"
1598         done
1599         # FIXME # Hard-coded batch option!
1600         $NORMALIZER -b $NORMALIZEROPTS $TRACKFILES
1601         RETURN=$?
1602         if [ "$RETURN" != "0" ]; then
1603                 echo "batch-normalize: $NORMALIZER returned code $RETURN" >> "${ABCDETEMPDIR}/errors"
1604         else
1605                 for UTRACKNUM in $TRACKQUEUE
1606                 do
1607                         echo "normalizetrack-$UTRACKNUM" >> "${ABCDETEMPDIR}/status"
1608                 done
1609         fi
1610         )
1611 }
1612
1613 # do_batch_normalize
1614 # variables used:
1615 # NORMALIZER, NORMALIZEROPTS
1616 do_batch_normalize ()
1617 {
1618         # The commands here don't go through run_command because they're never supposed to be silenced
1619         echo "Batch normalizing tracks: $TRACKQUEUE"
1620         (
1621         cd "${ABCDETEMPDIR}"
1622         BLURB=
1623         TRACKFILES=
1624         for UTRACKNUM in $TRACKQUEUE
1625         do
1626                 TRACKFILES="$TRACKFILES track$UTRACKNUM.wav"
1627         done
1628         # XXX: Hard-coded batch option!
1629         $NORMALIZER -b $NORMALIZEROPTS $TRACKFILES
1630         RETURN=$?
1631         if [ "$RETURN" != "0" ]; then
1632                 echo "batch-normalize: $NORMALIZER returned code $RETURN" >> "${ABCDETEMPDIR}/errors"
1633         else
1634                 for UTRACKNUM in $TRACKQUEUE
1635                 do
1636                         echo "normalizetrack-$UTRACKNUM" >> "${ABCDETEMPDIR}/status"
1637                 done
1638         fi
1639         )
1640 }
1641
1642 # do_normalize [tracknumber]
1643 # variables used:
1644 # TRACKS, TRACKNAME, NORMALIZER, NORMALIZEROPTS
1645 do_normalize ()
1646 {
1647         IN="${ABCDETEMPDIR}/track$1.wav"
1648         if [ -e "$IN" ] ; then
1649                 run_command "" echo "Normalizing track $1 of $TRACKS: $TRACKNAME..."
1650                 run_command "normalizetrack-$1" $NORMALIZER $NORMALIZEROPTS "$IN"
1651         else
1652                 if [ "$(checkstatus encode-output)" = "loud" ]; then
1653                         echo "HEH! The file we were about to normalize disappeared:"
1654                         echo ">> $IN"
1655                 fi
1656                 run_command "normalizetrack-$1" false "File $IN was not found"
1657         fi
1658 }
1659
1660 # do_move [tracknumber]
1661 # Deduces the outfile from environment variables
1662 # Creates directory if necessary
1663 # variables used:
1664 # TRACKNUM, TRACKNAME, TRACKARTIST, DALBUM, OUTPUTFORMAT, CDGENRE, CDYEAR
1665 do_move ()
1666 {
1667         for TMPOUTPUT in $(echo "$OUTPUTTYPE" | tr , \ )
1668         do
1669                 # For now, set OUTPUT as TMPOUTPUT, and then change it once we have
1670                 # defined the OUTPUTFILE:
1671                 OUTPUT="$TMPOUTPUT"
1672
1673                 # Create ALBUMFILE, ARTISTFILE, TRACKFILE
1674                 ALBUMFILE="$(mungealbumname "$DALBUM")"
1675                 ARTISTFILE="$(mungeartistname "$TRACKARTIST")"
1676                 TRACKFILE="$(mungetrackname "$TRACKNAME")"
1677                 GENRE="$(mungegenre "$GENRE")"
1678                 YEAR=${CDYEAR:-$CDYEAR}
1679                 # If we want to start the tracks with a given number, we need to modify
1680                 # the TRACKNUM value before evaluation
1681                 gettracknum
1682                 # Supported variables for OUTPUTFORMAT are GENRE, YEAR, ALBUMFILE,
1683                 # ARTISTFILE, TRACKFILE, and TRACKNUM.
1684                 if [ "$ONETRACK" = "y" ]; then
1685                         if [ "$VARIOUSARTISTS" = "y" ]; then
1686                                 OUTPUTFILE="$(eval echo \""$VAONETRACKOUTPUTFORMAT"\")"
1687                         else
1688                                 OUTPUTFILE="$(eval echo \""$ONETRACKOUTPUTFORMAT"\")"
1689                         fi
1690                 else
1691                         if [ "$VARIOUSARTISTS" = "y" ]; then
1692                                 OUTPUTFILE="$(eval echo \""$VAOUTPUTFORMAT"\")"
1693                         else
1694                                 OUTPUTFILE="$(eval echo \""$OUTPUTFORMAT"\")"
1695                         fi
1696                 fi
1697                 if checkerrors "tagtrack-$OUTPUT-$1"; then :; else
1698                         # Once we know the specific output was successful, we can change
1699                         # the OUTPUT to the value containing the container
1700                         case $TMPOUTPUT in
1701                                 vorbis|ogg)
1702                                         OUTPUT=$OGGOUTPUTCONTAINER
1703                                         ;;
1704                                 opus)
1705                                         OUTPUT=$OPUSOUTPUTCONTAINER
1706                                         ;;
1707                                 mka)
1708                                         OUTPUT=$MKAOUTPUTCONTAINER
1709                                         ;;
1710                                 aiff)
1711                                         OUTPUT=$AIFFOUTPUTCONTAINER
1712                                         ;;
1713                                 flac)
1714                                         OUTPUT=$FLACOUTPUTCONTAINER
1715                                         ;;
1716                                 *)
1717                                         OUTPUT=$TMPOUTPUT
1718                                         ;;
1719                         esac
1720                         # Check that the directory for OUTPUTFILE exists, if it doesn't, create it
1721                         OUTPUTFILEDIR="$(dirname "$OUTPUTDIR/$OUTPUTFILE")"
1722                         case $OUTPUT in
1723                                 wav)
1724                                         if [ "$DOCLEAN" != "y" ] && [ "$FORCE" != "y" ]; then
1725                                                 # FIXME # introduce warnings?
1726                                                 :
1727                                         else
1728                                                 # mkdir -p shouldn't return an error if the directory already exists
1729                                                 mkdir -p "$OUTPUTFILEDIR"
1730                                                 run_command "movetrack-$1" mv "${ABCDETEMPDIR}/track$1.$OUTPUT" "$OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
1731                                                 if checkstatus "movetrack-output-$OUTPUT"; then :; else
1732                                                         run_command "movetrack-output-$OUTPUT" true
1733                                                 fi
1734                                         fi
1735                                         ;;
1736                                 *)
1737                                         # mkdir -p shouldn't return an error if the directory already exists
1738                                         mkdir -p "$OUTPUTFILEDIR"
1739                                         run_command "movetrack-$1" mv "${ABCDETEMPDIR}/track$1.$OUTPUT" "$OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
1740                                         if checkstatus "movetrack-output-$OUTPUT"; then :; else
1741                                                 run_command "movetrack-output-$OUTPUT" true
1742                                         fi
1743                                         ;;
1744                         esac
1745                         # Lets move the cue file
1746                         if CUEFILE=$(checkstatus cuefile) >/dev/null ; then
1747                                 if [ -r "${ABCDETEMPDIR}/$CUEFILE" ]; then
1748                                         if checkstatus "movecue-$OUTPUT"; then :; else
1749                                                 # Silence the Copying output since it overlaps with encoding processes...
1750                                                 #run_command "" vecho "Copying cue file to its destination directory..."
1751                                                 if checkstatus onetrack >/dev/null ; then
1752                                                         case $OUTPUT in
1753                                                                 wav)
1754                                                                         if [ "$DOCLEAN" != "y" ] && [ "$FORCE" != "y" ]; then
1755                                                                                 # We dont have the dir, since it was not created before.
1756                                                                                 :
1757                                                                         else
1758                                                                                 run_command "movecue-$OUTPUT" cp "${ABCDETEMPDIR}/$CUEFILE" "$OUTPUTDIR/$OUTPUTFILE.cue"
1759                                                                         fi
1760                                                                         ;;
1761                                                                 # NOTE: Creating a cue file with the 3-char-extension files is to comply with
1762                                                                 # http://brianvictor.tripod.com/mp3cue.htm#details
1763                                                                 [a-z0-9][a-z0-9][a-z0-9])
1764                                                                         run_command "movecue-$OUTPUT" cp "${ABCDETEMPDIR}/$CUEFILE" "$OUTPUTDIR/$OUTPUTFILE.cue"
1765                                                                         ;;
1766                                                                 *)
1767                                                                         run_command "movecue-$OUTPUT" cp "${ABCDETEMPDIR}/$CUEFILE" "$OUTPUTDIR/$OUTPUTFILE.$OUTPUT.cue"
1768                                                                         ;;
1769                                                         esac
1770                                                 else
1771                                                         run_command "movecue-$OUTPUT" cp "${ABCDETEMPDIR}/$CUEFILE" "$OUTPUTFILEDIR/$CUEFILE"
1772                                                 fi
1773                                                 echo "movecue-$OUTPUT" >> "${ABCDETEMPDIR}/status"
1774                                         fi
1775                                 fi
1776                         fi
1777                 fi
1778         done
1779 }
1780
1781 # do_playlist
1782 # Create the playlist if wanted
1783 # Variables used:
1784 # PLAYLISTFORMAT, PLAYLISTDATAPREFIX, VAPLAYLISTFORMAT, VAPLAYLISTDATAPREFIX,
1785 # VARIOUSARTISTS, OUTPUTDIR
1786 do_playlist ()
1787 {
1788         for TMPOUTPUT in $(echo "$OUTPUTTYPE" | tr , \ )
1789         do
1790                 case $TMPOUTPUT in
1791                         vorbis|ogg)
1792                                 OUTPUT=$OGGOUTPUTCONTAINER
1793                                 ;;
1794                         opus)
1795                                 OUTPUT=$OPUSOUTPUTCONTAINER
1796                                 ;;
1797                         mka)
1798                                 OUTPUT=$MKAOUTPUTCONTAINER
1799                                 ;;
1800                         aiff)
1801                                 OUTPUT=$AIFFOUTPUTCONTAINER
1802                                 ;;
1803                         flac)
1804                                 OUTPUT=$FLACOUTPUTCONTAINER
1805                                 ;;
1806                         *)
1807                                 OUTPUT=$TMPOUTPUT
1808                                 ;;
1809                 esac
1810                 # Create a playlist file for the playlist data to go into.
1811                 # We used to wipe it out if it existed. Now we request permission if interactive.
1812                 for LASTTRACK in $TRACKQUEUE; do :; done
1813                 ALBUMFILE="$(mungealbumname "$DALBUM")"
1814                 ARTISTFILE="$(mungeartistname "$DARTIST")"
1815                 GENRE="$(mungegenre "$GENRE")"
1816                 YEAR=${CDYEAR:-$CDYEAR}
1817                 if [ "$VARIOUSARTISTS" = "y" ] ; then
1818                         PLAYLISTFILE="$(eval echo "$VAPLAYLISTFORMAT")"
1819                 else
1820                         PLAYLISTFILE="$(eval echo "$PLAYLISTFORMAT")"
1821                 fi
1822                 FINALPLAYLISTDIR="$(dirname "$OUTPUTDIR/$PLAYLISTFILE")"
1823                 mkdir -p "$FINALPLAYLISTDIR"
1824                 if [ -s "$OUTPUTDIR/$PLAYLISTFILE" ]; then
1825                         echo -n "Erase, Append to, or Keep the existing playlist file? [e/a/k] (e): " >&2
1826                         if [ "$INTERACTIVE" = "y" ]; then
1827                                 while [ "$DONE" != "y" ]; do
1828                                         read ERASEPLAYLIST
1829                                         case $ERASEPLAYLIST in
1830                                                 e|E|a|A|k|K) DONE=y ;;
1831                                                 "") ERASEPLAYLIST=e ; DONE=y ;;
1832                                                 *) ;;
1833                                         esac
1834                                 done
1835                         else
1836                                 echo e >&2
1837                                 ERASEPLAYLIST=e
1838                         fi
1839                         # Once we erase the playlist, we use append to create the new one.
1840                         [ "$ERASEPLAYLIST" = "e" ] || [ "$ERASEPLAYLIST" = "E" ] && rm -f "$OUTPUTDIR/$PLAYLISTFILE" && ERASEPLAYLIST=a
1841                 else
1842                         # The playlist does not exist, so we can safelly use append to create the new list
1843                         ERASEPLAYLIST=a
1844                 fi
1845                 if [ "$ERASEPLAYLIST" = "a" ] || [ "$ERASEPLAYLIST" = "A" ]; then
1846                         touch "$OUTPUTDIR/$PLAYLISTFILE"
1847                         for UTRACKNUM in $TRACKQUEUE
1848                         do
1849                                 # Shares some code with do_move since the filenames have to match
1850                                 CDDBTRACKNUM=$(expr $UTRACKNUM - 1) # Unpad
1851                                 getcddbinfo TRACKNAME
1852                                 splitvarious
1853                                 TRACKFILE="$(mungetrackname "$TRACKNAME")"
1854                                 ARTISTFILE="$(mungeartistname "$TRACKARTIST")"
1855                                 ALBUMFILE="$(mungealbumname "$DALBUM")"
1856                                 # If we want to start the tracks with a given number, we need to modify the
1857                                 # TRACKNUM value before evaluation
1858                                 gettracknum
1859                                 if [ "$VARIOUSARTISTS" = "y" ]; then
1860                                         OUTPUTFILE="$(eval echo \""$VAOUTPUTFORMAT\"")"
1861                                 else
1862                                         OUTPUTFILE="$(eval echo \""$OUTPUTFORMAT\"")"
1863                                 fi
1864                                 if [ "$VARIOUSARTISTS" = "y" ]; then
1865                                         if [ "$VAPLAYLISTDATAPREFIX" ] ; then
1866                                                 echo "${VAPLAYLISTDATAPREFIX}$OUTPUTFILE.$OUTPUT" >> "$OUTPUTDIR/$PLAYLISTFILE"
1867                                         else
1868                                                 relpath "$PLAYLISTFILE", "$OUTPUTFILE.$OUTPUT" >> "$OUTPUTDIR/$PLAYLISTFILE"
1869                                         fi
1870                                 else
1871                                         if [ "$PLAYLISTDATAPREFIX" ]; then
1872                                                 echo "${PLAYLISTDATAPREFIX}$OUTPUTFILE.$OUTPUT" >> "$OUTPUTDIR/$PLAYLISTFILE"
1873                                         else
1874                                                 relpath "$PLAYLISTFILE", "$OUTPUTFILE.$OUTPUT" >> "$OUTPUTDIR/$PLAYLISTFILE"
1875                                         fi
1876                                 fi
1877                         done
1878                 fi
1879                 ## this will convert the playlist to have CRLF line-endings, if specified
1880                 ## (some hardware players insist on CRLF endings)
1881                 if [ "$DOSPLAYLIST" = "y" ]; then
1882                         awk '{sub("\r$",""); printf "%s\r\n", $0}' "$OUTPUTDIR/$PLAYLISTFILE" > "${ABCDETEMPDIR}/PLAYLISTFILE.tmp"
1883                         cat "${ABCDETEMPDIR}/PLAYLISTFILE.tmp" | sed 's/\//\\/' > "$OUTPUTDIR/$PLAYLISTFILE"
1884                 fi
1885                 echo "playlistcomplete" >> "${ABCDETEMPDIR}/status"
1886         done
1887 }
1888
1889 # abcde.cue2discid
1890 # This function reads a cuefile on stdin and writes an extended
1891 # cddb query on stdout.  Any PREGAP for track 1 is properly
1892 # handled, although cue files embedded in FLAC files do not
1893 # appear to properly store the PREGAP setting. :(
1894 abcde.cue2discid () {
1895
1896         cddb_sum () {
1897                 val=$1
1898                 ret=0
1899                 while [ "$val" -gt 0 ] ; do
1900                         ret=$(( $ret + ( $val % 10) ))
1901                         val=$(( $val / 10 ))
1902                 done
1903                 echo "$ret"
1904         }
1905
1906         msf2lba () {
1907                 OIFS="$IFS"
1908                 IFS=":"
1909                 set -- $1
1910                 IFS="$OIFS"
1911                 local first second third
1912                 first=$(( $1 + 0 ))
1913                 second=$(( $2 + 0 ))
1914                 third=$(( $3 + 0 ))
1915
1916                 echo $(( ((($first * 60) + $second) * 75) + $third ))
1917         }
1918
1919         OFFSET=150
1920         PREGAP=0
1921         LEADOUT=0
1922         LEADIN=88200
1923         i=0
1924         N=0
1925
1926         while read line ; do
1927                 set -- $line
1928                 case "$1" in
1929                 TRACK)
1930                         i=$(( i + 1 ))
1931                         ;;
1932                 INDEX)
1933                         if [ "$2" -eq 1 ] ; then
1934                                 LBA=$(msf2lba $3)
1935                                 START=$(( $LBA + $PREGAP + $OFFSET ))
1936                                 eval TRACK$i=$START
1937                                 X=$(cddb_sum $(( $START / 75 )) )
1938                                 N=$(( $N + $X ))
1939                         fi
1940                         ;;
1941                 PREGAP)
1942                         PREGAP=$(msf2lba $2)
1943                         ;;
1944                 REM)
1945                         case "$2" in
1946                         FLAC__lead-out)
1947                                 LEADOUT=$(( $4 / 588 ))
1948                                 ;;
1949                         FLAC__lead-in)
1950                                 LEADIN=$(( $3 / 588 ))
1951                                 ;;
1952                         esac
1953                         ;;
1954                 esac
1955
1956         done
1957
1958         TRACKS=$i
1959         LEADOUT=$(( $LEADOUT + $LEADIN ))
1960
1961         LENGTH=$(( $LEADOUT/75 - $TRACK1/75 ))
1962         CDDBDISCID=$(( ( $N % 255 ) * 2**24 | $LENGTH * 2**8 | $TRACKS ))
1963         printf %08x %i ${CDDBDISCID} $TRACKS
1964
1965         j=1
1966         while [ $j -le $TRACKS ] ; do
1967                 eval echo -n "\" \$TRACK$j\""
1968                 j=$(( $j + 1))
1969         done
1970         echo  $(( $LEADOUT / 75 ))
1971 }
1972
1973 # abcde.mkcue
1974 # abcde.mkcue [--wholedisk]
1975 # This creates a cuefile directly from the extended discid information
1976 # The --wholedisk option controls whether we're ripping data from the
1977 # start of track one or from the start of the disk (usually, but not
1978 # always the same thing!)
1979 #
1980 # Track one leadin/pregap (if any) handeling:
1981 # --wholedisk specified:
1982 #   TRACK 01 AUDIO
1983 #     INDEX 00 00:00:00
1984 #     INDEX 01 <pregap value>
1985 #   Remaining track index values unchanged from disc TOC
1986 #
1987 # --wholedisk not specified
1988 #   TRACK 01 AUDIO
1989 #     PREGAP <pregap value>
1990 #     INDEX 01 00:00:00
1991 #   Remaining track index values offset by <pregap value>
1992 #
1993 # Variables used:
1994 # CDDBTRACKINFO
1995 abcde.mkcue () {
1996
1997         echomsf () {
1998                 printf "$1%02i:%02i:%02i\n" $(($2/4500)) $((($2/75)%60)) $(($2%75))
1999         }
2000
2001         local MODE DISCID TRACKS
2002         local i OFFSET LBA
2003         local CUEWAVFILE
2004
2005         if [ "$1" = "--wholedisk" ] ; then
2006                 MODE=INDEX
2007         else
2008                 MODE=PREGAP
2009         fi
2010
2011         vecho "One track is $ONETRACK"
2012         TRACKFILE="$(mungetrackname "$TRACKNAME")"
2013         ARTISTFILE="$(mungeartistname "$TRACKARTIST")"
2014         ALBUMFILE="$(mungealbumname "$DALBUM")"
2015         if [ "$ONETRACK" = "y" ]; then
2016                 if [ "$VARIOUSARTISTS" = "y" ]; then
2017                         CUEWAVFILE="$(eval echo \""$VAONETRACKOUTPUTFORMAT"\" | sed -e 's@^.*/@@').$OUTPUT"
2018                 else
2019                         CUEWAVFILE="$(eval echo \""$ONETRACKOUTPUTFORMAT"\" | sed -e 's@^.*/@@').$OUTPUT"
2020                 fi
2021                 vecho "Cue wav file is $CUEWAVFILE"
2022         else
2023                 CUEWAVFILE="dummy.wav"
2024         fi
2025
2026         set -- $CDDBTRACKINFO
2027
2028         DISCID=$1
2029         TRACKS=$2
2030         shift 2
2031
2032         echo "REM DISCID $DISCID"
2033         echo FILE \""$CUEWAVFILE"\" WAVE
2034
2035         if [ $1 -ne 150 ] && [ $MODE = "PREGAP" ] ; then
2036                 OFFSET=$1
2037         else
2038                 OFFSET=150
2039         fi
2040
2041         i=1
2042         while [ $i -le "$TRACKS" ] ; do
2043                 LBA=$(( $1 - $OFFSET ))
2044                 printf "  TRACK %02i AUDIO\n" $i
2045                 if [ "$i" -eq 1 ] && [ "$1" -ne 150 ] ; then
2046                         if [ "$MODE" = PREGAP ] ; then
2047                                 echomsf "    PREGAP " $(($"OFFSET" - 150))
2048                         else
2049                                 echo    "    INDEX 00 00:00:00"
2050                         fi
2051                 fi
2052                 echomsf "    INDEX 01 " "$LBA"
2053                 i=$(( $i + 1))
2054                 shift
2055         done
2056 }
2057
2058 # do_discid
2059 # This is essentially the start of things
2060 do_discid ()
2061 {
2062         if [ -z "${CDDBDISCID}" ]; then
2063                 vecho -n "Getting CD track info... "
2064                 # In OSX, unmount the disc before a query
2065                 if [ "$OSFLAVOUR" = "OSX" ]; then
2066                         diskutil unmount "${CDROM#/dev/}"
2067                 fi
2068                 case "$CDROMREADERSYNTAX" in
2069                         flac)
2070                                 if $METAFLAC $METAFLACOPTS --export-cuesheet-to=- "$CDROM" > /dev/null 2>&1 ; then
2071                                         case "$CUE2DISCID" in
2072                                                 # FIXME # right now we have 2 cue2discid internal
2073                                                 # implementations: builtin and abcde.cue2discid. Test
2074                                                 # both of them and decide which one we want to use.
2075                                                 builtin)
2076                                                         #vecho "Using builtin cue2discid implementation..."
2077                                                         CUESHEET="$(metaflac $METAFLACOPTS --export-cuesheet-to=- "$CDROM")"
2078
2079                                                         #TRACKS=$(echo $CUESHEET | grep -E "TRACK \+[[:digit:]]\+ \+AUDIO" |wc -l)
2080                                                         #TRACKS=0
2081                                                         OFFSETTIMES=( $(echo "$CUESHEET" | sed -n -e's/\ *INDEX 01\ \+//p' ) )
2082                                                         TRACKS=${#OFFSETTIMES[@]}
2083                                                         unset OFFSETS
2084                                                         vecho "processing offsetimes ${OFFSETTIMES[@]}"
2085                                                         for OFFSETTIME in ${OFFSETTIMES[@]}; do
2086                                                                 OFFSETS="$OFFSETS $(( 10#${OFFSETTIME:0:2} * 4500 + 10#${OFFSETTIME:3:2} * 75 + 10#${OFFSETTIME:6:2} ))"
2087                                                         done
2088
2089                                                         LEADOUT=$(( $(echo "$CUESHEET" | grep lead-out | get_last) * 75 / 44100 ))
2090                                                         LEADIN=$(( $(echo "$CUESHEET" | grep lead-in | get_last) * 75 / 44100 ))
2091                                                         makeids
2092                                                 ;;
2093                                                 *)
2094                                                         #vecho "Using external python cue2discid implementation..."
2095                                                         CDDBTRACKINFO=$($METAFLAC $METAFLACOPTS --export-cuesheet-to=- "$CDROM" | $CUE2DISCID)
2096                                                 ;;
2097                                         esac
2098                                 else
2099                                         log error "the input flac file does not contain a cuesheet."
2100                                         exit 1
2101                                 fi
2102                                 ;;
2103                         cdparanoia|debug)
2104                                 CDPARANOIAOUTPUT="$( $CDROMREADER -"$CDPARANOIACDROMBUS" "$CDROM" -Q --verbose 2>&1 )"
2105                                 RET=$?
2106                                 if [ ! "$RET" = "0" ];then
2107                                         log warning "something went wrong while querying the CD... Maybe a DATA CD or the CD is not loaded?"
2108                                 fi
2109
2110                                 TRACKS="$(echo "$CDPARANOIAOUTPUT" | grep -E '^[[:space:]]+[[:digit:]]' | tail -n 1 | get_first | tr -d "." | tr '\n' ' ')"
2111                                 CDPARANOIAAUDIOTRACKS="$TRACKS"
2112
2113                                 LEADOUT="$(echo "$CDPARANOIAOUTPUT" | grep -Eo '^TOTAL[[:space:]]+([[:digit:]]+)' | get_last)"
2114                                 OFFSETS="$(echo "$CDPARANOIAOUTPUT" | awk '/^ +.*\[/ {print $4}')"
2115                                 makeids
2116                                 ;;
2117                         *)
2118                                 # Calculate the cddb discid in all
2119                                 # cases now. We'll use the cddb discid
2120                                 # for reference in most cases for
2121                                 # consistency. Also calculate the
2122                                 # musicbrainz discid if we need it.
2123                                 CDDBTRACKINFO=$($CDDISCID "$CDROM")
2124                                 if [ "$CDDISCID_NEEDS_PAUSE"x = "y"x ]; then
2125                                         sleep 6
2126                                 fi
2127                                 CDDBDISCID=$(echo "$CDDBTRACKINFO" | cut -d' ' -f1)
2128                                 case "$CDDBMETHOD" in
2129                                         *musicbrainz*)
2130                                                 MBTRACKINFO=$($MUSICBRAINZ --command id --device "$CDROM")
2131                                                 error=$?
2132                                                 if [ $error != 0 ]; then
2133                                                         log error "$MUSICBRAINZ failed to run; ABORT"
2134                                                         exit $error
2135                                                 fi
2136                                                 MBDISCID=$(echo "$MBTRACKINFO" | cut -d' ' -f1)
2137                                                 ;;
2138                                 esac
2139                 esac
2140                 # Make sure there's a CD in there by checking cd-discid's return code
2141                 if [ ! "$?" = "0" ]; then
2142                         if [ "$CDROMREADERSYNTAX" = "flac" ] ; then
2143                                 log error "cuesheet information from the flac file could not be read."
2144                                 log error "Perhaps the flac file does not contain a cuesheet?."
2145                                 exit 1
2146                         else
2147                                 log error "CD could not be read. Perhaps there's no CD in the drive?"
2148                                 exit 1
2149                         fi
2150                 fi
2151                 # In OSX, remount the disc again
2152                 if [ "$OSFLAVOUR" = "OSX" ]; then
2153                         diskutil mount "${CDROM#/dev/}"
2154                 fi
2155                 WEHAVEACD=y
2156                 CDDBDISCID=$(echo "$CDDBTRACKINFO" | cut -f1 -d' ')
2157         else
2158                 CDDBTRACKINFO=$(cat "$WAVOUTPUTDIR/abcde.${CDDBDISCID}/cddbdiscid")
2159         fi
2160
2161         # Get a full enumeration of tracks, sort it, and put it in the TRACKQUEUE.
2162         # This needs to be done now because a section of the resuming code will need
2163         # it later.
2164
2165         # get the number of digits to pad TRACKNUM with - we'll use this later
2166         # a CD can only hold 99 tracks, but since we support a feature for starting
2167         # numbering the tracks from a given number, we might need to set it as a
2168         # variable for the user to define... or obtain it somehow.
2169         if [ "$PADTRACKS" = "y" ] ; then
2170                 TRACKNUMPADDING=2
2171         fi
2172
2173         ABCDETEMPDIR="$WAVOUTPUTDIR/abcde.${CDDBDISCID}"
2174         if [ -z "$TRACKQUEUE" ]; then
2175                 if [ ! "$STRIPDATATRACKS" = "n" ]; then
2176                         case "$CDROMREADERSYNTAX" in
2177                                 cdparanoia|libcdio|debug)
2178                                         if [ "$WEHAVEACD" = "y" ]; then
2179                                                 vecho "Querying the CD for audio tracks..."
2180                                                 CDPARANOIAOUTPUT="$( $CDROMREADER -"$CDPARANOIACDROMBUS" "$CDROM" -Q --verbose 2>&1 )"
2181                                                 RET=$?
2182                                                 if [ ! "$RET" = "0" ];then
2183                                                         log warning "something went wrong while querying the CD... Maybe a DATA CD?"
2184                                                 fi
2185                                                 TRACKS="$(echo "$CDPARANOIAOUTPUT" | grep -E '^[[:space:]]+[[:digit:]]' | tail -n 1 | get_first | tr -d "." | tr '\n' ' ')"
2186                                                 CDPARANOIAAUDIOTRACKS="$TRACKS"
2187                                         else
2188                                                 # Previous versions of abcde would store the tracks on a file, instead of the status record.
2189                                                 if [ -f "${ABCDETEMPDIR}/cdparanoia-audio-tracks" ]; then
2190                                                         echo "cdparanoia-audio-tracks=$( cat "${ABCDETEMPDIR}/cdparanoia-audio-tracks" )" >> "${ABCDETEMPDIR}/status"
2191                                                         rm -f "${ABCDETEMPDIR}/cdparanoia-audio-tracks"
2192                                                 fi
2193                                                 if [ -f "${ABCDETEMPDIR}/status" ] && TRACKS=$(checkstatus cdparanoia-audio-tracks); then :; else
2194                                                         TRACKS=$(echo "$CDDBTRACKINFO" | cut -f2 -d' ')
2195                                                 fi
2196                                         fi
2197                                         ;;
2198                                 *)      TRACKS=$(echo "$CDDBTRACKINFO" | cut -f2 -d' ') ;;
2199                         esac
2200                 else
2201                         TRACKS=$(echo "$CDDBTRACKINFO" | cut -f2 -d' ')
2202                 fi
2203                 if echo "$TRACKS" | grep "[[:digit:]]" > /dev/null 2>&1 ;then :;else
2204                         log info "The disc does not contain any tracks. Giving up..."
2205                         exit 0
2206                 fi
2207                 echo -n "Grabbing entire CD - tracks: "
2208                 if [ ! "$PADTRACKS" = "y" ] ; then
2209                         TRACKNUMPADDING=$(echo -n $TRACKS | wc -c | tr -d ' ')
2210                 fi
2211
2212                 TRACKS=$(printf "%0.${TRACKNUMPADDING}d" $TRACKS)
2213                 X=1
2214                 while [ $X -le $TRACKS ]
2215                 do
2216                         PT=$(printf "%0.${TRACKNUMPADDING}d" $X)
2217                         TRACKQUEUE="$TRACKQUEUE $PT"
2218                         X=$(($X + 1))
2219                 done
2220                 echo "$TRACKQUEUE"
2221         else
2222                 TRACKS=$(echo "$CDDBTRACKINFO" | cut -f2 -d' ')
2223                 # User-supplied track queue.
2224                 # Weed out non-numbers, whitespace, then sort and weed out duplicates
2225                 TRACKQUEUE=$(echo "$TRACKQUEUE" | sed 's-[^0-9 ]--g' | tr ' ' '\n' | grep -v ^$ | sort -n | uniq | tr '\n' ' ' | sed 's- $--g')
2226                 # Once cleaned, obtain the highest value in the trackqueue for number padding
2227                 for LASTTRACK in $TRACKQUEUE; do :; done
2228                 if [ ! "$PADTRACKS" = "y" ] ; then
2229                         TRACKNUMPADDING=$(echo -n "$LASTTRACK" | wc -c | tr -d ' ')
2230                 fi
2231                 # Now we normalize the trackqueue
2232                 for TRACK in $TRACKQUEUE; do
2233                         PADTRACKNUM=$(printf "%0.${TRACKNUMPADDING}d" $(expr ${TRACK} + 0 ))
2234                         PADTRACKQUEUE="$PADTRACKQUEUE $PADTRACKNUM"
2235                 done
2236                 TRACKQUEUE="$PADTRACKQUEUE"
2237                 echo Grabbing tracks: "$TRACKQUEUE"
2238         fi
2239
2240         QUEUEDTRACKS=$(echo $TRACKQUEUE | wc -w | tr -d ' ')
2241
2242         # We have the discid, create a temp directory after it to store all the temp
2243         # info
2244
2245         if [ -e "${ABCDETEMPDIR}" ]; then
2246                 echo -n "abcde: attempting to resume from ${ABCDETEMPDIR}"
2247                 # It already exists, see if it's a directory
2248                 if [ ! -d "${ABCDETEMPDIR}" ]; then
2249                         # This is a file/socket/fifo/device/etc, not a directory
2250                         # Complain and exit
2251                         echo >&2
2252                         echo "abcde: file ${ABCDETEMPDIR} already exists and does not belong to abcde." >&2
2253                         echo "Please investigate, remove it, and rerun abcde." >&2
2254                         exit 1
2255                 fi
2256                 echo -n .
2257                 # It's a directory, let's see if it's writable by us
2258                 if [ ! -r "${ABCDETEMPDIR}" ] || [ ! -w "${ABCDETEMPDIR}" ] || [ ! -x "${ABCDETEMPDIR}" ]; then
2259                         # Nope, complain and exit
2260                         echo >&2
2261                         echo "abcde: directory ${ABCDETEMPDIR} already exists and is not writeable." >&2
2262                         echo "Please investigate, remove it, and rerun abcde." >&2
2263                         exit 1
2264                 fi
2265                 echo .
2266                 # Check to see the version of abcde that was used. If it's not
2267                 # the same version as we are, bail.
2268                 RESUME_VERSION="$(checkstatus abcde-version)"
2269                 if [ "$RESUME_VERSION" != "$VERSION" ]; then
2270                         # Nope, complain and exit
2271                         echo >&2
2272                         echo "abcde: Working directory ${ABCDETEMPDIR} was created using version $RESUME_VERSION." >&2
2273                         echo "abcde: Current version is $VERSION." >&2
2274                         echo "abcde: Refusing to resume with a version mismatch." >&2
2275                         echo "Please remove it, and rerun abcde." >&2
2276                         exit 1
2277                 fi
2278                 echo .
2279                 # See if it's populated
2280                 if [ ! -f "${ABCDETEMPDIR}/cddbdiscid" ]; then
2281                         # Wipe and start fresh
2282                         echo "abcde: ${ABCDETEMPDIR}/cddbdiscid not found. Abcde must remove and recreate" >&2
2283                         echo -n "this directory to continue. Continue [y/N]? " >&2
2284                         if [ "$INTERACTIVE" = "y" ]; then
2285                                 read ANSWER
2286                         else
2287                                 echo y >&2
2288                                 ANSWER=y
2289                         fi
2290                         if [ "$ANSWER" != "y" ]; then
2291                                 exit 1
2292                         fi
2293                         rm -rf "${ABCDETEMPDIR}" || exit 1
2294                         mkdir -p "${ABCDETEMPDIR}"
2295                         if [ "$?" -gt "0" ]; then
2296                                 # Directory already exists or could not be created
2297                                 echo "abcde: Temp directory ${ABCDETEMPDIR} could not be created." >&2
2298                                 exit 1
2299                         fi
2300                 else
2301                         # Everything is fine. Check for ^encodetracklocation-
2302                         # and encode-output entries in the status file and
2303                         # remove them. These are not relevant across sessions.
2304                         echo "Resuming" >> "${ABCDETEMPDIR}/status"
2305                         if [ -f "${ABCDETEMPDIR}/status" ]; then
2306                                 mv "${ABCDETEMPDIR}/status" "${ABCDETEMPDIR}/status.old"
2307                                 grep -v ^encodetracklocation- < "${ABCDETEMPDIR}/status.old" \
2308                                         | grep -v ^encode-output > "${ABCDETEMPDIR}/status"
2309                         fi
2310                         # Remove old error messages
2311                         if [ -f "${ABCDETEMPDIR}/errors" ]; then
2312                                 rm -f "${ABCDETEMPDIR}/errors"
2313                         fi
2314                 fi
2315         else
2316                 # We are starting from scratch
2317                 mkdir -p "${ABCDETEMPDIR}"
2318                 if [ "$?" -gt "0" ]; then
2319                         # Directory already exists or could not be created
2320                         echo "abcde: Temp directory ${ABCDETEMPDIR} could not be created." >&2
2321                         exit 1
2322                 fi
2323                 cat /dev/null > "${ABCDETEMPDIR}/status"
2324                 # Store the abcde version in the status file.
2325                 echo "abcde-version=$VERSION" >> "${ABCDETEMPDIR}/status"
2326         fi
2327         if [ X"$DOCUE" = "Xy" ] && [ X"$WEHAVEACD" = "Xy" ]; then
2328                 if checkstatus cuefile > /dev/null 2>&1 ; then :; else
2329                         CUEFILE=cue-$(echo "$CDDBTRACKINFO" | cut -f1 -d' ').txt
2330                         vecho "Creating cue file..."
2331                         case "$CDROMREADERSYNTAX" in
2332                                 flac)
2333                                         if $METAFLAC --export-cuesheet-to=- "$CDROM" > "${ABCDETEMPDIR}/$CUEFILE"; then
2334                                                 echo "cuefile=$CUEFILE" >> "${ABCDETEMPDIR}/status"
2335                                         else
2336                                                 log warning "the input flac file does not contain a cuesheet."
2337                                         fi
2338                                         ;;
2339                                 *)
2340                                         if $CUEREADER $CUEREADEROPTS > "${ABCDETEMPDIR}/$CUEFILE"; then
2341                                                 echo "cuefile=$CUEFILE" >> "${ABCDETEMPDIR}/status"
2342                                         else
2343                                                 log warning "reading the CUE sheet is still considered experimental"
2344                                                 log warning "and there was a problem with the CD reading. abcde will continue,"
2345                                                 log warning "but consider reporting the problem to the abcde authors"
2346                                         fi
2347                                         ;;
2348                         esac
2349                 fi
2350         fi
2351         # If we got the CDPARANOIA status and it is not recorded, save it now
2352         if [ -n "$CDPARANOIAAUDIOTRACKS" ]; then
2353                 if checkstatus cdparanoia-audio-tracks > /dev/null 2>&1; then :; else
2354                         echo "cdparanoia-audio-tracks=$CDPARANOIAAUDIOTRACKS" >> "${ABCDETEMPDIR}/status"
2355                 fi
2356         fi
2357
2358         # Create the discid files
2359         echo "$CDDBTRACKINFO" > "${ABCDETEMPDIR}/cddbdiscid"
2360         case "$CDDBMETHOD" in
2361                 *musicbrainz*)
2362                         echo "$MBTRACKINFO" > "${ABCDETEMPDIR}/mbdiscid"
2363                         ;;
2364         esac
2365 }
2366
2367 # do_cleancue
2368 # Create a proper CUE file based on the CUE file we created before.
2369 do_cleancue()
2370 {
2371         if CUEFILE_IN="${ABCDETEMPDIR}"/$(checkstatus cuefile); then
2372                 CUEFILE_OUT=$CUEFILE_IN.out
2373                 ### FIXME ### checkstatus cddb
2374                 if [ -e "$CDDBDATA" ]; then
2375                         vecho "Adding metadata to the cue file..."
2376                         # FIXME It doesn't preserve spaces! Why?
2377                         # FIXME parse $track into PERFORMER and TITLE - abcde already has code for this?
2378                         n=1
2379                         echo "PERFORMER \"$DARTIST\"" >> "$CUEFILE_OUT"
2380                         echo "TITLE \"$DALBUM\"" >> "$CUEFILE_OUT"
2381                         # Set IFS to <newline> to prevent read from swallowing spaces and tabs
2382                         OIFS="$IFS"
2383                         IFS='
2384 '
2385                         cat "$CUEFILE_IN" | while read line
2386                         do
2387                                 if echo "$line" | grep "INDEX 01" > /dev/null 2>&1 ; then
2388 # FIXME # Possible patch: remove the line above, uncomment the 2 lines below.
2389 #                               echo "$line" >> "$CUEFILE_OUT"
2390 #                               if echo "$line" | grep "^[[:space:]]*TRACK" > /dev/null 2>&1 ; then
2391                                         eval track="\$TRACK$n"
2392                                         n=$(( $n + 1 ))
2393                                         echo "    TITLE \"$track\"" >> "$CUEFILE_OUT"
2394                                 # When making a single-track rip, put the
2395                                 # actual file name into the file declaration
2396                                 # in the cue file so that it is usable by
2397                                 # music players and the like
2398                                 elif [ "$ONETRACK" = "y" ] &&
2399                                                 echo "$line" | grep '^FILE "dummy.wav" WAVE' > /dev/null 2>&1 ; then
2400
2401                                         TRACKFILE="$(mungetrackname "$TRACKNAME")"
2402                                         ARTISTFILE="$(mungeartistname "$TRACKARTIST")"
2403                                         ALBUMFILE="$(mungealbumname "$DALBUM")"
2404
2405                                         if [ "$VARIOUSARTISTS" = "y" ]; then
2406                                                 OUTPUTFILE="$(eval echo \""$VAONETRACKOUTPUTFORMAT"\" | sed -e 's@^.*/@@').$OUTPUT"
2407                                         else
2408                                                 OUTPUTFILE="$(eval echo \""$ONETRACKOUTPUTFORMAT"\" | sed -e 's@^.*/@@').$OUTPUT"
2409                                         fi
2410
2411                                         echo "FILE \"$OUTPUTFILE\" WAVE" >> "$CUEFILE_OUT"
2412                                         continue
2413                                 fi
2414 # FIXME # If the lines above are uncommented, remove the line below.
2415                                 echo "$line" >> "$CUEFILE_OUT"
2416                         done
2417                         IFS="$OIFS"
2418                         mv "$CUEFILE_OUT" "$CUEFILE_IN"
2419                         echo "cleancuefile" >> "${ABCDETEMPDIR}/status"
2420                 fi
2421         fi
2422 }
2423
2424 # do_cddbparse
2425 # Parses a CDDB file and outputs the title and the track names.
2426 # Variables: CDDBFILE
2427 do_cddbparse ()
2428 {
2429         CDDBPARSEFILE="$1"
2430         # List out disc title/author and contents
2431         if [ "$ONETRACK" = "y" ]; then
2432                 vecho "ONETRACK mode selected: displaying only the title of the CD..."
2433         fi
2434         echo "---- $(grep -a DTITLE "${CDDBPARSEFILE}" | cut '-d=' -f2- | tr -d \\r\\n ) ----"
2435         if [ X"$SHOWCDDBYEAR" = "Xy" ]; then
2436                 PARSEDYEAR=$(grep -a DYEAR "${CDDBPARSEFILE}" | cut '-d=' -f2-)
2437                 if [ ! X"$PARSEDYEAR" = "X" ]; then
2438                         echo "Year: $PARSEDYEAR"
2439                 fi
2440         fi
2441         if [ X"$SHOWCDDBGENRE" = "Xy" ]; then
2442                 PARSEDGENRE=$(grep -a DGENRE "${CDDBPARSEFILE}" | cut '-d=' -f2-)
2443                 if [ ! X"$PARSEDGENRE" = "X" ]; then
2444                         echo "Genre: $PARSEDGENRE"
2445                 fi
2446         fi
2447         if [ ! "$ONETRACK" = "y" ]; then
2448                 for TRACK in $(f_seq_row 1 $TRACKS)
2449                 do
2450                         echo "$TRACK": "$(grep -a ^TTITLE$(($TRACK - 1))= "${CDDBPARSEFILE}" | cut -f2- -d= | tr -d \\r\\n)"
2451                 done
2452         fi
2453 }
2454
2455 # do_localcddb_read
2456 # Check for a local CDDB file, and report success
2457 do_localcddb_read ()
2458 {
2459         if checkstatus cddb-readcomplete && checkstatus cddb-choice >/dev/null; then :; else
2460
2461                 CDDBLOCALSTATUS="notfound"
2462                 CDDBDISCID=$(echo "$CDDBTRACKINFO" | cut -d' ' -f1)
2463                 USELOCALRESP="y"
2464
2465                 if [ "$CDDBLOCALRECURSIVE" = "y" ]; then
2466                         CDDBLOCALRESULTS="$(find "${CDDBLOCALDIR}" -name "${CDDBDISCID}" -type f 2> /dev/null)"
2467                         if [ ! "${CDDBLOCALRESULTS}" = "" ]; then
2468                                 if   (( $(echo "${CDDBLOCALRESULTS}" | wc -l) == 1 )); then
2469                                         CDDBLOCALFILE="${CDDBLOCALRESULTS}"
2470                                         CDDBLOCALMATCH=single
2471                                 elif (( $(echo "${CDDBLOCALRESULTS}" | wc -l) > 1 )); then
2472                                         CDDBLOCALMATCH=multiple
2473                                 fi
2474                         else
2475                                 CDDBLOCALMATCH=none
2476                         fi
2477                 elif [ "$CDDBLOCALMATCH" = "none" ] && [ -r "${CDDBLOCALDIR}/${CDDBDISCID}" ]; then
2478                         CDDBLOCALFILE="${CDDBLOCALDIR}/${CDDBDISCID}"
2479                         CDDBLOCALMATCH=single
2480                 else
2481                         CDDBLOCALMATCH=none
2482                 fi
2483
2484                 # If the user has selected to check a local CDDB repo, we proceed with it
2485                 case $CDDBLOCALMATCH in
2486                         multiple)
2487                                 echo "Processing multiple matching CDDB entries..." > "${ABCDETEMPDIR}/cddblocalchoices"
2488                                 X=0
2489                                 echo "$CDDBLOCALRESULTS" | while read RESULT ; do
2490                                         X=$(( $X + 1 ))
2491                                         # List out disc title/author and contents
2492                                         CDDBLOCALREAD="${ABCDETEMPDIR}/cddblocalread.$X"
2493                                         cat "$RESULT" > "${CDDBLOCALREAD}"
2494                                         {
2495                                                 echo -n "#$X: "
2496                                                 do_cddbparse "${CDDBLOCALREAD}"
2497                                                 echo ""
2498                                                 ##FIXME## QUICK HACK !!!!
2499                                                 if [ ! "$INTERACTIVE" = "y" ]; then break ; fi
2500                                         } >> "${ABCDETEMPDIR}/cddblocalchoices"
2501                                 done
2502                                 page "${ABCDETEMPDIR}/cddblocalchoices"
2503                                 CDDBLOCALCHOICES=$( echo "$CDDBLOCALRESULTS" | wc -l )
2504                                 # Setting the choice to an impossible integer to avoid errors in the numeric comparisons
2505                                 CDDBLOCALCHOICENUM=-1
2506                                 if [ "$INTERACTIVE" = "y" ]; then
2507                                         while [ "$CDDBLOCALCHOICENUM" -lt 0 ] || [ "$CDDBLOCALCHOICENUM" -gt "$CDDBLOCALCHOICES" ]; do
2508                                                 echo "Locally cached CDDB entries found." >&2
2509                                                 echo -n "Which one would you like to use (0 for none)? [0-$CDDBLOCALCHOICES]: " >&2
2510                                                 read CDDBLOCALCHOICE
2511                                                 [ x"$CDDBLOCALCHOICE" = "x" ] && CDDBLOCALCHOICE="1"
2512                                                 # FIXME # Introduce diff's
2513                                                 if echo $CDDBLOCALCHOICE | grep -E "[[:space:]]*[[:digit:]]+,[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
2514                                                         diffentries cddblocalread "$CDDBLOCALCHOICES" "$CDDBLOCALCHOICE"
2515                                                 elif echo $CDDBLOCALCHOICE | grep -E "[[:space:]]*[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
2516                                                         # Make sure we get a valid choice
2517                                                         CDDBLOCALCHOICENUM=$(echo $CDDBLOCALCHOICE | xargs printf %d 2>/dev/null)
2518                                                         if [ "$CDDBLOCALCHOICENUM" -lt 0 ] || [ "$CDDBLOCALCHOICENUM" -gt "$CDDBLOCALCHOICES" ]; then
2519                                                                 echo "Invalid selection. Please choose a number between 0 and $CDDBLOCALCHOICES." >&2
2520                                                         fi
2521                                                 fi
2522                                         done
2523                                 else
2524                                         CDDBLOCALCHOICENUM=1
2525                                 fi
2526                                 if [ ! "$CDDBLOCALCHOICENUM" = "0" ]; then
2527                                         #echo "Using local copy of CDDB data"
2528                                         echo "# DO NOT ERASE THIS LINE! Added by abcde to imitate cddb output" > "${ABCDETEMPDIR}/cddbread.1"
2529                                         cat "${ABCDETEMPDIR}/cddblocalread.$CDDBLOCALCHOICENUM" >> "${ABCDETEMPDIR}/cddbread.1"
2530                                         echo "local" > "${ABCDETEMPDIR}/datasource.1"
2531                                         echo 999 > "${ABCDETEMPDIR}/cddbquery" # Assuming 999 isn't used by CDDB
2532                                         echo cddb-readcomplete >> "${ABCDETEMPDIR}/status"
2533                                         do_cddbparse "${ABCDETEMPDIR}/cddbread.1" > "${ABCDETEMPDIR}/cddbchoices"
2534                                         echo cddb-choice=1 >> "${ABCDETEMPDIR}/status"
2535                                         CDDBLOCALSTATUS="found"
2536                                 else
2537                                         #echo "Not using local copy of CDDB data"
2538                                         CDDBLOCALSTATUS="notfound"
2539                                 fi
2540                                 ;;
2541                         single)
2542                                 # List out disc title/author and contents
2543                                 do_cddbparse "${CDDBLOCALFILE}"
2544                                 #if [ "$CDROMREADERSYNTAX" = "flac" ] ; then
2545                                 #       echo -n "Embedded cuesheet entry found, use it [Y/n]? " >&2
2546                                 #else
2547                                         echo -n "Locally cached CDDB entry found, use it [Y/n]? " >&2
2548                                 #fi
2549                                 if [ "$INTERACTIVE" = "y" ]; then
2550                                         read USELOCALRESP
2551                                         while [ "$USELOCALRESP" != "y" ] && [ "$USELOCALRESP" != "n" ] && [ "$USELOCALRESP" != "" ] ; do
2552                                                 echo -n 'Invalid selection. Please answer "y" or "n": ' >&2
2553                                                 read USELOCALRESP
2554                                         done
2555                                         [ x"$USELOCALRESP" = "x" ] && USELOCALRESP="y"
2556                                 else
2557                                         echo "y" >&2
2558                                 fi
2559                                 if [ "$USELOCALRESP" = "y" ]; then
2560                                         #echo "Using local copy of CDDB data"
2561                                         echo "# DO NOT ERASE THIS LINE! Added by abcde to imitate cddb output" > "${ABCDETEMPDIR}/cddbread.1"
2562                                         cat "${CDDBLOCALFILE}" >> "${ABCDETEMPDIR}/cddbread.1"
2563                                         echo "local" > "${ABCDETEMPDIR}/datasource.1"
2564                                         echo 999 > "${ABCDETEMPDIR}/cddbquery" # Assuming 999 isn't used by CDDB
2565                                         echo cddb-readcomplete >> "${ABCDETEMPDIR}/status"
2566                                         do_cddbparse "${CDDBLOCALFILE}" > "${ABCDETEMPDIR}/cddbchoices"
2567                                         echo cddb-choice=1 >> "${ABCDETEMPDIR}/status"
2568                                         CDDBLOCALSTATUS="single"
2569                                 else
2570                                         #echo "Not using local copy of CDDB data"
2571                                         CDDBLOCALSTATUS="notfound"
2572                                 fi
2573                                 ;;
2574                         none)
2575                                 CDDBLOCALSTATUS="notfound"
2576                                 ;;
2577                 esac
2578         fi
2579 }
2580
2581 # do_cdtext_read
2582 # Try to read CD-Text from the drive using icedax / cdda2wav
2583 do_cdtext_read ()
2584 {
2585         if new_checkexec icedax; then
2586                 CDTEXT_READER=icedax
2587         elif new_checkexec cdda2wav; then
2588                 CDTEXT_READER=cdda2wav
2589         else
2590                 # Didn't find either, bail
2591                 return 0
2592         fi
2593
2594         if checkstatus cdtext-readcomplete; then
2595                 NUM_RESPONSES=$(checkstatus "cdtext-entries")
2596                 NUM_CDDB_MATCHES=$(($NUM_CDDB_MATCHES + $NUM_RESPONSES))
2597                 vecho "Re-using existing data from CD-Text"
2598         else
2599                 vecho "Obtaining CD-Text results..."
2600                 local SOURCE_WORKDIR="${ABCDETEMPDIR}/data-cdtext"
2601                 mkdir -p "${SOURCE_WORKDIR}"
2602
2603                 if [ "$OSFLAVOUR" = "OSX" ] ; then
2604                         # Hei, we have to unmount the device before running anything like cdda2wav/icedax in OSX
2605                         diskutil unmount "${CDROM#/dev/}"
2606                         # Also, in OSX the cdrom device for cdda2wav/icedax changes...
2607                         CDDA2WAVCDROM="IODVDServices"
2608                 elif [ "$OSFLAVOUR" = "FBSD" ] || [ "$OSFLAVOUR" = "IRIX" ]; then
2609                         CDDA2WAVCDROM="$CDROMID"
2610                 else
2611                         if [ "$CDROMID" = "" ]; then
2612                                 CDDA2WAVCDROM="$CDROM"
2613                         else
2614                                 CDDA2WAVCDROM="$CDROMID"
2615                         fi
2616                 fi
2617
2618                 # Do we have CD-Text on the disc (and can the drive read it?)
2619                 (
2620                         cd "${SOURCE_WORKDIR}" && rm -f audio.* audio_*
2621                         # Even redirect fd 0 to our output file. It seems cdda2wav
2622                         # writes to stdin too these days. Bizarre, buggy
2623                         # behaviour! Should be harmless for icedax.
2624                         ${CDTEXT_READER} -J -v titles -D "${CDDA2WAVCDROM}" > "${SOURCE_WORKDIR}/cd-text" 2>&1 0>&1
2625                 )
2626                 grep -a -q '^CD-Text: detected' "${SOURCE_WORKDIR}/cd-text"
2627                 ERRORCODE=$?
2628                 if [ $ERRORCODE -ne 0 ]; then
2629                         # No CD-Text found, bail
2630                         return 0
2631                 fi
2632
2633                 NUM_CDDB_MATCHES=$(($NUM_CDDB_MATCHES + 1))
2634                 # Make an empty template
2635                 $CDDBTOOL template "$(cat "${ABCDETEMPDIR}/cddbdiscid")" > "${SOURCE_WORKDIR}/cddbread.${NUM_CDDB_MATCHES}"
2636                 echo "cddb-read-${NUM_CDDB_MATCHES}-complete" >> "${ABCDETEMPDIR}/status"
2637                 rm -f "${SOURCE_WORKDIR}/cddbread.${NUM_CDDB_MATCHES}"
2638
2639                 # XXX FIXME - this is a hack and should be replaced by proper
2640                 # character set tracking for the CDDB data we have.
2641                 if [ "$CDDBPROTO" -ge 6 ]; then
2642                         # convert to Unicode
2643                         iconv -f iso-8859-1 -t utf-8 <"${SOURCE_WORKDIR}/audio.cddb" >"${SOURCE_WORKDIR}/cddbread.${NUM_CDDB_MATCHES}"
2644                 else
2645                         # copy verbatim, assuming CD-TEXT is in ISO-8859-1 format
2646                         # apparently icedax/cdda2wav have no support for 16-bit
2647                         # characters yet, either
2648                         cp -p "${SOURCE_WORKDIR}/audio.cddb" "${SOURCE_WORKDIR}/cddbread.${NUM_CDDB_MATCHES}"
2649                 fi
2650
2651                 CDDBDISCID=$(echo "$CDDBTRACKINFO" | cut -d' ' -f1)
2652                 ATITLE=$(grep -a -e '^DTITLE=' "${SOURCE_WORKDIR}/cddbread.${NUM_CDDB_MATCHES}" | cut -c8- | tr -d \\r\\n)
2653                 echo "CD-Text" > "${SOURCE_WORKDIR}/datasource.${NUM_CDDB_MATCHES}"
2654                 echo "none ${CDDBDISCID} ${ATITLE}" >> "${SOURCE_WORKDIR}/cddbquery.${NUM_CDDB_MATCHES}"
2655
2656                 (
2657                         cd "${SOURCE_WORKDIR}"
2658                         rm -f audio_* audio.*
2659                         for file in cddbread.* cddbquery.* datasource.*; do
2660                                 if [ -f "$file" ]; then
2661                                         cp "$file" "${ABCDETEMPDIR}"
2662                                 fi
2663                         done
2664                 )
2665                 echo "cdtext-readcomplete" >> "${ABCDETEMPDIR}/status"
2666                 echo "cdtext-entries=1" >> "${ABCDETEMPDIR}/status"
2667         fi
2668 }
2669
2670 # do_musicbrainz_read
2671 # Work with the musicbrainz WS API, then transform the results here so
2672 # they look (very) like the results from CDDB. Maybe not the best way
2673 # to go, but it Works For Me (TM)
2674 #
2675 # List out all the matches individually into $SOURCE_WORKDIR/cddbread.X
2676 #
2677 do_musicbrainz_read ()
2678 {
2679         if checkstatus musicbrainz-readcomplete; then
2680                 NUM_RESPONSES=$(checkstatus "musicbrainz-entries")
2681                 NUM_CDDB_MATCHES=$(($NUM_CDDB_MATCHES + $NUM_RESPONSES))
2682                 vecho "Re-using existing data from $NUM_RESPONSES Musicbrainz match(es)"
2683         else
2684                 vecho "Obtaining Musicbrainz results..."
2685                 # If MB is to be used, interpret the query results and read all
2686                 # the available entries.
2687                 CDDBDISCID=$(echo "$CDDBTRACKINFO" | cut -d' ' -f1)
2688                 MBDISCID=$(echo "$MBTRACKINFO" | cut -d' ' -f1)
2689                 local SOURCE_WORKDIR="${ABCDETEMPDIR}/data-musicbrainz"
2690                 mkdir -p "${SOURCE_WORKDIR}"
2691                 ${MUSICBRAINZ} --command data --discid "${MBDISCID}" --workdir "${SOURCE_WORKDIR}" --start ${NUM_CDDB_MATCHES}
2692                 error=$?
2693                 if [ $error != 0 ]; then
2694                         log error "$MUSICBRAINZ failed to run; ABORT"
2695                         exit $error
2696                 fi
2697
2698                 # Check for no matches.
2699                 # The helper script will write disc matches out to
2700                 # cddbread.*. Count how many we have
2701                 NUM_RESPONSES=$(ls -1 "${SOURCE_WORKDIR}"/cddbread.* 2>/dev/null | wc -l)
2702                 if [ $NUM_RESPONSES -gt 0 ] ; then
2703                         # One or more exact matches
2704                         i=1
2705                         while [ $i -le $NUM_RESPONSES ]; do
2706                                 NUM_CDDB_MATCHES=$(($NUM_CDDB_MATCHES + 1))
2707                                 i=$(($i + 1))
2708                                 echo cddb-read-${NUM_CDDB_MATCHES}-complete >> "${ABCDETEMPDIR}/status"
2709                                 ATITLE=$(grep -a -e '^DTITLE=' "${SOURCE_WORKDIR}/cddbread.${NUM_CDDB_MATCHES}" | cut -c8- | tr -d \\r\\n)
2710                                 echo "none ${CDDBDISCID} ${ATITLE}" >> "${SOURCE_WORKDIR}/cddbquery.${NUM_CDDB_MATCHES}"
2711                                 echo "Musicbrainz" > "${SOURCE_WORKDIR}/datasource.${NUM_CDDB_MATCHES}"
2712                                 cp -f "${SOURCE_WORKDIR}/"*."${NUM_CDDB_MATCHES}" "${ABCDETEMPDIR}"
2713                         done
2714                 fi
2715                 echo "musicbrainz-readcomplete" >> "${ABCDETEMPDIR}/status"
2716                 echo "musicbrainz-entries=$NUM_RESPONSES" >> "${ABCDETEMPDIR}/status"
2717         fi
2718 }
2719
2720 # do_cddb_read
2721 do_cddb_read ()
2722 {
2723         local SOURCE_WORKDIR="${ABCDETEMPDIR}/data-cddb"
2724         mkdir -p "${SOURCE_WORKDIR}"
2725
2726         ###########
2727         # cddbstat
2728         ###########
2729
2730         # Perform CDDB protocol version check if it hasn't already been done
2731         if checkstatus cddb-statcomplete; then :; else
2732                 if [ "$CDDBAVAIL" = "n" ]; then
2733                         ERRORCODE=no_query
2734                         echo 503 > "${SOURCE_WORKDIR}/cddbstat"
2735                 else
2736                         rc=1
2737                         CDDBUSER=$(echo "$HELLOINFO" | cut -f1 -d'@')
2738                         CDDBHOST=$(echo "$HELLOINFO" | cut -f2- -d'@')
2739                         while [ $rc -eq 1 ] && [ "$CDDBPROTO" -ge 3 ]; do
2740                                 vecho "Checking CDDB server status..."
2741                                 $CDDBTOOL stat "$CDDBURL" "$CDDBUSER" "$CDDBHOST" "$CDDBPROTO" > "${SOURCE_WORKDIR}/cddbstat"
2742                                 RESPONSECODE=$(head -n 1 "${SOURCE_WORKDIR}/cddbstat" | cut -f1 -d' ')
2743                                 case "$RESPONSECODE" in
2744                                 210)    # 210 OK, status information follows (until terminating `.')
2745                                         rc=0
2746                                         ;;
2747                                 501)  # 501 Illegal CDDB protocol level: <n>.
2748                                         CDDBPROTO=$(($CDDBPROTO - 1))
2749                                         ;;
2750                                 *)      # Try a cddb query, since freedb2.org doesn't support the stat or ver commands
2751                                         # FreeDB TESTCD disc-id is used for query
2752                                         $CDDBTOOL query "$CDDBURL" "$CDDBPROTO" "$CDDBUSER" "$CDDBHOST" 03015501 1 296 344 > "${SOURCE_WORKDIR}/cddbstat"
2753                                         RESPONSECODE=$(head -n 1 "${SOURCE_WORKDIR}/cddbstat" | cut -f1 -d' ')
2754                                         case "$RESPONSECODE" in
2755                                                 2??)    # Server responded, everything seems OK
2756                                                         rc=0
2757                                                         ;;
2758                                                 *)      # unknown error
2759                                                         break
2760                                                         ;;
2761                                         esac
2762                                         ;;
2763                                 esac
2764                         done
2765                         if [ $rc -eq 1 ]; then
2766                                 CDDBAVAIL="n"
2767                         fi
2768                 fi
2769                 echo cddb-statcomplete >> "${ABCDETEMPDIR}/status"
2770         fi
2771
2772         ###########
2773         # cddbquery
2774         ###########
2775
2776         CDDBDISCID=$(echo "$CDDBTRACKINFO" | cut -d' ' -f1)
2777         CDDBLOCALFILE="${CDDBLOCALDIR}/${CDDBDISCID}"
2778
2779         # Perform CDDB query if it hasn't already been done
2780         if checkstatus cddb-querycomplete; then :; else
2781                 if [ "$CDDBAVAIL" = "n" ]; then
2782                         ERRORCODE=no_query
2783                         echo 503 > "${SOURCE_WORKDIR}/cddbquery"
2784                 # The default CDDBLOCALSTATUS is "notfound"
2785                 # This part will be triggered if the user CDDB repo does not
2786                 # contain the entry, or if we are not trying to use the repo.
2787                 else
2788                         vecho "Querying the CDDB server..."
2789                         CDDBUSER=$(echo "$HELLOINFO" | cut -f1 -d'@')
2790                         CDDBHOST=$(echo "$HELLOINFO" | cut -f2- -d'@')
2791                         $CDDBTOOL query "$CDDBURL" "$CDDBPROTO" "$CDDBUSER" "$CDDBHOST" "$CDDBTRACKINFO" > "${SOURCE_WORKDIR}/cddbquery"
2792                         ERRORCODE=$?
2793                         case $ERRORCODE in
2794                                 0)  # success
2795                                 ;;
2796                                 12|13|14)
2797                                         # no match found in database, wget/fetch error,
2798                                         # or user requested not to use CDDB. Make up an
2799                                         # error code (503) that abcde will recognize later
2800                                         # and compensate by making a template
2801                                         echo 503 > "${SOURCE_WORKDIR}/cddbquery"
2802                                 ;;
2803                                 *) # strange and unknown error
2804                                         echo "ERRORCODE=$ERRORCODE"
2805                                         echo "abcde: $CDDBTOOL returned unknown error code"
2806                                 ;;
2807                         esac
2808                 fi
2809                 echo cddb-querycomplete >> "${ABCDETEMPDIR}/status"
2810         fi
2811
2812         ###########
2813         # cddbread
2814         ###########
2815
2816         # If it's not to be used, generate a template.
2817         # Then, display it (or them) and let the user choose/edit it
2818         if checkstatus cddb-readcomplete; then
2819                 NUM_RESPONSES=$(checkstatus "cddb-entries")
2820                 NUM_CDDB_MATCHES=$(($NUM_CDDB_MATCHES + $NUM_RESPONSES))
2821                 vecho "Re-using existing data from $NUM_RESPONSES cddb match(es)"
2822         else
2823                 RESPONSECODE=$(head -n 1 "${SOURCE_WORKDIR}/cddbquery" | cut -f1 -d' ')
2824                 vecho "Obtaining CDDB results..."
2825                 case "$RESPONSECODE" in
2826                 200)
2827                         # One exact match, retrieve it
2828                         # 200 [section] [discid] [artist] / [title]
2829                         NUM_RESPONSES=1
2830                         NUM_CDDB_MATCHES=$(($NUM_CDDB_MATCHES + $NUM_RESPONSES))
2831                         $CDDBTOOL read "$CDDBURL" "$CDDBPROTO" "$CDDBUSER" "$CDDBHOST" \
2832                                           $(cut -f2,3 -d' ' "${SOURCE_WORKDIR}/cddbquery") \
2833                                           > "${SOURCE_WORKDIR}/cddbread.${NUM_CDDB_MATCHES}"
2834                         cat "${SOURCE_WORKDIR}/cddbquery" | cut -f2- -d' ' > "${SOURCE_WORKDIR}/cddbquery.${NUM_CDDB_MATCHES}"
2835                         echo "cddb" > "${SOURCE_WORKDIR}/datasource.${NUM_CDDB_MATCHES}"
2836                         echo "cddb-read-${NUM_CDDB_MATCHES}-complete" >> "${ABCDETEMPDIR}/status"
2837                         ;;
2838                 202|403|409|500|503)
2839                         # TODO: Explain these error codes a little more accurately:
2840                         # http://ftp.freedb.org/pub/freedb/misc/freedb_CDDB_protcoldoc.zip
2841                         # No match response:
2842                         ;;
2843                 210|211)
2844                         # Multiple exact, (possibly multiple) inexact matches
2845                         vecho -n "Retrieving multiple matches... "
2846                         grep -v '^[.]$' "${SOURCE_WORKDIR}/cddbquery" | (
2847                                 # IN A SUB-SHELL - VARIABLES MODIFIED
2848                                 # HERE DO NOT PERSIST IN THE PARENT
2849                                 read DISCINFO # eat top line
2850                                 while read DISCINFO
2851                                 do
2852                                         NUM_CDDB_MATCHES=$(($NUM_CDDB_MATCHES + 1))
2853                                         if checkstatus "cddb-read-${NUM_CDDB_MATCHES}-complete"; then :; else
2854                                                 $CDDBTOOL read "$CDDBURL" "$CDDBPROTO" "$CDDBUSER" "$CDDBHOST" \
2855                                                                   $(echo "$DISCINFO" | cut -f1,2 -d' ') \
2856                                                                   > "${SOURCE_WORKDIR}/cddbread.${NUM_CDDB_MATCHES}"
2857                                                 echo "$DISCINFO" > "${SOURCE_WORKDIR}/cddbquery.${NUM_CDDB_MATCHES}"
2858                                                 echo "cddb" > "${SOURCE_WORKDIR}/datasource.${NUM_CDDB_MATCHES}"
2859                                                 echo "cddb-read-${NUM_CDDB_MATCHES}-complete" >> "${ABCDETEMPDIR}/status"
2860                                         fi
2861                                 done )
2862                         # Need to re-count the entries here to be able
2863                         # to incrememnt $NUM_CDDB_MATCHES in the
2864                         # parent
2865                         NUM_RESPONSES=$(ls -1 "${SOURCE_WORKDIR}"/datasource.* 2>/dev/null | wc -l)
2866                         NUM_CDDB_MATCHES=$(($NUM_CDDB_MATCHES + $NUM_RESPONSES))
2867                         vecho "done."
2868                         ;;
2869                 999)
2870                         # Using local copy.
2871                         NUM_RESPONSES=1
2872                         NUM_CDDB_MATCHES=$(($NUM_CDDB_MATCHES + $NUM_RESPONSES))
2873                         echo "cddb-read-${NUM_CDDB_MATCHES}-complete" >> "${ABCDETEMPDIR}/status"
2874                         ;;
2875                 esac
2876                 (
2877                         cd "${SOURCE_WORKDIR}"
2878                         for file in cddbread.* cddbquery.* datasource.*; do
2879                                 if [ -f "$file" ]; then
2880                                         cp "$file" "${ABCDETEMPDIR}"
2881                                 fi
2882                         done
2883                 )
2884                 echo "cddb-readcomplete" >> "${ABCDETEMPDIR}/status"
2885                 echo "cddb-entries=$NUM_RESPONSES" >> "${ABCDETEMPDIR}/status"
2886         fi
2887 }
2888
2889 # do_cddbedit
2890 do_cddbedit ()
2891 {
2892         if checkstatus cddb-edit >/dev/null; then
2893                 CDDBDATA="${ABCDETEMPDIR}/cddbread.$(checkstatus cddb-choice)"
2894                 VARIOUSARTISTS="$(checkstatus variousartists)"
2895                 VARIOUSARTISTSTYLE="$(checkstatus variousartiststyle)"
2896                 return 0
2897         fi
2898         if [ "$INTERACTIVE" = "y" ]; then
2899                 # We should show the CDDB results both when we are not using the local CDDB repo
2900                 # or when we are using it but we could not find a proper match
2901                 if [ "$CDDBUSELOCAL" = "y" ] && [ "$CDDBLOCALSTATUS" = "notfound" ] || [ ! "$CDDBUSELOCAL" = "y" ]; then
2902                         # Display the ${ABCDETEMPDIR}/cddbchoices file created above
2903                         if [ -r "${ABCDETEMPDIR}/cddbchoices" ]; then
2904                                 CHOICE=$(checkstatus cddb-choice)
2905                                 if [ -n "$CHOICE" ] ; then
2906                                         case $NUM_CDDB_MATCHES in
2907                                                 1) cat "${ABCDETEMPDIR}/cddbchoices" ;;
2908                                                 *)
2909                                                 ATITLE=$(grep -a ^DTITLE= "${ABCDETEMPDIR}/cddbread.$CHOICE" | cut -f2- -d= | tr -d \\r\\n)
2910                                                 SOURCE=$(cat "${ABCDETEMPDIR}/datasource.$CHOICE")
2911                                                 echo "Selected: #$CDCHOICENUM ($SOURCE) ($ATITLE)"
2912                                                 do_cddbparse "${ABCDETEMPDIR}/cddbread.$CHOICE"
2913                                                 ;;
2914                                         esac
2915                                 else
2916                                         page "${ABCDETEMPDIR}/cddbchoices"
2917                                         CDDBCHOICENUM=""
2918                                         # Setting the choice to an impossible integer to avoid errors in the numeric comparisons
2919                                         CDCHOICENUM=-1
2920                                         # I'll take CDDB read #3 for $400, Alex
2921                                         while [ $CDCHOICENUM -lt 0 ] || [ $CDCHOICENUM -gt $NUM_CDDB_MATCHES ]; do
2922                                                 echo -n "Which entry would you like abcde to use (0 for none)? [0-$NUM_CDDB_MATCHES]: " >&2
2923                                                 read CDDBCHOICE
2924                                                 [ X"$CDDBCHOICE" = "X" ] && CDDBCHOICE=1
2925                                                 if echo $CDDBCHOICE | grep -E "[[:space:]]*[[:digit:]]+,[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
2926                                                         if [ ! X"$DIFF" = "X" ]; then
2927                                                                 PARSECHOICE1=$(echo $CDDBCHOICE | cut -d"," -f1 | xargs printf %d 2>/dev/null)
2928                                                                 PARSECHOICE2=$(echo $CDDBCHOICE | cut -d"," -f2 | xargs printf %d 2>/dev/null)
2929                                                                 if [ "$PARSECHOICE1" -lt 1 ] || [ "$PARSECHOICE1" -gt "$NUM_CDDB_MATCHES" ] || \
2930                                                                    [ "$PARSECHOICE2" -lt 1 ] || [ "$PARSECHOICE2" -gt "$NUM_CDDB_MATCHES" ] || \
2931                                                                    [ "$PARSECHOICE1" -eq "$PARSECHOICE2" ]; then
2932                                                                         echo "Invalid diff range." >&2
2933                                                                         echo "Please select two comma-separated numbers between 1 and $NUM_CDDB_MATCHES" >&2
2934                                                                 else
2935                                                                         # We parse the 2 choices to diff, store them in temporary files and diff them.
2936                                                                         for PARSECHOICE in $(echo $CDDBCHOICE | tr , \ ); do
2937                                                                                 do_cddbparse "${ABCDETEMPDIR}/cddbread.$PARSECHOICE" \
2938                                                                                                          > "${ABCDETEMPDIR}/cddbread.parsechoice.$PARSECHOICE"
2939                                                                         done
2940                                                                         echo "Showing diff between choices $PARSECHOICE1 and $PARSECHOICE2..." \
2941                                                                                  > "${ABCDETEMPDIR}/cddbread.diff"
2942                                                                         $DIFF $DIFFOPTS "${ABCDETEMPDIR}/cddbread.parsechoice.$PARSECHOICE1" \
2943                                                                                   "${ABCDETEMPDIR}/cddbread.parsechoice.$PARSECHOICE2" \
2944                                                                                   >> "${ABCDETEMPDIR}/cddbread.diff"
2945                                                                         page "${ABCDETEMPDIR}/cddbread.diff"
2946                                                                 fi
2947                                                         else
2948                                                                 echo "The diff program was not found in your path." >&2
2949                                                                 echo "Please choose a number between 0 and $NUM_CDDB_MATCHES." >&2
2950                                                         fi
2951                                                 elif echo $CDDBCHOICE | grep -E "[[:space:]]*[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
2952                                                         # Make sure we get a valid choice
2953                                                         CDCHOICENUM=$(echo $CDDBCHOICE | xargs printf %d 2>/dev/null)
2954                                                         if [ "$CDCHOICENUM" -lt 0 ] || [ "$CDCHOICENUM" -gt "$NUM_CDDB_MATCHES" ]; then
2955                                                                 echo "Invalid selection. Please choose a number between 0 and $NUM_CDDB_MATCHES." >&2
2956                                                         fi
2957                                                 fi
2958                                         done
2959                                         if [ "$CDCHOICENUM" = "0" ]; then
2960                                                 vecho "Creating empty CDDB template..."
2961                                                 UNKNOWNDISK=y
2962                                                 $CDDBTOOL template $(cat "${ABCDETEMPDIR}/cddbdiscid") > "${ABCDETEMPDIR}/cddbread.0"
2963                                                 echo "template" > "${ABCDETEMPDIR}/datasource.0"
2964                                         else
2965                                                 ATITLE=$(grep -a ^DTITLE= "${ABCDETEMPDIR}/cddbread.$CDCHOICENUM" | cut -f2- -d= | tr -d \\r\\n)
2966                                                 SOURCE=$(cat "${ABCDETEMPDIR}/datasource.$CDCHOICENUM")
2967                                                 echo "Selected: #$CDCHOICENUM ($SOURCE) ($ATITLE)" >&2
2968                                         fi
2969                                         do_cddbparse "${ABCDETEMPDIR}/cddbread.$CDCHOICENUM"
2970                                         echo "cddb-choice=$CDCHOICENUM" >> "${ABCDETEMPDIR}/status"
2971                                 fi
2972                         fi
2973                 else
2974                         # We need some code to show the selected option when local repository is selected and we have found a match
2975                         vecho "Using cached CDDB match..." >&2
2976                         # Display the ${ABCDETEMPDIR}/cddbchoices file created above
2977                         if [ -r "${ABCDETEMPDIR}/cddbchoices" ]; then
2978                                 CHOICE=$(checkstatus cddb-choice)
2979                                 if [ "$USELOCALRESP" = "y" ]; then :; else
2980                                         if [ -n "$CHOICE" ] ; then
2981                                                 case $NUM_CDDB_MATCHES in
2982                                                         0)
2983                                                         UNKNOWNDISK=y
2984                                                         echo "Selected template."
2985                                                         ;;
2986                                                         1) cat "${ABCDETEMPDIR}/cddbchoices" ;;
2987                                                         *)
2988                                                         echo "Selected: #$CHOICE"
2989                                                         do_cddbparse "${ABCDETEMPDIR}/cddbread.$CHOICE"
2990                                                         ;;
2991                                                 esac
2992                                         fi
2993                                 fi
2994                         fi
2995                 fi
2996         else
2997                 # We're noninteractive - pick the first choice.
2998                 # But in case we run a previous instance and selected a choice, use it.
2999                 if [ -r "${ABCDETEMPDIR}/cddbchoices" ]; then
3000                         # Show the choice if we are not using the locally stored one
3001                         # or when the local search failed to find a match.
3002                         PREVIOUSCHOICE=$(checkstatus cddb-choice)
3003                         if [ "$CDDBUSELOCAL" = "y" ] && [ "$CDDBLOCALSTATUS" = "notfound" ] || [ ! "$CDDBUSELOCAL" = "y" ]; then
3004                                 #if [ "$PREVIOUSCHOICE" ]; then
3005                                         cat "${ABCDETEMPDIR}/cddbchoices"
3006                                 #fi
3007                         fi
3008                         if [ ! -z "$PREVIOUSCHOICE" ] ; then
3009                                 CDCHOICENUM=$PREVIOUSCHOICE
3010                         else
3011                                 CDCHOICENUM=1
3012                                 echo "cddb-choice=$CDCHOICENUM" >> "${ABCDETEMPDIR}/status"
3013                         fi
3014                         echo "Selected: #$CDCHOICENUM ($(grep -a ^DTITLE= "${ABCDETEMPDIR}/cddbread.$CDCHOICENUM" | cut -f2- -d= | tr -d \\r\\n))" >&2
3015                 fi
3016         fi
3017
3018         # sanity check
3019         if checkstatus cddb-choice >/dev/null; then :; else
3020                 echo "abcde: internal error: cddb-choice not recorded." >&2
3021                 exit 1
3022         fi
3023         CDDBDATA="${ABCDETEMPDIR}/cddbread.$(checkstatus cddb-choice)"
3024         CDDBSOURCE=$(cat "${ABCDETEMPDIR}/datasource.$(checkstatus cddb-choice)")
3025         echo -n "Edit selected CDDB data " >&2
3026         if [ "$INTERACTIVE" = "y" ]; then
3027                 if [ "$UNKNOWNDISK" = "y" ]; then
3028                         echo -n "[Y/n]? " >&2
3029                         read EDITCDDB
3030                         [ "$EDITCDDB" != "n" ] && EDITCDDB=y
3031                 else
3032                         echo -n "[y/N]? " >&2
3033                         read EDITCDDB
3034                 fi
3035         else
3036                 echo "[y/N]? " >&2
3037                 EDITCDDB=n
3038         fi
3039         if [ "$EDITCDDB" = "y" ]; then
3040                 CDDBDATAMD5SUM=$($MD5SUM "$CDDBDATA" | cut -d " " -f 1);
3041
3042                 # Use the debian sensible-editor wrapper to pick the editor that the
3043                 # user has requested via their $EDITOR environment variable
3044                 if [ -x "/usr/bin/sensible-editor" ]; then
3045                         /usr/bin/sensible-editor "$CDDBDATA"
3046                 elif [ -n "$EDITOR" ]; then
3047                         if [ -x "$(which "${EDITOR%%\ *}")" ]; then
3048                                 # That failed, try to load the preferred editor, starting
3049                                 # with their EDITOR variable
3050                                 $EDITOR "$CDDBDATA"
3051                         fi
3052                 # If that fails, check for a vi
3053                 elif which vi >/dev/null 2>&1; then
3054                         vi "$CDDBDATA"
3055                 elif [ -x /usr/bin/vim ]; then
3056                         /usr/bin/vim "$CDDBDATA"
3057                 elif [ -x /usr/bin/vi ]; then
3058                         /usr/bin/vi "$CDDBDATA"
3059                 elif [ -x /bin/vi ]; then
3060                         /bin/vi "$CDDBDATA"
3061                 # nano should be on all (modern, i.e., sarge) debian systems
3062                 elif which nano >/dev/null 2>&1 ; then
3063                         nano "$CDDBDATA"
3064                 elif [ -x /usr/bin/nano ]; then
3065                         /usr/bin/nano "$CDDBDATA"
3066                 # mg should be on all OpenBSD systems
3067                 elif which mg >/dev/null 2>&1 ; then
3068                         mg "$CDDBDATA"
3069                 elif [ -x /usr/bin/mg ]; then
3070                         /usr/bin/mg "$CDDBDATA"
3071                 # bomb out
3072                 else
3073                         log warning "no editor available. Check your EDITOR environment variable."
3074                 fi
3075                 # delete editor backup file if it exists
3076                 if [ -w "$CDDBDATA~" ]; then
3077                         rm -f "$CDDBDATA~"
3078                 fi
3079         fi
3080
3081         # Some heuristics first. Look at Disc Title, and if it starts with
3082         # "Various", then we'll assume Various Artists
3083         if [ "$(grep -a ^DTITLE= "$CDDBDATA" | cut -f2- -d= | grep -aEci '^(various|soundtrack|varios|sonora|ost)')" != "0" ]; then
3084                 echo "Looks like a Multi-Artist CD" >&2
3085                 VARIOUSARTISTS=y
3086         else
3087                 echo -n "Is the CD multi-artist [y/N]? " >&2
3088                 if [ "$INTERACTIVE" = "y" ]; then
3089                         read VARIOUSARTISTS
3090                 else
3091                         echo n >&2
3092                         VARIOUSARTISTS=n
3093                 fi
3094         fi
3095         if [ "$VARIOUSARTISTS" = "y" ] && [ ! "$ONETRACK" = "y" ]; then
3096                 # Set a default
3097                 DEFAULTSTYLE=1
3098                 # Need NUMTRACKS before cddb-tool will return it:
3099                 NUMTRACKS=$(grep -a -E '^TTITLE[0-9]+=' "$CDDBDATA" | wc -l)
3100                 if [ "$(grep -ac "^TTITLE.*\/" "$CDDBDATA")" -gt "$(( $NUMTRACKS / 2 ))" ]; then
3101                         # More than 1/2 tracks contain a "/", so guess forward
3102                         DEFAULTSTYLE=1
3103                 elif [ "$(grep -ac "^TTITLE.*\-" "$CDDBDATA")" -gt "$(( $NUMTRACKS / 2 ))" ]; then
3104                         # More than 1/2 contain a "-", so guess forward-dash
3105                         DEFAULTSTYLE=2
3106                 elif [ "$(grep -ac "^TTITLE.*(.*)" "$CDDBDATA")" -gt "$(( $NUMTRACKS / 2 ))" ]; then
3107                         # More than 1/2 contain something in parens, so guess trailing-paren
3108                         DEFAULTSTYLE=6
3109                 fi
3110
3111                 echo "1) Artist / Title" >&2
3112                 echo "2) Artist - Title" >&2
3113                 echo "3) Title / Artist" >&2
3114                 echo "4) Title - Artist" >&2
3115                 echo "5) Artist: Title" >&2
3116                 echo "6) Title (Artist)" >&2
3117                 echo "7) This is a single-artist CD" >&2
3118                 echo -n "Which style of multiple artist entries is it? [1-7] ($DEFAULTSTYLE): " >&2
3119                 if [ "$INTERACTIVE" = "y" ]; then
3120                         read VARIOUSARTISTSTYLE
3121                 else
3122                         echo $DEFAULTSTYLE >&2
3123                         VARIOUSARTISTSTYLE=$DEFAULTSTYLE
3124                 fi
3125                 VARIOUSARTISTSTYLE=$(echo 0$VARIOUSARTISTSTYLE | xargs printf %d)
3126                 # If they press Enter, then the default style (0) was chosen
3127                 while [ "$VARIOUSARTISTSTYLE" -lt 0 ] || [ "$VARIOUSARTISTSTYLE" -gt 7 ]; do
3128                         echo "Invalid selection. Please choose a number between 1 and 7."
3129                         echo -n "Selection [1-7]: "
3130                         read VARIOUSARTISTSTYLE
3131                         VARIOUSARTISTSTYLE=$(echo "0$VARIOUSARTISTSTYLE" | xargs printf "%d")
3132                 done
3133                 if [ "$VARIOUSARTISTSTYLE" = "0" ]; then
3134                         VARIOUSARTISTSTYLE="$DEFAULTSTYLE"
3135                 fi
3136                 vecho "Selected: $VARIOUSARTISTSTYLE"
3137                 case "$VARIOUSARTISTSTYLE" in
3138                 1) # Artist / Title
3139                         VARIOUSARTISTSTYLE=forward
3140                         ;;
3141                 2) # Artist - Title
3142                         VARIOUSARTISTSTYLE=forward-dash
3143                         ;;
3144                 3) # Title / Artist
3145                         VARIOUSARTISTSTYLE=reverse
3146                         ;;
3147                 4) # Title - Artist
3148                         VARIOUSARTISTSTYLE=reverse-dash
3149                         ;;
3150                 5) # Artist: Title
3151                         VARIOUSARTISTSTYLE=colon
3152                         ;;
3153                 6) # Title (Artist)
3154                         VARIOUSARTISTSTYLE=trailing-paren
3155                         ;;
3156                 7) # Single Artist
3157                         VARIOUSARTISTS=n
3158                         ;;
3159                 esac
3160         fi
3161
3162         echo "variousartists=$VARIOUSARTISTS" >> "${ABCDETEMPDIR}/status"
3163         echo "variousartiststyle=$VARIOUSARTISTSTYLE" >> "${ABCDETEMPDIR}/status"
3164
3165         if [ "$EDITCDDB" = "y" ] && [ "$CDDBSOURCE" = "cddb" ] && [ "$UNINTENTIONALLY_ANGER_THE_FREEDB_PEOPLE" = "y" ]; then
3166                 if [ "$CDDBDATAMD5SUM" != "" ]  && [ "$CDDBDATAMD5SUM" != "$($MD5SUM "$CDDBDATA" | cut -d " " -f 1)" ]; then
3167                         # This works but does not have the necessary error checking
3168                         # yet. If you are familiar with the CDDB spec
3169                         # (see http://www.freedb.org/src/latest/DBFORMAT)
3170                         # and can create an error-free entry on your own, then put
3171                         # UNINTENTIONALLY_ANGER_THE_FREEDB_PEOPLE=y in your
3172                         # abcde.conf to enable it. Put CDDBSUBMIT=email@address in
3173                         # your abcde.conf to change the email address submissions are
3174                         # sent to.
3175
3176                         # submit the modified file, if they want
3177                         if [ "$NOSUBMIT" != "y" ]; then
3178                                 echo -n "Do you want to submit this entry to $CDDBSUBMIT [y/N]? "
3179                                 read YESNO
3180                                 while [ "$YESNO" != "y" ] && [ "$YESNO" != "n" ] && [ "$YESNO" != "Y" ] && \
3181                                         [ "$YESNO" != "N" ] && [ "$YESNO" != "" ]
3182                                 do
3183                                         echo -n 'Invalid selection. Please answer "y" or "n": '
3184                                         read YESNO
3185                                 done
3186                                 if [ "$YESNO" = "y" ] || [ "$YESNO" = "Y" ]; then
3187                                         echo -n "Sending..."
3188                                         $CDDBTOOL send "$CDDBDATA" "$CDDBSUBMIT"
3189                                         echo "done."
3190                                 fi
3191                         fi
3192                 fi
3193         fi
3194         ### FIXME ###
3195         # User CDDBLOCALPOLICY to find out if we store the file or not...
3196         # Cache edited CDDB entry in the user's cddb dir
3197         if [ "$CDDBCOPYLOCAL" = "y" ]; then
3198                 # Make sure the cache directory exists
3199                 mkdir -p "$CDDBLOCALDIR"
3200                 NUM_LINES=$(( $(wc -l < "$CDDBDATA") - 1 ))
3201                 OUTPUT_FILE="$(echo "$CDDBTRACKINFO" | cut -d' ' -f1)"
3202                 tail -n "$NUM_LINES" < "$CDDBDATA" > "${CDDBLOCALDIR}/${OUTPUT_FILE}"
3203         fi
3204
3205         echo "cddb-edit" >> "${ABCDETEMPDIR}/status"
3206 }
3207
3208 # do_getalbumart
3209 # try to download CD cover
3210 do_getalbumart()
3211 {
3212         if checkstatus "get-album-art"; then :; else
3213                 # set variables
3214                 ALBUMFILE="$(mungealbumname "$DALBUM")"
3215                 ARTISTFILE="$(mungeartistname "$DARTIST")"
3216                 GENRE="$(mungegenre "$GENRE")"
3217                 YEAR=${CDYEAR:-$CDYEAR}
3218                 # have we got a musicbrainz mbid or amazon asin?
3219                 case "$CDDBMETHOD" in
3220                         *musicbrainz*)
3221                                 # try musicbrainz mbid
3222                                 if [ -s "${ABCDETEMPDIR}/mbid.$(checkstatus cddb-choice)" ]; then
3223                                         MBID=$(cat "${ABCDETEMPDIR}/mbid.$(checkstatus cddb-choice)")
3224                                         vecho "trying to get cover from coverartarchive.orq with musicbrainz mbid $MBID" >&2
3225                                         ALBUMARTURL="http://coverartarchive.org/release/$MBID/front"
3226                                         vecho "cover URL: $ALBUMARTURL" >&2
3227                                         $HTTPGET "$ALBUMARTURL" > "${ABCDETEMPDIR}/$ALBUMARTFILE"
3228                                         if [ $? -ne 0 ]; then
3229                                                 vecho "could not download cover from musicbrainz" >&2
3230                                                 # try amazon asin
3231                                                 if [ -s "${ABCDETEMPDIR}/asin.$(checkstatus cddb-choice)" ]; then
3232                                                         ASIN=$(cat "${ABCDETEMPDIR}/asin.$(checkstatus cddb-choice)")
3233                                                         vecho "trying to get cover from amazon.com with asin $ASIN" >&2
3234                                                         ALBUMARTURL="http://ec1.images-amazon.com/images/P/$ASIN.01.LZZZZZZZZ.jpg"
3235                                                         vecho "cover URL: $ALBUMARTURL" >&2
3236                                                         $HTTPGET "$ALBUMARTURL" > "${ABCDETEMPDIR}/$ALBUMARTFILE"
3237                                                         if [ $? -ne 0 ]; then
3238                                                                 vecho "could not download cover from amazon" >&2
3239                                                         else
3240                                                                 # Check that size is reasonable; sometimes when there is no cover image
3241                                                                 # on amazon.com a 1x1 pixel gif image will be downloaded instead:
3242                                                                 FILESIZE=$(wc -c < "${ABCDETEMPDIR}/$ALBUMARTFILE")
3243                             &n