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