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