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