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