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