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