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