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