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