r47@yurie: data | 2005-02-16 03:38:54 +0200
[abcde.git] / abcde
1 #!/bin/sh
2 # Copyright (c) 1998-2001 Robert Woodcock <rcw@debian.org>
3 # Copyright (c) 2003-2004 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
8 # along with this program; if not, write to the Free Software
9 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
10 #
11 # Copyright for this work is to expire January 1, 2010, after which it
12 # shall be public domain.
13
14 VERSION="2.2.4"
15
16 usage ()
17 {
18 echo "This is abcde v$VERSION."
19 echo "Usage: abcde [options] [tracks]"
20 echo "Options:"
21 echo "-1     Encode the whole CD in a single file"
22 echo "-a <action1[,action2]...>"
23 echo "       Actions to perform (cddb,read,normalize,encode,tag,move,playlist,clean)"
24 #echo "-A     Experimental actions (retag, transcode)"
25 echo "-b     Batch mode: enable album normalization and nogap encoding"
26 echo "-c <file>"
27 echo "       Specify a configuration file (overrides system and user config files)"
28 echo "-C <discid#>"
29 echo "       Specify discid to resume from (only needed if you no longer have the cd)"
30 echo "-d <device>"
31 echo "       Specify CDROM device to grab"
32 echo "-D     Debugging mode (equivalent to sh -x abcde)"
33 echo "-h     This help information"
34 #echo "-i    Tag files while encoding, when possible (local only) -NWY-"
35 echo "-j <#> Number of encoder processes to run at once (localhost)"
36 echo "-k     Keep the wav tracks for later use"
37 echo "-l     Use low disk space algorithm"
38 echo "-L     Use local CDDB storage directory"
39 echo "-n     No lookup. Don't query CDDB, just create and use template"
40 echo "-N     Noninteractive. Never prompt for anything"
41 echo "-m     Modify playlist to include CRLF endings, to comply with some players"
42 echo "-o <type1[,type2]...>"
43 echo "       Output file type(s) (ogg,mp3,flac,spx,mpc). Defaults to ogg"
44 echo "-p     Pad track numbers with 0's (if less than 10 tracks)"
45 echo "-P     Use UNIX pipes to read+encode without wav files"
46 echo "-r <host1[,host2]...>"
47 echo "       Also encode on these remote hosts"
48 echo "-R     Add replaygain values to the tag info (only for ogg,flac)"
49 echo "-S <#> Set the CD speed"
50 #echo "-t    File types to preprocess (wav)"
51 #echo "-T    Set postprocessing options"
52 echo "-t <#> Start the track numbering at a given number"
53 echo "-T <#> Same as -t but modifies tag numbering"
54 echo "-v     Show version number and exit"
55 echo "-V     Be a bit more verbose about what is happening behind the scenes"
56 echo "-x     Eject CD after all tracks are read"
57 echo "-w <comment>"
58 echo "       Add a comment to the CD tracks"
59 echo "-W <#> Contatenate CDs: -T #01 -w \"CD #\"" 
60 echo ""
61 echo "Tracks is a space-delimited list of tracks to grab."
62 echo "Ranges specified with hyphens are allowed."
63 }
64
65 # Funtions to replace the need of seq, which is too distribution dependant.
66 f_seq_row ()
67 {
68         i=$1
69         while [ $i -ne `expr $2 + 1` ]
70         do
71                 echo $i
72                 i=`expr $i + 1`
73         done
74 }
75
76 f_seq_line ()
77 {
78         i=$1
79         while [ $i -ne `expr $2 + 1` ]
80         do
81                 printf $i" "
82                 i=`expr $i + 1`
83         done
84         echo
85 }
86
87 # checkstatus [blurb]
88 # Returns "0" if the blurb was found, returns 1 if it wasn't
89 # Puts the blurb content, if available, on stdout.
90 # Otherwise, returns "".
91 checkstatus ()
92 {
93         # Take the last line in the status file if there's multiple matches
94         PATTERN="^$1(=.*)?$"
95         BLURB=$(egrep $PATTERN "$ABCDETEMPDIR/status" | tail -n 1)
96
97         if [ -z "$BLURB" ]; then
98                 # No matches found
99                 return 1
100         else
101                 # Matches found
102                 # See if there's a = in it
103                 if [ "$(echo $BLURB | grep -c =)" != "0" ]; then
104                         echo "$(echo $BLURB | cut -f2- -d=)"
105                 fi
106                 return 0
107         fi
108 }
109
110 # checkerrors [blurb]
111 # Returns "0" if the blurb was found (meaning there was an error),
112 # returns 1 if it wasn't (yes this is a little backwards).
113 # Does not print the blurb on stdout.
114 # Otherwise, returns "".
115 checkerrors ()
116 {
117         if [ -e "$ABCDETEMPDIR/errors" ]; then :; else
118                 return 1
119         fi
120         # Take the last line in the status file if there's multiple matches
121         PATTERN="^$1(:.*)?$"
122         BLURB="$(egrep $PATTERN $ABCDETEMPDIR/errors | tail -n 1)"
123
124         if [ -z "$BLURB" ]; then
125                 # negative, we did not have a negative...
126                 return 1
127         else
128                 # affirmative, we had a negative...
129                 return 0
130         fi
131 }
132
133 # run_command [blurb] [command...]
134 # Runs a command, silently if necessary, and updates the status file
135 run_command ()
136 {
137         BLURB="$1"
138         shift
139         # See if this is supposed to be silent
140         if [ "$(checkstatus encode-output)" = "loud" ]; then
141                 "$@" >&2
142                 RETURN=$?
143         else
144                 # Special case for SMP, since
145                 # encoder output is never displayed, don't mute echos
146                 if [ -z "$BLURB" -a "$MAXPROCS" != "1" ]; then
147                         "$@" >&2
148                         RETURN=$?
149                 else
150                         "$@" >/dev/null 2>&1
151                         RETURN=$?
152                 fi
153         fi
154         case "$1" in
155         normalize)
156                 if [ "$RETURN" = "2" ]; then
157                         # File was already normalized.
158                         RETURN=0
159                 fi
160                 ;;
161         esac
162         if [ "$RETURN" != "0" ]; then
163                 # Put an error in the errors file. For various reasons we
164                 # can't capture a copy of the program's output but we can
165                 # log what we attempted to execute and the error code
166                 # returned by the program.
167                 if [ "$BLURB" ]; then
168                         TWEAK="$BLURB: "
169                 fi
170                 echo "${TWEAK}returned code $RETURN: $@" >> "$ABCDETEMPDIR/errors"
171                 return $RETURN # Do not pass go, do not update the status file
172         fi
173         if [ "$BLURB" ]; then
174                 echo $BLURB >> "$ABCDETEMPDIR/status"
175         fi
176 }
177
178 # relpath() and slash() are Copyright (c) 1999 Stuart Ballard and
179 # distributed under the terms of the GNU GPL v2 or later, at your option
180
181 # Function to determine if a word contains a slash.
182 slash ()
183 {
184         case "$1" in
185         */*) return 0;;
186         *) return 1;;
187         esac
188 }
189
190 # Function to give the relative path from one file to another.
191 # Usage: relpath fromfile tofile
192 # eg relpath music/Artist/Album.m3u music/Artist/Album/Song.mp3
193 # (the result would be Album/Song.mp3)
194 # Output is relative path to $2 from $1 on stdout
195
196 # This code has the following restrictions:
197 # Multiple ////s are not collapsed into single /s, with strange effects.
198 # Absolute paths and ../s are handled wrong in FR (but they work in TO)
199 # If FR is a directory it must have a trailing /
200
201 relpath ()
202 {
203         FR="$1"
204         TO="$2"
205
206         case "$TO" in
207         /*) ;; # No processing is needed for absolute paths
208         *)
209                 # Loop through common prefixes, ignoring them.
210                 while slash "$FR" && [ "$(echo "$FR" | cut -d/ -f1)" = "$(echo "$TO" | cut -d/ -f1)" ]
211                 do
212                         FR="$(echo "$FR" | cut -d/ -f2-)"
213                         TO="$(echo "$TO" | cut -d/ -f2-)"
214                 done
215                 # Loop through directory portions left in FR, adding appropriate ../s.
216                 while slash "$FR"
217                 do
218                         FR="$(echo "$FR" | cut -d/ -f2-)"
219                         TO="../$TO"
220                 done
221         esac
222
223         echo $TO
224 }
225
226 # This code splits the a Various Artist track name from one of the following
227 # forms:
228 #
229 #  forward:        Artist / Track
230 #  forward-dash:   Artist - Track
231 #  reverse:        Track / Artist
232 #  reverse-dash:   Track - Artist
233 #  colon:          Artist: Track
234 #  trailing-paren: Artist (Track)
235 #
236 # variables used:
237 # VARIOUSARTISTS, VARIOUSARTISTSTYLE, TRACKNAME, TRACKARTIST
238 splitvarious ()
239 {
240         if [ "$VARIOUSARTISTS" = "y" ] && [ ! "$ONETRACK" = "y" ]; then
241                 case "$VARIOUSARTISTSTYLE" in
242                 forward)
243                         DTITLEARTIST="$(echo $TRACKNAME | sed 's- / -~-g')"
244                         TRACKARTIST="$(echo $DTITLEARTIST | cut -f1 -d~)"
245                         TRACKNAME="$(echo $DTITLEARTIST | cut -f2 -d~)"
246                         ;;
247                 forward-dash)
248                         DTITLEARTIST="$(echo $TRACKNAME | sed 's, - ,~,g')"
249                         TRACKARTIST="$(echo $DTITLEARTIST | cut -f1 -d~)"
250                         TRACKNAME="$(echo $DTITLEARTIST | cut -f2 -d~)"
251                         ;;
252                 reverse)
253                         DTITLEARTIST="$(echo $TRACKNAME | sed 's- / -~-g')"
254                         TRACKARTIST="$(echo $DTITLEARTIST | cut -f2 -d~)"
255                         TRACKNAME="$(echo $DTITLEARTIST | cut -f1 -d~)"
256                         ;;
257                 reverse-dash)
258                         DTITLEARTIST="$(echo $TRACKNAME | sed 's, - ,~,g')"
259                         TRACKARTIST="$(echo $DTITLEARTIST | cut -f2 -d~)"
260                         TRACKNAME="$(echo $DTITLEARTIST | cut -f1 -d~)"
261                         ;;
262                 colon)
263                         DTITLEARTIST="$(echo $TRACKNAME | sed 's-: -~-g')"
264                         TRACKARTIST="$(echo $DTITLEARTIST | cut -f1 -d~)"
265                         TRACKNAME="$(echo $DTITLEARTIST | cut -f2 -d~)"
266                         ;;
267                 trailing-paren)
268                         DTITLEARTIST="$(echo $TRACKNAME | sed 's,^\(.*\) (\(.*\)),\1~\2,')"
269                         TRACKARTIST="$(echo $DTITLEARTIST | cut -f2 -d~)"
270                         TRACKNAME="$(echo $DTITLEARTIST | cut -f1 -d~)"
271                         ;;
272                 esac
273         elif [ "$ONETRACK" = "y" ]; then
274                 TRACKARTIST="Various"
275         else
276                 TRACKARTIST=$DARTIST
277         fi
278 }
279
280 # do_tag [tracknumber]
281 # id3 tags a filename
282 # variables used:
283 # TRACKS, TRACKNAME, TRACKARTIST, TAGGER, TAGGEROPTS, VORBISCOMMENT, METAFLAC, 
284 # COMMENT, DALBUM, DARTIST, CDYEAR, CDGENRE (and temporarily) ID3TAGV
285 do_tag ()
286 {
287         COMMENTOUTPUT="$(eval echo ${COMMENT})"
288         run_command '' echo "Tagging track $1 of $TRACKS: $TRACKNAME..."
289         # If we want to start the tracks with a given number, we need to modify the
290         # TRACKNUM value before evaluation
291         if [ -n "$STARTTRACKNUMBER" -a -n "$STARTTRACKNUMBERTAG" ] ; then
292                 # Get the trackpadding from the current track
293                 CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
294                 TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - 1 ))
295         fi
296         for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
297         do
298         case "$OUTPUT" in
299         mp3)
300                 # id3v2 v0.1.9 claims to have solved the -c bug, so we merge both id3 and id3v2
301                 run_command tagtrack-$1 $TAGGER $TAGGEROPTS -c "$COMMENTOUTPUT" \
302                         -A "$DALBUM" -a "$TRACKARTIST" -t "$TRACKNAME" -y "$CDYEAR" \
303                         -g "$CDGENRE" -T "$1/$TRACKS" "$ABCDETEMPDIR/track$1.$OUTPUT"
304                 ;;
305         ogg)
306                 case "$OGGENCODERSYNTAX" in
307                         vorbize|oggenc)
308                                 # vorbiscomment can't do in-place modification, mv the file first
309                                 if [ -f "$ABCDETEMPDIR/track$1.$OUTPUT" -a ! -f "$ABCDETEMPDIR/track$1.uncommented.$OUTPUT" ]; then
310                                         mv "$ABCDETEMPDIR/track$1.$OUTPUT" "$ABCDETEMPDIR/track$1.uncommented.$OUTPUT"
311                                 fi
312                                 (
313                                 # These are from http://www.xiph.org/ogg/vorbis/doc/v-comment.html
314                                 echo ARTIST=$TRACKARTIST
315                                 echo ALBUM="$DALBUM"
316                                 echo TITLE=$TRACKNAME
317                                 if [ -n "$CDYEAR" ]; then
318                                         echo DATE="$CDYEAR"
319                                 fi
320                                 if [ -n "$CDGENRE" ]; then
321                                         echo GENRE="$CDGENRE"
322                                 fi      
323                                 echo TRACKNUMBER=$1
324                                 echo CDDB=$CDDBDISCID
325                                 if [ "$(eval echo ${COMMENT})" != "" ]; then
326                                         case "$COMMENTOUTPUT" in
327                                                 *=*) echo "$COMMENTOUTPUT";;
328                                                 *)   echo COMMENT="$COMMENTOUTPUT";;
329                                         esac    
330                                 fi
331                                 ) | run_command tagtrack-$1 $VORBISCOMMENT -w \
332                                         "$ABCDETEMPDIR/track$1.uncommented.$OUTPUT" "$ABCDETEMPDIR/track$1.$OUTPUT"
333                                 # Doublecheck that the commented file was created successfully before wiping the original
334                                 if [ -f "$ABCDETEMPDIR/track$1.$OUTPUT" ]; then
335                                         rm -f "$ABCDETEMPDIR/track$1.uncommented.$OUTPUT"
336                                 else
337                                         mv "$ABCDETEMPDIR/track$1.uncommented.$OUTPUT" "$ABCDETEMPDIR/track$1.$OUTPUT"
338                                 fi
339                                 ;;
340                 esac
341                 ;;
342         flac)
343                 (
344                 echo ARTIST="$TRACKARTIST"
345                 echo ALBUM="$DALBUM"
346                 echo TITLE="$TRACKNAME"
347                 if [ -n "$CDYEAR" ]; then
348                         echo DATE="$CDYEAR"
349                 fi
350                 if [ -n "$CDGENRE" ]; then
351                         echo GENRE="$CDGENRE"
352                 fi      
353                 echo TRACKNUMBER=${TRACKNUM:-$1}
354                 echo CDDB=$CDDBDISCID
355                 if [ "$(eval echo ${COMMENT})" != "" ]; then
356                         case "$COMMENTOUTPUT" in
357                                 *=*) echo "$COMMENTOUTPUT";;
358                                 *)   echo COMMENT="$COMMENTOUTPUT";;
359                         esac    
360                 fi
361                 ) | run_command tagtrack-$1 $METAFLAC --import-tags-from=- --no-utf8-convert "$ABCDETEMPDIR/track$1.$OUTPUT"
362                 ;;
363         spx)
364                 run_command tagtrack-$1 true
365                 ;;
366         mpc)
367                 run_command tagtrack-$1 true
368                 ;;
369         esac
370         done
371 }
372
373 # do_batch_encode
374 # variables used:
375 # OUTPUTTYPE, {FOO}ENCODERSYNTAX, ENCNICE, ENCODER, ENCODEROPTS
376 do_batch_encode ()
377 {
378         # The commands here don't go through run_command because they're never supposed to be silenced
379         echo "Batch encoding tracks: $TRACKQUEUE"
380         OUTPUT=$(echo $OUTPUTTYPE | grep "mp3" )
381         case "$OUTPUT" in
382         mp3)
383                 case "$MP3ENCODERSYNTAX" in
384                 lame)
385                         (
386                         cd "$ABCDETEMPDIR"
387                         TRACKFILES=
388                         for UTRACKNUM in $TRACKQUEUE
389                         do
390                                 TRACKFILES="$TRACKFILES track$UTRACKNUM.wav"
391                         done
392                         nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS --nogap $TRACKFILES
393                         RETURN=$?
394                         if [ "$RETURN" != "0" ]; then
395                                 echo "batch-encode: $ENCODER returned code $RETURN" >> errors
396                         else
397                                 for UTRACKNUM in $TRACKQUEUE
398                                 do
399                                         echo encodetrack-$UTRACKNUM >> status
400                                 done
401                         fi
402                         )
403                         ;;
404                 esac
405                 ;;
406         esac
407         # Other encoders fall through to normal encoding as the tracks
408         # have not been entered in the status file.
409 }
410
411 # do_encode [tracknumber] [hostname]
412 # If no hostname is specified, encode locally
413 # variables used:
414 # TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
415 do_encode ()
416 {
417         IN="$ABCDETEMPDIR/track$1.wav"
418         # We need IN to proceed.
419         if [ -s "$IN" ] ; then
420                 for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
421                 do
422                         OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
423                         run_command '' echo "Encoding track $1 of $TRACKS: $TRACKNAME..."
424                         case "$OUTPUT" in
425                         mp3)
426                                 case "$2" in
427                                 %local*%)
428                                         case "$MP3ENCODERSYNTAX" in
429                                         lame|gogo) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS "$IN" "$OUT" ;;
430                                         bladeenc) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS -quit "$IN" ;;
431                                         l3enc|xingmp3enc) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $MP3ENCODER "$IN" "$OUT" $MP3ENCODEROPTS ;;
432                                         mp3enc) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $MP3ENCODER -if "$IN" -of "$OUT" $MP3ENCODEROPTS ;;
433                                         esac
434                                         ;;
435                                 *)
436                                         run_command encodetrack-$OUTPUT-$1 nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" >/dev/null 2>&1
437                                         ;;
438                                 esac
439                                 ;;
440                         ogg)
441                                 case "$2" in
442                                 %local*%)
443                                         case "$OGGENCODERSYNTAX" in
444                                         vorbize) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $OGGENCODER $OGGENCODEROPTS -w "$OUT" "$IN" ;;
445                                         oggenc) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $OGGENCODER $OGGENCODEROPTS -o "$OUT" "$IN" ;;
446                                         esac
447                                         ;;
448                                 *)
449                                         run_command encodetrack-$OUTPUT-$1 nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" >/dev/null 2>&1
450                                         ;;
451                                 esac
452                                 ;;
453                         flac)
454                                 case "$2" in
455                                 %local*%)
456                                         case "$FLACENCODERSYNTAX" in
457                                         flac) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $FLACENCODER  $FLACENCODEROPTS -o "$OUT" "$IN" ;; 
458                                         esac
459                                         ;;
460         
461                                 *)
462                                         echo -n "DISTMP3:"
463                                         echo "$DISTMP3 $DISTMP3OPTS $2 $IN $OUT >/dev/null 2>&1"
464                                         run_command encodetrack-$OUTPUT-$1 nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" > /dev/null 2>&1
465                                         ;;
466                                 esac
467                                 ;;
468                         spx)
469                                 if [ "$(eval echo ${COMMENT})" != "" ]; then
470                                         case "$COMMENT" in
471                                                 *=*) ;;
472                                                 *)   COMMENT="COMMENT=$COMMENT" ;;
473                                         esac    
474                                         COMMENT="--comment \"$COMMENT\""
475                                 fi
476                                 # Quick hack to avoid tagging Ogg/Speex, since there is no other way to tag than inline tagging
477                                 if [ ! "$DOTAG" = "y" ]; then
478                                         run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $SPEEXENCODER $SPEEXENCODEROPTS --author "$TRACKARTIST" --title "$TRACKNAME" "$COMMENT" "$IN" "$OUT"
479                                 else
480                                         run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $SPEEXENCODER $SPEEXENCODEROPTS "$IN" "$OUT"
481                                 fi
482                                 ;;
483                         mpc)    
484                                 # MPP/MP+(Musepack) format (.mpc) is done locally, with inline
485                                 # tagging.
486                                 # I tried compiling the mppenc from corecodecs.org and got some
487                                 # errors, so I have not tried it myself.
488                                 ## FIXME ## Needs some cleanup to determine if an empty tag sent
489                                 ## FIXME ## to the encoder ends up empty.
490                                 run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $MPPENCODER $MPPENCODEROPTS --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --track "$1" --genre "$CDGENRE" --year "$CDYEAR" --comment "$COMMENT" "$IN" "$OUT"
491                                 ;;
492                         esac
493                 done
494                 # Only remove .wav if the encoding succeeded
495                 if checkerrors "encodetrack-(.{3,4})-$1"; then 
496                         run_command encodetrack-$1 false
497                 else
498                         run_command encodetrack-$1 true
499                         if [ ! "$KEEPWAVS" = "y" ] ; then
500                                 rm -f "$IN"
501                         fi
502                 fi
503         else
504                 if [ "$(checkstatus encode-output)" = "loud" ]; then
505                         echo "HEH! The file we were about to encode disappeared:"
506                         echo ">> $IN"
507                 fi
508                 run_command encodetrack-$1 false
509         fi
510 }
511
512 # do_preprocess [tracknumber]
513 # variables used:
514 # TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
515 #do_preprocess ()
516 #{
517 #       IN="$ABCDETEMPDIR/track$1.wav"
518 #       # We need IN to proceed.
519 #       if [ -s "$IN" ] ; then
520 #               for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
521 #               do
522 #                       #OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
523 #                       run_command '' echo "Pre-processing track $1 of $TRACKS..."
524 #                       case "$POSTPROCESSFORMAT" in
525 #                       all|wav*)
526 #                               run_command preprocess-$OUTPUT-$1 nice $PRENICE $WAV_PRE $IF $OF ;;
527 #                       mp3)
528 #                               run_command preprocess-$OUTPUT-$1 nice $PRENICE $MP3_PRE $IF $OF ;;
529 #                       ogg)
530 #                               run_command preprocess-$OUTPUT-$1 nice $PRENICE $OGG_PRE $IF $OF ;;
531 #                       flac)
532 #                               run_command preprocess-$OUTPUT-$1 nice $PRENICE $FLAC_PRE $IF $OF ;;
533 #                       spx)
534 #                               run_command preprocess-$OUTPUT-$1 nice $PRENICE $SPX_PRE $IF $OF ;;
535 #                       esac
536 #               done
537 #               # Only remove .wav if the encoding succeeded
538 #               if checkerrors "preprocess-(.{3,4})-$1"; then 
539 #                       run_command preprocess-$1 false
540 #               else
541 #                       run_command preprocess-$1 true
542 #               fi
543 #       else
544 #               if [ "$(checkstatus encode-output)" = "loud" ]; then
545 #                       echo "HEH! The file we were about to pre-process disappeared:"
546 #                       echo ">> $IN"
547 #               fi
548 #               run_command preprocess-$1 false
549 #       fi
550 #}
551
552
553 # do_postprocess [tracknumber]
554 # variables used:
555 # TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
556 #do_postprocess ()
557 #{
558 #       for POSTPROCESSFORMAT in $(echo $POSTPROCESSFORMATS | tr , \ )
559 #       do
560 #               IN="$ABCDETEMPDIR/track$1.$POSTPROCESSFORMAT"
561 #               # We need IN to proceed.
562 #               if [ -s "$IN" ] ; then
563 #                       #OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
564 #                       run_command '' echo "Post-processing track $1 of $TRACKS..."
565 #                       case "$POSTPROCESSFORMAT" in
566 #                               mp3)
567 #                                       run_command postprocess-$OUTPUT-$1 nice $POSTNICE $MP3_POST $IF $OF ;;
568 #                               ogg)
569 #                                       run_command postprocess-$OUTPUT-$1 nice $POSTNICE $OGG_POST $IF $OF ;;
570 #                               flac)
571 #                                       run_command postprocess-$OUTPUT-$1 nice $POSTNICE $FLAC_POST $IF $OF ;;
572 #                               spx)
573 #                                       run_command postprocess-$OUTPUT-$1 nice $POSTNICE $SPX_POST $IF $OF ;;
574 #                       esac
575 #                       # Only remove .wav if the encoding succeeded
576 #                       if checkerrors "postprocess-(.{3,4})-$1"; then 
577 #                               run_command postprocess-$1 false
578 #                       else
579 #                               run_command postprocess-$1 true
580 #                       fi
581 #               else
582 #                       if [ "$(checkstatus encode-output)" = "loud" ]; then
583 #                               echo "HEH! The file we were about to post-process disappeared:"
584 #                               echo ">> $IN"
585 #                       fi
586 #                       run_command postprocess-$1 false
587 #               fi
588 #       done
589 #}
590
591 # do_batch_gain
592 # variables used:
593 # MP3GAIN, MP3GAINOPTS, VORBISGAIN, VORBISGAINOPTS
594 do_batch_gain ()
595 {
596         # The commands here don't go through run_command because they're never supposed to be silenced
597         echo "Batch analizing gain in tracks: $TRACKQUEUE"
598         (
599         cd "$ABCDETEMPDIR"
600         BLURB=
601         TRACKFILES=
602         for UTRACKNUM in $TRACKQUEUE
603         do
604                 MP3FILES="$TRACKFILES track$UTRACKNUM.mp3"
605         done
606         # XXX: Hard-coded batch option!
607         $NORMALIZER -b $NORMALIZEROPTS $TRACKFILES
608         RETURN=$?
609         if [ "$RETURN" != "0" ]; then
610                 echo "batch-normalize: $NORMALIZER returned code $RETURN" >> errors
611         else
612                 for UTRACKNUM in $TRACKQUEUE
613                 do
614                         echo normalizetrack-$UTRACKNUM >> status
615                 done
616         fi
617         )
618 }
619
620 # do_batch_normalize
621 # variables used:
622 # NORMALIZER, NORMALIZEROPTS
623 do_batch_normalize ()
624 {
625         # The commands here don't go through run_command because they're never supposed to be silenced
626         echo "Batch normalizing tracks: $TRACKQUEUE"
627         (
628         cd "$ABCDETEMPDIR"
629         BLURB=
630         TRACKFILES=
631         for UTRACKNUM in $TRACKQUEUE
632         do
633                 TRACKFILES="$TRACKFILES track$UTRACKNUM.wav"
634         done
635         # XXX: Hard-coded batch option!
636         $NORMALIZER -b $NORMALIZEROPTS $TRACKFILES
637         RETURN=$?
638         if [ "$RETURN" != "0" ]; then
639                 echo "batch-normalize: $NORMALIZER returned code $RETURN" >> errors
640         else
641                 for UTRACKNUM in $TRACKQUEUE
642                 do
643                         echo normalizetrack-$UTRACKNUM >> status
644                 done
645         fi
646         )
647 }
648
649 # do_normalize [tracknumber]
650 # variables used:
651 # TRACKS, TRACKNAME, NORMALIZER, NORMALIZEROPTS
652 do_normalize ()
653 {
654         IN="$ABCDETEMPDIR/track$1.wav"
655         if [ -e "$IN" ] ; then
656                 run_command '' echo "Normalizing track $1 of $TRACKS: $TRACKNAME..."
657                 run_command normalizetrack-$1 $NORMALIZER $NORMALIZEROPTS "$IN"
658         else
659                 if [ "$(checkstatus encode-output)" = "loud" ]; then
660                         echo "HEH! The file we were about to normalize disappeared:"
661                         echo ">> $IN"
662                 fi
663                 run_command normalizetrack-$1 false "File $IN was not found"
664         fi
665 }
666
667 # do_move [tracknumber]
668 # Deduces the outfile from environment variables
669 # Creates directory if necessary
670 # variables used:
671 # TRACKNUM, TRACKNAME, TRACKARTIST, DALBUM, OUTPUTFORMAT, CDGENRE, CDYEAR
672 do_move ()
673 {
674         for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
675         do
676                 # Create ALBUMFILE, ARTISTFILE, TRACKFILE
677                 # Munge filenames as follows:
678                 # ' ' -> '_'
679                 # '/' -> '_'
680                 # ''' -> ''
681                 # '?' -> ''
682                 # Eat control characters
683                 ALBUMFILE=$(mungefilename "$DALBUM")
684                 ARTISTFILE=$(mungefilename "$TRACKARTIST")
685                 TRACKFILE=$(mungefilename "$TRACKNAME")
686                 GENRE=$(mungegenre "$GENRE")
687                 YEAR=$(echo $CDYEAR)
688                 # If we want to start the tracks with a given number, we need to modify the
689                 # TRACKNUM value before evaluation
690                 if [ -n "$STARTTRACKNUMBER" ] ; then
691                         # Get the trackpadding from the current track
692                         CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
693                         TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - 1 ))
694                 else
695                         TRACKNUM=${UTRACKNUM}
696                 fi
697                 # Supported variables for OUTPUTFORMAT are GENRE, ALBUMFILE, ARTISTFILE,
698                 # TRACKFILE, and TRACKNUM.
699                 if [ "$VARIOUSARTISTS" = "y" ]; then
700                         OUTPUTFILE=$(eval echo $VAOUTPUTFORMAT)
701                         else
702                         OUTPUTFILE=$(eval echo $OUTPUTFORMAT)
703                 fi
704                 # Check that the directory for OUTPUTFILE exists, if it doesn't, create it
705                 OUTPUTFILEDIR=$(dirname "$OUTPUTDIR/$OUTPUTFILE")
706                 # mkdir -p shouldn't return an error if the directory already exists
707                 mkdir -p "$OUTPUTFILEDIR"
708                 run_command movetrack-$1 mv "$ABCDETEMPDIR/track$1.$OUTPUT" "$OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
709         done
710 }
711
712 # do_playlist
713 # Create the playlist if wanted
714 # Variables used:
715 # PLAYLISTFORMAT, PLAYLISTDATAPREFIX, VAPLAYLISTFORMAT, VAPLAYLISTDATAPREFIX,
716 # VARIOUSARTISTS, OUTPUTDIR
717 do_playlist ()
718 {
719         for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
720         do
721                 # Create a playlist file for the playlist data to go into.
722                 # We used to wipe it out if it existed. Now we request permision if interactive.
723                 for LASTTRACK in $TRACKQUEUE; do :; done
724                 ALBUMFILE=$(mungefilename "$DALBUM")
725                 ARTISTFILE=$(mungefilename "$DARTIST")
726                 GENRE=$(mungegenre "$GENRE")
727                 if [ "$VARIOUSARTISTS" = "y" ] ; then
728                         PLAYLISTFILE=$(eval echo $VAPLAYLISTFORMAT)
729                 else
730                         PLAYLISTFILE=$(eval echo $PLAYLISTFORMAT)
731                 fi
732                 FINALPLAYLISTDIR=$(dirname "$OUTPUTDIR/$PLAYLISTFILE")
733                 mkdir -p "$FINALPLAYLISTDIR"
734                 if [ -s "$OUTPUTDIR/$PLAYLISTFILE" ]; then
735                         echo -n "Erase, Append to, or Keep the existing playlist file? [e/a/k] (e): " >&2
736                         if [ "$INTERACTIVE" = "y" ]; then
737                                 while [ "$DONE" != "y" ]; do
738                                         read ERASEPLAYLIST
739                                         case $ERASEPLAYLIST in
740                                                 e|E|a|A|k|K) DONE=y ;;
741                                                 *) ;;
742                                         esac
743                                 done
744                         else
745                                 echo e >&2
746                                 ERASEPLAYLIST=e
747                         fi
748                         # Once we erase the playlist, we use append to create the new one.
749                         [ "$ERASEPLAYLIST" = "e" -o "$ERASEPLAYLIST" = "E" ] && rm -f "$OUTPUTDIR/$PLAYLISTFILE" && ERASEPLAYLIST=a
750                 else
751                         # The playlist does not exist, so we can safelly use append to create the new list
752                         ERASEPLAYLIST=a
753                 fi
754                 if [ "$ERASEPLAYLIST" = "a" -o "$ERASEPLAYLIST" = "A" ]; then
755                         touch "$OUTPUTDIR/$PLAYLISTFILE"
756                         for UTRACKNUM in $TRACKQUEUE
757                         do
758                                 # Shares some code with do_move since the filenames have to match
759                                 CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
760                                 TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2 -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')
761                                 splitvarious
762                                 TRACKFILE=$(mungefilename "$TRACKNAME")
763                                 ARTISTFILE=$(mungefilename "$TRACKARTIST")
764                                 # If we want to start the tracks with a given number, we need to modify the
765                                 # TRACKNUM value before evaluation
766                                 if [ -n "$STARTTRACKNUMBER" ] ; then
767                                         # Get the trackpadding from the current track
768                                         CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
769                                         TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - 1 ))
770                                 else
771                                         TRACKNUM=${UTRACKNUM}
772                                 fi
773                                 if [ "$VARIOUSARTISTS" = "y" ]; then
774                                         OUTPUTFILE=$(eval echo $VAOUTPUTFORMAT)
775                                 else
776                                         OUTPUTFILE=$(eval echo $OUTPUTFORMAT)
777                                 fi
778                                 if [ "$VARIOUSARTISTS" = "y" ]; then
779                                         if [ "$VAPLAYLISTDATAPREFIX" ] ; then
780                                                 echo ${VAPLAYLISTDATAPREFIX}$OUTPUTFILE.$OUTPUT >> "$OUTPUTDIR/$PLAYLISTFILE"
781                                         else
782                                                 relpath "$PLAYLISTFILE", "$OUTPUTFILE.$OUTPUT" >> "$OUTPUTDIR/$PLAYLISTFILE"
783                                         fi
784                                 else
785                                         if [ "$PLAYLISTDATAPREFIX" ]; then
786                                                 echo ${PLAYLISTDATAPREFIX}$OUTPUTFILE.$OUTPUT >> "$OUTPUTDIR/$PLAYLISTFILE"
787                                         else
788                                                 relpath "$PLAYLISTFILE", "$OUTPUTFILE.$OUTPUT" >> "$OUTPUTDIR/$PLAYLISTFILE"
789                                         fi
790                                 fi
791                         done
792                 fi
793                 ## this will convert the playlist to have CRLF line-endings, if specified
794                 ## (some hardware players insist on CRLF endings)
795                 if [ "$DOSPLAYLIST" = "y" ]; then
796                         awk '{substr("\r",""); printf "%s\r\n", $0}' "$OUTPUTDIR/$PLAYLISTFILE" > "$ABCDETEMPDIR/PLAYLISTFILE.tmp"
797 #                       mv -f "$ABCDETEMPDIR/PLAYLISTFILE.tmp" "$OUTPUTDIR/$PLAYLISTFILE"
798                         cat "$ABCDETEMPDIR/PLAYLISTFILE.tmp" | sed 's/\//\\/' > "$OUTPUTDIR/$PLAYLISTFILE"
799                 fi
800                 echo "playlistcomplete" >> "$ABCDETEMPDIR/status"
801         done
802 }
803
804 # do_discid
805 # This essentially the start of things
806 do_discid ()
807 {
808         # Query the CD to get the track info, unless the user specified -C
809         # or we are using some actions which do not need the CDDB data at all
810         #if [ ! X"$EXPACTIONS" = "X" ]; then
811         #       :
812         #elif [ -z "$DISCID" ]; then
813         if [ -z "$DISCID" ]; then
814                 vecho -n "Getting CD track info... "
815                 TRACKINFO=$($CDDISCID $CDROM)
816                 # Make sure there's a CD in there by checking cd-discid's return code
817                 if [ "$?" = "1" ]; then
818                         echo "abcde error: CD could not be read. Perhaps there's no CD in the drive?" >&2
819                         exit 1
820                 fi
821                 WEHAVEACD=y
822         else
823                 TRACKINFO=$(cat "$WAVOUTPUTDIR/abcde.$DISCID/discid")
824         fi
825
826         # Get a full enumeration of tracks, sort it, and put it in the TRACKQUEUE.
827         # This needs to be done now because a section of the resuming code will need
828         # it later.
829
830         # get the number of digits to pad TRACKNUM with - we'll use this later
831         # a CD can only hold 99 tracks, but since we support a feature for starting
832         # numbering the tracks from a given number, we might need to set it as a
833         # variable for the user to define... or obtain it somehow.
834         if [ "$PADTRACKS" = "y" ] ; then
835                 TRACKNUMPADDING=2
836         fi
837
838         ABCDETEMPDIR="$WAVOUTPUTDIR/abcde.$(echo $TRACKINFO | cut -f1 -d' ')"
839         if [ -z "$TRACKQUEUE" ]; then
840                 if [ ! "$STRIPDATATRACKS" = "y" ]; then
841                         case "$CDROMREADERSYNTAX" in
842                                 cdparanoia|debug)
843                                         if [ "$WEHAVEACD" = "y" ]; then
844                                                 vecho "Querying the CD for audio tracks..."
845                                                 CDPARANOIAOUTPUT=$( $CDROMREADER -d $CDROM -Q --verbose 2>&1 )
846                                                 RET=$?
847                                                 if [ ! "$RET" = "0" ];then
848                                                         echo "Warning: Something went wrong while querying the CD... Maybe a DATA CD?"
849                                                 fi
850                                                 TRACKS=$( echo $CDPARANOIAOUTPUT | egrep '^[[:space:]]+[[:digit:]]' | tail -n 1 |  awk '{print $1}' | tr -d "." | tr '\n' ' ' )
851                                                 CDPARANOIAAUDIOTRACKS="$TRACKS"
852                                         else
853                                                 if [ -f "$ABCDETEMPDIR/status" ] && checkstatus cdparanoia-audio-tracks ; then
854                                                         TRACKS=$( cat $ABCDETEMPDIR/cdparanoia-audio-tracks )
855                                                 else
856                                                         TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
857                                                 fi
858                                         fi
859                                         ;;
860                                 *)      TRACKS=$(echo $TRACKINFO | cut -f2 -d' ') ;;
861                         esac
862                 else
863                         TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
864                 fi
865                 if echo "$TRACKS" | grep [[:digit:]] > /dev/null 2>&1 ;then :;else
866                         echo "The disc does not contain any tracks. Giving up..."
867                         exit 0
868                 fi
869                 echo -n "Grabbing entire CD - tracks: "
870                 if [ ! "$PADTRACKS" = "y" ] ; then
871                         TRACKNUMPADDING=$(echo -n $TRACKS | wc -c | tr -d ' ')
872                 fi
873                 TRACKS=$(printf "%0.${TRACKNUMPADDING}d" $TRACKS)
874                 X=0
875                 while [ "$X" -ne "$TRACKS" ]
876                 do
877                         X=$(printf "%0.${TRACKNUMPADDING}d" $(expr $X + 1))
878                         TRACKQUEUE=$(echo "$TRACKQUEUE" $X)
879                 done
880                 echo $TRACKQUEUE
881         else
882                 TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
883                 # User-supplied track queue.
884                 # Weed out non-numbers, whitespace, then sort and weed out duplicates
885                 TRACKQUEUE=$(echo $TRACKQUEUE | sed 's-[^0-9 ]--g' | tr ' ' '\n' | grep -v ^$ | sort -n | uniq | tr '\n' ' ' | sed 's- $--g')
886                 # Once cleaned, obtain the highest value in the trackqueue for number padding
887                 for LASTTRACK in $TRACKQUEUE; do :; done
888                 if [ ! "$PADTRACKS" = "y" ] ; then
889                         TRACKNUMPADDING=$(echo -n $LASTTRACK | wc -c | tr -d ' ')
890                 fi
891                 # Now we normalize the trackqueue
892                 for TRACK in $TRACKQUEUE ; do
893                         TRACKNUM=$(printf %0.${TRACKNUMPADDING}d $(expr ${TRACK} + 0 ))
894                         PADTRACKQUEUE=$(echo $PADTRACKQUEUE $TRACKNUM)
895                 done
896                 TRACKQUEUE=$PADTRACKQUEUE
897                 echo Grabbing tracks: "$TRACKQUEUE"
898         fi
899
900 #       for LASTTRACK in $TRACKQUEUE; do :; done
901
902         QUEUEDTRACKS=$(echo $TRACKQUEUE | wc -w | tr -d ' ')
903
904         # We have the discid, create a temp directory after it to store all the temp
905         # info
906
907         if [ -e "$ABCDETEMPDIR" ]; then
908                 echo -n "abcde: attempting to resume from $ABCDETEMPDIR"
909                 # It already exists, see if it's a directory
910                 if [ ! -d "$ABCDETEMPDIR" ]; then
911                         # This is a file/socket/fifo/device/etc, not a directory
912                         # Complain and exit
913                         echo >&2
914                         echo "abcde: file $ABCDETEMPDIR already exists and does not belong to abcde." >&2
915                         echo "Please investigate, remove it, and rerun abcde." >&2
916                         exit 1
917                 fi
918                 echo -n .
919                 # It's a directory, let's see if it's owned by us
920                 if [ ! -O "$ABCDETEMPDIR" ]; then
921                         # Nope, complain and exit
922                         echo >&2
923                         echo "abcde: directory $ABCDETEMPDIR already exists and is not owned by you." >&2
924                         echo "Please investigate, remove it, and rerun abcde." >&2
925                         exit 1
926                 fi
927                 echo .
928                 # See if it's populated
929                 if [ ! -f "$ABCDETEMPDIR/discid" ]; then
930                         # Wipe and start fresh
931                         echo "abcde: $ABCDETEMPDIR/discid not found. Abcde must remove and recreate" >&2
932                         echo -n "this directory to continue. Continue? [y/n] (n)" >&2
933                         if [ "$INTERACTIVE" = "y" ]; then
934                                 read ANSWER
935                         else
936                                 echo y >&2
937                                 ANSWER=y
938                         fi
939                         if [ "$ANSWER" != "y" ]; then
940                                 exit 1
941                         fi
942                         rm -rf "$ABCDETEMPDIR" || exit 1
943                         mkdir "$ABCDETEMPDIR"
944                         if [ "$?" -gt "0" ]; then
945                                 # Directory already exists or could not be created
946                                 echo "abcde: Temp directory $ABCDETEMPDIR could not be created." >&2
947                                 exit 1
948                         fi
949                 else
950                         # Everything is fine. Check for ^encodetracklocation-
951                         # and encode-output entries in the status file and
952                         # remove them. These are not relevant across sessions.
953                         if [ -f "$ABCDETEMPDIR/status" ]; then
954                                 mv "$ABCDETEMPDIR/status" "$ABCDETEMPDIR/status.old"
955                                 grep -v ^encodetracklocation- < "$ABCDETEMPDIR/status.old" \
956                                         | grep -v ^encode-output > "$ABCDETEMPDIR/status"
957                         fi
958                         # Remove old error messages
959                         if [ -f "$ABCDETEMPDIR/errors" ]; then
960                                 rm -f "$ABCDETEMPDIR/errors"
961                         fi
962                 fi
963         else
964                 # We are starting from scratch
965                 mkdir "$ABCDETEMPDIR"
966                 if [ "$?" -gt "0" ]; then
967                         # Directory already exists or could not be created
968                         echo "abcde: Temp directory $ABCDETEMPDIR could not be created." >&2
969                         exit 1
970                 fi
971                 cat /dev/null > "$ABCDETEMPDIR/status"
972         fi
973         
974         # If we got the CDPARANOIA status and it is not recorded, save it now
975         if [ -n "$CDPARANOIAAUDIOTRACKS" ]; then
976                 if checkstatus cdparanoia-audio-tracks; then :; else
977                         if echo "$CDPARANOIAAUDIOTRACKS" >> "$ABCDETEMPDIR/cdparanoia-audio-tracks"; then
978                                 echo "cdparanoia-audio-tracks" >> "$ABCDETEMPDIR/status"
979                         fi
980                 fi
981         fi
982         
983         # Create the discid file
984         echo "$TRACKINFO" > "$ABCDETEMPDIR/discid"
985 }
986
987 # do_cddbparse
988 # Parses a CDDB file and outputs the title and the track names.
989 # Variables: CDDBFILE
990 do_cddbparse ()
991 {
992         CDDBPARSEFILE="$1"
993         # List out disc title/author and contents
994         if [ "$ONETRACK" = "y" ]; then
995                 vecho "ONETRACK mode selected: displaying only the title of the CD..."
996         fi
997         echo "---- $(grep DTITLE "${CDDBPARSEFILE}" | cut '-d=' -f2- | tr -d \\r\\n ) ----"
998         if [ ! "$ONETRACK" = "y" ]; then
999                 for TRACK in $(f_seq_row 1 $TRACKS)
1000                 do
1001                         echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "${CDDBPARSEFILE}" | cut -f2- -d= | tr -d \\r\\n)"
1002                 done
1003         fi
1004 }
1005
1006 # do_localcddb
1007 # Check for a local CDDB file, and report success
1008 do_localcddb ()
1009 {
1010         if checkstatus cddb-readcomplete && checkstatus cddb-choice >/dev/null; then :; else
1011         
1012                 CDDBLOCALSUCCESS="n"
1013                 CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
1014                 CDDBLOCALFILE="${CDDBLOCALDIR}/${CDDBDISCID}"
1015                 USELOCALRESP="y"
1016                 
1017                 # If the user has selected to check a local CDDB repo, we proceed with it
1018                 if [ -r "${CDDBLOCALFILE}" ]; then
1019                         # List out disc title/author and contents
1020                         do_cddbparse "${CDDBLOCALFILE}"
1021                         echo -n "Locally cached CDDB entry found, use it? [y/n] (y): "
1022                         if [ "$INTERACTIVE" = "y" ]; then
1023                                 read USELOCALRESP
1024                                 while [ "$USELOCALRESP" != "y" ] && [ "$USELOCALRESP" != "n" ] && [ "$USELOCALRESP" != "" ] ; do
1025                                         echo -n 'Invalid selection. Please answer "y" or "n": '
1026                                         read USELOCALRESP
1027                                 done
1028                                 [ x"$USELOCALRESP" = "x" ] && USELOCALRESP="y"
1029                         else
1030                                 echo "y" >&2
1031                         fi
1032                         if [ "$USELOCALRESP" = "y" ]; then
1033                         #echo "Using local copy of CDDB data"
1034                                 cp "${CDDBLOCALFILE}" "$ABCDETEMPDIR/cddbread.1"
1035                                 echo 999 > "$ABCDETEMPDIR/cddbquery" # Assuming 999 isn't used by CDDB
1036                                 echo cddb-readcomplete >> "$ABCDETEMPDIR/status"
1037                                 do_cddbparse "${CDDBLOCALFILE}" > "$ABCDETEMPDIR/cddbchoices"
1038                                 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
1039                                 CDDBLOCALSUCCESS="y"
1040                         else
1041                                 #echo "Not using local copy of CDDB data"
1042                                 CDDBLOCALSUCCESS="n"
1043                         fi
1044                         CDDBLOCALSUCCESS="y"
1045                 else
1046                         CDDBLOCALSUCCESS="n"
1047                 fi
1048         fi
1049 }
1050
1051 # do_cddbstat
1052 do_cddbstat ()
1053 {
1054         # Perform CDDB protocol version check if it hasn't already been done
1055         if checkstatus cddb-statcomplete; then :; else
1056                 if [ "$CDDBAVAIL" = "n" ]; then
1057                         ERRORCODE=no_query
1058                         echo 503 > "$ABCDETEMPDIR/cddbstat"
1059                 else
1060                         rc=1
1061                         CDDBUSER=$(echo $HELLOINFO | cut -f1 -d'@')
1062                         CDDBHOST=$(echo $HELLOINFO | cut -f2- -d'@')
1063                         while test $rc -eq 1 -a $CDDBPROTO -ge 3; do
1064                                 vecho "Checking CDDB server status..."
1065                                 $CDDBTOOL stat $CDDBURL $CDDBUSER $CDDBHOST $CDDBPROTO > "$ABCDETEMPDIR/cddbstat"
1066                                 RESPONSECODE=$(head -n 1 "$ABCDETEMPDIR/cddbstat" | cut -f1 -d' ')
1067                                 case "$RESPONSECODE" in
1068                                 210)    # 210 OK, status information follows (until terminating `.')
1069                                         rc=0;
1070                                         ;;
1071                                 501|*)  # 501 Illegal CDDB protocol level: <n>. 
1072                                         CDDBPROTO=`expr $CDDBPROTO - 1`
1073                                         ;;
1074                                 esac 
1075                         done
1076                         if test $rc -eq 1; then
1077                                 CDDBAVAIL="n" 
1078                         fi
1079                 fi
1080                 echo cddb-statcomplete >> "$ABCDETEMPDIR/status"
1081         fi
1082 }
1083
1084
1085 # do_cddbquery
1086 do_cddbquery ()
1087 {
1088         CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
1089         CDDBLOCALFILE="${CDDBLOCALDIR}/${CDDBDISCID}"
1090         
1091         # Perform CDDB query if it hasn't already been done
1092         if checkstatus cddb-querycomplete; then :; else
1093                 if [ "$CDDBAVAIL" = "n" ]; then
1094                         ERRORCODE=no_query
1095                         echo 503 > "$ABCDETEMPDIR/cddbquery"
1096                 # The default CDDBLOCALSUCCESS is "n"
1097                 # This part will be triggered if the user CDDB repo does not 
1098                 # contain the entry, or if we are not trying to use the repo.
1099                 else
1100                         vecho "Querying the CDDB server..."
1101                         CDDBUSER=$(echo $HELLOINFO | cut -f1 -d'@')
1102                         CDDBHOST=$(echo $HELLOINFO | cut -f2- -d'@')
1103                         $CDDBTOOL query $CDDBURL $CDDBPROTO $CDDBUSER $CDDBHOST $TRACKINFO > "$ABCDETEMPDIR/cddbquery"
1104                         ERRORCODE=$?
1105                         case $ERRORCODE in
1106                                 0)  # success
1107                                 ;;
1108                                 12|13|14)
1109                                         # no match found in database,
1110                                         # wget/fetch error, or user requested not to use CDDB
1111                                         # Make up an error code (503) that abcde
1112                                         # will recognize in do_cddbread
1113                                         # and compensate by making a template
1114                                         echo 503 > "$ABCDETEMPDIR/cddbquery"
1115                                 ;;
1116                                 *) # strange and unknown error
1117                                         echo ERRORCODE=$ERRORCODE
1118                                         echo "abcde: $CDDBTOOL returned unknown error code"
1119                                 ;;
1120                         esac
1121                 fi
1122                 echo cddb-querycomplete >> "$ABCDETEMPDIR/status"
1123         fi
1124 }
1125
1126 # do_cddbread
1127 do_cddbread ()
1128 {
1129         # If it's not to be used, generate a template.
1130         # Then, display it (or them) and let the user choose/edit it
1131         if checkstatus cddb-readcomplete; then :; else
1132                 vecho "Obtaining CDDB results..."
1133                 # If CDDB is to be used, interpret the query results and read all
1134                 # the available entries.
1135                 rm -f "$ABCDETEMPDIR/cddbchoices"
1136                 CDDBCHOICES=1 # Overridden by multiple matches
1137                 RESPONSECODE=$(head -n 1 "$ABCDETEMPDIR/cddbquery" | cut -f1 -d' ')
1138                 case "$RESPONSECODE" in
1139                 200)
1140                         # One exact match, retrieve it
1141                         # 200 [section] [discid] [artist] / [title]
1142                         if checkstatus cddb-read-1-complete; then :; else
1143                                 echo -n "Retrieving 1 CDDB match..." >> "$ABCDETEMPDIR/cddbchoices"
1144                                 $CDDBTOOL read $CDDBURL $CDDBPROTO $CDDBUSER $CDDBHOST $(cut -f2,3 -d' ' "$ABCDETEMPDIR/cddbquery") > "$ABCDETEMPDIR/cddbread.1"
1145                                 echo "done." >> "$ABCDETEMPDIR/cddbchoices"
1146                                 echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
1147                                 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
1148                         fi
1149                         # List out disc title/author and contents
1150                         echo ---- "$(cut '-d ' -f4- "$ABCDETEMPDIR/cddbquery")" ---- >> "$ABCDETEMPDIR/cddbchoices"
1151                         for TRACK in $(f_seq_row 1 $TRACKS)
1152                         do
1153                                 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.1" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
1154                         done
1155                         echo >> "$ABCDETEMPDIR/cddbchoices"
1156                         ;;
1157                 202|403|409|503)
1158                         # No match
1159                         case "$RESPONSECODE" in
1160                         202) echo "No CDDB match." >> "$ABCDETEMPDIR/cddbchoices" ;;
1161                         403|409) echo "CDDB entry is corrupt, or the handshake failed." >> "$ABCDETEMPDIR/cddbchoices" ;;
1162                         503) echo "CDDB unavailable." >> "$ABCDETEMPDIR/cddbchoices" ;;
1163                         esac
1164                         $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.0"
1165                         # List out disc title/author and contents of template
1166                         echo ---- Unknown Artist / Unknown Album ---- >> "$ABCDETEMPDIR/cddbchoices"
1167                         UNKNOWNDISK=y
1168                         for TRACK in $(f_seq_row 1 $TRACKS)
1169                         do
1170                                 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.0" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
1171                         done
1172                         echo >> "$ABCDETEMPDIR/cddbchoices"
1173                         echo cddb-read-0-complete >> "$ABCDETEMPDIR/status"
1174                         echo cddb-choice=0 >> "$ABCDETEMPDIR/status"
1175                         ;;
1176                 210|211)
1177                         # Multiple exact, (possibly multiple) inexact matches
1178                         IN=
1179                         if [ "$RESPONSECODE" = "211" ]; then IN=in; fi
1180                         if [ "$(wc -l < $ABCDETEMPDIR/cddbquery | tr -d ' ')" -eq 3 ]; then
1181                                 echo "One ${IN}exact match:" >> "$ABCDETEMPDIR/cddbchoices"
1182                                 tail -n +2 "$ABCDETEMPDIR/cddbquery" | head -n 1 >> "$ABCDETEMPDIR/cddbchoices"
1183                                 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
1184                         else
1185                                 echo "Multiple ${IN}exact matches:" >> "$ABCDETEMPDIR/cddbchoices"
1186                         fi
1187                         vecho -n "Retrieving multiple matches... "
1188                         grep -v ^[.]$ "$ABCDETEMPDIR/cddbquery" | ( X=0
1189                         read DISCINFO # eat top line
1190                         while read DISCINFO
1191                         do
1192                                 X=$(expr $X + 1)
1193                                 if checkstatus cddb-read-$X-complete; then :; else
1194                                         $CDDBTOOL read $CDDBURL $CDDBPROTO $CDDBUSER $CDDBHOST $(echo $DISCINFO | cut -f1,2 -d' ') > "$ABCDETEMPDIR/cddbread.$X"
1195                                         echo cddb-read-$X-complete >> "$ABCDETEMPDIR/status"
1196                                 fi
1197                                 # List out disc title/author and contents
1198                                 echo \#$X: ---- "$DISCINFO" ---- >> "$ABCDETEMPDIR/cddbchoices"
1199                                 for TRACK in $(f_seq_row 1 $TRACKS)
1200                                 do
1201                                         echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.$X" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
1202                                 done
1203                                 echo >> "$ABCDETEMPDIR/cddbchoices"
1204                         done )
1205                         vecho "done."
1206                         CDDBCHOICES=$(expr $(cat "$ABCDETEMPDIR/cddbquery" | wc -l) - 2)
1207                         ;;
1208                 999)
1209                         # Using local copy.
1210                         for TRACK in $(f_seq_row 1 $TRACKS)
1211                         do
1212                                 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.1" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
1213                         done
1214                         echo >> "$ABCDETEMPDIR/cddbchoices"
1215                         echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
1216                         echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
1217                         ;;
1218                 esac    
1219                 echo "cddb-readcomplete" >> "$ABCDETEMPDIR/status"
1220         fi
1221 }
1222
1223 # do_cddbedit
1224 do_cddbedit ()
1225 {
1226         if checkstatus cddb-edit >/dev/null; then
1227                 CDDBDATA="$ABCDETEMPDIR/cddbread.$(checkstatus cddb-choice)"
1228                 VARIOUSARTISTS="$(checkstatus variousartists)"
1229                 VARIOUSARTISTSTYLE="$(checkstatus variousartiststyle)"
1230                 return 0
1231         fi
1232         if [ "$INTERACTIVE" = "y" ]; then
1233                 # We should show the CDDB results both when we are not using the local CDDB repo
1234                 # or when we are using it but we could not find a proper match
1235                 if [ "$CDDBUSELOCAL" = "y" ] && [ ! "$CDDBLOCALSUCCESS" = "y" ] || [ ! "$CDDBUSELOCAL" = "y" ]; then
1236                         # Display the $ABCDETEMPDIR/cddbchoices file created above
1237                         # Pick a pager so that if the tracks overflow the screen the user can still view everything
1238                         if [ -r "$ABCDETEMPDIR/cddbchoices" ]; then
1239                                 CDDBCHOICES=$(expr $(cat "$ABCDETEMPDIR/cddbquery" | wc -l) - 2)
1240                                 CHOICE=$(checkstatus cddb-choice)
1241                                 if [ -n "$CHOICE" ] ; then
1242                                         case $CDDBCHOICES in
1243                                                 1) cat "$ABCDETEMPDIR/cddbchoices" ;;
1244                                                 *)
1245                                                 echo "Selected: #$CHOICE"
1246                                                 do_cddbparse "$ABCDETEMPDIR/cddbread.$CHOICE"
1247                                                 ;;
1248                                         esac
1249                                 else
1250                                         # The user has a choice to make, display the info in a pager if necessary
1251                                         if [ $(cat "$ABCDETEMPDIR/cddbchoices" | wc -l) -ge 24 ]; then
1252                                                 # Use the debian sensible-pager wrapper to pick the pager
1253                                                 # user has requested via their $PAGER environment variable
1254                                                 if [ -x "/usr/bin/sensible-pager" ]; then
1255                                                         /usr/bin/sensible-pager "$ABCDETEMPDIR/cddbchoices"
1256                                                 elif [ -x "$PAGER" ]; then
1257                                                         # That failed, try to load the preferred editor, starting
1258                                                         # with their PAGER variable
1259                                                         $PAGER "$ABCDETEMPDIR/cddbchoices"
1260                                                         # If that fails, check for less
1261                                                 elif [ -x /usr/bin/less ]; then
1262                                                         /usr/bin/less -f "$ABCDETEMPDIR/cddbchoices"
1263                                                         # more should be on all UNIX systems
1264                                                 elif [ -x /bin/more ]; then
1265                                                         /bin/more "$ABCDETEMPDIR/cddbchoices"
1266                                                 else
1267                                                         # No bananas, just cat the thing
1268                                                         cat "$ABCDETEMPDIR/cddbchoices" >&2
1269                                                 fi
1270                                         else
1271                                                 # It's all going to fit in one page, cat it
1272                                                 cat "$ABCDETEMPDIR/cddbchoices" >&2
1273                                         fi
1274                                         
1275                                         # I'll take CDDB read #3 for $400, Alex
1276                                         echo -n "Which entry would you like abcde to use (0 for none)? [0-$CDDBCHOICES]: " >&2
1277                                         read CDDBCHOICE
1278                                         [ X"$CDDBCHOICE" = "X" ] && CDDBCHOICE=1
1279                                         # Make sure we get a valid choice
1280                                         CDCHOICENUM=$(echo $CDDBCHOICE | xargs printf %d 2>/dev/null)
1281                                         while [ $CDCHOICENUM -lt 0 ] || [ $CDCHOICENUM -gt $CDDBCHOICES ]; do
1282                                                 echo "Invalid selection. Please choose a number between 1 and $CDDBCHOICES." >&2
1283                                                 echo -n "Selection [0-$CDDBCHOICES]: " >&2
1284                                                 read CDDBCHOICE
1285                                                 CDCHOICENUM=$(echo $CDDBCHOICE | xargs printf %d 2>/dev/null)
1286                                         done
1287                                         if [ "$CDCHOICENUM" = "0" ]; then
1288                                                 vecho "Creating empty CDDB template..."
1289                                                 UNKNOWNDISK=y
1290                                                 $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > $ABCDETEMPDIR/cddbread.0
1291                                         else
1292                                                 echo "Selected: #$CDCHOICENUM ($(grep ^DTITLE= $ABCDETEMPDIR/cddbread.$CDCHOICENUM | cut -f2- -d= | tr -d \\r\\n))" >&2
1293                                                 do_cddbparse "$ABCDETEMPDIR/cddbread.$CDCHOICENUM"
1294                                         fi
1295                                         echo "cddb-choice=$CDCHOICENUM" >> "$ABCDETEMPDIR/status"
1296                                 fi
1297                         fi
1298                 else
1299                         # We need some code to show the selected option when local repository is selected and we have found a match
1300                         vecho "Using cached CDDB match..." >&2
1301                         # Display the $ABCDETEMPDIR/cddbchoices file created above
1302                         # Pick a pager so that if the tracks overflow the screen the user can still view everything
1303                         if [ -r "$ABCDETEMPDIR/cddbchoices" ]; then
1304                                 CDDBCHOICES=$(expr $(cat "$ABCDETEMPDIR/cddbquery" | wc -l) - 2)
1305                                 CHOICE=$(checkstatus cddb-choice)
1306                                 if [ "$USELOCALRESP" = "y" ]; then :; else
1307                                         if [ -n "$CHOICE" ] ; then
1308                                                 case $CDDBCHOICES in
1309                                                         0) 
1310                                                         UNKNOWNDISK=y
1311                                                         echo "Selected template."
1312                                                         ;;
1313                                                         1) cat "$ABCDETEMPDIR/cddbchoices" ;;
1314                                                         *)
1315                                                         echo "Selected: #$CHOICE"
1316                                                         do_cddbparse "$ABCDETEMPDIR/cddbread.$CHOICE"
1317                                                         ;;
1318                                                 esac
1319                                         fi
1320                                 fi
1321                         fi
1322                 fi
1323         else
1324                 # We're noninteractive - pick the first choice.
1325                 # But in case we run a previous instance and selected a choice, use it.
1326                 if [ -r "$ABCDETEMPDIR/cddbchoices" ]; then
1327                         # Show the choice if we are not using the locally stored one
1328                         # or when the local search failed to find a match.
1329                         PREVIOUSCHOICE=$(checkstatus cddb-choice)
1330                         if [ "$CDDBUSELOCAL" = "y" ] && [ "$CDDBLOCALSUCCESS" = "n" ] || [ ! "$CDDBUSELOCAL" = "y" ]; then
1331                                 #if [ "$PREVIOUSCHOICE" ]; then
1332                                         cat "$ABCDETEMPDIR/cddbchoices"
1333                                 #fi
1334                         fi
1335                         if [ ! -z "$PREVIOUSCHOICE" ] ; then
1336                                 CDCHOICENUM=$PREVIOUSCHOICE
1337                         else
1338                                 CDCHOICENUM=1
1339                                 echo "cddb-choice=$CDCHOICENUM" >> "$ABCDETEMPDIR/status"
1340                         fi
1341                         echo "Selected: #$CDCHOICENUM ($(grep ^DTITLE= $ABCDETEMPDIR/cddbread.$CDCHOICENUM | cut -f2- -d= | tr -d \\r\\n))" >&2
1342                 fi
1343         fi
1344
1345         # sanity check
1346         if checkstatus cddb-choice >/dev/null; then :; else
1347                 echo "abcde: internal error: cddb-choice not recorded." >&2
1348                 exit 1
1349         fi
1350         CDDBDATA="$ABCDETEMPDIR/cddbread.$(checkstatus cddb-choice)"
1351         echo -n "Edit selected CDDB data? [y/n] (" >&2
1352         if [ "$INTERACTIVE" = "y" ]; then
1353                 if [ "$UNKNOWNDISK" = "y" ]; then
1354                         echo -n "y): " >&2
1355                         read EDITCDDB
1356                         [ "$EDITCDDB" != "n" ] && EDITCDDB=y
1357                 else
1358                         echo -n "n): " >&2
1359                         read EDITCDDB
1360                 fi
1361         else
1362                 echo "n): n" >&2
1363                 EDITCDDB=n
1364         fi
1365         if [ "$EDITCDDB" = "y" ]; then
1366                 CDDBDATAMD5SUM=$($MD5SUM "$CDDBDATA" | cut -d " " -f 1);
1367                 
1368                 # Use the debian sensible-editor wrapper to pick the editor that the
1369                 # user has requested via their $EDITOR environment variable
1370                 if [ -x "/usr/bin/sensible-editor" ]; then
1371                         /usr/bin/sensible-editor "$CDDBDATA"
1372                 elif [ -n "$EDITOR" ]; then
1373                         if [ -x $(which "${EDITOR%%\ *}") ]; then
1374                                 # That failed, try to load the preferred editor, starting
1375                                 # with their EDITOR variable
1376                                 eval $(echo "$EDITOR") "$CDDBDATA"
1377                         fi
1378                 # If that fails, check for a vi
1379                 elif [ -x /usr/bin/vi ]; then
1380                         /usr/bin/vi "$CDDBDATA"
1381                 # nano should be on all (modern, i.e., sarge) debian systems
1382                 elif [ -x /usr/bin/nano ]; then
1383                         /usr/bin/nano "$CDDBDATA"
1384                 # mg should be on all OpenBSD systems
1385                 elif [ -x /usr/bin/mg ]; then
1386                         /usr/bin/mg "$CDDBDATA"
1387                 # bomb out
1388                 else
1389                         echo "No editor available. Check your EDITOR environment variable." >&2
1390                 fi
1391                 # delete editor backup file if it exists
1392                 if [ -w "$CDDBDATA~" ]; then
1393                         rm -f "$CDDBDATA~"
1394                 fi
1395         fi
1396
1397         # Some heuristics first. Look at Disc Title, and if it starts with
1398         # "Various", then we'll assume Various Artists
1399         if [ "$(grep ^DTITLE= "$CDDBDATA" | cut -f2 -d= | egrep -ci '^(various|soundtrack|varios|sonora|ost)')" != "0" ]; then
1400                 echo "Looks like a Multi-Artist CD" >&2
1401                 VARIOUSARTISTS=y
1402         else
1403                 echo -n "Is the CD multi-artist? [y/n] (n): " >&2
1404                 if [ "$INTERACTIVE" = "y" ]; then
1405                         read VARIOUSARTISTS
1406                 else
1407                         echo n >&2
1408                         VARIOUSARTISTS=n
1409                 fi
1410         fi
1411         if [ "$VARIOUSARTISTS" = "y" ] && [ ! "$ONETRACK" = "y" ]; then
1412                 # Set a default
1413                 DEFAULTSTYLE=1
1414                 # Need NUMTRACKS before cddb-tool will return it:
1415                 NUMTRACKS=$(egrep '^TTITLE[0-9]+=' "$CDDBDATA" | wc -l)
1416                 if [ "$(grep -c "^TTITLE.*\/" "$CDDBDATA")" -gt "$(expr $NUMTRACKS / 2 )" ]; then
1417                         # More than 1/2 tracks contain a "/", so guess forward
1418                         DEFAULTSTYLE=1
1419                 elif [ "$(grep -c "^TTITLE.*\-" "$CDDBDATA")" -gt "$(expr $NUMTRACKS / 2 )" ]; then
1420                         # More than 1/2 contain a "-", so guess forward-dash
1421                         DEFAULTSTYLE=2
1422                 elif [ "$(grep -c "^TTITLE.*(.*)" "$CDDBDATA")" -gt "$(expr $NUMTRACKS / 2 )" ]; then
1423                         # More than 1/2 contain something in parens, so guess trailing-paren
1424                         DEFAULTSTYLE=6
1425                 fi
1426
1427                 echo "1) Artist / Title" >&2
1428                 echo "2) Artist - Title" >&2
1429                 echo "3) Title / Artist" >&2
1430                 echo "4) Title - Artist" >&2
1431                 echo "5) Artist: Title" >&2
1432                 echo "6) Title (Artist)" >&2
1433                 echo "7) This is a single-artist CD" >&2
1434                 echo -n "Which style of multiple artist entries is it? [1-7] ($DEFAULTSTYLE): " >&2
1435                 if [ "$INTERACTIVE" = "y" ]; then
1436                         read VARIOUSARTISTSTYLE
1437                 else
1438                         echo $DEFAULTSTYLE >&2
1439                         VARIOUSARTISTSTYLE=$DEFAULTSTYLE
1440                 fi
1441                 VARIOUSARTISTSTYLE=$(echo 0$VARIOUSARTISTSTYLE | xargs printf %d)
1442                 # If they press Enter, then the default style (0) was chosen
1443                 while [ $VARIOUSARTISTSTYLE -lt 0 ] || [ $VARIOUSARTISTSTYLE -gt 7 ]; do
1444                         echo "Invalid selection. Please choose a number between 1 and 7."
1445                         echo -n "Selection [1-7]: "
1446                         read VARIOUSARTISTSTYLE
1447                         VARIOUSARTISTSTYLE=$(echo 0$VARIOUSARTISTSTYLE | xargs printf %d)
1448                 done
1449                 if [ "$VARIOUSARTISTSTYLE" = "0" ]; then
1450                         VARIOUSARTISTSTYLE=$DEFAULTSTYLE
1451                 fi
1452                 vecho "Selected: $VARIOUSARTISTSTYLE"
1453                 case "$VARIOUSARTISTSTYLE" in
1454                 1) # Artist / Title
1455                         VARIOUSARTISTSTYLE=forward
1456                         ;;
1457                 2) # Artist - Title
1458                         VARIOUSARTISTSTYLE=forward-dash
1459                         ;;
1460                 3) # Title / Artist
1461                         VARIOUSARTISTSTYLE=reverse
1462                         ;;
1463                 4) # Title - Artist
1464                         VARIOUSARTISTSTYLE=reverse-dash
1465                         ;;
1466                 5) # Artist: Title
1467                         VARIOUSARTISTSTYLE=colon
1468                         ;;
1469                 6) # Title (Artist)
1470                         VARIOUSARTISTSTYLE=trailing-paren
1471                         ;;
1472                 7) # Single Artist
1473                         VARIOUSARTISTS=n
1474                         ;;
1475                 esac
1476         fi
1477
1478         echo "variousartists=$VARIOUSARTISTS" >> "$ABCDETEMPDIR/status"
1479         echo "variousartiststyle=$VARIOUSARTISTSTYLE" >> "$ABCDETEMPDIR/status"
1480
1481         if [ "$EDITCDDB" = "y" ] && [ "$UNINTENTIONALLY_ANGER_THE_FREEDB_PEOPLE" = "y" ]; then
1482                 if [ $CDDBDATAMD5SUM != "" ]  && [ $CDDBDATAMD5SUM != $($MD5SUM "$CDDBDATA" | cut -d " " -f 1) ]; then
1483                         # This works but does not have the necessary error checking
1484                         # yet. If you are familiar with the CDDB spec
1485                         # (see http://www.freedb.org/src/latest/DBFORMAT) 
1486                         # and can create an error-free entry on your own, then put
1487                         # UNINTENTIONALLY_ANGER_THE_FREEDB_PEOPLE=y in your
1488                         # abcde.conf to enable it. Put CDDBSUBMIT=email@address in
1489                         # your abcde.conf to change the email address submissions are
1490                         # sent to.
1491
1492                         # submit the modified file, if they want
1493                         if [ "$NOSUBMIT" != "y" ]; then
1494                                 echo -n "Do you want to submit this entry to $CDDBSUBMIT? [y/n] (n): "
1495                                 read YESNO
1496                                 while [ "$YESNO" != "y" ] && [ "$YESNO" != "n" ] && [ "$YESNO" != "Y" ] && \
1497                                         [ "$YESNO" != "N" ] && [ "$YESNO" != "" ]
1498                                 do
1499                                         echo -n 'Invalid selection. Please answer "y" or "n": '
1500                                         read YESNO
1501                                 done
1502                                 if [ "$YESNO" = "y" ] || [ "$YESNO" = "Y" ]; then
1503                                         echo -n "Sending..."
1504                                         $CDDBTOOL send "$CDDBDATA" $CDDBSUBMIT
1505                                         echo "done."
1506                                 fi
1507                         fi
1508                 fi
1509         fi
1510         # Make sure the cache directory exists
1511         mkdir -p $CDDBLOCALDIR
1512         # Cache edited CDDB entry in the user's cddb dir
1513         if [ "$CDDBCOPYLOCAL" = "y" ] || [ "$COPYCDDBLOCAL" = "Y" ]; then
1514                 cat "$CDDBDATA" | tail -n $(expr $(cat "$CDDBDATA" | wc -l ) - 1 ) > ${CDDBLOCALDIR}/$(echo "$TRACKINFO" | cut -d' ' -f1)
1515         fi
1516
1517         echo "cddb-edit" >> "$ABCDETEMPDIR/status"
1518 }
1519
1520 # do_cdread_one [lasttrack] [firsttrack]
1521
1522 # Reads the CD in a single track. Live performances, concerts, mixes,... benefit from this.
1523 do_cdread_one ()
1524 {
1525         # The commands here don't go through run_command because they're never supposed to be silenced
1526         # return codes need to be doublechecked anyway, however
1527         LASTTRACKNUMBER=$1
1528         FIRSTTRACKNUMBER=$2
1529         WAVDATA="$ABCDETEMPDIR/track$FIRSTTRACKNUMBER.wav"
1530         echo "Grabbing the CD to a single track..." >&2
1531         case "$CDROMREADERSYNTAX" in
1532                 cdparanoia) nice $READNICE $CDROMREADER -d $CDROM "1-" "$WAVDATA" >&2 ;;
1533                 cdda2wav)
1534                         if [ "$OSFLAVOUR" = "OSX" ] ; then
1535                                 # Hei, we have to unmount the device before running anything like cdda2wav in OSX
1536                                 disktool -u ${CDROM#/dev/} 0
1537                                 # Also, in OSX the cdrom device for cdda2wav changes...
1538                                 CDDA2WAVCDROM="IODVDServices"
1539                         elif [ "$OSFLAVOUR" = "FBSD" ] ; then
1540                                 CDDA2WAVCDROM="$CDROMID"
1541                         else
1542                                 if [ "$CDROMID" = "" ]; then
1543                                         CDDA2WAVCDROM="$CDROM"
1544                                 else
1545                                         CDDA2WAVCDROM="$CDROMID"
1546                                 fi
1547                         fi
1548                         nice $READNICE $CDROMREADER -D $CDDA2WAVCDROM -t 1+$LASTTRACKNUM "$WAVDATA" >&2 
1549                         ;;
1550                 dagrab) nice $READNICE $CDROMREADER -d $CDROM -f $WAVDATA -v $UTRACKNUM >&2 ;;
1551                 cddafs)
1552                         # Find the track's mounted path
1553                         REALTRACKNUM=$(expr $UTRACKNUM + 0)
1554                         FILEPATH=$(mount | grep "$CDROM on" | sed 's/^[^ ]* on \(.*\) (.*/\1/')
1555                         FILEPATH=$(find "$FILEPATH" | grep "/$REALTRACKNUM ");
1556                         # If the file exists, copy it
1557                         if [ -e "$FILEPATH" ] ; then
1558                                 nice $READNICE $CDROMREADER "$FILEPATH" "$WAVDATA" >&2
1559                         else
1560                                 false
1561                         fi ;;
1562                 debug) nice $READNICE $CDROMREADER -d $CDROM -w $UTRACKNUM-[:1] "$WAVDATA" >&2 ;;
1563         esac
1564         RETURN=$?
1565         if [ "$RETURN" != "0" -o ! -s "$WAVDATA" ]; then
1566                 # Thank goodness errors is only machine-parseable up to the
1567                 # first colon, otherwise this woulda sucked
1568                 if [ "$RETURN" = "0" -a ! -s "$WAVDATA" ]; then
1569                         RETURN=73 # fake a return code as cdparanoia return 0 also on aborted reads
1570                 fi
1571                 echo "readtrack-$FIRSTTRACKNUMBER: $CDROMREADER returned code $RETURN" >> "$ABCDETEMPDIR/errors"
1572                 return $RETURN
1573         else
1574                 echo readtrack-$FIRSTTRACKNUMBER >> "$ABCDETEMPDIR/status"
1575         fi
1576 }
1577
1578 # do_cdread [tracknumber]
1579 # do_cdread onetrack [firsttrack] [lasttrack]
1580
1581 do_cdread ()
1582 {
1583         # The commands here don't go through run_command because they're never supposed to be silenced
1584         # return codes need to be doublechecked anyway, however
1585         if [ "$1" = "onetrack" ]; then
1586                 # FIXME # Add the possibility of grabbing ranges of tracks in onetrack
1587                 # FIXME # Until then, we grab the whole CD in one track, no matter what
1588                 # the user said
1589                 # We need the first and last track for cdda2wav
1590                 FIRSTTRACK=$2
1591                 LASTTRACK=$3
1592                 case "$CDROMREADERSYNTAX" in
1593                         cdparanoia) UTRACKNUM="1-" ;;
1594                         cdda2wav) UTRACKNUM="1+$LASTRACK" ;;
1595                         *) echo "abcde error: $CDROMREADERSYNTAX does not support ONETRACK mode"
1596                            exit 1 ;;
1597                 esac
1598         else
1599                 UTRACKNUM=$1
1600         fi
1601         CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
1602         WAVDATA="$ABCDETEMPDIR/track$UTRACKNUM.wav"
1603         if [ -r "$CDDBDATA" ]; then
1604                 TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -n 1 | cut -f2 -d= | tr -d \[:cntrl:\])
1605                 echo "Grabbing track $UTRACKNUM: $TRACKNAME..." >&2
1606         else
1607                 echo "Grabbing track $UTRACKNUM..." >&2
1608         fi
1609         if [ "$USEPIPES" = "y" ]; then
1610                 TEMPARG="PIPE_$CDROMREADERSYNTAX"
1611                 FILEARG=${!TEMPARG}
1612                 REDIR=""
1613         else
1614                 case "$CDROMREADERSYNTAX" in
1615                 ## FIXME ## Find the case for dagrab, to avoid exceptions
1616                         dagrab)
1617                                 FILEARG="-f $WAVDATA"
1618                                 ;;
1619                         *)
1620                                 FILEARG=$WAVDATA
1621                                 ;;
1622                 esac
1623                 REDIR=">&2"
1624         fi
1625         case "$CDROMREADERSYNTAX" in
1626                 cdparanoia) nice $READNICE $CDROMREADER -d $CDROM $UTRACKNUM $FILEARG $REDIR ;;
1627                 cdda2wav)
1628                         if [ "$OSFLAVOUR" = "OSX" ] ; then
1629                                 # Hei, we have to unmount the device before running anything like cdda2wav in OSX
1630                                 disktool -u ${CDROM#/dev/} 0
1631                                 # Also, in OSX the cdrom device for cdda2wav changes...
1632                                 CDDA2WAVCDROM="IODVDServices"
1633                         elif [ "$OSFLAVOUR" = "FBSD" ] ; then
1634                                 CDDA2WAVCDROM="$CDROMID"
1635                         else
1636                                 if [ "$CDROMID" = "" ]; then
1637                                         CDDA2WAVCDROM="$CDROM"
1638                                 else
1639                                         CDDA2WAVCDROM="$CDROMID"
1640                                 fi
1641                         fi
1642                         nice $READNICE $CDROMREADER -D $CDDA2WAVCDROM -t $UTRACKNUM $FILEARG $REDIR
1643                         ;;
1644                 ## FIXME ## We have an exception for dagrab, since it uses -f
1645                 ## FIXME ## Shall we just use -f $FILEARG ??
1646                 dagrab) nice $READNICE $CDROMREADER -d $CDROM -v $UTRACKNUM $FILEARG $REDIR
1647                         ;;
1648                 cddafs)
1649                         # Find the track's mounted path
1650                         REALTRACKNUM=$(expr $UTRACKNUM + 0)
1651                         FILEPATH=$(mount | grep "$CDROM on" | sed 's/^[^ ]* on \(.*\) (.*/\1/')
1652                         FILEPATH=$(find "$FILEPATH" | grep "/$REALTRACKNUM ");
1653                         # If the file exists, copy it
1654                         if [ -e "$FILEPATH" ] ; then
1655                                 nice $READNICE $CDROMREADER "$FILEPATH" $FILEARG $REDIR
1656                         else
1657                                 false
1658                         fi ;;
1659                 debug) nice $READNICE $CDROMREADER -d $CDROM -w $UTRACKNUM-[:1] $FILEARG $REDIR
1660                         ;;
1661         esac
1662         RETURN=$?
1663         # If we get some error or we get some missing wav 
1664         # (as long as we dont use pipes)
1665         if [ "$RETURN" != "0" -o \( ! -s "$WAVDATA" -a X"$USEPIPES" != "Xy" \) ]; then
1666                 # Thank goodness errors is only machine-parseable up to the
1667                 # first colon, otherwise this woulda sucked
1668                 if [ "$RETURN" = "0" -a ! -s "$WAVDATA" ]; then
1669                         RETURN=73 # fake a return code as cdparanoia return 0 also on aborted reads
1670                 fi
1671                 if [ "$USEPIPES" = "y" ]; then
1672                         echo "readencodetrack-$UTRACKNUM: $CDROMREADER returned code $RETURN" >> "$ABCDETEMPDIR/errors"
1673                 else
1674                         echo "readtrack-$UTRACKNUM: $CDROMREADER returned code $RETURN" >> "$ABCDETEMPDIR/errors"
1675                 fi
1676                 return $RETURN
1677         else
1678                 if [ "$USEPIPES" = "y" ]; then
1679                         echo readencodetrack-$UTRACKNUM >> "$ABCDETEMPDIR/status"
1680                 else
1681                         echo readtrack-$UTRACKNUM >> "$ABCDETEMPDIR/status"
1682                 fi
1683         fi
1684 }
1685
1686 # do_cdspeed
1687 # No values accepted, only uses env variables
1688 do_cdspeed () 
1689 {
1690         if "$CDSPEED" "$CDSPEEDOPTS" "$CDSPEEDVALUE" >/dev/null ; then
1691                 vecho "Setting CD speed to ${CDSPEEDVALUE}x"
1692         else
1693                 echo "abcde: unable to set the device speed" >&2
1694         fi
1695 }
1696
1697 # vecho [message]
1698 #
1699 # vecho outputs a message if EXTRAVERBOSE is selected
1700 vecho ()
1701 {
1702 if [ x"$EXTRAVERBOSE" != "x" ]; then
1703         echo $@
1704 fi
1705 }
1706
1707 # User-redefinable functions
1708 # Custom filename munging:
1709 mungefilename ()
1710 {
1711         echo "$@" | sed s,:,\ -,g | tr \ /\* __+ | tr -d \'\"\?\[:cntrl:\]
1712 }
1713
1714 # Custom genre munging:
1715 mungegenre ()
1716 {
1717         echo $CDGENRE | tr "[:upper:]" "[:lower:]"
1718 }
1719
1720 # pre_read
1721 # Empty pre_read function, to be defined in the configuration file.
1722 pre_read ()
1723 {
1724 :
1725 }
1726
1727 ###############################################################################
1728 # End of functions
1729 #
1730 # Start of execution
1731 ###############################################################################
1732
1733 # Builtin defaults
1734 CDDBURL="http://freedb.freedb.org/~cddb/cddb.cgi"
1735 CDDBSUBMIT=freedb-submit@freedb.org
1736 CDDBPROTO=5
1737 HELLOINFO="$(whoami)@$(hostname)"
1738 INTERACTIVE=y
1739 CDROMREADERSYNTAX=cdparanoia
1740 OUTPUTTYPE=ogg
1741 ENCODERSYNTAX=default
1742
1743 MP3ENCODERSYNTAX=default
1744 OGGENCODERSYNTAX=default
1745 FLACENCODERSYNTAX=default
1746 SPEEXENCODERSYNTAX=default
1747 MPPENCODERSYNTAX=default
1748 NORMALIZERSYNTAX=default
1749
1750 OUTPUTFORMAT='${ARTISTFILE}-${ALBUMFILE}/${TRACKNUM}.${TRACKFILE}'
1751 # Use the following VAOUTPUTFORMAT to revert to 2.0.x VA format:
1752 #VAOUTPUTFORMAT=${OUTPUTFORMAT}
1753 VAOUTPUTFORMAT='Various-${ALBUMFILE}/${TRACKNUM}.${ARTISTFILE}-${TRACKFILE}'
1754 ONETRACKOUTPUTFORMAT='${ARTISTFILE}-${ALBUMFILE}/${ALBUMFILE}'
1755 VAONETRACKOUTPUTFORMAT='Various-${ALBUMFILE}/${ALBUMFILE}'
1756 PLAYLISTFORMAT='${ARTISTFILE}-${ALBUMFILE}.${OUTPUT}.m3u'
1757 PLAYLISTDATAPREFIX=''
1758 VAPLAYLISTFORMAT='${ARTISTFILE}-${ALBUMFILE}.${OUTPUT}.m3u'
1759 VAPLAYLISTDATAPREFIX=''
1760 DOSPLAYLIST=n
1761 COMMENT=''
1762 ID3TAGV=2
1763 ENCNICE=10
1764 READNICE=10
1765 DISTMP3NICE=10
1766 VARIOUSARTISTS=n
1767 VARIOUSARTISTSTYLE=forward
1768 KEEPWAVS=n
1769 PADTRACKS=n
1770 CDDBCOPYLOCAL="n"
1771 CDDBLOCALDIR="$HOME/.cddb"
1772 CDDBUSELOCAL="n"
1773
1774 # If using scsi devices, cdda2wav needs a CDROMID, instead of a device node
1775 # i.e. CDROMID="1,0,0"
1776 CDROMID=""
1777
1778 # program paths - defaults to checking your $PATH
1779 # mp3
1780 LAME=lame
1781 GOGO=gogo
1782 BLADEENC=bladeenc
1783 L3ENC=l3enc
1784 XINGMP3ENC=xingmp3enc
1785 MP3ENC=mp3enc
1786 # ogg
1787 VORBIZE=vorbize
1788 OGGENC=oggenc
1789 # flac
1790 FLAC=flac
1791 # speex
1792 SPEEXENC=speexenc
1793 # mpp (Musepack)
1794 MPPENC=mppenc
1795
1796 ID3=id3
1797 ID3V2=id3v2
1798 CDPARANOIA=cdparanoia
1799 CDDA2WAV=cdda2wav
1800 DAGRAB=dagrab
1801 CDDAFS=cp
1802 CDDISCID=cd-discid
1803 CDDBTOOL=cddb-tool
1804 EJECT=eject
1805 MD5SUM=md5sum
1806 DISTMP3=distmp3
1807 VORBISCOMMENT=vorbiscomment
1808 METAFLAC=metaflac
1809 NORMALIZE=normalize-audio
1810 CDSPEED=eject
1811 VORBISGAIN=vorbisgain
1812
1813 # Options for programs called from abcde
1814 # mp3
1815 LAMEOPTS=
1816 GOGOOPTS=
1817 BLADEENCOPTS=
1818 L3ENCOPTS=
1819 XINGMP3ENCOPTS=
1820 MP3ENCOPTS=
1821 # ogg
1822 VORBIZEOPTS=
1823 OGGENCOPTS=
1824 # flac
1825 FLACOPTS=
1826 # speex
1827 SPEEXENCOPTS=
1828 # mpc
1829 MPPENCOPTS=
1830
1831 ID3OPTS=
1832 ID3V2OPTS=
1833 CDPARANOIAOPTS=
1834 CDDA2WAVOPTS=
1835 DAGRABOPTS=
1836 CDDAFSOPTS="-f"
1837 CDDBTOOLOPTS=
1838 EJECTOPTS=
1839 DISTMP3OPTS=
1840 NORMALIZEOPTS=
1841 CDSPEEDOPTS="-x"
1842 CDSPEEDVALUE=
1843
1844 # Default to one process if -j isn't specified
1845 MAXPROCS=1
1846
1847 # List of actions to perform - by default, run to completion
1848 ACTIONS=cddb,read,encode,tag,move,clean
1849
1850 # Asume fetch if under FreeBSD. curl is used for Mac OS X. wget is used for Linux/OpenBSD/NetBSD.
1851 # Let's use these checkings to determine the OS flavour, which will be used later
1852 if [ X$(uname) = "XFreeBSD" ] ; then
1853         HTTPGET=fetch
1854         NEEDCDROMID=y
1855         OSFLAVOUR=FBSD
1856 elif [ X$(uname) = "XDarwin" ] ; then
1857         HTTPGET=curl
1858         OSFLAVOUR=OSX
1859         # We should have disktool in OSX, but let's be sure...
1860         NEEDDISKTOOL=y
1861 elif [ X$(uname) = "XOpenBSD" ] ; then
1862         HTTPGET=wget
1863         MD5SUM=md5
1864 else
1865         HTTPGET=wget
1866 fi
1867
1868 # If CDDBAVAIL is set to n, no CDDB read is done
1869 # If USEID3 is set to n, no ID3 tagging is done
1870 CDDBAVAIL=y
1871 USEID3=y
1872
1873 if [ -z "$OUTPUTDIR" ]; then
1874         OUTPUTDIR=$(pwd)
1875 fi
1876
1877 if [ -z "$WAVOUTPUTDIR" ]; then
1878         WAVOUTPUTDIR="$OUTPUTDIR"
1879 fi
1880
1881 # Load system defaults
1882 if [ -r /etc/abcde.conf ]; then
1883         . /etc/abcde.conf
1884 fi
1885 # Load user preference defaults
1886 if [ -r $HOME/.abcde.conf ]; then
1887         . $HOME/.abcde.conf
1888 fi
1889
1890 # By this time, we need some HTTPGETOPTS already defined.
1891 # If the user has defined a non-default HTTPGET method, we should not be empty.
1892
1893 if [ "$HTTPGETOPTS" = "" ] ; then
1894         case $HTTPGET in
1895                 wget) HTTPGETOPTS="-q -O -";;
1896                 curl) HTTPGETOPTS="-f -s";;
1897                 fetch)HTTPGETOPTS="-q -o -";;
1898                 *) echo "abcde warning: HTTPGET in non-standard and HTTPGETOPTS are not defined." >&2 ;;
1899         esac
1900 fi
1901
1902 # If the CDROM has not been set yet, find a suitable one.
1903 # If this is a devfs system, default to /dev/cdroms/cdrom0
1904 # instead of /dev/cdrom
1905 if [ "$CDROM" = "" ] ; then
1906         if [ -e /dev/cdroms/cdrom0 ]; then
1907                 CDROM=/dev/cdroms/cdrom0
1908         elif [ -e /dev/cdrom ]; then
1909                 CDROM=/dev/cdrom
1910         elif [ -e /dev/cd0c ]; then
1911                 CDROM=/dev/cd0c
1912         elif [ -e /dev/acd0c ]; then
1913                 CDROM=/dev/acd0c
1914         elif [ -e /dev/disk1 ]; then
1915                 CDROM=/dev/disk1
1916         fi
1917 fi
1918
1919 # Parse command line options
1920 #while getopts 1a:bc:C:d:Dhj:klLnNo:pr:S:t:T:vVx opt ; do
1921 while getopts 1a:A:bc:C:d:Dhj:klLnNo:pPr:Rs:S:t:T:vVxw:W: opt ; do
1922         case "$opt" in
1923                 1) ONETRACK=y ;;
1924                 a) ACTIONS="$OPTARG" ;;
1925                 A) EXPACTIONS="$OPTARG";;
1926                 b) BATCH=y ;;
1927                 c) if [ -e "$OPTARG" ] ; then . "$OPTARG" ; else echo "abcde error: config file \"$OPTARG\" cannot be found." >&2 ; exit 1 ; fi ;;
1928                 C) DISCID="${OPTARG#abcde.}" ;;
1929                 d) CDROM="$OPTARG" ;;
1930                 D) set -x ;;
1931                 h) usage; exit ;;
1932 #               f) FORCECDDBUSELOCAL=y ;;
1933                 i) INLINETAG=y ;;
1934                 j) MAXPROCS="$OPTARG" ;;
1935                 k) KEEPWAVS=y ;;
1936                 l) LOWDISK=y ;;
1937                 L) CDDBUSELOCAL=y ;;
1938                 n) CDDBAVAIL=n ;;
1939                 N) INTERACTIVE=n ;;
1940                 m) DOSPLAYLIST=y ;;
1941                 o) OUTPUTTYPE="$OPTARG" ;;
1942                 p) PADTRACKS=y ;;
1943                 P) USEPIPES=y ;;
1944                 r) REMOTEHOSTS="$OPTARG" ;;
1945                 R) REPLAYGAIN=y ;;
1946                 s) STARTTRACKNUMBER="$OPTARG" ;;
1947                 S) CDSPEEDVALUE="$OPTARG" ;;
1948 #               t) PREPROCESSFORMATS="$OPTARG"
1949 #                  PREPROCESS=y ;;
1950 #               T) POSTPROCESSFORMATS="$OPTARG" ;;
1951                 t) STARTTRACKNUMBER="$OPTARG" ;;
1952                 T) STARTTRACKNUMBER="$OPTARG" ; STARTTRACKNUMBERTAG="y" ;;
1953                 v) 
1954                    echo "This is abcde v$VERSION."
1955                    echo "Usage: abcde [options] [tracks]"
1956                    echo "abcde -h for extra help"
1957                    exit
1958                    ;;
1959                 V) EXTRAVERBOSE="y" ;;
1960                 x) EJECTCD="y" ;;
1961                 w) COMMENT="$OPTARG" ;;
1962                 W) STARTTRACKNUMBER="${OPTARG}01" ; STARTTRACKNUMBERTAG="y" ; COMMENT="CD${OPTARG}" ;;
1963                 ?) usage; exit ;;
1964         esac
1965 done
1966
1967 shift $(($OPTIND - 1))
1968
1969 # Decide if we can continue.
1970 if [ "$ONETRACK" = "y" ]; then 
1971         # FIXME # remove check as soon as we find out about the other readers
1972         case "$CDROMREADERSYNTAX" in
1973                 dagrab|cddafs|debug) echo "abcde error: ONETRACK reading is not suported with "$CDROMREADERSYNTAX" yet"
1974                                          exit 1 ;;
1975         esac
1976         if [ "$BATCH" = "y" ]; then
1977                 echo "abcde error: BATCH mode is not compatible with ONETRACK mode"
1978                 BATCH=n
1979         fi
1980         # It does not matter how many tracks we want. In ONETRACK mode we grab them all
1981         # FIXME # allow ranges of tracks to be selected for onetrack ripping
1982         if [ $# -gt 0 ]; then
1983                 vecho "abcde warning: ONETRACK mode selected, grabbing all tracks..."
1984         fi
1985 else
1986         while [ $# -gt 0 ]; do
1987                 # Range parsing code courtesy of Vincent Ho
1988                 RSTART=$(echo $1 | cut -f1 -d-)
1989                 REND=$(echo $1 | cut -f2 -d-)
1990                 if [ "$RSTART" = "$REND" ]; then
1991                         NEWTRACKS="$RSTART"
1992                 else
1993                         NEWTRACKS=$(f_seq_line $RSTART $REND)
1994                 fi
1995                 TRACKQUEUE=$(echo "$TRACKQUEUE" "$NEWTRACKS")
1996                 shift
1997         done
1998 fi
1999
2000 # List of valid actions: cddb,read,normalize,encode,tag,move,playlist,clean
2001 # List of experimental actions: retag,transcode
2002
2003 # Determine what actions are to be done from $ACTIONS and set the
2004 # following environment variables for them:
2005 DOCDDB=n
2006 DOREAD=n
2007 DONORMALIZE=n
2008 DOPREPROCESS=n
2009 DOENCODE=n
2010 DOPOSTPROCESS=n
2011 DOTAG=n
2012 DOMOVE=n
2013 DOPLAYLIST=n
2014 DOCLEAN=n
2015
2016 for ACTION in $(echo $ACTIONS | tr , \ )
2017 do
2018         case $ACTION in
2019         cddb) DOCDDB=y;;
2020         read) DOREAD=y;;
2021         normalize) DONORMALIZE=y; DOREAD=y;;
2022 #       preprocess) DOPREPROCESS=y; DOREAD=y;;
2023         encode) DOENCODE=y; DOREAD=y;;
2024 #       postprocess) DOPREPROCESS=y; DOENCODE=y; DOREAD=y;;
2025         tag) DOTAG=y; DOREAD=y; DOENCODE=y; DOCDDB=y;;
2026         move) DOMOVE=y; DOTAG=y; DOREAD=y; DOENCODE=y; DOCDDB=y;;
2027         playlist) DOCDDB=y; DOPLAYLIST=y;;
2028         clean) DOCLEAN=y;;
2029         esac
2030 done
2031
2032 # Sanity checks:
2033
2034 # At this point a CDROM has to be defined, so we check it exists.
2035 if [ "$CDROM" != "" ] ; then 
2036         if [ "$CDROMREADERSYNTAX" = "cdda2wav" ] && [ "$NEEDCDROMID" = "y" ] ; then
2037                 if [ "$OSFLAVOUR" = "FBSD" ]; then
2038                         if ! echo "$CDROMID" | grep "^[0-9],[0-9],[0-9]$" >/dev/null 2>&1 ; then
2039                                 echo "abcde error: CDROMID not in the right format for $CDROMREADERSYNTAX"
2040                                 echo "Use \"cdrecord -scanbus\" to obtain a adecuate ID an set CDROMID accordingly"
2041                                 exit 1
2042                         fi
2043                 fi
2044         elif [ ! -e $CDROM ] ; then
2045                 echo "abcde error: CDROM device cannot be found." >&2
2046                 exit 1
2047         fi
2048 else
2049         echo "abcde error: CDROM has not been defined or cannot be found" >&2
2050         exit 1
2051 fi
2052
2053 # USEPIPES pre-tests, before we get into more serious stuff
2054 # Not compatible with:
2055 # - multiple outputs
2056 # - normalize
2057 # - lowdisk algorithm
2058 # - anything else?
2059 if [ X"$USEPIPES" = "Xy" ]; then
2060         if [ $(echo $OUTPUTTYPE | tr , \  | wc -w ) -gt 1 ]; then
2061                 echo "abcde error: Unix pipes not compatible with multiple outputs"
2062                 exit 1
2063         fi
2064         if [ X"$DONORMALIZE" = "Xy" ]; then
2065                 echo "abcde error: Unix pipes not compatible with normalizer"
2066                 # FIXME # Do we need to exit or shall we just disable the mode?
2067                 exit 1
2068         fi
2069         if [ X"$DOENCODE" = "Xn" ]; then
2070                 vecho "Disabling Unix pipes since we are not encoding!"
2071                 USEPIPES=n
2072         fi
2073         if [ X"$LOWDISK" = "Xy" ]; then
2074                 vecho "abcde error: Unix pipes not compatible with lowdisk algorithm"
2075                 exit 1
2076         fi
2077 fi
2078
2079 # Decide which CDROM reader we're gonna use
2080 case "$CDROMREADERSYNTAX" in
2081         cdparanoia|debug)
2082                 CDROMREADER="$CDPARANOIA"
2083                 CDROMREADEROPTS="$CDPARANOIAOPTS"
2084                 ;;
2085         cdda2wav)
2086                 CDROMREADER="$CDDA2WAV"
2087                 CDROMREADEROPTS="$CDDA2WAVOPTS"
2088                 ;;
2089         dagrab)
2090                 CDROMREADER="$DAGRAB"
2091                 CDROMREADEROPTS="$DAGRABOPTS"
2092                 ;;
2093         cddafs)
2094                 CDROMREADER="$CDDAFS"
2095                 CDROMREADEROPTS="$CDDAFSOPTS"
2096                 ;;
2097 esac
2098
2099 # There's only one normalize...
2100 case "$NORMALIZERSYNTAX" in
2101         default|normalize)
2102                 NORMALIZER="$NORMALIZE"
2103                 NORMALIZEROPTS="$NORMALIZEOPTS"
2104                 ;;
2105 esac
2106
2107 # If nothing has been specified, use oggenc for oggs and lame for mp3s and flac for flacs and speexenc for speex and mppenc for mpps
2108
2109 # Getting ready for multiple output changes
2110 for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
2111 do
2112         case $OUTPUT in
2113                 ogg)  [ "$OGGENCODERSYNTAX" = "default" ] && OGGENCODERSYNTAX=oggenc
2114                       [ "$DOTAG" = "y" ] && NEEDCOMMENTER=y
2115                       [ "$REPLAYGAIN" = "y" ] && NEEDVORBISGAIN=y
2116                         ;;
2117                 mp3)  [ "$MP3ENCODERSYNTAX" = "default" ] && MP3ENCODERSYNTAX=lame
2118                       [ "$DOTAG" = "y" ] && NEEDTAGGER=y
2119                         ;;
2120                 flac) [ "$FLACENCODERSYNTAX" = "default" ] && FLACENCODERSYNTAX=flac
2121                       [ "$DOTAG" = "y" ] && NEEDMETAFLAC=y
2122                         ;;
2123                 spx) [ "$SPEEXENCODERSYNTAX" = "default" ] && SPEEXENCODERSYNTAX=speexenc ;;
2124                 mpc) [ "$MPPENCODERSYNTAX" = "default" ] && MPPENCODERSYNTAX=mppenc ;;
2125                 *) echo "abcde error: Invalid OUTPUTTYPE defined" >&2
2126                    exit 1
2127                    ;;
2128         esac
2129 done
2130
2131 # decide which encoder
2132 case "$MP3ENCODERSYNTAX" in
2133         lame)
2134                 MP3ENCODEROPTS="$LAMEOPTS"
2135                 MP3ENCODER="$LAME"
2136                 ;;
2137         gogo)
2138                 MP3ENCODEROPTS="$GOGOOPTS"
2139                 MP3ENCODER="$GOGO"
2140                 ;;
2141         bladeenc)
2142                 MP3ENCODEROPTS="$BLADEENCOPTS"
2143                 MP3ENCODER="$BLADEENC"
2144                 ;;
2145         l3enc)
2146                 MP3ENCODEROPTS="$L3ENCOPTS"
2147                 MP3ENCODER="$L3ENC"
2148                 ;;
2149         xingmp3enc)
2150                 MP3ENCODEROPTS="$XINGMP3ENCOPTS"
2151                 MP3ENCODER="$XINGMP3ENC"
2152                 ;;
2153         mp3enc)
2154                 MP3ENCODEROPTS="$MP3ENCOPTS"
2155                 MP3ENCODER="$MP3ENC"
2156                 ;;
2157 esac
2158 case "$OGGENCODERSYNTAX" in
2159         vorbize)
2160                 OGGENCODEROPTS="$VORBIZEOPTS"
2161                 OGGENCODER="$VORBIZE"
2162                 ;;
2163         oggenc)
2164                 OGGENCODEROPTS="$OGGENCOPTS"
2165                 OGGENCODER="$OGGENC"
2166                 ;;
2167 esac
2168 case "$FLACENCODERSYNTAX" in
2169         flac)
2170                 FLACENCODEROPTS="$FLACOPTS"
2171                 FLACENCODER="$FLAC"
2172                 ;;
2173 esac
2174 case "$SPEEXENCODERSYNTAX" in
2175         speexenc)
2176                 SPEEXENCODEROPTS="$SPEEXENCOPTS"
2177                 SPEEXENCODER="$SPEEXENC"
2178                 ;;
2179 esac
2180 case "$MPPENCODERSYNTAX" in
2181         mppenc)
2182                 MPPENCODEROPTS="$MPPENCOPTS"
2183                 MPPENCODER="$MPPENC"
2184                 ;;
2185 esac
2186
2187 # and which tagger
2188
2189 if [ "$ID3TAGV" = "1" ]; then
2190         TAGGER="$ID3"
2191         TAGGEROPTS="$ID3OPTS"
2192 else
2193         TAGGER="$ID3V2"
2194         TAGGEROPTS="$ID3V2OPTS"
2195 fi
2196
2197 # Clean up nice options (either use '-n NICELEVEL or -NICELEVEL')
2198
2199 if [ "$ENCNICE" ]; then
2200         ENCNICE="-n $ENCNICE"
2201 fi
2202 if [ "$READNICE" ]; then
2203         READNICE="-n $READNICE"
2204 fi
2205 if [ "$DISTMP3NICE" ]; then
2206         DISTMP3NICE="-n $DISTMP3NICE"
2207 fi
2208
2209 # Don't check for stuff if it's not needed
2210 if [ "$REMOTEHOSTS" ]; then NEEDDISTMP3=y; fi
2211 [ "$DONORMALIZE" = "y" ] && NEEDNORMALIZER=y
2212 [ "$EJECTCD" = "y" ] && NEEDEJECT=y
2213 [ ! "$CDDBAVAIL" = "n" ] && [ "$DOCDDB" = "y" ] && NEEDHTTPGET=y
2214
2215 if [ X"$CDSPEEDVALUE" != "X" ]; then
2216         case "$CDROMREADERSYNTAX" in
2217                 cdparanoia|debug) CDROMREADEROPTS="$CDPARANOIAOPTS -S $CDSPEEDVALUE" ;;
2218                 *) NEEDCDSPEED=y ;;
2219         esac
2220 fi
2221
2222 # Rippers with USEPIPE support
2223 # FIXME # Include here all the rippers we can figure out support pipes
2224 PIPE_cdparanoia="-"
2225
2226 # Encoders with USEPIPE support
2227 # FIXME # Include here all the encoders we can figure out support pipes
2228 PIPE_lame="-"
2229 PIPE_oggenc="-"
2230
2231 # Figure out if we can use pipes with the ripper/encoder combination
2232 # exit otherwise
2233 if [ "$USEPIPES" = "y" ]; then
2234         PIPERIPPERSVARCHECK="PIPE_${CDROMREADER}"
2235         case "$OUTPUT" in
2236                 mp3)
2237                         PIPEENCODERSVARCHECK="PIPE_$MP3ENCODERSYNTAX" ;;
2238                 ogg)
2239                         PIPEENCODERSVARCHECK="PIPE_$OGGENCODERSYNTAX" ;;
2240                 flac)
2241                         PIPEENCODERSVARCHECK="PIPE_$FLACENCODERSYTNAX" ;;
2242                 spx)
2243                         PIPEENCODERSVARCHECK="PIPE_$SPEEXENCODER" ;;
2244                 mpc)
2245                         PIPEENCODERSVARCHECK="PIPE_$MPPENCODER" ;;
2246         esac
2247         if [ ! -n "${!PIPERIPPERSVARCHECK}" ] ; then
2248                 echo "abcde error: no support for pipes with given ripper" >&2
2249                 echo "read the USEPIPES file from the source tarball to help" >&2
2250                 echo "on a Debian system, read /usr/share/doc/abcde/USEPIPES.gz" >&2
2251                 exit 1;
2252         fi
2253         if [ ! -n "${!PIPEENCODERSVARCHECK}" ] ; then
2254                 echo "abcde error: no support for pipes with given encoder" >&2
2255                 echo "read the USEPIPES file from the source tarball to help" >&2
2256                 echo "on a Debian system, read /usr/share/doc/abcde/USEPIPES.gz" >&2
2257                 exit 1;
2258         fi
2259 fi
2260
2261
2262 # Make sure a buncha things exist
2263 for X in $CDROMREADER $CDDISCID ${NEEDTAGGER+$TAGGER} $MP3ENCODER \
2264         $OGGENCODER $FLACENCODER $SPEEXENCODER $MPPENCODER \
2265         ${NEEDHTTPGET+$HTTPGET} ${NEEDDISTMP3+$DISTMP3} \
2266         ${NEEDCOMMENTER+$VORBISCOMMENT} ${NEEDMETAFLAC+$METAFLAC} \
2267         ${NEEDNORMALIZER+$NORMALIZER} ${NEEDEJECT+$EJECT} \
2268         ${NEEDDISKTOOL+disktool} ${NEEDCDSPEED+$CDSPEED} \
2269         ${NEEDVORBISGAIN+$VORBISGAIN} ${NEEDMKCUE+$MKCUE}
2270 do
2271         # Cut off the command-line options we just added in
2272         X=$(echo $X | cut -d' ' -f2)
2273         if [ "$(which $X)" = "" ]; then
2274                 echo "abcde error: $X is not in your path." >&2
2275                 exit 1
2276         elif [ ! -x $(which $X) ]; then
2277                 echo "abcde error: $X is not executable." >&2
2278                 exit 1
2279         fi
2280 done
2281
2282 CDROMREADER="$CDROMREADER $CDROMREADEROPTS"
2283 CDDBTOOL="$CDDBTOOL $CDDBTOOLOPTS"
2284 HTTPGET="$HTTPGET $HTTPGETOPTS"
2285
2286 # Here it used to say:
2287 # One thousand lines in, we can start doing stuff with things
2288 # Well, right now we are at line 2157 ;)
2289
2290 # Export needed things so they can be read in this subshell
2291 export CDDBTOOL ABCDETEMPDIR TRACKQUEUE LOWDISK EJECTCD EJECT EJECTOPTS
2292 export CDROM CDDBDATA REMOTEHOSTS MAXPROCS HTTPGET MD5SUM
2293
2294 # User-definable function to set some things. Use it for
2295 #  - closing the CD tray with eject -t
2296 #  - set the CD speed value with eject -x
2297 vecho -n "Executing customizable pre-read function... "
2298
2299 pre_read # Execute the user-defined pre-read funtion. Close the CD with it.
2300
2301 vecho "done."
2302
2303 do_discid # Get ABCDETEMPDIR created and status file initialized
2304
2305 if [ "$DOCDDB" = "y" ]; then
2306         if [ $CDDBUSELOCAL = "y" ]; then
2307                 do_localcddb
2308         fi
2309         if checkstatus cddb-choice > /dev/null; then
2310                 :
2311         else 
2312                 if [ ! "$CDDBLOCALSUCCESS" = "y" ] ; then
2313                         do_cddbstat
2314                         do_cddbquery
2315                         do_cddbread
2316                 fi
2317         fi
2318         do_cddbedit
2319
2320         eval $($CDDBTOOL parse "$CDDBDATA")
2321 fi
2322
2323 # Before reading tracks, we set the speed of the device
2324
2325 if [ X"$CDSPEEDVALUE" != "X" ]; then
2326         case "$CDROMREADERSYNTAX" in
2327                 cdparanoia|debug) : ;;
2328                 *) do_cdspeed ;;
2329         esac
2330 fi
2331
2332 # Create playlist if needed (backgroundable) and start reading in tracks
2333
2334 (
2335
2336 if [ ! "$ONETRACK" = "y" ]; then
2337         if [ "$DOPLAYLIST" = "y" ]; then
2338                 echo Creating playlist... >&2
2339                 do_playlist
2340         fi
2341 fi
2342
2343 # For the lowdisk option, only one program is running at once so the encoder
2344 # can be unsilenced right away.
2345 if [ "$LOWDISK" = "y" ] || [ "$ONETRACK" = "y" ]; then
2346         echo "encode-output=loud" >> "$ABCDETEMPDIR/status"
2347 fi
2348
2349 if [ "$ONETRACK" = "y" ]; then 
2350         FIRSTTRACK=$( echo $TRACKQUEUE | awk '{print $1}' )
2351         TRACKS="$FIRSTTRACK"
2352         for UTRACKNUM in $TRACKQUEUE; do :;done
2353         if checkstatus readtrack-$FIRSTTRACK; then :; else
2354                 #do_cdread_one $UTRACKNUM $FIRSTTRACK
2355                 do_cdread onetrack $FIRSTTRACK $UTRACKNUM
2356         fi
2357 else
2358         for UTRACKNUM in $TRACKQUEUE
2359         do
2360                 if [ "$DOREAD" = "y" ]; then
2361                         if [ "$USEPIPES" = "y" ]; then
2362                                 if checkstatus readencodetrack-$UTRACKNUM; then :; else
2363                                         do_cdread $UTRACKNUM | do_encode $UTRACKNUM %local0%
2364                                 fi
2365                         else
2366                                 if checkstatus readtrack-$UTRACKNUM; then :; else
2367                                         do_cdread $UTRACKNUM
2368                                 fi
2369                                 if [ "$?" != "0" ]; then
2370                                         # CD read failed - don't give the goahead to
2371                                         # the encoder
2372                                         echo NO
2373                                         exit
2374                                 fi
2375                         fi
2376                 fi
2377                 if [ "$BATCH" = "y" ]; then
2378                     :
2379                 else
2380                         echo NEXTTRACK # Get the encoder machine churning again
2381                         if [ "$DOREAD" = "y" ]; then
2382                                 if [ "$LOWDISK" = "y" ] && [ "$DOENCODE" = "y" ]; then
2383                                         until checkstatus encodetrack-$UTRACKNUM
2384                                         do
2385                                                 if checkerrors encodetrack-$UTRACKNUM; then
2386                                                         break
2387                                                 fi
2388                                                 sleep 2
2389                                         done
2390                                 fi
2391                         fi
2392                 fi
2393         done
2394 fi
2395
2396 # Now that we're done the encoding can be loud again -
2397 # if we're not using SMP.
2398 if [ "$MAXPROCS" = "1" ]; then
2399         echo "encode-output=loud" >> "$ABCDETEMPDIR/status"
2400 fi
2401
2402 # All tracks read, start encoding.
2403 if [ "$BATCH" = "y" ] || [ "$ONETRACK" = "y" ]; then
2404         echo NEXTTRACK
2405 fi
2406
2407 # We are now finished with the cdrom - it can be safely ejected. Note that
2408 # abcde will not have completed yet.
2409 if [ "$EJECTCD" = "y" ] && [ -x $(which $EJECT) ]; then
2410         # We check if the disk we are processing is actually the disk inside the 
2411         # CD tray. If not, we do not eject the CD, since it might be so that the
2412         # user ejected it manually.
2413         #CURRENTTRACKINFO=$($CDDISCID $CDROM)
2414         #if if [ "$?" != "1" ] && [ "$CURRENTTRACKINFO" = "$TRACKINFO" ] ; then 
2415         # More FreeBSD bits.
2416         if [ X"$(uname)" = X"FreeBSD" ] ; then
2417                 # FreeBSD eject uses the EJECT environment variable to name the CDROM
2418                 # but in this script EJECT is in the envionment and names the program
2419                 eject=$EJECT
2420                 unset EJECT
2421                 # The FreeBSD eject needs "adc0" not "/dev/adc0c"
2422                 cd="$(echo $CDROM | sed -e 's=.*/==;s=[a-h]$==;')"
2423                 $eject $EJECTOPTS $cd
2424         elif [ X"$(uname)" = X"Darwin" ] ; then
2425                 disktool -e ${CDROM#/dev/} 0
2426         else
2427                 $EJECT $EJECTOPTS $CDROM
2428         fi
2429         #fi
2430 fi
2431
2432 ) | (
2433
2434 ## Do we need to pre-process 
2435 #if [ x"$PREPROCESS" = "x" ] ; then
2436 #       cat
2437 #else
2438 #       for PRETRACKNUM in $TRACKQUEUE
2439 #       do
2440 #               read GOAHEAD
2441 #               if [ "$GOAHEAD" = "NO" ]; then break; fi
2442 #               PREPROCEED=
2443 #               until [ $PREPROCEED ]
2444 #               do
2445 #                       if checkstatus readtrack-$PRETRACKNUM; then PREPROCEED=y; break; fi
2446 #                       # all locations are working, wait and try again later
2447 #                       if [ ! $PREPROCEED ]; then sleep 3; fi
2448 #               done
2449 #               ( do_preprocess $PRETRACKNUM 
2450 #               echo "NEXTTRACK"
2451 #               ) &
2452 #       done
2453 #fi
2454 #
2455 #) | (
2456
2457 # In batch mode, we want all tracks to be read first.
2458 if [ "$BATCH" = "y" ]; then
2459         read GOAHEAD # For blocking - will contain either "NO" or "NEXTTRACK"
2460         if [ "$GOAHEAD" = "NO" ]; then break; fi
2461         for LASTTRACK in $TRACKQUEUE; do :; done
2462         if checkstatus readtrack-$LASTTRACK; then
2463                 if [ "$DONORMALIZE" = "y" ]; then
2464                         if checkstatus normalizetrack-$LASTTRACK; then :; else do_batch_normalize; fi
2465                         if checkerrors batch-normalize; then exit; fi
2466                 fi
2467                 if [ "$DOENCODE" = "y" ]; then
2468                         if checkstatus encodetrack-$LASTTRACK; then :; else do_batch_encode; fi
2469                         if checkerrors batch-encode; then exit; fi
2470                 fi
2471         fi
2472 fi
2473
2474 # If we are using ONETRACK, we can proceed with the normal encoding using just the $FIRSTTRACK as TRACKQUEUE
2475 if [ "$ONETRACK" = "y" ] ; then
2476         FIRSTTRACK=$( echo $TRACKQUEUE | awk '{print $1}')
2477         TRACKQUEUE=$FIRSTTRACK
2478         TRACKS="$FIRSTTRACK"
2479 fi
2480
2481 # Do the encoding, including parallelization of remote encoding
2482 # Figure out where each track is going to be encoded
2483 ENCODELOCATIONS="$(echo $REMOTEHOSTS | tr , ' ')"
2484 if [ "$MAXPROCS" != "0" ]; then
2485         for NUM in $(f_seq_row 1 "$MAXPROCS")
2486         do
2487                 ENCODELOCATIONS="$ENCODELOCATIONS %local$NUM%"
2488         done
2489 fi
2490 # Strip whitespace
2491 ENCODELOCATIONS=$(echo $ENCODELOCATIONS)
2492 for UTRACKNUM in $TRACKQUEUE
2493 do
2494         # Wait for our cue
2495         read GOAHEAD # For blocking - will contain either "NO" or "NEXTTRACK"
2496         if [ "$GOAHEAD" = "NO" ]; then break; fi
2497         # find out where this track is to be encoded
2498         if [ "$DOENCODE" = "y" -a "$USEPIPES" != "y" ]; then
2499                 # Make sure we have a place to encode this, if not, exit stage right
2500                 if [ -z "$ENCODELOCATIONS" ]; then
2501                         continue
2502                 fi
2503                 PROCEED=
2504                 until [ $PROCEED ]
2505                 do
2506                         for LOCATION in $ENCODELOCATIONS
2507                         do
2508                                 PREVIOUSTRACK="$(checkstatus encodetracklocation-$LOCATION)"
2509                                 # check first if a track has ever been assigned to this location
2510                                 if [ -z "$PREVIOUSTRACK" ]; then PROCEED=y; break; fi
2511                                 # If it errored out, rebuild $ENCODELOCATIONS without this location in it
2512                                 if checkerrors encodetrack-$PREVIOUSTRACK; then
2513                                         for TEMPLOCATION in $ENCODELOCATIONS
2514                                         do
2515                                                 if [ "$TEMPLOCATION" != "$LOCATION" ]; then
2516                                                         TEMPENCODELOCATIONS="$TEMPENCODELOCATIONS $TEMPLOCATION"
2517                                                 fi
2518                                         done
2519                                         ENCODELOCATIONS=$(echo $TEMPENCODELOCATIONS)
2520                                         ABORT=y
2521                                         PROCEED=y
2522                                         break
2523                                 fi
2524                                 # We're still here, this location must have been previously assigned,
2525                                 # and last completed without error - check if it's done with the
2526                                 # previous track yet
2527                                 if checkstatus encodetrack-$PREVIOUSTRACK; then PROCEED=y; break; fi
2528                         done
2529                         # all locations are working, wait and try again later
2530                         if [ ! $PROCEED ]; then sleep 3; fi
2531                 done
2532                 # Record the location we're about to encode the next track at
2533                 echo "encodetracklocation-$LOCATION=$UTRACKNUM" >> "$ABCDETEMPDIR/status"
2534         fi
2535         # Don't proceed with the rest of the loop if we can't encode
2536         if [ "$ABORT" ]; then continue; fi
2537         # Set TRACKNUM, TRACKNAME
2538         if [ -e "$CDDBDATA" ]; then
2539                 if [ "$ONETRACK" = "y" ]; then 
2540                         TRACKNAME="$DALBUM"
2541                         TRACKNUM="$FIRSTTRACK"
2542                         splitvarious
2543                 else
2544 #                       TRACKNUM=$(printf %0.${TRACKNUMPADDING}d $(expr ${UTRACKNUM} + 0))
2545                         TRACKNUM=$UTRACKNUM
2546                         CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
2547                         TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2 -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')
2548                         splitvarious
2549                 fi
2550         fi
2551         # You can't encode a file which needs to be normalized before finishing
2552         # You can't tag a file before it's finished encoding -
2553         # thus all of this is backgrounded together
2554         (
2555         if [ "$DONORMALIZE" = "y" ]; then
2556                 if checkstatus readtrack-$UTRACKNUM; then
2557                         if checkstatus normalizetrack-$UTRACKNUM; then :; else do_normalize $UTRACKNUM; fi
2558                 fi
2559         fi
2560         if [ "$DOENCODE" = "y" -a "$USEPIPES" != "y" ]; then
2561                 if checkstatus readtrack-$UTRACKNUM; then
2562                         #if checkstatus encodetrack-$UTRACKNUM; then :; else do_encode $UTRACKNUM $LOCATION; fi
2563                         if [ "$DONORMALIZE" = "y" ]; then
2564                                 if checkstatus normalizetrack-$UTRACKNUM; then
2565                                         if checkstatus encodetrack-$UTRACKNUM; then :; else do_encode $UTRACKNUM $LOCATION $OUTPUT; fi
2566                                 fi
2567                         else
2568                                 if checkstatus encodetrack-$UTRACKNUM; then :; else do_encode $UTRACKNUM $LOCATION $OUTPUT; fi
2569                         fi
2570                 fi
2571         fi
2572         if [ "$DOTAG" = "y" ]; then
2573                 if checkstatus encodetrack-$UTRACKNUM; then
2574                         if checkstatus tagtrack-$UTRACKNUM; then :; else do_tag $UTRACKNUM; fi
2575                 fi
2576         fi
2577         if [ "$DOMOVE" = "y" ]; then
2578                 if checkstatus tagtrack-$UTRACKNUM; then
2579                         if checkstatus movetrack-$UTRACKNUM; then :; else do_move $UTRACKNUM; fi
2580                 fi
2581         fi
2582         ) &
2583 done
2584 # Go through it again and make sure there's no distmp3 stragglers, otherwise
2585 # we'll delete the files they're working on
2586 ## FIXME ## Check also for files which are encoded using PIPEs.
2587 if [ "$DOENCODE" = "y" -a "$USEPIPES" != "y" ]; then
2588         PROCEED=
2589         until [ $PROCEED ]
2590         do
2591                 PROCEED=y
2592                 for LOCATION in $ENCODELOCATIONS
2593                 do
2594                         CHECKTRACK="$(checkstatus encodetracklocation-$LOCATION)"
2595                         # "How can he give us a status update, if he's DEAD?"
2596                         if checkstatus encodetrack-$CHECKTRACK; then
2597                                 continue
2598                         fi
2599                         # Nothing to see here please go quietly back to your homes
2600                         if [ -z "$CHECKTRACK" ]; then continue; fi
2601                         # You're still here? Maybe there is something...
2602                         if checkstatus encodetrack-$CHECKTRACK; then :; else PROCEED= ; break; fi
2603                 done
2604                 # hold up
2605                 if [ ! $PROCEED ]; then sleep 5; fi
2606         done
2607 fi
2608 # If the above didn't catch the stragglers, this will
2609 wait
2610 # Check to see if run_command logged any errors
2611 if [ -f "$ABCDETEMPDIR/errors" ]; then
2612         echo "The following commands failed to run:"
2613         cat "$ABCDETEMPDIR/errors"
2614         # Don't clean up
2615         DOCLEAN=n
2616 fi
2617 if [ "$KEEPWAVS" = "y" ];then
2618         # Don't clean up
2619         DOCLEAN=n
2620 fi
2621 if [ "$DOCLEAN" = "y" ]; then
2622         # Wipe all the evidence
2623         # Gimme gimme gimme some more time!
2624         sleep 5
2625         rm -rf "$ABCDETEMPDIR"
2626         echo "Finished."
2627 else
2628         echo "Finished. Not cleaning $ABCDETEMPDIR."
2629 fi
2630 )
2631 exit 0
2632
2633 # b:is_bash
2634 # vim:tabstop=4