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