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