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