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