Modded the output for fields in the parser code
[clinton/abcde.git] / abcde
CommitLineData
c9c2ca27 1#!/bin/sh
2# Copyright (c) 1998-2001 Robert Woodcock <rcw@debian.org>
5895f404 3# Copyright (c) 2003-2005 Jesus Climent <jesus.climent@hispalinux.es>
c9c2ca27 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
9ff5088c 14VERSION="2.3.99"
c9c2ca27 15
16usage ()
17{
18echo "This is abcde v$VERSION."
19echo "Usage: abcde [options] [tracks]"
20echo "Options:"
ad8b6531 21echo "-1 Encode the whole CD in a single file"
22echo "-a <action1[,action2]...>"
23echo " Actions to perform (cddb,read,normalize,encode,tag,move,playlist,clean)"
9a0af871 24#echo "-A Experimental actions (retag, transcode)"
ad8b6531 25echo "-b Batch mode: enable album normalization and nogap encoding"
26echo "-c <file>"
27echo " Specify a configuration file (overrides system and user config files)"
28echo "-C <discid#>"
29echo " Specify discid to resume from (only needed if you no longer have the cd)"
30echo "-d <device>"
31echo " Specify CDROM device to grab"
32echo "-D Debugging mode (equivalent to sh -x abcde)"
bef9c2d8 33echo "-e Erase encoded track information from status file"
ad8b6531 34echo "-h This help information"
c9c2ca27 35#echo "-i Tag files while encoding, when possible (local only) -NWY-"
ad8b6531 36echo "-j <#> Number of encoder processes to run at once (localhost)"
37echo "-k Keep the wav tracks for later use"
38echo "-l Use low disk space algorithm"
39echo "-L Use local CDDB storage directory"
40echo "-n No lookup. Don't query CDDB, just create and use template"
41echo "-N Noninteractive. Never prompt for anything"
42echo "-m Modify playlist to include CRLF endings, to comply with some players"
24f444d7 43echo "-M Create a CUE file"
ad8b6531 44echo "-o <type1[,type2]...>"
b82e2042 45echo " Output file type(s) (vorbis,mp3,flac,spx,mpc). Defaults to vorbis"
ad8b6531 46echo "-p Pad track numbers with 0's (if less than 10 tracks)"
d5200e72 47echo "-P Use UNIX pipes to read+encode without wav files"
ad8b6531 48echo "-r <host1[,host2]...>"
49echo " Also encode on these remote hosts"
b82e2042 50echo "-R Add replaygain values to the tag info (only for vorbis,flac)"
9ff5088c 51echo "-s <field>"
52echo " Show dielfs from the CDDB info (year,genre)"
ad8b6531 53echo "-S <#> Set the CD speed"
c9c2ca27 54#echo "-t File types to preprocess (wav)"
55#echo "-T Set postprocessing options"
ad8b6531 56echo "-t <#> Start the track numbering at a given number"
57echo "-T <#> Same as -t but modifies tag numbering"
58echo "-v Show version number and exit"
59echo "-V Be a bit more verbose about what is happening behind the scenes"
60echo "-x Eject CD after all tracks are read"
61echo "-w <comment>"
62echo " Add a comment to the CD tracks"
63echo "-W <#> Contatenate CDs: -T #01 -w \"CD #\""
64echo ""
c9c2ca27 65echo "Tracks is a space-delimited list of tracks to grab."
6119aaa1 66echo "Ranges specified with hyphens are allowed (i.e., 1-5)."
67echo ""
efc6de2f 68#echo "Double hyphens are used to concatenate tracks"
c9c2ca27 69}
70
71# Funtions to replace the need of seq, which is too distribution dependant.
72f_seq_row ()
73{
74 i=$1
75 while [ $i -ne `expr $2 + 1` ]
76 do
77 echo $i
78 i=`expr $i + 1`
79 done
80}
81
82f_seq_line ()
83{
84 i=$1
85 while [ $i -ne `expr $2 + 1` ]
86 do
87 printf $i" "
88 i=`expr $i + 1`
89 done
90 echo
91}
92
bef9c2d8 93# Functions to replace the need of awk {print $1} and {print $NF}
94get_first()
95{
96if [ X"$1" = "X" ]; then
97 for first in `cat`; do
98 break
99 done
100else
101 first=$1
102fi
103echo $first
104}
105
106get_last()
107{
108if [ X"$1" = "X" ]; then
109 for stdin in `cat`; do
110 last=$stdin
111 done
112else
113 for last in $@ ; do :; done
114fi
115echo $last
116}
117
c9c2ca27 118# checkstatus [blurb]
119# Returns "0" if the blurb was found, returns 1 if it wasn't
120# Puts the blurb content, if available, on stdout.
121# Otherwise, returns "".
122checkstatus ()
123{
124 # Take the last line in the status file if there's multiple matches
125 PATTERN="^$1(=.*)?$"
126 BLURB=$(egrep $PATTERN "$ABCDETEMPDIR/status" | tail -n 1)
127
128 if [ -z "$BLURB" ]; then
129 # No matches found
130 return 1
131 else
132 # Matches found
133 # See if there's a = in it
134 if [ "$(echo $BLURB | grep -c =)" != "0" ]; then
135 echo "$(echo $BLURB | cut -f2- -d=)"
136 fi
137 return 0
138 fi
139}
140
141# checkerrors [blurb]
142# Returns "0" if the blurb was found (meaning there was an error),
143# returns 1 if it wasn't (yes this is a little backwards).
144# Does not print the blurb on stdout.
145# Otherwise, returns "".
146checkerrors ()
147{
99d009fa 148 if [ -e "$ABCDETEMPDIR/errors" ]; then :; else
c9c2ca27 149 return 1
150 fi
151 # Take the last line in the status file if there's multiple matches
152 PATTERN="^$1(:.*)?$"
03308bc7 153 BLURB="$(egrep $PATTERN "$ABCDETEMPDIR/errors" | tail -n 1)"
c9c2ca27 154
155 if [ -z "$BLURB" ]; then
156 # negative, we did not have a negative...
157 return 1
158 else
159 # affirmative, we had a negative...
160 return 0
161 fi
162}
163
164# run_command [blurb] [command...]
165# Runs a command, silently if necessary, and updates the status file
166run_command ()
167{
168 BLURB="$1"
169 shift
170 # See if this is supposed to be silent
171 if [ "$(checkstatus encode-output)" = "loud" ]; then
172 "$@" >&2
173 RETURN=$?
174 else
175 # Special case for SMP, since
176 # encoder output is never displayed, don't mute echos
177 if [ -z "$BLURB" -a "$MAXPROCS" != "1" ]; then
178 "$@" >&2
179 RETURN=$?
180 else
181 "$@" >/dev/null 2>&1
182 RETURN=$?
183 fi
184 fi
185 case "$1" in
186 normalize)
187 if [ "$RETURN" = "2" ]; then
188 # File was already normalized.
189 RETURN=0
190 fi
191 ;;
192 esac
193 if [ "$RETURN" != "0" ]; then
194 # Put an error in the errors file. For various reasons we
195 # can't capture a copy of the program's output but we can
196 # log what we attempted to execute and the error code
197 # returned by the program.
198 if [ "$BLURB" ]; then
199 TWEAK="$BLURB: "
200 fi
201 echo "${TWEAK}returned code $RETURN: $@" >> "$ABCDETEMPDIR/errors"
202 return $RETURN # Do not pass go, do not update the status file
203 fi
204 if [ "$BLURB" ]; then
205 echo $BLURB >> "$ABCDETEMPDIR/status"
206 fi
207}
208
209# relpath() and slash() are Copyright (c) 1999 Stuart Ballard and
210# distributed under the terms of the GNU GPL v2 or later, at your option
211
212# Function to determine if a word contains a slash.
213slash ()
214{
215 case "$1" in
216 */*) return 0;;
217 *) return 1;;
218 esac
219}
220
221# Function to give the relative path from one file to another.
222# Usage: relpath fromfile tofile
223# eg relpath music/Artist/Album.m3u music/Artist/Album/Song.mp3
224# (the result would be Album/Song.mp3)
225# Output is relative path to $2 from $1 on stdout
226
227# This code has the following restrictions:
228# Multiple ////s are not collapsed into single /s, with strange effects.
229# Absolute paths and ../s are handled wrong in FR (but they work in TO)
230# If FR is a directory it must have a trailing /
231
232relpath ()
233{
234 FR="$1"
235 TO="$2"
236
237 case "$TO" in
238 /*) ;; # No processing is needed for absolute paths
239 *)
240 # Loop through common prefixes, ignoring them.
241 while slash "$FR" && [ "$(echo "$FR" | cut -d/ -f1)" = "$(echo "$TO" | cut -d/ -f1)" ]
242 do
243 FR="$(echo "$FR" | cut -d/ -f2-)"
244 TO="$(echo "$TO" | cut -d/ -f2-)"
245 done
246 # Loop through directory portions left in FR, adding appropriate ../s.
247 while slash "$FR"
248 do
249 FR="$(echo "$FR" | cut -d/ -f2-)"
250 TO="../$TO"
251 done
6c124acf 252 ;;
c9c2ca27 253 esac
254
255 echo $TO
256}
257
258# This code splits the a Various Artist track name from one of the following
259# forms:
260#
261# forward: Artist / Track
262# forward-dash: Artist - Track
263# reverse: Track / Artist
264# reverse-dash: Track - Artist
265# colon: Artist: Track
266# trailing-paren: Artist (Track)
267#
268# variables used:
269# VARIOUSARTISTS, VARIOUSARTISTSTYLE, TRACKNAME, TRACKARTIST
270splitvarious ()
271{
272 if [ "$VARIOUSARTISTS" = "y" ] && [ ! "$ONETRACK" = "y" ]; then
273 case "$VARIOUSARTISTSTYLE" in
274 forward)
eb1474fe 275 DTITLEARTIST=$(echo "$TRACKNAME" | sed 's- / -~-g')
276 TRACKARTIST=$(echo "$DTITLEARTIST" | cut -f1 -d~)
277 TRACKNAME=$(echo "$DTITLEARTIST" | cut -f2 -d~)
c9c2ca27 278 ;;
279 forward-dash)
eb1474fe 280 DTITLEARTIST=$(echo "$TRACKNAME" | sed 's, - ,~,g')
281 TRACKARTIST=$(echo "$DTITLEARTIST" | cut -f1 -d~)
282 TRACKNAME=$(echo "$DTITLEARTIST" | cut -f2 -d~)
c9c2ca27 283 ;;
284 reverse)
eb1474fe 285 DTITLEARTIST=$(echo "$TRACKNAME" | sed 's- / -~-g')
286 TRACKARTIST=$(echo "$DTITLEARTIST" | cut -f2 -d~)
287 TRACKNAME=$(echo "$DTITLEARTIST" | cut -f1 -d~)
c9c2ca27 288 ;;
289 reverse-dash)
eb1474fe 290 DTITLEARTIST=$(echo "$TRACKNAME" | sed 's, - ,~,g')
291 TRACKARTIST=$(echo "$DTITLEARTIST" | cut -f2 -d~)
292 TRACKNAME=$(echo "$DTITLEARTIST" | cut -f1 -d~)
c9c2ca27 293 ;;
294 colon)
eb1474fe 295 DTITLEARTIST=$(echo "$TRACKNAME" | sed 's-: -~-g')
296 TRACKARTIST=$(echo "$DTITLEARTIST" | cut -f1 -d~)
297 TRACKNAME=$(echo "$DTITLEARTIST" | cut -f2 -d~)
c9c2ca27 298 ;;
299 trailing-paren)
eb1474fe 300 DTITLEARTIST=$(echo "$TRACKNAME" | sed 's,^\(.*\) (\(.*\)),\1~\2,')
301 TRACKARTIST=$(echo "$DTITLEARTIST" | cut -f2 -d~)
302 TRACKNAME=$(echo "$DTITLEARTIST" | cut -f1 -d~)
c9c2ca27 303 ;;
304 esac
305 elif [ "$ONETRACK" = "y" ]; then
306 TRACKARTIST="Various"
307 else
eb1474fe 308 TRACKARTIST="$DARTIST"
c9c2ca27 309 fi
310}
311
2994a485 312do_getgenreid () {
313local genre=$(echo "${@}" | tr '[A-Z]' '[a-z]')
314local id=""
315 case ${genre} in
316 "blues") id=0 ;;
317 "classic rock") id=1 ;;
318 "country") id=2 ;;
319 "dance") id=3 ;;
320 "disco") id=4 ;;
321 "funk") id=5 ;;
322 "grunge") id=6 ;;
323 "hip-hop") id=7 ;;
324 "jazz") id=8 ;;
325 "metal") id=9 ;;
326 "new age") id=10 ;;
327 "oldies") id=11 ;;
328 "other") id=12 ;;
329 "pop") id=13 ;;
330 "r&b") id=14 ;;
331 "rap") id=15 ;;
332 "reggae") id=16 ;;
333 "rock") id=17 ;;
334 "techno") id=18 ;;
335 "industrial") id=19 ;;
336 "alternative") id=20 ;;
337 "ska") id=21 ;;
338 "death metal") id=22 ;;
339 "pranks") id=23 ;;
340 "soundtrack") id=24 ;;
341 "euro-techno") id=25 ;;
342 "ambient") id=26 ;;
343 "trip-hop") id=27 ;;
344 "vocal") id=28 ;;
345 "jazz+funk") id=29 ;;
346 "fusion") id=30 ;;
347 "trance") id=31 ;;
348 "classical") id=32 ;;
349 "instrumental") id=33 ;;
350 "acid") id=34 ;;
351 "house") id=35 ;;
352 "game") id=36 ;;
353 "sound clip") id=37 ;;
354 "gospel") id=38 ;;
355 "noise") id=39 ;;
356 "alt. rock") id=40 ;;
357 "bass") id=41 ;;
358 "soul") id=42 ;;
359 "punk") id=43 ;;
360 "space") id=44 ;;
361 "meditative") id=45 ;;
362 "instrum. pop") id=46 ;;
363 "instrum. rock") id=47 ;;
364 "ethnic") id=48 ;;
365 "gothic") id=49 ;;
366 "darkwave") id=50 ;;
367 "techno-indust.") id=51 ;;
368 "electronic") id=52 ;;
369 "pop-folk") id=53 ;;
370 "eurodance") id=54 ;;
371 "dream") id=55 ;;
372 "southern rock") id=56 ;;
373 "comedy") id=57 ;;
374 "cult") id=58 ;;
375 "gangsta") id=59 ;;
376 "top 40") id=60 ;;
377 "christian rap") id=61 ;;
378 "pop/funk") id=62 ;;
379 "jungle") id=63 ;;
380 "native american") id=64 ;;
381 "cabaret") id=65 ;;
382 "new wave") id=66 ;;
383 "psychadelic") id=67 ;;
384 "rave") id=68 ;;
385 "showtunes") id=69 ;;
386 "trailer") id=70 ;;
387 "lo-fi") id=71 ;;
388 "tribal") id=72 ;;
389 "acid punk") id=73 ;;
390 "acid jazz") id=74 ;;
391 "polka") id=75 ;;
392 "retro") id=76 ;;
393 "musical") id=77 ;;
394 "rock & roll") id=78 ;;
395 "hard rock") id=79 ;;
396 "folk") id=80 ;;
397 "folk/rock") id=81 ;;
398 "national folk") id=82 ;;
399 "swing") id=83 ;;
400 "fusion") id=84 ;;
401 "bebob") id=85 ;;
402 "latin") id=86 ;;
403 "revival") id=87 ;;
404 "celtic") id=88 ;;
405 "bluegrass") id=89 ;;
406 "avantgarde") id=90 ;;
407 "gothic rock") id=91 ;;
408 "progress. rock") id=92 ;;
409 "psychadel. rock") id=93 ;;
410 "symphonic rock") id=94 ;;
411 "slow rock") id=95 ;;
412 "big band") id=96 ;;
413 "chorus") id=97 ;;
414 "easy listening") id=98 ;;
415 "acoustic") id=99 ;;
416 "humour") id=100 ;;
417 "speech") id=101 ;;
418 "chanson") id=102 ;;
419 "opera") id=103 ;;
420 "chamber music") id=104 ;;
421 "sonata") id=105 ;;
422 "symphony") id=106 ;;
423 "booty bass") id=107 ;;
424 "primus") id=108 ;;
425 "porn groove") id=109 ;;
426 "satire") id=110 ;;
427 "slow jam") id=111 ;;
428 "club") id=112 ;;
429 "tango") id=113 ;;
430 "samba") id=114 ;;
431 "folklore") id=115 ;;
432 "ballad") id=116 ;;
433 "power ballad") id=117 ;;
434 "rhythmic soul") id=118 ;;
435 "freestyle") id=119 ;;
436 "duet") id=120 ;;
437 "punk rock") id=121 ;;
438 "drum solo") id=122 ;;
439 "a capella") id=123 ;;
440 "euro-house") id=124 ;;
441 "dance hall") id=125 ;;
442 "goa") id=126 ;;
443 "drum & bass") id=127 ;;
444 "club-house") id=128 ;;
445 "hardcore") id=129 ;;
446 "terror") id=130 ;;
447 "indie") id=131 ;;
448 "britpop") id=132 ;;
449 "negerpunk") id=133 ;;
450 "polsk punk") id=134 ;;
451 "beat") id=135 ;;
452 "christian gangsta rap") id=136 ;;
453 "heavy metal") id=137 ;;
454 "black metal") id=138 ;;
455 "crossover") id=139 ;;
456 "contemporary christian")id=140 ;;
457 "christian rock") id=141 ;;
458 "merengue") id=142 ;;
459 "salsa") id=143 ;;
460 "thrash metal") id=144 ;;
461 "anime") id=145 ;;
462 "jpop") id=146 ;;
463 "synthpop") id=147 ;;
464 *) return 1 ;;
465 esac
466echo ${id}
467return 0
468}
469
c9c2ca27 470# do_tag [tracknumber]
471# id3 tags a filename
472# variables used:
473# TRACKS, TRACKNAME, TRACKARTIST, TAGGER, TAGGEROPTS, VORBISCOMMENT, METAFLAC,
474# COMMENT, DALBUM, DARTIST, CDYEAR, CDGENRE (and temporarily) ID3TAGV
475do_tag ()
476{
477 COMMENTOUTPUT="$(eval echo ${COMMENT})"
478 run_command '' echo "Tagging track $1 of $TRACKS: $TRACKNAME..."
79e3928b 479 # If we want to start the tracks with a given number, we need to modify the
480 # TRACKNUM value before evaluation
481 if [ -n "$STARTTRACKNUMBER" -a -n "$STARTTRACKNUMBERTAG" ] ; then
482 # Get the trackpadding from the current track
483 CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
bef9c2d8 484 TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - $FIRSTTRACK ))
79e3928b 485 fi
c9c2ca27 486 for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
487 do
488 case "$OUTPUT" in
489 mp3)
490 # id3v2 v0.1.9 claims to have solved the -c bug, so we merge both id3 and id3v2
2994a485 491 GENREID=$(do_getgenreid "${CDGENRE}")
492
1c4ae87c 493 # FIXME # track numbers in mp3 come with 1/10, so we cannot happily substitute them with $TRACKNUM
c9c2ca27 494 run_command tagtrack-$1 $TAGGER $TAGGEROPTS -c "$COMMENTOUTPUT" \
495 -A "$DALBUM" -a "$TRACKARTIST" -t "$TRACKNAME" -y "$CDYEAR" \
2994a485 496 -g "$GENREID" -T "${TRACKNUM:-$1/$TRACKS}" \
497 "$ABCDETEMPDIR/track$1.$OUTPUT"
c9c2ca27 498 ;;
b82e2042 499 vorbis|ogg)
c9c2ca27 500 case "$OGGENCODERSYNTAX" in
501 vorbize|oggenc)
502 # vorbiscomment can't do in-place modification, mv the file first
b82e2042 503 if [ -f "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER" -a ! -f "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER" ]; then
504 mv "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER" "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER"
c9c2ca27 505 fi
506 (
507 # These are from http://www.xiph.org/ogg/vorbis/doc/v-comment.html
eb1474fe 508 echo ARTIST="$TRACKARTIST"
c9c2ca27 509 echo ALBUM="$DALBUM"
eb1474fe 510 echo TITLE="$TRACKNAME"
f97e22a3 511 if [ -n "$CDYEAR" ]; then
c9c2ca27 512 echo DATE="$CDYEAR"
f97e22a3 513 fi
514 if [ -n "$CDGENRE" ]; then
c9c2ca27 515 echo GENRE="$CDGENRE"
f97e22a3 516 fi
6c124acf 517 echo TRACKNUMBER=${TRACKNUM:-$1}
d0491de2 518 echo CDDB=$CDDBDISCID
c9c2ca27 519 if [ "$(eval echo ${COMMENT})" != "" ]; then
520 case "$COMMENTOUTPUT" in
521 *=*) echo "$COMMENTOUTPUT";;
522 *) echo COMMENT="$COMMENTOUTPUT";;
523 esac
524 fi
b33dc0c1 525 ) | run_command tagtrack-$1 $VORBISCOMMENT $VORBISCOMMENTOPTS -w \
b82e2042 526 "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER" "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER"
c9c2ca27 527 # Doublecheck that the commented file was created successfully before wiping the original
b82e2042 528 if [ -f "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER" ]; then
529 rm -f "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER"
c9c2ca27 530 else
b82e2042 531 mv "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER" "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER"
c9c2ca27 532 fi
533 ;;
534 esac
535 ;;
536 flac)
bef9c2d8 537 (
c9c2ca27 538 echo ARTIST="$TRACKARTIST"
539 echo ALBUM="$DALBUM"
540 echo TITLE="$TRACKNAME"
bef9c2d8 541 if [ -n "$CDYEAR" ]; then
c9c2ca27 542 echo DATE="$CDYEAR"
bef9c2d8 543 fi
544 if [ -n "$CDGENRE" ]; then
c9c2ca27 545 echo GENRE="$CDGENRE"
bef9c2d8 546 fi
79e3928b 547 echo TRACKNUMBER=${TRACKNUM:-$1}
d0491de2 548 echo CDDB=$CDDBDISCID
c9c2ca27 549 if [ "$(eval echo ${COMMENT})" != "" ]; then
550 case "$COMMENTOUTPUT" in
551 *=*) echo "$COMMENTOUTPUT";;
552 *) echo COMMENT="$COMMENTOUTPUT";;
553 esac
554 fi
b82e2042 555 ) | run_command tagtrack-$1 $METAFLAC $METAFLACOPTS --import-tags-from=- "$ABCDETEMPDIR/track$1.$FLACOUTPUTCONTAINER"
c9c2ca27 556 ;;
557 spx)
558 run_command tagtrack-$1 true
559 ;;
99d009fa 560 mpc)
561 run_command tagtrack-$1 true
562 ;;
c9c2ca27 563 esac
564 done
565}
566
567# do_batch_encode
568# variables used:
569# OUTPUTTYPE, {FOO}ENCODERSYNTAX, ENCNICE, ENCODER, ENCODEROPTS
570do_batch_encode ()
571{
572 # The commands here don't go through run_command because they're never supposed to be silenced
573 echo "Batch encoding tracks: $TRACKQUEUE"
574 OUTPUT=$(echo $OUTPUTTYPE | grep "mp3" )
575 case "$OUTPUT" in
576 mp3)
577 case "$MP3ENCODERSYNTAX" in
578 lame)
579 (
580 cd "$ABCDETEMPDIR"
581 TRACKFILES=
582 for UTRACKNUM in $TRACKQUEUE
583 do
584 TRACKFILES="$TRACKFILES track$UTRACKNUM.wav"
585 done
586 nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS --nogap $TRACKFILES
587 RETURN=$?
588 if [ "$RETURN" != "0" ]; then
589 echo "batch-encode: $ENCODER returned code $RETURN" >> errors
590 else
591 for UTRACKNUM in $TRACKQUEUE
592 do
593 echo encodetrack-$UTRACKNUM >> status
594 done
595 fi
596 )
597 ;;
598 esac
599 ;;
600 esac
601 # Other encoders fall through to normal encoding as the tracks
602 # have not been entered in the status file.
603}
604
605# do_encode [tracknumber] [hostname]
606# If no hostname is specified, encode locally
607# variables used:
608# TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
609do_encode ()
610{
0a3a2dcc 611 if [ "$USEPIPES" = "y" ]; then
612 case "$OUTPUT" in
613 mp3)
614 TEMPARG="PIPE_$MP3ENCODERSYNTAX"
615 ;;
b82e2042 616 vorbis|ogg)
0a3a2dcc 617 TEMPARG="PIPE_$OGGENCODERSYNTAX"
618 ;;
619 flac)
620 TEMPARG="PIPE_$FLACENCODERSYNTAX"
621 ;;
622 spx)
623 TEMPARG="PIPE_$SPEEXENCODER"
624 ;;
625 mpc)
626 TEMPARG="PIPE_$MPPENCODER"
627 ;;
628 esac
629 IN=${!TEMPARG}
630 else
631 IN="$ABCDETEMPDIR/track$1.wav"
632 case "$OUTPUT" in
633 mp3)
634 case "$MP3ENCODERSYNTAX" in
635 # FIXME # check if mp3enc needs -if for pipes
636 mp3enc)
637 FILEARG="-if $IN"
638 ;;
639 *)
03308bc7 640 FILEARG="$IN"
0a3a2dcc 641 ;;
642 esac
643 ;;
644 *)
03308bc7 645 FILEARG="$IN"
0a3a2dcc 646 ;;
647 esac
648 fi
649 # We need IN to proceed, if we are not using pipes.
650 if [ -s "$IN" -o X"$USEPIPES" = "Xy" ] ; then
c9c2ca27 651 for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
652 do
b82e2042 653 case "$OUTPUT" in
654 vorbis|ogg)
655 OUT="$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER"
656 ;;
657 flac)
658 OUT="$ABCDETEMPDIR/track$1.$FLACOUTPUTCONTAINER"
659 ;;
660 *)
661 OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
662 ;;
663 esac
0a3a2dcc 664 if [ X"$USEPIPES" = "Xy" ]; then
665 RUN_COMMAND=""
666 else
667 run_command '' echo "Encoding track $1 of $TRACKS: $TRACKNAME..."
668 RUN_COMMAND="run_command encodetrack-$OUTPUT-$1"
669 fi
c9c2ca27 670 case "$OUTPUT" in
671 mp3)
672 case "$2" in
673 %local*%)
674 case "$MP3ENCODERSYNTAX" in
0a3a2dcc 675 lame|gogo) $RUN_COMMAND nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS "$IN" "$OUT" ;;
676 bladeenc) $RUN_COMMAND nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS -quit "$IN" ;;
677 l3enc|xingmp3enc) $RUN_COMMAND nice $ENCNICE $MP3ENCODER "$IN" "$OUT" $MP3ENCODEROPTS ;;
678 # FIXME # Relates to the previous FIXME since it might need the "-if" removed.
679 mp3enc) $RUN_COMMAND nice $ENCNICE $MP3ENCODER -if "$IN" -of "$OUT" $MP3ENCODEROPTS ;;
c9c2ca27 680 esac
681 ;;
682 *)
0a3a2dcc 683 $RUN_COMMAND nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" >/dev/null 2>&1
c9c2ca27 684 ;;
685 esac
686 ;;
b82e2042 687 vorbis|ogg)
c9c2ca27 688 case "$2" in
689 %local*%)
690 case "$OGGENCODERSYNTAX" in
0a3a2dcc 691 vorbize) $RUN_COMMAND nice $ENCNICE $OGGENCODER $OGGENCODEROPTS -w "$OUT" "$IN" ;;
692 oggenc) $RUN_COMMAND nice $ENCNICE $OGGENCODER $OGGENCODEROPTS -o "$OUT" "$IN" ;;
c9c2ca27 693 esac
694 ;;
695 *)
0a3a2dcc 696 $RUN_COMMAND nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" >/dev/null 2>&1
c9c2ca27 697 ;;
698 esac
699 ;;
700 flac)
c9c2ca27 701 case "$2" in
702 %local*%)
703 case "$FLACENCODERSYNTAX" in
6119aaa1 704 flac) $RUN_COMMAND nice $ENCNICE $FLACENCODER $FLACENCODEROPTS -o "$OUT" "$IN" ;;
c9c2ca27 705 esac
706 ;;
707
708 *)
709 echo -n "DISTMP3:"
710 echo "$DISTMP3 $DISTMP3OPTS $2 $IN $OUT >/dev/null 2>&1"
0a3a2dcc 711 $RUN_COMMAND nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" > /dev/null 2>&1
c9c2ca27 712 ;;
713 esac
714 ;;
715 spx)
716 if [ "$(eval echo ${COMMENT})" != "" ]; then
717 case "$COMMENT" in
718 *=*) ;;
719 *) COMMENT="COMMENT=$COMMENT" ;;
720 esac
721 COMMENT="--comment \"$COMMENT\""
722 fi
723 # Quick hack to avoid tagging Ogg/Speex, since there is no other way to tag than inline tagging
724 if [ ! "$DOTAG" = "y" ]; then
0a3a2dcc 725 $RUN_COMMAND nice $ENCNICE $SPEEXENCODER $SPEEXENCODEROPTS --author "$TRACKARTIST" --title "$TRACKNAME" "$COMMENT" "$IN" "$OUT"
c9c2ca27 726 else
0a3a2dcc 727 $RUN_COMMAND nice $ENCNICE $SPEEXENCODER $SPEEXENCODEROPTS "$IN" "$OUT"
c9c2ca27 728 fi
729 ;;
99d009fa 730 mpc)
731 # MPP/MP+(Musepack) format (.mpc) is done locally, with inline
732 # tagging.
733 # I tried compiling the mppenc from corecodecs.org and got some
734 # errors, so I have not tried it myself.
735 ## FIXME ## Needs some cleanup to determine if an empty tag sent
736 ## FIXME ## to the encoder ends up empty.
0a3a2dcc 737 $RUN_COMMAND nice $ENCNICE $MPPENCODER $MPPENCODEROPTS --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --track "$1" --genre "$CDGENRE" --year "$CDYEAR" --comment "$COMMENT" "$IN" "$OUT"
99d009fa 738 ;;
c9c2ca27 739 esac
740 done
741 # Only remove .wav if the encoding succeeded
742 if checkerrors "encodetrack-(.{3,4})-$1"; then
743 run_command encodetrack-$1 false
744 else
745 run_command encodetrack-$1 true
746 if [ ! "$KEEPWAVS" = "y" ] ; then
747 rm -f "$IN"
748 fi
749 fi
750 else
751 if [ "$(checkstatus encode-output)" = "loud" ]; then
752 echo "HEH! The file we were about to encode disappeared:"
753 echo ">> $IN"
754 fi
755 run_command encodetrack-$1 false
756 fi
757}
758
99d009fa 759# do_preprocess [tracknumber]
760# variables used:
761# TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
762#do_preprocess ()
763#{
764# IN="$ABCDETEMPDIR/track$1.wav"
765# # We need IN to proceed.
766# if [ -s "$IN" ] ; then
767# for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
768# do
769# #OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
770# run_command '' echo "Pre-processing track $1 of $TRACKS..."
771# case "$POSTPROCESSFORMAT" in
772# all|wav*)
773# run_command preprocess-$OUTPUT-$1 nice $PRENICE $WAV_PRE $IF $OF ;;
774# mp3)
775# run_command preprocess-$OUTPUT-$1 nice $PRENICE $MP3_PRE $IF $OF ;;
776# ogg)
777# run_command preprocess-$OUTPUT-$1 nice $PRENICE $OGG_PRE $IF $OF ;;
778# flac)
779# run_command preprocess-$OUTPUT-$1 nice $PRENICE $FLAC_PRE $IF $OF ;;
780# spx)
781# run_command preprocess-$OUTPUT-$1 nice $PRENICE $SPX_PRE $IF $OF ;;
782# esac
783# done
784# # Only remove .wav if the encoding succeeded
785# if checkerrors "preprocess-(.{3,4})-$1"; then
786# run_command preprocess-$1 false
787# else
788# run_command preprocess-$1 true
789# fi
790# else
791# if [ "$(checkstatus encode-output)" = "loud" ]; then
792# echo "HEH! The file we were about to pre-process disappeared:"
793# echo ">> $IN"
794# fi
795# run_command preprocess-$1 false
796# fi
797#}
798
799
800# do_postprocess [tracknumber]
801# variables used:
802# TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
803#do_postprocess ()
804#{
805# for POSTPROCESSFORMAT in $(echo $POSTPROCESSFORMATS | tr , \ )
806# do
807# IN="$ABCDETEMPDIR/track$1.$POSTPROCESSFORMAT"
808# # We need IN to proceed.
809# if [ -s "$IN" ] ; then
810# #OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
811# run_command '' echo "Post-processing track $1 of $TRACKS..."
812# case "$POSTPROCESSFORMAT" in
813# mp3)
814# run_command postprocess-$OUTPUT-$1 nice $POSTNICE $MP3_POST $IF $OF ;;
815# ogg)
816# run_command postprocess-$OUTPUT-$1 nice $POSTNICE $OGG_POST $IF $OF ;;
817# flac)
818# run_command postprocess-$OUTPUT-$1 nice $POSTNICE $FLAC_POST $IF $OF ;;
819# spx)
820# run_command postprocess-$OUTPUT-$1 nice $POSTNICE $SPX_POST $IF $OF ;;
821# esac
822# # Only remove .wav if the encoding succeeded
823# if checkerrors "postprocess-(.{3,4})-$1"; then
824# run_command postprocess-$1 false
825# else
826# run_command postprocess-$1 true
827# fi
828# else
829# if [ "$(checkstatus encode-output)" = "loud" ]; then
830# echo "HEH! The file we were about to post-process disappeared:"
831# echo ">> $IN"
832# fi
833# run_command postprocess-$1 false
834# fi
835# done
836#}
837
79c8f4d0 838# do_single_gain
839# variables used:
840# FIXME #
841do_single_gain ()
842{
843:
844}
845
99d009fa 846# do_batch_gain
847# variables used:
848# MP3GAIN, MP3GAINOPTS, VORBISGAIN, VORBISGAINOPTS
79c8f4d0 849# FIXME #
99d009fa 850do_batch_gain ()
851{
852 # The commands here don't go through run_command because they're never supposed to be silenced
853 echo "Batch analizing gain in tracks: $TRACKQUEUE"
854 (
855 cd "$ABCDETEMPDIR"
856 BLURB=
857 TRACKFILES=
858 for UTRACKNUM in $TRACKQUEUE
859 do
860 MP3FILES="$TRACKFILES track$UTRACKNUM.mp3"
861 done
862 # XXX: Hard-coded batch option!
863 $NORMALIZER -b $NORMALIZEROPTS $TRACKFILES
864 RETURN=$?
865 if [ "$RETURN" != "0" ]; then
866 echo "batch-normalize: $NORMALIZER returned code $RETURN" >> errors
867 else
868 for UTRACKNUM in $TRACKQUEUE
869 do
870 echo normalizetrack-$UTRACKNUM >> status
871 done
872 fi
873 )
874}
875
c9c2ca27 876# do_batch_normalize
877# variables used:
878# NORMALIZER, NORMALIZEROPTS
879do_batch_normalize ()
880{
881 # The commands here don't go through run_command because they're never supposed to be silenced
882 echo "Batch normalizing tracks: $TRACKQUEUE"
883 (
884 cd "$ABCDETEMPDIR"
885 BLURB=
886 TRACKFILES=
887 for UTRACKNUM in $TRACKQUEUE
888 do
889 TRACKFILES="$TRACKFILES track$UTRACKNUM.wav"
890 done
891 # XXX: Hard-coded batch option!
892 $NORMALIZER -b $NORMALIZEROPTS $TRACKFILES
893 RETURN=$?
894 if [ "$RETURN" != "0" ]; then
895 echo "batch-normalize: $NORMALIZER returned code $RETURN" >> errors
896 else
897 for UTRACKNUM in $TRACKQUEUE
898 do
899 echo normalizetrack-$UTRACKNUM >> status
900 done
901 fi
902 )
903}
904
905# do_normalize [tracknumber]
906# variables used:
907# TRACKS, TRACKNAME, NORMALIZER, NORMALIZEROPTS
908do_normalize ()
909{
910 IN="$ABCDETEMPDIR/track$1.wav"
911 if [ -e "$IN" ] ; then
912 run_command '' echo "Normalizing track $1 of $TRACKS: $TRACKNAME..."
913 run_command normalizetrack-$1 $NORMALIZER $NORMALIZEROPTS "$IN"
914 else
915 if [ "$(checkstatus encode-output)" = "loud" ]; then
916 echo "HEH! The file we were about to normalize disappeared:"
917 echo ">> $IN"
918 fi
919 run_command normalizetrack-$1 false "File $IN was not found"
920 fi
921}
922
923# do_move [tracknumber]
924# Deduces the outfile from environment variables
925# Creates directory if necessary
926# variables used:
99d009fa 927# TRACKNUM, TRACKNAME, TRACKARTIST, DALBUM, OUTPUTFORMAT, CDGENRE, CDYEAR
c9c2ca27 928do_move ()
929{
930 for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
931 do
b82e2042 932 case $OUTPUT in
933 vorbis|ogg)
934 OUTPUT=$OGGOUTPUTCONTAINER
935 ;;
936 flac)
937 OUTPUT=$FLACOUTPUTCONTAINER
938 ;;
939 esac
c9c2ca27 940 # Create ALBUMFILE, ARTISTFILE, TRACKFILE
941 # Munge filenames as follows:
942 # ' ' -> '_'
943 # '/' -> '_'
944 # ''' -> ''
945 # '?' -> ''
946 # Eat control characters
947 ALBUMFILE=$(mungefilename "$DALBUM")
948 ARTISTFILE=$(mungefilename "$TRACKARTIST")
949 TRACKFILE=$(mungefilename "$TRACKNAME")
7acef70b 950 GENRE=$(mungegenre "$GENRE")
99d009fa 951 YEAR=$(echo $CDYEAR)
952 # If we want to start the tracks with a given number, we need to modify the
953 # TRACKNUM value before evaluation
954 if [ -n "$STARTTRACKNUMBER" ] ; then
955 # Get the trackpadding from the current track
956 CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
bef9c2d8 957 TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - $FIRSTTRACK ))
99d009fa 958 else
959 TRACKNUM=${UTRACKNUM}
960 fi
961 # Supported variables for OUTPUTFORMAT are GENRE, ALBUMFILE, ARTISTFILE,
962 # TRACKFILE, and TRACKNUM.
c9c2ca27 963 if [ "$VARIOUSARTISTS" = "y" ]; then
964 OUTPUTFILE=$(eval echo $VAOUTPUTFORMAT)
965 else
966 OUTPUTFILE=$(eval echo $OUTPUTFORMAT)
967 fi
c9c2ca27 968 # Check that the directory for OUTPUTFILE exists, if it doesn't, create it
969 OUTPUTFILEDIR=$(dirname "$OUTPUTDIR/$OUTPUTFILE")
970 # mkdir -p shouldn't return an error if the directory already exists
971 mkdir -p "$OUTPUTFILEDIR"
937d027e 972 # Silence the Moving output since it overlaps with encoding processes...
973 #run_command '' vecho "Moving track$1.$OUTPUT to $OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
c9c2ca27 974 run_command movetrack-$1 mv "$ABCDETEMPDIR/track$1.$OUTPUT" "$OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
b82e2042 975 # Lets move the cue file
976 if CUEFILE=$(checkstatus cuefile) >/dev/null ; then
977 if [ -r "$ABCDETEMPDIR/$CUEFILE" ]; then
978 if checkstatus movecue-$OUTPUT; then :; else
937d027e 979 # Silence the Copying output since it overlaps with encoding processes...
980 #run_command '' vecho "Copying cue file to its destination directory..."
3416c114 981 if checkstatus onetrack >/dev/null ; then
982 case $OUTPUT in
6119aaa1 983 # NOTE: Creating a cue file with the 3-char-extension files is to comply with
984 # http://brianvictor.tripod.com/mp3cue.htm#details
985 [a-z0-9][a-z0-9][a-z0-9])
3416c114 986 run_command movecue-$OUTPUT cp "$ABCDETEMPDIR/$CUEFILE" "$OUTPUTDIR/$OUTPUTFILE.cue"
987 ;;
988 *)
989 run_command movecue-$OUTPUT cp "$ABCDETEMPDIR/$CUEFILE" "$OUTPUTDIR/$OUTPUTFILE.$OUTPUT.cue"
990 ;;
991 esac
992 else
993 run_command movecue-$OUTPUT cp "$ABCDETEMPDIR/$CUEFILE" "$OUTPUTFILEDIR/$CUEFILE"
994 fi
6119aaa1 995 echo movecue-$OUTPUT >> "$ABCDETEMPDIR/status"
b82e2042 996 fi
997 fi
998 fi
c9c2ca27 999 done
1000}
1001
1002# do_playlist
1003# Create the playlist if wanted
1004# Variables used:
1005# PLAYLISTFORMAT, PLAYLISTDATAPREFIX, VAPLAYLISTFORMAT, VAPLAYLISTDATAPREFIX,
1006# VARIOUSARTISTS, OUTPUTDIR
1007do_playlist ()
1008{
1009 for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
1010 do
1011 # Create a playlist file for the playlist data to go into.
1012 # We used to wipe it out if it existed. Now we request permision if interactive.
1013 for LASTTRACK in $TRACKQUEUE; do :; done
1014 ALBUMFILE=$(mungefilename "$DALBUM")
1015 ARTISTFILE=$(mungefilename "$DARTIST")
7acef70b 1016 GENRE=$(mungegenre "$GENRE")
76448acf 1017 YEAR=${CDYEAR:-$CDYEAR}
c9c2ca27 1018 if [ "$VARIOUSARTISTS" = "y" ] ; then
1019 PLAYLISTFILE=$(eval echo $VAPLAYLISTFORMAT)
1020 else
1021 PLAYLISTFILE=$(eval echo $PLAYLISTFORMAT)
1022 fi
1023 FINALPLAYLISTDIR=$(dirname "$OUTPUTDIR/$PLAYLISTFILE")
1024 mkdir -p "$FINALPLAYLISTDIR"
1025 if [ -s "$OUTPUTDIR/$PLAYLISTFILE" ]; then
99d009fa 1026 echo -n "Erase, Append to, or Keep the existing playlist file? [e/a/k] (e): " >&2
c9c2ca27 1027 if [ "$INTERACTIVE" = "y" ]; then
99d009fa 1028 while [ "$DONE" != "y" ]; do
1029 read ERASEPLAYLIST
1030 case $ERASEPLAYLIST in
1031 e|E|a|A|k|K) DONE=y ;;
1032 *) ;;
1033 esac
1034 done
c9c2ca27 1035 else
99d009fa 1036 echo e >&2
1037 ERASEPLAYLIST=e
c9c2ca27 1038 fi
40c61684 1039 # Once we erase the playlist, we use append to create the new one.
1040 [ "$ERASEPLAYLIST" = "e" -o "$ERASEPLAYLIST" = "E" ] && rm -f "$OUTPUTDIR/$PLAYLISTFILE" && ERASEPLAYLIST=a
1041 else
1042 # The playlist does not exist, so we can safelly use append to create the new list
1043 ERASEPLAYLIST=a
c9c2ca27 1044 fi
99d009fa 1045 if [ "$ERASEPLAYLIST" = "a" -o "$ERASEPLAYLIST" = "A" ]; then
1046 touch "$OUTPUTDIR/$PLAYLISTFILE"
1047 for UTRACKNUM in $TRACKQUEUE
1048 do
1049 # Shares some code with do_move since the filenames have to match
1050 CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
cc2fac68 1051 TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2 -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')
99d009fa 1052 splitvarious
1053 TRACKFILE=$(mungefilename "$TRACKNAME")
1054 ARTISTFILE=$(mungefilename "$TRACKARTIST")
1055 # If we want to start the tracks with a given number, we need to modify the
1056 # TRACKNUM value before evaluation
d5200e72 1057 if [ -n "$STARTTRACKNUMBER" ] ; then
99d009fa 1058 # Get the trackpadding from the current track
1059 CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
bef9c2d8 1060 TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - $FIRSTTRACK ))
c9c2ca27 1061 else
99d009fa 1062 TRACKNUM=${UTRACKNUM}
c9c2ca27 1063 fi
99d009fa 1064 if [ "$VARIOUSARTISTS" = "y" ]; then
1065 OUTPUTFILE=$(eval echo $VAOUTPUTFORMAT)
c9c2ca27 1066 else
99d009fa 1067 OUTPUTFILE=$(eval echo $OUTPUTFORMAT)
c9c2ca27 1068 fi
99d009fa 1069 if [ "$VARIOUSARTISTS" = "y" ]; then
1070 if [ "$VAPLAYLISTDATAPREFIX" ] ; then
1071 echo ${VAPLAYLISTDATAPREFIX}$OUTPUTFILE.$OUTPUT >> "$OUTPUTDIR/$PLAYLISTFILE"
1072 else
1073 relpath "$PLAYLISTFILE", "$OUTPUTFILE.$OUTPUT" >> "$OUTPUTDIR/$PLAYLISTFILE"
1074 fi
1075 else
1076 if [ "$PLAYLISTDATAPREFIX" ]; then
1077 echo ${PLAYLISTDATAPREFIX}$OUTPUTFILE.$OUTPUT >> "$OUTPUTDIR/$PLAYLISTFILE"
1078 else
1079 relpath "$PLAYLISTFILE", "$OUTPUTFILE.$OUTPUT" >> "$OUTPUTDIR/$PLAYLISTFILE"
1080 fi
1081 fi
1082 done
1083 fi
c9c2ca27 1084 ## this will convert the playlist to have CRLF line-endings, if specified
1085 ## (some hardware players insist on CRLF endings)
1086 if [ "$DOSPLAYLIST" = "y" ]; then
1087 awk '{substr("\r",""); printf "%s\r\n", $0}' "$OUTPUTDIR/$PLAYLISTFILE" > "$ABCDETEMPDIR/PLAYLISTFILE.tmp"
99d009fa 1088# mv -f "$ABCDETEMPDIR/PLAYLISTFILE.tmp" "$OUTPUTDIR/$PLAYLISTFILE"
1089 cat "$ABCDETEMPDIR/PLAYLISTFILE.tmp" | sed 's/\//\\/' > "$OUTPUTDIR/$PLAYLISTFILE"
c9c2ca27 1090 fi
1091 echo "playlistcomplete" >> "$ABCDETEMPDIR/status"
1092 done
1093}
1094
1095# do_discid
1096# This essentially the start of things
1097do_discid ()
1098{
1099 # Query the CD to get the track info, unless the user specified -C
12c5d978 1100 # or we are using some actions which do not need the CDDB data at all
1101 #if [ ! X"$EXPACTIONS" = "X" ]; then
1102 # :
1103 #elif [ -z "$DISCID" ]; then
c9c2ca27 1104 if [ -z "$DISCID" ]; then
9f659ada 1105 vecho -n "Getting CD track info... "
b82e2042 1106 # In OSX, unmount the disc before a query
f97e22a3 1107 if [ "$OSFLAVOUR" = "OSX" ]; then
1108 disktool -u ${CDROM#/dev/}
1109 fi
c9c2ca27 1110 TRACKINFO=$($CDDISCID $CDROM)
1111 # Make sure there's a CD in there by checking cd-discid's return code
1112 if [ "$?" = "1" ]; then
1113 echo "abcde error: CD could not be read. Perhaps there's no CD in the drive?" >&2
1114 exit 1
1115 fi
b82e2042 1116 # In OSX, remount the disc again
1117 if [ "$OSFLAVOUR" = "OSX" ]; then
1118 disktool -m ${CDROM#/dev/}
1119 fi
99d009fa 1120 WEHAVEACD=y
c9c2ca27 1121 else
1122 TRACKINFO=$(cat "$WAVOUTPUTDIR/abcde.$DISCID/discid")
1123 fi
1124
1125 # Get a full enumeration of tracks, sort it, and put it in the TRACKQUEUE.
1126 # This needs to be done now because a section of the resuming code will need
1127 # it later.
1128
1129 # get the number of digits to pad TRACKNUM with - we'll use this later
12c5d978 1130 # a CD can only hold 99 tracks, but since we support a feature for starting
1131 # numbering the tracks from a given number, we might need to set it as a
1132 # variable for the user to define... or obtain it somehow.
c9c2ca27 1133 if [ "$PADTRACKS" = "y" ] ; then
1134 TRACKNUMPADDING=2
1135 fi
1136
99d009fa 1137 ABCDETEMPDIR="$WAVOUTPUTDIR/abcde.$(echo $TRACKINFO | cut -f1 -d' ')"
c9c2ca27 1138 if [ -z "$TRACKQUEUE" ]; then
99d009fa 1139 if [ ! "$STRIPDATATRACKS" = "y" ]; then
1140 case "$CDROMREADERSYNTAX" in
1141 cdparanoia|debug)
1142 if [ "$WEHAVEACD" = "y" ]; then
1143 vecho "Querying the CD for audio tracks..."
c008b56d 1144 CDPARANOIAOUTPUT="$( $CDROMREADER -$CDPARANOIACDROMBUS $CDROM -Q --verbose 2>&1 )"
09a85005 1145 RET=$?
1146 if [ ! "$RET" = "0" ];then
1147 echo "Warning: Something went wrong while querying the CD... Maybe a DATA CD?"
1148 fi
bef9c2d8 1149 TRACKS="$( echo "$CDPARANOIAOUTPUT" | egrep '^[[:space:]]+[[:digit:]]' | tail -n 1 | get_first | tr -d "." | tr '\n' ' ' )"
99d009fa 1150 CDPARANOIAAUDIOTRACKS="$TRACKS"
1151 else
1152 if [ -f "$ABCDETEMPDIR/status" ] && checkstatus cdparanoia-audio-tracks ; then
b82e2042 1153 TRACKS=$( cat "$ABCDETEMPDIR/cdparanoia-audio-tracks" )
99d009fa 1154 else
1155 TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
1156 fi
1157 fi
1158 ;;
1159 *) TRACKS=$(echo $TRACKINFO | cut -f2 -d' ') ;;
1160 esac
1161 else
1162 TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
1163 fi
bef9c2d8 1164 if echo "$TRACKS" | grep "[[:digit:]]" > /dev/null 2>&1 ;then :;else
09a85005 1165 echo "The disc does not contain any tracks. Giving up..."
1166 exit 0
1167 fi
c9c2ca27 1168 echo -n "Grabbing entire CD - tracks: "
1169 if [ ! "$PADTRACKS" = "y" ] ; then
1170 TRACKNUMPADDING=$(echo -n $TRACKS | wc -c | tr -d ' ')
1171 fi
1172 TRACKS=$(printf "%0.${TRACKNUMPADDING}d" $TRACKS)
1173 X=0
1174 while [ "$X" -ne "$TRACKS" ]
1175 do
1176 X=$(printf "%0.${TRACKNUMPADDING}d" $(expr $X + 1))
1177 TRACKQUEUE=$(echo "$TRACKQUEUE" $X)
1178 done
1179 echo $TRACKQUEUE
1180 else
99d009fa 1181 TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
c9c2ca27 1182 # User-supplied track queue.
1183 # Weed out non-numbers, whitespace, then sort and weed out duplicates
1184 TRACKQUEUE=$(echo $TRACKQUEUE | sed 's-[^0-9 ]--g' | tr ' ' '\n' | grep -v ^$ | sort -n | uniq | tr '\n' ' ' | sed 's- $--g')
1185 # Once cleaned, obtain the highest value in the trackqueue for number padding
1186 for LASTTRACK in $TRACKQUEUE; do :; done
1187 if [ ! "$PADTRACKS" = "y" ] ; then
1188 TRACKNUMPADDING=$(echo -n $LASTTRACK | wc -c | tr -d ' ')
1189 fi
1190 # Now we normalize the trackqueue
1191 for TRACK in $TRACKQUEUE ; do
1192 TRACKNUM=$(printf %0.${TRACKNUMPADDING}d $(expr ${TRACK} + 0 ))
1193 PADTRACKQUEUE=$(echo $PADTRACKQUEUE $TRACKNUM)
1194 done
1195 TRACKQUEUE=$PADTRACKQUEUE
1196 echo Grabbing tracks: "$TRACKQUEUE"
1197 fi
1198
c9c2ca27 1199 QUEUEDTRACKS=$(echo $TRACKQUEUE | wc -w | tr -d ' ')
1200
1201 # We have the discid, create a temp directory after it to store all the temp
1202 # info
1203
c9c2ca27 1204 if [ -e "$ABCDETEMPDIR" ]; then
1205 echo -n "abcde: attempting to resume from $ABCDETEMPDIR"
1206 # It already exists, see if it's a directory
1207 if [ ! -d "$ABCDETEMPDIR" ]; then
1208 # This is a file/socket/fifo/device/etc, not a directory
1209 # Complain and exit
1210 echo >&2
1211 echo "abcde: file $ABCDETEMPDIR already exists and does not belong to abcde." >&2
1212 echo "Please investigate, remove it, and rerun abcde." >&2
1213 exit 1
1214 fi
1215 echo -n .
1216 # It's a directory, let's see if it's owned by us
1217 if [ ! -O "$ABCDETEMPDIR" ]; then
1218 # Nope, complain and exit
1219 echo >&2
1220 echo "abcde: directory $ABCDETEMPDIR already exists and is not owned by you." >&2
1221 echo "Please investigate, remove it, and rerun abcde." >&2
1222 exit 1
1223 fi
1224 echo .
1225 # See if it's populated
1226 if [ ! -f "$ABCDETEMPDIR/discid" ]; then
1227 # Wipe and start fresh
1228 echo "abcde: $ABCDETEMPDIR/discid not found. Abcde must remove and recreate" >&2
1229 echo -n "this directory to continue. Continue? [y/n] (n)" >&2
1230 if [ "$INTERACTIVE" = "y" ]; then
1231 read ANSWER
1232 else
1233 echo y >&2
1234 ANSWER=y
1235 fi
1236 if [ "$ANSWER" != "y" ]; then
1237 exit 1
1238 fi
1239 rm -rf "$ABCDETEMPDIR" || exit 1
1240 mkdir "$ABCDETEMPDIR"
1241 if [ "$?" -gt "0" ]; then
1242 # Directory already exists or could not be created
1243 echo "abcde: Temp directory $ABCDETEMPDIR could not be created." >&2
1244 exit 1
1245 fi
1246 else
1247 # Everything is fine. Check for ^encodetracklocation-
1248 # and encode-output entries in the status file and
1249 # remove them. These are not relevant across sessions.
1250 if [ -f "$ABCDETEMPDIR/status" ]; then
1251 mv "$ABCDETEMPDIR/status" "$ABCDETEMPDIR/status.old"
1252 grep -v ^encodetracklocation- < "$ABCDETEMPDIR/status.old" \
1253 | grep -v ^encode-output > "$ABCDETEMPDIR/status"
1254 fi
1255 # Remove old error messages
1256 if [ -f "$ABCDETEMPDIR/errors" ]; then
1257 rm -f "$ABCDETEMPDIR/errors"
1258 fi
1259 fi
1260 else
1261 # We are starting from scratch
1262 mkdir "$ABCDETEMPDIR"
1263 if [ "$?" -gt "0" ]; then
1264 # Directory already exists or could not be created
1265 echo "abcde: Temp directory $ABCDETEMPDIR could not be created." >&2
1266 exit 1
1267 fi
1268 cat /dev/null > "$ABCDETEMPDIR/status"
1269 fi
b82e2042 1270 if [ X"$MAKECUEFILE" = "Xy" -a X"$WEHAVEACD" = "Xy" ]; then
1271 if checkstatus cuefile > /dev/null 2>&1 ; then :; else
1272 CUEFILE=cue-$(echo "$TRACKINFO" | cut -f1 -d' ').txt
1273 vecho "Creating cue file..."
1274 $MKCUE $MKCUEOPTS > "$ABCDETEMPDIR/$CUEFILE"
f97e22a3 1275 echo cuefile=$CUEFILE >> "$ABCDETEMPDIR/status"
24f444d7 1276 fi
1277 fi
99d009fa 1278 # If we got the CDPARANOIA status and it is not recorded, save it now
c7ea851d 1279 if [ -n "$CDPARANOIAAUDIOTRACKS" ]; then
1280 if checkstatus cdparanoia-audio-tracks; then :; else
1281 if echo "$CDPARANOIAAUDIOTRACKS" >> "$ABCDETEMPDIR/cdparanoia-audio-tracks"; then
1282 echo "cdparanoia-audio-tracks" >> "$ABCDETEMPDIR/status"
1283 fi
99d009fa 1284 fi
1285 fi
1286
c9c2ca27 1287 # Create the discid file
1288 echo "$TRACKINFO" > "$ABCDETEMPDIR/discid"
c9c2ca27 1289}
1290
79c8f4d0 1291# do_cleancue
1292# Create a proper CUE file based on the CUE file we created before.
1293do_cleancue()
f97e22a3 1294{
b82e2042 1295 # FIXME # we can get the name of the cuefile from the status file
1296 if CUEFILE_IN="$ABCDETEMPDIR"/$(checkstatus cuefile); then
1297 CUEFILE_OUT=$CUEFILE_IN.out
79c8f4d0 1298 ### FIXME ### checkstatus cddb
b82e2042 1299 if [ -e "$CDDBDATA" ]; then
1300 vecho "Adding metadata to the cue file..."
1301 # FIXME It doesn't preserve spaces! Why?
1302 # FIXME parse $track into PERFORMER and TITLE - abcde already has code for this?
1303 n=1
1304 echo "PERFORMER \"$DARTIST\"" >> "$CUEFILE_OUT"
1305 echo "TITLE \"$DALBUM\"" >> "$CUEFILE_OUT"
1306 cat "$CUEFILE_IN" | while read line
1307 do
1308 if echo "$line" | grep -q "INDEX"
1309 then
1310 eval track="\$TRACK$n"
1311 n=$(expr $n + 1)
1312 echo "TITLE \"$track\"" >> "$CUEFILE_OUT"
1313 fi
1314 echo "$line" >> "$CUEFILE_OUT"
1315 done
1316 mv "$CUEFILE_OUT" "$CUEFILE_IN"
1317 echo "cleancuefile" >> "$ABCDETEMPDIR/status"
79c8f4d0 1318 fi
1319 fi
f97e22a3 1320}
1321
c9c2ca27 1322# do_cddbparse
1323# Parses a CDDB file and outputs the title and the track names.
1324# Variables: CDDBFILE
1325do_cddbparse ()
1326{
1327 CDDBPARSEFILE="$1"
1328 # List out disc title/author and contents
1329 if [ "$ONETRACK" = "y" ]; then
1330 vecho "ONETRACK mode selected: displaying only the title of the CD..."
1331 fi
1332 echo "---- $(grep DTITLE "${CDDBPARSEFILE}" | cut '-d=' -f2- | tr -d \\r\\n ) ----"
9ff5088c 1333 if [ X"$SHOWCDDBYEAR" = "Xy" ]; then
1334 PARSEDYEAR=$(grep DYEAR "${CDDBPARSEFILE}" | cut '-d=' -f2-)
1335 if [ X"$PARSEDYEAR" = "X" ]; then
f75dae43 1336 echo "Year: $PARSEDYEAR"
9ff5088c 1337 fi
1338 fi
1339 if [ X"$SHOWCDDBGENRE" = "Xy" ]; then
1340 PARSEDGENRE=$(grep DGENRE "${CDDBPARSEFILE}" | cut '-d=' -f2-)
1341 if [ X"$PARSEDGENRE" = "X" ]; then
f75dae43 1342 echo "Genre: $PARSEDGENRE"
9ff5088c 1343 fi
1344 fi
99d009fa 1345 if [ ! "$ONETRACK" = "y" ]; then
c9c2ca27 1346 for TRACK in $(f_seq_row 1 $TRACKS)
1347 do
1348 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "${CDDBPARSEFILE}" | cut -f2- -d= | tr -d \\r\\n)"
1349 done
1350 fi
1351}
1352
1353# do_localcddb
1354# Check for a local CDDB file, and report success
1355do_localcddb ()
1356{
1357 if checkstatus cddb-readcomplete && checkstatus cddb-choice >/dev/null; then :; else
1358
1359 CDDBLOCALSUCCESS="n"
1360 CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
1361 CDDBLOCALFILE="${CDDBLOCALDIR}/${CDDBDISCID}"
1362 USELOCALRESP="y"
1363
1364 # If the user has selected to check a local CDDB repo, we proceed with it
04609998 1365 if [ -r "${CDDBLOCALFILE}" ]; then
1366 # List out disc title/author and contents
1367 do_cddbparse "${CDDBLOCALFILE}"
1368 echo -n "Locally cached CDDB entry found, use it? [y/n] (y): "
1369 if [ "$INTERACTIVE" = "y" ]; then
1370 read USELOCALRESP
1371 while [ "$USELOCALRESP" != "y" ] && [ "$USELOCALRESP" != "n" ] && [ "$USELOCALRESP" != "" ] ; do
1372 echo -n 'Invalid selection. Please answer "y" or "n": '
c9c2ca27 1373 read USELOCALRESP
04609998 1374 done
1375 [ x"$USELOCALRESP" = "x" ] && USELOCALRESP="y"
1376 else
1377 echo "y" >&2
1378 fi
1379 if [ "$USELOCALRESP" = "y" ]; then
786d4b2e 1380 #echo "Using local copy of CDDB data"
04609998 1381 cp "${CDDBLOCALFILE}" "$ABCDETEMPDIR/cddbread.1"
1382 echo 999 > "$ABCDETEMPDIR/cddbquery" # Assuming 999 isn't used by CDDB
1383 echo cddb-readcomplete >> "$ABCDETEMPDIR/status"
1384 do_cddbparse "${CDDBLOCALFILE}" > "$ABCDETEMPDIR/cddbchoices"
1385 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
1386 CDDBLOCALSUCCESS="y"
c9c2ca27 1387 else
04609998 1388 #echo "Not using local copy of CDDB data"
c9c2ca27 1389 CDDBLOCALSUCCESS="n"
1390 fi
04609998 1391 else
1392 CDDBLOCALSUCCESS="n"
c9c2ca27 1393 fi
1394 fi
1395}
1396
1397# do_cddbstat
1398do_cddbstat ()
1399{
1400 # Perform CDDB protocol version check if it hasn't already been done
1401 if checkstatus cddb-statcomplete; then :; else
1402 if [ "$CDDBAVAIL" = "n" ]; then
1403 ERRORCODE=no_query
1404 echo 503 > "$ABCDETEMPDIR/cddbstat"
1405 else
1406 rc=1
1407 CDDBUSER=$(echo $HELLOINFO | cut -f1 -d'@')
1408 CDDBHOST=$(echo $HELLOINFO | cut -f2- -d'@')
1409 while test $rc -eq 1 -a $CDDBPROTO -ge 3; do
1410 vecho "Checking CDDB server status..."
1411 $CDDBTOOL stat $CDDBURL $CDDBUSER $CDDBHOST $CDDBPROTO > "$ABCDETEMPDIR/cddbstat"
1412 RESPONSECODE=$(head -n 1 "$ABCDETEMPDIR/cddbstat" | cut -f1 -d' ')
1413 case "$RESPONSECODE" in
1414 210) # 210 OK, status information follows (until terminating `.')
1415 rc=0;
1416 ;;
1417 501|*) # 501 Illegal CDDB protocol level: <n>.
1418 CDDBPROTO=`expr $CDDBPROTO - 1`
1419 ;;
1420 esac
1421 done
1422 if test $rc -eq 1; then
1423 CDDBAVAIL="n"
1424 fi
1425 fi
1426 echo cddb-statcomplete >> "$ABCDETEMPDIR/status"
1427 fi
1428}
1429
1430
1431# do_cddbquery
1432do_cddbquery ()
1433{
1434 CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
1435 CDDBLOCALFILE="${CDDBLOCALDIR}/${CDDBDISCID}"
1436
1437 # Perform CDDB query if it hasn't already been done
1438 if checkstatus cddb-querycomplete; then :; else
1439 if [ "$CDDBAVAIL" = "n" ]; then
1440 ERRORCODE=no_query
1441 echo 503 > "$ABCDETEMPDIR/cddbquery"
1442 # The default CDDBLOCALSUCCESS is "n"
1443 # This part will be triggered if the user CDDB repo does not
1444 # contain the entry, or if we are not trying to use the repo.
1445 else
1446 vecho "Querying the CDDB server..."
1447 CDDBUSER=$(echo $HELLOINFO | cut -f1 -d'@')
1448 CDDBHOST=$(echo $HELLOINFO | cut -f2- -d'@')
1449 $CDDBTOOL query $CDDBURL $CDDBPROTO $CDDBUSER $CDDBHOST $TRACKINFO > "$ABCDETEMPDIR/cddbquery"
1450 ERRORCODE=$?
1451 case $ERRORCODE in
1452 0) # success
1453 ;;
1454 12|13|14)
1455 # no match found in database,
1456 # wget/fetch error, or user requested not to use CDDB
1457 # Make up an error code (503) that abcde
1458 # will recognize in do_cddbread
1459 # and compensate by making a template
1460 echo 503 > "$ABCDETEMPDIR/cddbquery"
1461 ;;
1462 *) # strange and unknown error
1463 echo ERRORCODE=$ERRORCODE
1464 echo "abcde: $CDDBTOOL returned unknown error code"
1465 ;;
1466 esac
1467 fi
1468 echo cddb-querycomplete >> "$ABCDETEMPDIR/status"
1469 fi
1470}
1471
1472# do_cddbread
1473do_cddbread ()
1474{
1475 # If it's not to be used, generate a template.
1476 # Then, display it (or them) and let the user choose/edit it
1477 if checkstatus cddb-readcomplete; then :; else
1478 vecho "Obtaining CDDB results..."
1479 # If CDDB is to be used, interpret the query results and read all
1480 # the available entries.
1481 rm -f "$ABCDETEMPDIR/cddbchoices"
1482 CDDBCHOICES=1 # Overridden by multiple matches
1483 RESPONSECODE=$(head -n 1 "$ABCDETEMPDIR/cddbquery" | cut -f1 -d' ')
1484 case "$RESPONSECODE" in
1485 200)
1486 # One exact match, retrieve it
1487 # 200 [section] [discid] [artist] / [title]
1488 if checkstatus cddb-read-1-complete; then :; else
1489 echo -n "Retrieving 1 CDDB match..." >> "$ABCDETEMPDIR/cddbchoices"
1490 $CDDBTOOL read $CDDBURL $CDDBPROTO $CDDBUSER $CDDBHOST $(cut -f2,3 -d' ' "$ABCDETEMPDIR/cddbquery") > "$ABCDETEMPDIR/cddbread.1"
1491 echo "done." >> "$ABCDETEMPDIR/cddbchoices"
1492 echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
1493 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
1494 fi
1495 # List out disc title/author and contents
1496 echo ---- "$(cut '-d ' -f4- "$ABCDETEMPDIR/cddbquery")" ---- >> "$ABCDETEMPDIR/cddbchoices"
1497 for TRACK in $(f_seq_row 1 $TRACKS)
1498 do
1499 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.1" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
1500 done
1501 echo >> "$ABCDETEMPDIR/cddbchoices"
1502 ;;
1503 202|403|409|503)
1504 # No match
1505 case "$RESPONSECODE" in
1506 202) echo "No CDDB match." >> "$ABCDETEMPDIR/cddbchoices" ;;
1507 403|409) echo "CDDB entry is corrupt, or the handshake failed." >> "$ABCDETEMPDIR/cddbchoices" ;;
1508 503) echo "CDDB unavailable." >> "$ABCDETEMPDIR/cddbchoices" ;;
1509 esac
99d009fa 1510 $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.0"
c9c2ca27 1511 # List out disc title/author and contents of template
1512 echo ---- Unknown Artist / Unknown Album ---- >> "$ABCDETEMPDIR/cddbchoices"
1513 UNKNOWNDISK=y
1514 for TRACK in $(f_seq_row 1 $TRACKS)
1515 do
99d009fa 1516 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.0" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
c9c2ca27 1517 done
1518 echo >> "$ABCDETEMPDIR/cddbchoices"
99d009fa 1519 echo cddb-read-0-complete >> "$ABCDETEMPDIR/status"
1520 echo cddb-choice=0 >> "$ABCDETEMPDIR/status"
c9c2ca27 1521 ;;
1522 210|211)
1523 # Multiple exact, (possibly multiple) inexact matches
1524 IN=
1525 if [ "$RESPONSECODE" = "211" ]; then IN=in; fi
b82e2042 1526 if [ "$(wc -l < "$ABCDETEMPDIR/cddbquery" | tr -d ' ')" -eq 3 ]; then
c9c2ca27 1527 echo "One ${IN}exact match:" >> "$ABCDETEMPDIR/cddbchoices"
1528 tail -n +2 "$ABCDETEMPDIR/cddbquery" | head -n 1 >> "$ABCDETEMPDIR/cddbchoices"
1529 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
1530 else
1531 echo "Multiple ${IN}exact matches:" >> "$ABCDETEMPDIR/cddbchoices"
1532 fi
99d009fa 1533 vecho -n "Retrieving multiple matches... "
c9c2ca27 1534 grep -v ^[.]$ "$ABCDETEMPDIR/cddbquery" | ( X=0
1535 read DISCINFO # eat top line
1536 while read DISCINFO
1537 do
1538 X=$(expr $X + 1)
1539 if checkstatus cddb-read-$X-complete; then :; else
1540 $CDDBTOOL read $CDDBURL $CDDBPROTO $CDDBUSER $CDDBHOST $(echo $DISCINFO | cut -f1,2 -d' ') > "$ABCDETEMPDIR/cddbread.$X"
1541 echo cddb-read-$X-complete >> "$ABCDETEMPDIR/status"
1542 fi
1543 # List out disc title/author and contents
1544 echo \#$X: ---- "$DISCINFO" ---- >> "$ABCDETEMPDIR/cddbchoices"
1545 for TRACK in $(f_seq_row 1 $TRACKS)
1546 do
1547 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.$X" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
1548 done
1549 echo >> "$ABCDETEMPDIR/cddbchoices"
1550 done )
99d009fa 1551 vecho "done."
c9c2ca27 1552 CDDBCHOICES=$(expr $(cat "$ABCDETEMPDIR/cddbquery" | wc -l) - 2)
1553 ;;
1554 999)
1555 # Using local copy.
1556 for TRACK in $(f_seq_row 1 $TRACKS)
1557 do
1558 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.1" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
1559 done
1560 echo >> "$ABCDETEMPDIR/cddbchoices"
1561 echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
1562 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
1563 ;;
1564 esac
1565 echo "cddb-readcomplete" >> "$ABCDETEMPDIR/status"
1566 fi
1567}
1568
1569# do_cddbedit
1570do_cddbedit ()
1571{
1572 if checkstatus cddb-edit >/dev/null; then
1573 CDDBDATA="$ABCDETEMPDIR/cddbread.$(checkstatus cddb-choice)"
1574 VARIOUSARTISTS="$(checkstatus variousartists)"
1575 VARIOUSARTISTSTYLE="$(checkstatus variousartiststyle)"
1576 return 0
1577 fi
1578 if [ "$INTERACTIVE" = "y" ]; then
1579 # We should show the CDDB results both when we are not using the local CDDB repo
1580 # or when we are using it but we could not find a proper match
1581 if [ "$CDDBUSELOCAL" = "y" ] && [ ! "$CDDBLOCALSUCCESS" = "y" ] || [ ! "$CDDBUSELOCAL" = "y" ]; then
1582 # Display the $ABCDETEMPDIR/cddbchoices file created above
1583 # Pick a pager so that if the tracks overflow the screen the user can still view everything
1584 if [ -r "$ABCDETEMPDIR/cddbchoices" ]; then
1585 CDDBCHOICES=$(expr $(cat "$ABCDETEMPDIR/cddbquery" | wc -l) - 2)
1586 CHOICE=$(checkstatus cddb-choice)
1587 if [ -n "$CHOICE" ] ; then
1588 case $CDDBCHOICES in
1589 1) cat "$ABCDETEMPDIR/cddbchoices" ;;
1590 *)
1591 echo "Selected: #$CHOICE"
1592 do_cddbparse "$ABCDETEMPDIR/cddbread.$CHOICE"
1593 ;;
1594 esac
1595 else
1596 # The user has a choice to make, display the info in a pager if necessary
1597 if [ $(cat "$ABCDETEMPDIR/cddbchoices" | wc -l) -ge 24 ]; then
1598 # Use the debian sensible-pager wrapper to pick the pager
1599 # user has requested via their $PAGER environment variable
1600 if [ -x "/usr/bin/sensible-pager" ]; then
1601 /usr/bin/sensible-pager "$ABCDETEMPDIR/cddbchoices"
1602 elif [ -x "$PAGER" ]; then
1603 # That failed, try to load the preferred editor, starting
1604 # with their PAGER variable
1605 $PAGER "$ABCDETEMPDIR/cddbchoices"
1606 # If that fails, check for less
1607 elif [ -x /usr/bin/less ]; then
1608 /usr/bin/less -f "$ABCDETEMPDIR/cddbchoices"
1609 # more should be on all UNIX systems
1610 elif [ -x /bin/more ]; then
1611 /bin/more "$ABCDETEMPDIR/cddbchoices"
1612 else
1613 # No bananas, just cat the thing
1614 cat "$ABCDETEMPDIR/cddbchoices" >&2
1615 fi
1616 else
1617 # It's all going to fit in one page, cat it
1618 cat "$ABCDETEMPDIR/cddbchoices" >&2
1619 fi
1620
9ff5088c 1621 CDDBCHOICENUM=""
f75dae43 1622 CDCHOICENUM=-1
c9c2ca27 1623 # I'll take CDDB read #3 for $400, Alex
9ff5088c 1624 while [ $CDCHOICENUM -lt 0 ] || [ $CDCHOICENUM -gt $CDDBCHOICES ]; do
1625 echo -n "Which entry would you like abcde to use (0 for none)? [0-$CDDBCHOICES]: " >&2
c9c2ca27 1626 read CDDBCHOICE
9ff5088c 1627 [ X"$CDDBCHOICE" = "X" ] && CDDBCHOICE=1
1628 if echo $CDDBCHOICE | egrep -q "[[:digit:]]+,[[:digit:]]+" ; then
1629 if [ ! X"$DIFF" = "X" ]; then
1630 # We parse the 2 choices to diff, store them in temporary files and diff them.
1631 for PARSECHOICE in $(echo $CDDBCHOICE | tr , \ ); do
1632 do_cddbparse "$ABCDETEMPDIR/cddbread.$PARSECHOICE" > "$ABCDETEMPDIR/cddbread.parsechoice.$PARSECHOICE"
1633 done
1634 PARSECHOICE1=$(echo $CDDBCHOICE | cut -d"," -f1)
1635 PARSECHOICE2=$(echo $CDDBCHOICE | cut -d"," -f2)
1636 echo "Showing diff between choices $PARSECHOICE1 and $PARSECHOICE2..."
1637 $DIFF $DIFFOPTS "$ABCDETEMPDIR/cddbread.parsechoice.$PARSECHOICE1" "$ABCDETEMPDIR/cddbread.parsechoice.$PARSECHOICE2"
1638 else
1639 echo "The diff program was not found in your path. Please choose a number between 0 and $CDDBCHOICES." >&2
1640 fi
1641 else
1642 if [ $CDCHOICENUM -lt 0 ] || [ $CDCHOICENUM -gt $CDDBCHOICES ]; then
1643 # Make sure we get a valid choice
1644 CDCHOICENUM=$(echo $CDDBCHOICE | xargs printf %d 2>/dev/null)
1645 echo "Invalid selection. Please choose a number between 0 and $CDDBCHOICES." >&2
1646 fi
1647 fi
c9c2ca27 1648 done
99d009fa 1649 if [ "$CDCHOICENUM" = "0" ]; then
1650 vecho "Creating empty CDDB template..."
1651 UNKNOWNDISK=y
b82e2042 1652 $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.0"
99d009fa 1653 else
03308bc7 1654 echo "Selected: #$CDCHOICENUM ($(grep ^DTITLE= "$ABCDETEMPDIR/cddbread.$CDCHOICENUM" | cut -f2- -d= | tr -d \\r\\n))" >&2
99d009fa 1655 do_cddbparse "$ABCDETEMPDIR/cddbread.$CDCHOICENUM"
1656 fi
c9c2ca27 1657 echo "cddb-choice=$CDCHOICENUM" >> "$ABCDETEMPDIR/status"
1658 fi
1659 fi
1660 else
1661 # We need some code to show the selected option when local repository is selected and we have found a match
9f659ada 1662 vecho "Using cached CDDB match..." >&2
c9c2ca27 1663 # Display the $ABCDETEMPDIR/cddbchoices file created above
1664 # Pick a pager so that if the tracks overflow the screen the user can still view everything
1665 if [ -r "$ABCDETEMPDIR/cddbchoices" ]; then
1666 CDDBCHOICES=$(expr $(cat "$ABCDETEMPDIR/cddbquery" | wc -l) - 2)
1667 CHOICE=$(checkstatus cddb-choice)
1668 if [ "$USELOCALRESP" = "y" ]; then :; else
1669 if [ -n "$CHOICE" ] ; then
1670 case $CDDBCHOICES in
04609998 1671 0)
1672 UNKNOWNDISK=y
1673 echo "Selected template."
1674 ;;
c9c2ca27 1675 1) cat "$ABCDETEMPDIR/cddbchoices" ;;
1676 *)
1677 echo "Selected: #$CHOICE"
1678 do_cddbparse "$ABCDETEMPDIR/cddbread.$CHOICE"
1679 ;;
1680 esac
1681 fi
1682 fi
1683 fi
1684 fi
1685 else
1686 # We're noninteractive - pick the first choice.
1687 # But in case we run a previous instance and selected a choice, use it.
1688 if [ -r "$ABCDETEMPDIR/cddbchoices" ]; then
1689 # Show the choice if we are not using the locally stored one
1690 # or when the local search failed to find a match.
1691 PREVIOUSCHOICE=$(checkstatus cddb-choice)
1692 if [ "$CDDBUSELOCAL" = "y" ] && [ "$CDDBLOCALSUCCESS" = "n" ] || [ ! "$CDDBUSELOCAL" = "y" ]; then
1693 #if [ "$PREVIOUSCHOICE" ]; then
1694 cat "$ABCDETEMPDIR/cddbchoices"
1695 #fi
1696 fi
1697 if [ ! -z "$PREVIOUSCHOICE" ] ; then
1698 CDCHOICENUM=$PREVIOUSCHOICE
1699 else
1700 CDCHOICENUM=1
1701 echo "cddb-choice=$CDCHOICENUM" >> "$ABCDETEMPDIR/status"
1702 fi
03308bc7 1703 echo "Selected: #$CDCHOICENUM ($(grep ^DTITLE= "$ABCDETEMPDIR/cddbread.$CDCHOICENUM" | cut -f2- -d= | tr -d \\r\\n))" >&2
c9c2ca27 1704 fi
1705 fi
1706
1707 # sanity check
1708 if checkstatus cddb-choice >/dev/null; then :; else
1709 echo "abcde: internal error: cddb-choice not recorded." >&2
1710 exit 1
1711 fi
1712 CDDBDATA="$ABCDETEMPDIR/cddbread.$(checkstatus cddb-choice)"
9f659ada 1713 echo -n "Edit selected CDDB data? [y/n] (" >&2
c9c2ca27 1714 if [ "$INTERACTIVE" = "y" ]; then
1715 if [ "$UNKNOWNDISK" = "y" ]; then
1716 echo -n "y): " >&2
1717 read EDITCDDB
1718 [ "$EDITCDDB" != "n" ] && EDITCDDB=y
1719 else
1720 echo -n "n): " >&2
1721 read EDITCDDB
1722 fi
1723 else
1724 echo "n): n" >&2
1725 EDITCDDB=n
1726 fi
1727 if [ "$EDITCDDB" = "y" ]; then
1728 CDDBDATAMD5SUM=$($MD5SUM "$CDDBDATA" | cut -d " " -f 1);
1729
1730 # Use the debian sensible-editor wrapper to pick the editor that the
1731 # user has requested via their $EDITOR environment variable
1732 if [ -x "/usr/bin/sensible-editor" ]; then
1733 /usr/bin/sensible-editor "$CDDBDATA"
1734 elif [ -n "$EDITOR" ]; then
1735 if [ -x $(which "${EDITOR%%\ *}") ]; then
1736 # That failed, try to load the preferred editor, starting
1737 # with their EDITOR variable
03308bc7 1738 eval $(echo "$EDITOR") \"$CDDBDATA\"
c9c2ca27 1739 fi
1740 # If that fails, check for a vi
2994a485 1741 elif which vi >/dev/null 2>&1; then
1742 vi "$CDDBDATA"
fd34b28b 1743 elif [ -x /usr/bin/vim ]; then
1744 /usr/bin/vim "$CDDBDATA"
c9c2ca27 1745 elif [ -x /usr/bin/vi ]; then
1746 /usr/bin/vi "$CDDBDATA"
fd34b28b 1747 elif [ -x /bin/vi ]; then
1748 /bin/vi "$CDDBDATA"
c9c2ca27 1749 # nano should be on all (modern, i.e., sarge) debian systems
2994a485 1750 elif which nano >/dev/null 2>&1 ; then
1751 nano "$CDDBDATA"
c9c2ca27 1752 elif [ -x /usr/bin/nano ]; then
1753 /usr/bin/nano "$CDDBDATA"
1754 # mg should be on all OpenBSD systems
2994a485 1755 elif which mg >/dev/null 2>&1 ; then
1756 mg "$CDDBDATA"
c9c2ca27 1757 elif [ -x /usr/bin/mg ]; then
1758 /usr/bin/mg "$CDDBDATA"
1759 # bomb out
1760 else
1761 echo "No editor available. Check your EDITOR environment variable." >&2
1762 fi
1763 # delete editor backup file if it exists
1764 if [ -w "$CDDBDATA~" ]; then
1765 rm -f "$CDDBDATA~"
1766 fi
1767 fi
1768
1769 # Some heuristics first. Look at Disc Title, and if it starts with
1770 # "Various", then we'll assume Various Artists
c81531ac 1771 if [ "$(grep ^DTITLE= "$CDDBDATA" | cut -f2- -d= | egrep -ci '^(various|soundtrack|varios|sonora|ost)')" != "0" ]; then
c9c2ca27 1772 echo "Looks like a Multi-Artist CD" >&2
1773 VARIOUSARTISTS=y
1774 else
1775 echo -n "Is the CD multi-artist? [y/n] (n): " >&2
1776 if [ "$INTERACTIVE" = "y" ]; then
1777 read VARIOUSARTISTS
1778 else
1779 echo n >&2
1780 VARIOUSARTISTS=n
1781 fi
1782 fi
1783 if [ "$VARIOUSARTISTS" = "y" ] && [ ! "$ONETRACK" = "y" ]; then
1784 # Set a default
1785 DEFAULTSTYLE=1
1786 # Need NUMTRACKS before cddb-tool will return it:
99d009fa 1787 NUMTRACKS=$(egrep '^TTITLE[0-9]+=' "$CDDBDATA" | wc -l)
c9c2ca27 1788 if [ "$(grep -c "^TTITLE.*\/" "$CDDBDATA")" -gt "$(expr $NUMTRACKS / 2 )" ]; then
1789 # More than 1/2 tracks contain a "/", so guess forward
1790 DEFAULTSTYLE=1
1791 elif [ "$(grep -c "^TTITLE.*\-" "$CDDBDATA")" -gt "$(expr $NUMTRACKS / 2 )" ]; then
1792 # More than 1/2 contain a "-", so guess forward-dash
1793 DEFAULTSTYLE=2
1794 elif [ "$(grep -c "^TTITLE.*(.*)" "$CDDBDATA")" -gt "$(expr $NUMTRACKS / 2 )" ]; then
1795 # More than 1/2 contain something in parens, so guess trailing-paren
1796 DEFAULTSTYLE=6
1797 fi
1798
1799 echo "1) Artist / Title" >&2
1800 echo "2) Artist - Title" >&2
1801 echo "3) Title / Artist" >&2
1802 echo "4) Title - Artist" >&2
1803 echo "5) Artist: Title" >&2
1804 echo "6) Title (Artist)" >&2
1805 echo "7) This is a single-artist CD" >&2
1806 echo -n "Which style of multiple artist entries is it? [1-7] ($DEFAULTSTYLE): " >&2
1807 if [ "$INTERACTIVE" = "y" ]; then
1808 read VARIOUSARTISTSTYLE
1809 else
1810 echo $DEFAULTSTYLE >&2
1811 VARIOUSARTISTSTYLE=$DEFAULTSTYLE
1812 fi
1813 VARIOUSARTISTSTYLE=$(echo 0$VARIOUSARTISTSTYLE | xargs printf %d)
1814 # If they press Enter, then the default style (0) was chosen
1815 while [ $VARIOUSARTISTSTYLE -lt 0 ] || [ $VARIOUSARTISTSTYLE -gt 7 ]; do
1816 echo "Invalid selection. Please choose a number between 1 and 7."
1817 echo -n "Selection [1-7]: "
1818 read VARIOUSARTISTSTYLE
1819 VARIOUSARTISTSTYLE=$(echo 0$VARIOUSARTISTSTYLE | xargs printf %d)
1820 done
1821 if [ "$VARIOUSARTISTSTYLE" = "0" ]; then
1822 VARIOUSARTISTSTYLE=$DEFAULTSTYLE
1823 fi
99d009fa 1824 vecho "Selected: $VARIOUSARTISTSTYLE"
c9c2ca27 1825 case "$VARIOUSARTISTSTYLE" in
1826 1) # Artist / Title
1827 VARIOUSARTISTSTYLE=forward
1828 ;;
1829 2) # Artist - Title
1830 VARIOUSARTISTSTYLE=forward-dash
1831 ;;
1832 3) # Title / Artist
1833 VARIOUSARTISTSTYLE=reverse
1834 ;;
1835 4) # Title - Artist
1836 VARIOUSARTISTSTYLE=reverse-dash
1837 ;;
1838 5) # Artist: Title
1839 VARIOUSARTISTSTYLE=colon
1840 ;;
1841 6) # Title (Artist)
1842 VARIOUSARTISTSTYLE=trailing-paren
1843 ;;
1844 7) # Single Artist
1845 VARIOUSARTISTS=n
1846 ;;
1847 esac
1848 fi
1849
1850 echo "variousartists=$VARIOUSARTISTS" >> "$ABCDETEMPDIR/status"
1851 echo "variousartiststyle=$VARIOUSARTISTSTYLE" >> "$ABCDETEMPDIR/status"
1852
1853 if [ "$EDITCDDB" = "y" ] && [ "$UNINTENTIONALLY_ANGER_THE_FREEDB_PEOPLE" = "y" ]; then
1854 if [ $CDDBDATAMD5SUM != "" ] && [ $CDDBDATAMD5SUM != $($MD5SUM "$CDDBDATA" | cut -d " " -f 1) ]; then
1855 # This works but does not have the necessary error checking
1856 # yet. If you are familiar with the CDDB spec
1857 # (see http://www.freedb.org/src/latest/DBFORMAT)
1858 # and can create an error-free entry on your own, then put
1859 # UNINTENTIONALLY_ANGER_THE_FREEDB_PEOPLE=y in your
1860 # abcde.conf to enable it. Put CDDBSUBMIT=email@address in
1861 # your abcde.conf to change the email address submissions are
1862 # sent to.
1863
1864 # submit the modified file, if they want
1865 if [ "$NOSUBMIT" != "y" ]; then
1866 echo -n "Do you want to submit this entry to $CDDBSUBMIT? [y/n] (n): "
1867 read YESNO
1868 while [ "$YESNO" != "y" ] && [ "$YESNO" != "n" ] && [ "$YESNO" != "Y" ] && \
1869 [ "$YESNO" != "N" ] && [ "$YESNO" != "" ]
1870 do
1871 echo -n 'Invalid selection. Please answer "y" or "n": '
1872 read YESNO
1873 done
1874 if [ "$YESNO" = "y" ] || [ "$YESNO" = "Y" ]; then
1875 echo -n "Sending..."
1876 $CDDBTOOL send "$CDDBDATA" $CDDBSUBMIT
1877 echo "done."
1878 fi
1879 fi
1880 fi
1881 fi
1882 # Make sure the cache directory exists
1883 mkdir -p $CDDBLOCALDIR
1884 # Cache edited CDDB entry in the user's cddb dir
1885 if [ "$CDDBCOPYLOCAL" = "y" ] || [ "$COPYCDDBLOCAL" = "Y" ]; then
1886 cat "$CDDBDATA" | tail -n $(expr $(cat "$CDDBDATA" | wc -l ) - 1 ) > ${CDDBLOCALDIR}/$(echo "$TRACKINFO" | cut -d' ' -f1)
1887 fi
1888
1889 echo "cddb-edit" >> "$ABCDETEMPDIR/status"
1890}
1891
c9c2ca27 1892# do_cdread [tracknumber]
d5200e72 1893# do_cdread onetrack [firsttrack] [lasttrack]
c9c2ca27 1894#
1895do_cdread ()
1896{
1897 # The commands here don't go through run_command because they're never supposed to be silenced
1898 # return codes need to be doublechecked anyway, however
d5200e72 1899 if [ "$1" = "onetrack" ]; then
1900 # FIXME # Add the possibility of grabbing ranges of tracks in onetrack
c7ea851d 1901 # FIXME # Until then, we grab the whole CD in one track, no matter what
1902 # the user said
d5200e72 1903 # We need the first and last track for cdda2wav
1904 FIRSTTRACK=$2
1905 LASTTRACK=$3
1366e842 1906 UTRACKNUM=$FIRSTTRACK
d5200e72 1907 case "$CDROMREADERSYNTAX" in
1366e842 1908 cdparanoia) READTRACKNUMS="$FIRSTTRACK-$LASTTRACK" ;;
1909 cdda2wav) READTRACKNUMS="$FIRSTTRACK+$LASTRACK" ;;
d5200e72 1910 *) echo "abcde error: $CDROMREADERSYNTAX does not support ONETRACK mode"
1911 exit 1 ;;
1912 esac
1913 else
1914 UTRACKNUM=$1
1915 fi
c9c2ca27 1916 CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
49c04b11 1917 if [ "$USEPIPES" = "y" ]; then
1918 TEMPARG="PIPE_$CDROMREADERSYNTAX"
03308bc7 1919 FILEARG="${!TEMPARG}"
49c04b11 1920 REDIR=""
0a3a2dcc 1921 PIPE_MESSAGE="and encoding "
49c04b11 1922 else
0a3a2dcc 1923 WAVDATA="$ABCDETEMPDIR/track$UTRACKNUM.wav"
49c04b11 1924 case "$CDROMREADERSYNTAX" in
c7ea851d 1925 ## FIXME ## Find the case for dagrab, to avoid exceptions
49c04b11 1926 dagrab)
1927 FILEARG="-f $WAVDATA"
1928 ;;
1929 *)
03308bc7 1930 FILEARG="$WAVDATA"
49c04b11 1931 ;;
1932 esac
1933 REDIR=">&2"
1934 fi
efc6de2f 1935 if [ "$1" = "onetrack" ]; then
b82e2042 1936 echo "Grabbing ${PIPE_MESSAGE}tracks $UTRACKNUM - $LASTTRACK as one track ..." >&2
0a3a2dcc 1937 else
efc6de2f 1938 if [ -r "$CDDBDATA" ]; then
1939 TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -n 1 | cut -f2 -d= | tr -d \[:cntrl:\])
1940 echo "Grabbing ${PIPE_MESSAGE}track $UTRACKNUM: $TRACKNAME..." >&2
1941 else
1942 echo "Grabbing ${PIPE_MESSAGE}track $UTRACKNUM..." >&2
1943 fi
0a3a2dcc 1944 fi
c9c2ca27 1945 case "$CDROMREADERSYNTAX" in
1366e842 1946 cdparanoia)
c008b56d 1947 nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS $CDROM ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG" $REDIR ;;
c9c2ca27 1948 cdda2wav)
1949 if [ "$OSFLAVOUR" = "OSX" ] ; then
1950 # Hei, we have to unmount the device before running anything like cdda2wav in OSX
1951 disktool -u ${CDROM#/dev/} 0
1952 # Also, in OSX the cdrom device for cdda2wav changes...
1953 CDDA2WAVCDROM="IODVDServices"
1954 elif [ "$OSFLAVOUR" = "FBSD" ] ; then
1955 CDDA2WAVCDROM="$CDROMID"
1956 else
1957 if [ "$CDROMID" = "" ]; then
1958 CDDA2WAVCDROM="$CDROM"
1959 else
1960 CDDA2WAVCDROM="$CDROMID"
1961 fi
1962 fi
03308bc7 1963 nice $READNICE $CDROMREADER -D $CDDA2WAVCDROM -t ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG" $REDIR
c7ea851d 1964 ;;
1965 ## FIXME ## We have an exception for dagrab, since it uses -f
1966 ## FIXME ## Shall we just use -f $FILEARG ??
03308bc7 1967 dagrab) nice $READNICE $CDROMREADER -d $CDROM -v $UTRACKNUM "$FILEARG" $REDIR
c9c2ca27 1968 ;;
c9c2ca27 1969 cddafs)
1970 # Find the track's mounted path
1971 REALTRACKNUM=$(expr $UTRACKNUM + 0)
47589f71 1972 FILEPATH=$(mount | grep "$CDROM on" | sed 's/^[^ ]* on \(.*\) (.*/\1/')
c9c2ca27 1973 FILEPATH=$(find "$FILEPATH" | grep "/$REALTRACKNUM ");
1974 # If the file exists, copy it
1975 if [ -e "$FILEPATH" ] ; then
03308bc7 1976 nice $READNICE $CDROMREADER "$FILEPATH" "$FILEARG" $REDIR
c9c2ca27 1977 else
1978 false
1979 fi ;;
c008b56d 1980 debug) nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS $CDROM -w $UTRACKNUM-[:1] "$FILEARG" $REDIR
c7ea851d 1981 ;;
c9c2ca27 1982 esac
1983 RETURN=$?
c7ea851d 1984 # If we get some error or we get some missing wav
1985 # (as long as we dont use pipes)
1986 if [ "$RETURN" != "0" -o \( ! -s "$WAVDATA" -a X"$USEPIPES" != "Xy" \) ]; then
c9c2ca27 1987 # Thank goodness errors is only machine-parseable up to the
1988 # first colon, otherwise this woulda sucked
93415558 1989 if [ "$RETURN" = "0" -a ! -s "$WAVDATA" ]; then
1990 RETURN=73 # fake a return code as cdparanoia return 0 also on aborted reads
1991 fi
49c04b11 1992 if [ "$USEPIPES" = "y" ]; then
1993 echo "readencodetrack-$UTRACKNUM: $CDROMREADER returned code $RETURN" >> "$ABCDETEMPDIR/errors"
1994 else
1995 echo "readtrack-$UTRACKNUM: $CDROMREADER returned code $RETURN" >> "$ABCDETEMPDIR/errors"
1996 fi
c9c2ca27 1997 return $RETURN
1998 else
49c04b11 1999 if [ "$USEPIPES" = "y" ]; then
2000 echo readencodetrack-$UTRACKNUM >> "$ABCDETEMPDIR/status"
2001 else
2002 echo readtrack-$UTRACKNUM >> "$ABCDETEMPDIR/status"
3416c114 2003 if [ "$1" = "onetrack" ]; then
2004 echo onetrack >> "$ABCDETEMPDIR/status"
2005 fi
49c04b11 2006 fi
c9c2ca27 2007 fi
2008}
2009
99d009fa 2010# do_cdspeed
2011# No values accepted, only uses env variables
c9c2ca27 2012do_cdspeed ()
2013{
2014 if "$CDSPEED" "$CDSPEEDOPTS" "$CDSPEEDVALUE" >/dev/null ; then
2015 vecho "Setting CD speed to ${CDSPEEDVALUE}x"
2016 else
2017 echo "abcde: unable to set the device speed" >&2
2018 fi
2019}
2020
2021# vecho [message]
2022#
2023# vecho outputs a message if EXTRAVERBOSE is selected
2024vecho ()
2025{
2026if [ x"$EXTRAVERBOSE" != "x" ]; then
2027 echo $@
2028fi
2029}
2030
b7f1cc84 2031# User-redefinable functions
2032# Custom filename munging:
2033mungefilename ()
2034{
eb1474fe 2035 #echo "$@" | sed s,:,\ -,g | tr \ /\* __+ | tr -d \'\"\?\[:cntrl:\]
2036 echo "$@" | sed s,:,\ -,g | tr \ / __ | tr -d \'\"\?\[:cntrl:\]
b7f1cc84 2037}
2038
2039# Custom genre munging:
2040mungegenre ()
2041{
2042 echo $CDGENRE | tr "[:upper:]" "[:lower:]"
2043}
2044
2045# pre_read
2046# Empty pre_read function, to be defined in the configuration file.
2047pre_read ()
2048{
2049:
2050}
2051
65f3ebb1 2052# post_read
2053# Empty post_read function, to be defined in the configuration file.
2054post_read ()
2055{
2056:
2057}
2058
a2052a00 2059###############################################################################
b7f1cc84 2060# End of functions
2061#
c9c2ca27 2062# Start of execution
a2052a00 2063###############################################################################
c9c2ca27 2064
2065# Builtin defaults
2066CDDBURL="http://freedb.freedb.org/~cddb/cddb.cgi"
2067CDDBSUBMIT=freedb-submit@freedb.org
2068CDDBPROTO=5
2069HELLOINFO="$(whoami)@$(hostname)"
9ff5088c 2070CDDBCOPYLOCAL="n"
2071CDDBLOCALDIR="$HOME/.cddb"
2072CDDBUSELOCAL="n"
2073
2074# List of fields we parse and show during the CDDB parsing...
2075SHOWCDDBFIELDS="year,genre"
2076
c9c2ca27 2077INTERACTIVE=y
2078CDROMREADERSYNTAX=cdparanoia
c9c2ca27 2079ENCODERSYNTAX=default
2080
2081MP3ENCODERSYNTAX=default
2082OGGENCODERSYNTAX=default
2083FLACENCODERSYNTAX=default
2084SPEEXENCODERSYNTAX=default
99d009fa 2085MPPENCODERSYNTAX=default
72d7162b 2086NORMALIZERSYNTAX=default
c9c2ca27 2087
2088OUTPUTFORMAT='${ARTISTFILE}-${ALBUMFILE}/${TRACKNUM}.${TRACKFILE}'
2089# Use the following VAOUTPUTFORMAT to revert to 2.0.x VA format:
2090#VAOUTPUTFORMAT=${OUTPUTFORMAT}
2091VAOUTPUTFORMAT='Various-${ALBUMFILE}/${TRACKNUM}.${ARTISTFILE}-${TRACKFILE}'
2092ONETRACKOUTPUTFORMAT='${ARTISTFILE}-${ALBUMFILE}/${ALBUMFILE}'
2093VAONETRACKOUTPUTFORMAT='Various-${ALBUMFILE}/${ALBUMFILE}'
2094PLAYLISTFORMAT='${ARTISTFILE}-${ALBUMFILE}.${OUTPUT}.m3u'
2095PLAYLISTDATAPREFIX=''
2096VAPLAYLISTFORMAT='${ARTISTFILE}-${ALBUMFILE}.${OUTPUT}.m3u'
2097VAPLAYLISTDATAPREFIX=''
2098DOSPLAYLIST=n
2099COMMENT=''
2100ID3TAGV=2
2101ENCNICE=10
2102READNICE=10
2103DISTMP3NICE=10
2104VARIOUSARTISTS=n
2105VARIOUSARTISTSTYLE=forward
c9c2ca27 2106KEEPWAVS=n
2107PADTRACKS=n
c9c2ca27 2108
2109# If using scsi devices, cdda2wav needs a CDROMID, instead of a device node
2110# i.e. CDROMID="1,0,0"
2111CDROMID=""
c008b56d 2112# If we are using the IDE bus, we need CDPARANOIACDROMBUS defined as "d"
2113# If we are using the ide-scsi emulation layer, we need to define a "g"
2114CDPARANOIACDROMBUS="d"
c9c2ca27 2115
2116# program paths - defaults to checking your $PATH
2117# mp3
2118LAME=lame
2119GOGO=gogo
2120BLADEENC=bladeenc
2121L3ENC=l3enc
2122XINGMP3ENC=xingmp3enc
2123MP3ENC=mp3enc
2124# ogg
2125VORBIZE=vorbize
2126OGGENC=oggenc
2127# flac
2128FLAC=flac
2129# speex
2130SPEEXENC=speexenc
99d009fa 2131# mpp (Musepack)
2132MPPENC=mppenc
c9c2ca27 2133
2134ID3=id3
2135ID3V2=id3v2
2136CDPARANOIA=cdparanoia
2137CDDA2WAV=cdda2wav
2138DAGRAB=dagrab
2139CDDAFS=cp
2140CDDISCID=cd-discid
2141CDDBTOOL=cddb-tool
2142EJECT=eject
2143MD5SUM=md5sum
2144DISTMP3=distmp3
2145VORBISCOMMENT=vorbiscomment
2146METAFLAC=metaflac
72d7162b 2147NORMALIZE=normalize-audio
c9c2ca27 2148CDSPEED=eject
12c5d978 2149VORBISGAIN=vorbisgain
24f444d7 2150MKCUE=mkcue
9ff5088c 2151MKTOC=cdrdao
2152DIFF=diff
c9c2ca27 2153
2154# Options for programs called from abcde
2155# mp3
2156LAMEOPTS=
2157GOGOOPTS=
2158BLADEENCOPTS=
2159L3ENCOPTS=
2160XINGMP3ENCOPTS=
2161MP3ENCOPTS=
2162# ogg
2163VORBIZEOPTS=
2164OGGENCOPTS=
2165# flac
6119aaa1 2166FLACOPTS="-f"
c9c2ca27 2167# speex
2168SPEEXENCOPTS=
9f659ada 2169# mpc
2170MPPENCOPTS=
c9c2ca27 2171
2172ID3OPTS=
2173ID3V2OPTS=
2174CDPARANOIAOPTS=
2175CDDA2WAVOPTS=
2176DAGRABOPTS=
2177CDDAFSOPTS="-f"
2178CDDBTOOLOPTS=
2179EJECTOPTS=
2180DISTMP3OPTS=
2181NORMALIZEOPTS=
2182CDSPEEDOPTS="-x"
2183CDSPEEDVALUE=
24f444d7 2184MKCUEOPTS=
9ff5088c 2185MKTOCOPTS=""
b33dc0c1 2186VORBISCOMMENTOPTS="-R"
2187METAFLACOPTS="--no-utf8-convert"
c9c2ca27 2188
2189# Default to one process if -j isn't specified
2190MAXPROCS=1
2191
2192# List of actions to perform - by default, run to completion
2193ACTIONS=cddb,read,encode,tag,move,clean
2194
b82e2042 2195# List of prefered outputs - by default, run with whatever we have in the path
2196# This option is basicaly for Debian package dependencies
2197DEFAULT_OUTPUT_BINARIES=vorbis:oggenc,flac:flac,mp3:lame,mp3:bladeenc,spx:speex
2198
c9c2ca27 2199# Asume fetch if under FreeBSD. curl is used for Mac OS X. wget is used for Linux/OpenBSD/NetBSD.
2200# Let's use these checkings to determine the OS flavour, which will be used later
2201if [ X$(uname) = "XFreeBSD" ] ; then
2202 HTTPGET=fetch
2203 NEEDCDROMID=y
2204 OSFLAVOUR=FBSD
2205elif [ X$(uname) = "XDarwin" ] ; then
2206 HTTPGET=curl
2207 OSFLAVOUR=OSX
2208 # We should have disktool in OSX, but let's be sure...
2209 NEEDDISKTOOL=y
f97e22a3 2210 CDROMREADERSYNTAX=cddafs
c9c2ca27 2211elif [ X$(uname) = "XOpenBSD" ] ; then
2212 HTTPGET=wget
2213 MD5SUM=md5
2214else
2215 HTTPGET=wget
2216fi
2217
2218# If CDDBAVAIL is set to n, no CDDB read is done
2219# If USEID3 is set to n, no ID3 tagging is done
2220CDDBAVAIL=y
2221USEID3=y
24f444d7 2222USEID3V2=y
c9c2ca27 2223
2224if [ -z "$OUTPUTDIR" ]; then
2225 OUTPUTDIR=$(pwd)
2226fi
2227
2228if [ -z "$WAVOUTPUTDIR" ]; then
2229 WAVOUTPUTDIR="$OUTPUTDIR"
2230fi
2231
2232# Load system defaults
2233if [ -r /etc/abcde.conf ]; then
2234 . /etc/abcde.conf
2235fi
2236# Load user preference defaults
2237if [ -r $HOME/.abcde.conf ]; then
2238 . $HOME/.abcde.conf
2239fi
2240
2241# By this time, we need some HTTPGETOPTS already defined.
b7f1cc84 2242# If the user has defined a non-default HTTPGET method, we should not be empty.
c9c2ca27 2243
2244if [ "$HTTPGETOPTS" = "" ] ; then
2245 case $HTTPGET in
2246 wget) HTTPGETOPTS="-q -O -";;
2247 curl) HTTPGETOPTS="-f -s";;
2248 fetch)HTTPGETOPTS="-q -o -";;
2249 *) echo "abcde warning: HTTPGET in non-standard and HTTPGETOPTS are not defined." >&2 ;;
2250 esac
2251fi
2252
2253# If the CDROM has not been set yet, find a suitable one.
2254# If this is a devfs system, default to /dev/cdroms/cdrom0
2255# instead of /dev/cdrom
2256if [ "$CDROM" = "" ] ; then
2257 if [ -e /dev/cdroms/cdrom0 ]; then
2258 CDROM=/dev/cdroms/cdrom0
2259 elif [ -e /dev/cdrom ]; then
2260 CDROM=/dev/cdrom
2261 elif [ -e /dev/cd0c ]; then
2262 CDROM=/dev/cd0c
2263 elif [ -e /dev/acd0c ]; then
2264 CDROM=/dev/acd0c
2265 elif [ -e /dev/disk1 ]; then
2266 CDROM=/dev/disk1
2267 fi
2268fi
2269
2270# Parse command line options
9ff5088c 2271#while getopts 1a:A:bc:C:d:Dehj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W: opt ; do
4667195a 2272while getopts 1a:bc:C:d:Dehj:klLmMnNo:pPr:Rs:S:t:T:vVxw:W: opt ; do
c9c2ca27 2273 case "$opt" in
2274 1) ONETRACK=y ;;
2275 a) ACTIONS="$OPTARG" ;;
12c5d978 2276 A) EXPACTIONS="$OPTARG";;
c9c2ca27 2277 b) BATCH=y ;;
2278 c) if [ -e "$OPTARG" ] ; then . "$OPTARG" ; else echo "abcde error: config file \"$OPTARG\" cannot be found." >&2 ; exit 1 ; fi ;;
2279 C) DISCID="${OPTARG#abcde.}" ;;
2280 d) CDROM="$OPTARG" ;;
2281 D) set -x ;;
bef9c2d8 2282 e) ERASEENCODEDSTATUS=y ;;
c9c2ca27 2283 h) usage; exit ;;
b33dc0c1 2284 e) ERASEENCODEDSTATUS=y ;;
b7f1cc84 2285# f) FORCECDDBUSELOCAL=y ;;
c9c2ca27 2286 i) INLINETAG=y ;;
2287 j) MAXPROCS="$OPTARG" ;;
2288 k) KEEPWAVS=y ;;
2289 l) LOWDISK=y ;;
b7f1cc84 2290 L) CDDBUSELOCAL=y ;;
2291 n) CDDBAVAIL=n ;;
2292 N) INTERACTIVE=n ;;
c9c2ca27 2293 m) DOSPLAYLIST=y ;;
b82e2042 2294 M) MAKECUEFILE=y ;;
c9c2ca27 2295 o) OUTPUTTYPE="$OPTARG" ;;
b7f1cc84 2296 p) PADTRACKS=y ;;
2297 P) USEPIPES=y ;;
c9c2ca27 2298 r) REMOTEHOSTS="$OPTARG" ;;
12c5d978 2299 R) REPLAYGAIN=y ;;
9ff5088c 2300 s) SHOWCDDBFIELDS="$OPTARG" ;;
c9c2ca27 2301 S) CDSPEEDVALUE="$OPTARG" ;;
79e3928b 2302# t) PREPROCESSFORMATS="$OPTARG"
2303# PREPROCESS=y ;;
2304# T) POSTPROCESSFORMATS="$OPTARG" ;;
2305 t) STARTTRACKNUMBER="$OPTARG" ;;
2306 T) STARTTRACKNUMBER="$OPTARG" ; STARTTRACKNUMBERTAG="y" ;;
c9c2ca27 2307 v)
2308 echo "This is abcde v$VERSION."
2309 echo "Usage: abcde [options] [tracks]"
2310 echo "abcde -h for extra help"
2311 exit
2312 ;;
2313 V) EXTRAVERBOSE="y" ;;
2314 x) EJECTCD="y" ;;
79e3928b 2315 w) COMMENT="$OPTARG" ;;
bef9c2d8 2316 W) if echo $OPTARG | grep -q "[[:digit:]]" ; then
2317 STARTTRACKNUMBER="${OPTARG}01" ; STARTTRACKNUMBERTAG="y" ; COMMENT="CD${OPTARG}"
2318 else
2319 echo "abcde error: argument of -W must be integer"
2320 exit 1
2321 fi
2322 ;;
c9c2ca27 2323 ?) usage; exit ;;
2324 esac
2325done
2326
2327shift $(($OPTIND - 1))
2328
d5200e72 2329# Decide if we can continue.
2330if [ "$ONETRACK" = "y" ]; then
2331 # FIXME # remove check as soon as we find out about the other readers
99d009fa 2332 case "$CDROMREADERSYNTAX" in
b82e2042 2333 cdparanoia) ;;
2334 cdda2wav) ;;
2335 *) echo "abcde error: $CDROMREADERSYNTAX does not support ONETRACK mode"
2336 exit 1 ;;
99d009fa 2337 esac
2338 if [ "$BATCH" = "y" ]; then
2339 echo "abcde error: BATCH mode is not compatible with ONETRACK mode"
d5200e72 2340 BATCH=n
99d009fa 2341 fi
2342 # It does not matter how many tracks we want. In ONETRACK mode we grab them all
d5200e72 2343 # FIXME # allow ranges of tracks to be selected for onetrack ripping
c9c2ca27 2344 if [ $# -gt 0 ]; then
d5200e72 2345 vecho "abcde warning: ONETRACK mode selected, grabbing all tracks..."
c9c2ca27 2346 fi
2347else
2348 while [ $# -gt 0 ]; do
2349 # Range parsing code courtesy of Vincent Ho
2350 RSTART=$(echo $1 | cut -f1 -d-)
2351 REND=$(echo $1 | cut -f2 -d-)
2352 if [ "$RSTART" = "$REND" ]; then
2353 NEWTRACKS="$RSTART"
2354 else
2355 NEWTRACKS=$(f_seq_line $RSTART $REND)
2356 fi
2357 TRACKQUEUE=$(echo "$TRACKQUEUE" "$NEWTRACKS")
c9c2ca27 2358 shift
2359 done
2360fi
2361
b7f1cc84 2362# List of valid actions: cddb,read,normalize,encode,tag,move,playlist,clean
2363# List of experimental actions: retag,transcode
2364
2365# Determine what actions are to be done from $ACTIONS and set the
2366# following environment variables for them:
2367DOCDDB=n
2368DOREAD=n
2369DONORMALIZE=n
2370DOPREPROCESS=n
2371DOENCODE=n
2372DOPOSTPROCESS=n
2373DOTAG=n
2374DOMOVE=n
2375DOPLAYLIST=n
2376DOCLEAN=n
2377
2378for ACTION in $(echo $ACTIONS | tr , \ )
2379do
2380 case $ACTION in
24f444d7 2381 cddb) DOCDDB=y;;
2382 read) DOREAD=y;;
2383 normalize) DONORMALIZE=y; DOREAD=y;;
2384# preprocess) DOPREPROCESS=y; DOREAD=y;;
2385 encode) DOENCODE=y; DOREAD=y;;
2386# postprocess) DOPREPROCESS=y; DOENCODE=y; DOREAD=y;;
2387 tag) DOTAG=y; DOREAD=y; DOENCODE=y; DOCDDB=y;;
2388 move) DOMOVE=y; DOTAG=y; DOREAD=y; DOENCODE=y; DOCDDB=y;;
2389 playlist) DOCDDB=y; DOPLAYLIST=y;;
2390 clean) DOCLEAN=y;;
b7f1cc84 2391 esac
2392done
2393
9ff5088c 2394for SHOWCDDBFIELD in $(echo $SHOWCDDBFIELDS | tr , \ ); do
2395 case $SHOWCDDBFIELD in
2396 y*|Y*) SHOWCDDBYEAR="y";;
2397 g*|G*) SHOWCDDBGENRE="y";;
2398 *) ;;
2399 esac
2400done
b7f1cc84 2401
9ff5088c 2402# Sanity checks:
b82e2042 2403
c9c2ca27 2404# At this point a CDROM has to be defined, so we check it exists.
6c124acf 2405if [ X"$CDROM" != "X" ] ; then
c9c2ca27 2406 if [ "$CDROMREADERSYNTAX" = "cdda2wav" ] && [ "$NEEDCDROMID" = "y" ] ; then
2407 if [ "$OSFLAVOUR" = "FBSD" ]; then
2408 if ! echo "$CDROMID" | grep "^[0-9],[0-9],[0-9]$" >/dev/null 2>&1 ; then
2409 echo "abcde error: CDROMID not in the right format for $CDROMREADERSYNTAX"
2410 echo "Use \"cdrecord -scanbus\" to obtain a adecuate ID an set CDROMID accordingly"
2411 exit 1
8fc41c4f 2412 fi
c9c2ca27 2413 fi
c81531ac 2414 elif [ ! -e "$CDROM" -a X"$DOREAD" = "Xy" ]; then
c9c2ca27 2415 echo "abcde error: CDROM device cannot be found." >&2
2416 exit 1
2417 fi
6c124acf 2418# avoid processing if we are not going to hit the CDROM.
c81531ac 2419elif [ X"$DOREAD" = "Xy" ]; then
c9c2ca27 2420 echo "abcde error: CDROM has not been defined or cannot be found" >&2
2421 exit 1
2422fi
2423
c7ea851d 2424# USEPIPES pre-tests, before we get into more serious stuff
2425# Not compatible with:
d5200e72 2426# - multiple outputs
2427# - normalize
c7ea851d 2428# - lowdisk algorithm
d5200e72 2429# - anything else?
c7ea851d 2430if [ X"$USEPIPES" = "Xy" ]; then
b82e2042 2431 if [ $(echo "$OUTPUTTYPE" | tr , \ | wc -w ) -gt 1 ]; then
2432 echo "abcde error: Unix pipes not compatible with multiple outputs" >&2
d5200e72 2433 exit 1
2434 fi
c7ea851d 2435 if [ X"$DONORMALIZE" = "Xy" ]; then
d5200e72 2436 echo "abcde error: Unix pipes not compatible with normalizer"
2437 # FIXME # Do we need to exit or shall we just disable the mode?
2438 exit 1
2439 fi
c7ea851d 2440 if [ X"$DOENCODE" = "Xn" ]; then
d5200e72 2441 vecho "Disabling Unix pipes since we are not encoding!"
2442 USEPIPES=n
2443 fi
c7ea851d 2444 if [ X"$LOWDISK" = "Xy" ]; then
2445 vecho "abcde error: Unix pipes not compatible with lowdisk algorithm"
2446 exit 1
2447 fi
d5200e72 2448fi
2449
9ff5088c 2450# Check the encoding format from the ones available in the system, if nothing has been configured in the system.
2451if [ X"$OUTPUTTYPE" = "X" ]; then
2452 for DEFAULT_OUTPUT in $( echo "$DEFAULT_OUTPUT_BINARIES" | tr , \ ); do
2453 DEFAULT_OUTPUT_FORMAT="$(echo $DEFAULT_OUTPUT | cut -d ":" -f 1)"
2454 DEFAULT_OUTPUT_BINARY="$(echo $DEFAULT_OUTPUT | cut -d ":" -f 2)"
2455 if [ -x $(which $DEFAULT_OUTPUT_BINARY) ] ; then
2456 OUTPUTTYPE=$DEFAULT_OUTPUT_FORMAT
2457 vecho "No default output type defined. Autoselecting $OUTPUTTYPE..." >&2
2458 break
2459 fi
2460 done
2461 if [ X"$OUTPUTTYPE" = "X" ]; then
2462 echo "abcde error: no encoder found in the PATH" >&2
2463 echo "hits: are all dependencies installed? has the \$PATH been modified?" >&2
2464 exit 1
2465 fi
2466fi
2467
c9c2ca27 2468# Decide which CDROM reader we're gonna use
2469case "$CDROMREADERSYNTAX" in
2470 cdparanoia|debug)
2471 CDROMREADER="$CDPARANOIA"
2472 CDROMREADEROPTS="$CDPARANOIAOPTS"
2473 ;;
2474 cdda2wav)
2475 CDROMREADER="$CDDA2WAV"
2476 CDROMREADEROPTS="$CDDA2WAVOPTS"
2477 ;;
2478 dagrab)
2479 CDROMREADER="$DAGRAB"
2480 CDROMREADEROPTS="$DAGRABOPTS"
2481 ;;
2482 cddafs)
2483 CDROMREADER="$CDDAFS"
2484 CDROMREADEROPTS="$CDDAFSOPTS"
2485 ;;
2486esac
2487
2488# There's only one normalize...
2489case "$NORMALIZERSYNTAX" in
2490 default|normalize)
2491 NORMALIZER="$NORMALIZE"
2492 NORMALIZEROPTS="$NORMALIZEOPTS"
2493 ;;
2494esac
2495
24f444d7 2496# Allow -o OUTPUT(1):OPTIONS(1),...,OUTPUT(N):OPTIONS(N) mode of operation
b82e2042 2497if echo "$OUTPUTTYPE" | grep ":" > /dev/null 2>&1 ; then
2498 for OUTPUT in $(echo $OUTPUTTYPE | tr , \ ); do
24f444d7 2499 case "$OUTPUT" in
b82e2042 2500 vorbis:*|ogg:*) OGGENCODEROPTSCLI="$( echo $OUTPUT | cut -d: -f2- )" ;;
24f444d7 2501 mp3:*) MP3ENCODEROPTSCLI="$( echo $OUTPUT | cut -d: -f2- )" ;;
2502 flac:*) FLACENCODEROPTSCLI="$( echo $OUTPUT | cut -d: -f2- )" ;;
2503 spx:*) SPEEXENCODEROPTSCLI="$( echo $OUTPUT | cut -d: -f2- )" ;;
2504 mpc:*) MPPENCODEROPTSCLI="$( echo $OUTPUT | cut -d: -f2- )" ;;
2505 esac
2506 done
b82e2042 2507 for OUTPUT in $(echo $OUTPUTTYPE | tr , \ ); do
24f444d7 2508 TEMPOUTPUT=$( echo "$OUTPUT" | cut -d: -f1 )
2509 TEMPOUTPUTTYPE="${TEMPOUTPUTTYPE:+$TEMPOUTPUTTYPE,}$TEMPOUTPUT"
2510 done
2511 OUTPUTTYPE="$TEMPOUTPUTTYPE"
24f444d7 2512fi
2513
99d009fa 2514# 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 2515
2516# Getting ready for multiple output changes
2517for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
2518do
2519 case $OUTPUT in
b82e2042 2520 vorbis|ogg)
2521 [ "$OGGENCODERSYNTAX" = "default" ] && OGGENCODERSYNTAX=oggenc
2522 [ "$DOTAG" = "y" ] && NEEDCOMMENTER=y
2523 [ "$REPLAYGAIN" = "y" ] && NEEDVORBISGAIN=y
2524 OGGOUTPUTCONTAINER=ogg
2525 ;;
2526 mp3)
2527 [ "$MP3ENCODERSYNTAX" = "default" ] && MP3ENCODERSYNTAX=lame
2528 [ "$DOTAG" = "y" ] && NEEDTAGGER=y
2529 ;;
2530 flac)
2531 [ "$FLACENCODERSYNTAX" = "default" ] && FLACENCODERSYNTAX=flac
2532 [ "$DOTAG" = "y" ] && NEEDMETAFLAC=y
2533 ;;
2534 spx)
2535 [ "$SPEEXENCODERSYNTAX" = "default" ] && SPEEXENCODERSYNTAX=speexenc
2536 ;;
2537 mpc)
2538 [ "$MPPENCODERSYNTAX" = "default" ] && MPPENCODERSYNTAX=mppenc
2539 ;;
24f444d7 2540 *) echo "abcde error: Invalid OUTPUTTYPE defined" >&2
2541 exit 1
c9c2ca27 2542 ;;
c9c2ca27 2543 esac
2544done
2545
2546# decide which encoder
2547case "$MP3ENCODERSYNTAX" in
2548 lame)
24f444d7 2549 MP3ENCODEROPTS="${MP3ENCODEROPTSCLI:-$LAMEOPTS}"
c9c2ca27 2550 MP3ENCODER="$LAME"
2551 ;;
2552 gogo)
24f444d7 2553 MP3ENCODEROPTS="${MP3ENCODEROPTSCLI:-$GOGOOPTS}"
c9c2ca27 2554 MP3ENCODER="$GOGO"
2555 ;;
2556 bladeenc)
24f444d7 2557 MP3ENCODEROPTS="${MP3ENCODEROPTSCLI:-$BLADEENCOPTS}"
c9c2ca27 2558 MP3ENCODER="$BLADEENC"
2559 ;;
2560 l3enc)
24f444d7 2561 MP3ENCODEROPTS="${MP3ENCODEROPTSCLI:-$L3ENCOPTS}"
c9c2ca27 2562 MP3ENCODER="$L3ENC"
2563 ;;
2564 xingmp3enc)
24f444d7 2565 MP3ENCODEROPTS="${MP3ENCODEROPTSCLI:-$XINGMP3ENCOPTS}"
c9c2ca27 2566 MP3ENCODER="$XINGMP3ENC"
2567 ;;
2568 mp3enc)
24f444d7 2569 MP3ENCODEROPTS="${MP3ENCODEROPTSCLI:-$MP3ENCOPTS}"
c9c2ca27 2570 MP3ENCODER="$MP3ENC"
2571 ;;
2572esac
2573case "$OGGENCODERSYNTAX" in
2574 vorbize)
24f444d7 2575 OGGENCODEROPTS="${OGGENCODEROPTSCLI:-$VORBIZEOPTS}"
c9c2ca27 2576 OGGENCODER="$VORBIZE"
2577 ;;
2578 oggenc)
24f444d7 2579 OGGENCODEROPTS="${OGGENCODEROPTSCLI:-$OGGENCOPTS}"
c9c2ca27 2580 OGGENCODER="$OGGENC"
2581 ;;
2582esac
2583case "$FLACENCODERSYNTAX" in
2584 flac)
24f444d7 2585 FLACENCODEROPTS="${FLACENCODEROPTSCLI:-$FLACOPTS}"
c81531ac 2586 FLACENCODER="$FLAC"
b82e2042 2587 # FLAC streams can be encapsulated on a Ogg transport layer
c81531ac 2588 if echo "$FLACENCODEROPTS" | egrep -q -- "(^| )--ogg($| )" ;then
b82e2042 2589 echo "abcde error: FLAC on an Ogg container is not yet supported" >&2
2590 echo " due to problem with adding comments to such files" >&2
2591 exit 1
2592 FLACOUTPUTCONTAINER=ogg
2593 else
2594 FLACOUTPUTCONTAINER=flac
c81531ac 2595 fi
c9c2ca27 2596 ;;
2597esac
2598case "$SPEEXENCODERSYNTAX" in
2599 speexenc)
24f444d7 2600 SPEEXENCODEROPTS="${SPEEXENCODEROPTSCLI:-$SPEEXENCOPTS}"
c9c2ca27 2601 SPEEXENCODER="$SPEEXENC"
2602 ;;
2603esac
99d009fa 2604case "$MPPENCODERSYNTAX" in
2605 mppenc)
24f444d7 2606 MPPENCODEROPTS="${MPPENCODEROPTSCLI:-$MPPENCOPTS}"
99d009fa 2607 MPPENCODER="$MPPENC"
2608 ;;
2609esac
24f444d7 2610
c9c2ca27 2611# and which tagger
2612
2613if [ "$ID3TAGV" = "1" ]; then
2614 TAGGER="$ID3"
2615 TAGGEROPTS="$ID3OPTS"
2616else
2617 TAGGER="$ID3V2"
2618 TAGGEROPTS="$ID3V2OPTS"
2619fi
2620
b82e2042 2621# Check if both OGGEOUTPUTCONTAINER and FLACOUTPUTCONTAINER are the same, and differentiante them
2622if [ X"$OGGOUTPUTCONTAINER" = "Xogg" ] && [ X"$FLACOUTPUTCONTAINER" = "Xogg" ]; then
2623 echo "abcde error: FLAC on an Ogg container is not yet supported" >&2
2624 echo " due to problem with adding comments to such files" >&2
2625 exit 1
2626 OGGOUTPUTCONTAINER=ogg.ogg
2627 FLACOUTPUTCONTAINER=flac.ogg
2628 vecho "abcde warning: modified file endings due to conflicting transport layers in Ogg/Vorbis and Ogg/FLAC"
2629fi
2630
c9c2ca27 2631# Clean up nice options (either use '-n NICELEVEL or -NICELEVEL')
2632
2633if [ "$ENCNICE" ]; then
2634 ENCNICE="-n $ENCNICE"
2635fi
2636if [ "$READNICE" ]; then
2637 READNICE="-n $READNICE"
2638fi
2639if [ "$DISTMP3NICE" ]; then
2640 DISTMP3NICE="-n $DISTMP3NICE"
2641fi
2642
2643# Don't check for stuff if it's not needed
bef9c2d8 2644if [ "$REMOTEHOSTS" ]; then
2645 NEEDDISTMP3=y
2646fi
2647if [ "$DONORMALIZE" = "y" ]; then
2648 NEEDNORMALIZER=y
2649fi
2650if [ "$EJECTCD" = "y" ]; then
2651 NEEDEJECT=y
2652fi
2653if [ ! "$CDDBAVAIL" = "n" ] && [ "$DOCDDB" = "y" ]; then
2654 NEEDHTTPGET=y
2655fi
2656if [ "$CUEFILE" = "y" ]; then
2657 NEEDMKCUE=y
2658fi
c9c2ca27 2659
2660if [ X"$CDSPEEDVALUE" != "X" ]; then
2661 case "$CDROMREADERSYNTAX" in
2662 cdparanoia|debug) CDROMREADEROPTS="$CDPARANOIAOPTS -S $CDSPEEDVALUE" ;;
2663 *) NEEDCDSPEED=y ;;
2664 esac
2665fi
2666
c7ea851d 2667# Rippers with USEPIPE support
2668# FIXME # Include here all the rippers we can figure out support pipes
2669PIPE_cdparanoia="-"
2670
2671# Encoders with USEPIPE support
2672# FIXME # Include here all the encoders we can figure out support pipes
2673PIPE_lame="-"
5a433968 2674PIPE_bladeenc="-"
c7ea851d 2675PIPE_oggenc="-"
2676
2677# Figure out if we can use pipes with the ripper/encoder combination
2678# exit otherwise
2679if [ "$USEPIPES" = "y" ]; then
2680 PIPERIPPERSVARCHECK="PIPE_${CDROMREADER}"
2681 case "$OUTPUT" in
2682 mp3)
2683 PIPEENCODERSVARCHECK="PIPE_$MP3ENCODERSYNTAX" ;;
b82e2042 2684 vorbis|ogg)
c7ea851d 2685 PIPEENCODERSVARCHECK="PIPE_$OGGENCODERSYNTAX" ;;
2686 flac)
3416c114 2687 PIPEENCODERSVARCHECK="PIPE_$FLACENCODERSYNTAX" ;;
c7ea851d 2688 spx)
6c124acf 2689 PIPEENCODERSVARCHECK="PIPE_$SPEEXENCODER" ;;
c7ea851d 2690 mpc)
2691 PIPEENCODERSVARCHECK="PIPE_$MPPENCODER" ;;
2692 esac
2693 if [ ! -n "${!PIPERIPPERSVARCHECK}" ] ; then
2694 echo "abcde error: no support for pipes with given ripper" >&2
2695 echo "read the USEPIPES file from the source tarball to help" >&2
2696 echo "on a Debian system, read /usr/share/doc/abcde/USEPIPES.gz" >&2
2697 exit 1;
2698 fi
2699 if [ ! -n "${!PIPEENCODERSVARCHECK}" ] ; then
2700 echo "abcde error: no support for pipes with given encoder" >&2
2701 echo "read the USEPIPES file from the source tarball to help" >&2
2702 echo "on a Debian system, read /usr/share/doc/abcde/USEPIPES.gz" >&2
2703 exit 1;
2704 fi
2705fi
2706
c9c2ca27 2707# Make sure a buncha things exist
2708for X in $CDROMREADER $CDDISCID ${NEEDTAGGER+$TAGGER} $MP3ENCODER \
99d009fa 2709 $OGGENCODER $FLACENCODER $SPEEXENCODER $MPPENCODER \
2710 ${NEEDHTTPGET+$HTTPGET} ${NEEDDISTMP3+$DISTMP3} \
2711 ${NEEDCOMMENTER+$VORBISCOMMENT} ${NEEDMETAFLAC+$METAFLAC} \
2712 ${NEEDNORMALIZER+$NORMALIZER} ${NEEDEJECT+$EJECT} \
12c5d978 2713 ${NEEDDISKTOOL+disktool} ${NEEDCDSPEED+$CDSPEED} \
c7ea851d 2714 ${NEEDVORBISGAIN+$VORBISGAIN} ${NEEDMKCUE+$MKCUE}
c9c2ca27 2715do
2716 # Cut off the command-line options we just added in
2717 X=$(echo $X | cut -d' ' -f2)
2718 if [ "$(which $X)" = "" ]; then
2719 echo "abcde error: $X is not in your path." >&2
2720 exit 1
2721 elif [ ! -x $(which $X) ]; then
2722 echo "abcde error: $X is not executable." >&2
2723 exit 1
2724 fi
2725done
2726
2727CDROMREADER="$CDROMREADER $CDROMREADEROPTS"
2728CDDBTOOL="$CDDBTOOL $CDDBTOOLOPTS"
2729HTTPGET="$HTTPGET $HTTPGETOPTS"
2730
9ff5088c 2731# And last but not least, check if we can diff between files
2732if [ -x $(which $DIFF) ]; then :; else
2733 vecho "[WAR] Disabling diff since we cannot find it in the \$PATH..."
2734 DIFF=""
2735fi
2736
12c5d978 2737# Here it used to say:
2738# One thousand lines in, we can start doing stuff with things
5895f404 2739# Well, right now we are at line 2736 ;)
c9c2ca27 2740
2741# Export needed things so they can be read in this subshell
2742export CDDBTOOL ABCDETEMPDIR TRACKQUEUE LOWDISK EJECTCD EJECT EJECTOPTS
2743export CDROM CDDBDATA REMOTEHOSTS MAXPROCS HTTPGET MD5SUM
2744
2745# User-definable function to set some things. Use it for
2746# - closing the CD tray with eject -t
2747# - set the CD speed value with eject -x
2748vecho -n "Executing customizable pre-read function... "
2749
2750pre_read # Execute the user-defined pre-read funtion. Close the CD with it.
2751
2752vecho "done."
2753
2754do_discid # Get ABCDETEMPDIR created and status file initialized
2755
2756if [ "$DOCDDB" = "y" ]; then
2757 if [ $CDDBUSELOCAL = "y" ]; then
2758 do_localcddb
2759 fi
b7f1cc84 2760 if checkstatus cddb-choice > /dev/null; then
2761 :
2762 else
2763 if [ ! "$CDDBLOCALSUCCESS" = "y" ] ; then
2764 do_cddbstat
2765 do_cddbquery
2766 do_cddbread
2767 fi
c9c2ca27 2768 fi
2769 do_cddbedit
2770
2771 eval $($CDDBTOOL parse "$CDDBDATA")
2772fi
2773
2774# Before reading tracks, we set the speed of the device
2775
2776if [ X"$CDSPEEDVALUE" != "X" ]; then
2777 case "$CDROMREADERSYNTAX" in
2778 cdparanoia|debug) : ;;
2779 *) do_cdspeed ;;
2780 esac
2781fi
2782
bef9c2d8 2783# Define the first and last track, since we might need them later in several places
2784FIRSTTRACK=$( get_first $TRACKQUEUE )
2785LASTTRACK=$( get_last $TRACKQUEUE )
2786
2787if [ -f "$ABCDETEMPDIR/status" ] && [ X"$ERASEENCODEDSTATUS" = "Xy" ]; then
2788 mv "$ABCDETEMPDIR/status" "$ABCDETEMPDIR/status.old"
2789 grep -v ^encodetracklocation- < "$ABCDETEMPDIR/status.old" \
2790 | grep -v ^encode-output > "$ABCDETEMPDIR/status"
2791fi
2792
c9c2ca27 2793# Create playlist if needed (backgroundable) and start reading in tracks
2794
2795(
99d009fa 2796
d5200e72 2797if [ ! "$ONETRACK" = "y" ]; then
c9c2ca27 2798 if [ "$DOPLAYLIST" = "y" ]; then
2799 echo Creating playlist... >&2
2800 do_playlist
2801 fi
2802fi
2803
2804# For the lowdisk option, only one program is running at once so the encoder
2805# can be unsilenced right away.
2806if [ "$LOWDISK" = "y" ] || [ "$ONETRACK" = "y" ]; then
2807 echo "encode-output=loud" >> "$ABCDETEMPDIR/status"
2808fi
2809
2810if [ "$ONETRACK" = "y" ]; then
c9c2ca27 2811 TRACKS="$FIRSTTRACK"
c9c2ca27 2812 if checkstatus readtrack-$FIRSTTRACK; then :; else
bef9c2d8 2813 do_cdread onetrack $FIRSTTRACK $LASTTRACK
c9c2ca27 2814 fi
2815else
2816 for UTRACKNUM in $TRACKQUEUE
2817 do
2818 if [ "$DOREAD" = "y" ]; then
49c04b11 2819 if [ "$USEPIPES" = "y" ]; then
2820 if checkstatus readencodetrack-$UTRACKNUM; then :; else
69ff9575 2821 # Read, pipe, shut up!
2822 do_cdread $UTRACKNUM | do_encode $UTRACKNUM %local0% > /dev/null 2>&1
49c04b11 2823 fi
2824 else
2825 if checkstatus readtrack-$UTRACKNUM; then :; else
2826 do_cdread $UTRACKNUM
2827 fi
c9c2ca27 2828 if [ "$?" != "0" ]; then
2829 # CD read failed - don't give the goahead to
2830 # the encoder
2831 echo NO
2832 exit
2833 fi
2834 fi
2835 fi
2836 if [ "$BATCH" = "y" ]; then
2837 :
2838 else
2839 echo NEXTTRACK # Get the encoder machine churning again
2840 if [ "$DOREAD" = "y" ]; then
2841 if [ "$LOWDISK" = "y" ] && [ "$DOENCODE" = "y" ]; then
2842 until checkstatus encodetrack-$UTRACKNUM
2843 do
2844 if checkerrors encodetrack-$UTRACKNUM; then
2845 break
2846 fi
2847 sleep 2
2848 done
2849 fi
2850 fi
2851 fi
2852 done
2853fi
2854
2855# Now that we're done the encoding can be loud again -
2856# if we're not using SMP.
2857if [ "$MAXPROCS" = "1" ]; then
2858 echo "encode-output=loud" >> "$ABCDETEMPDIR/status"
2859fi
2860
2861# All tracks read, start encoding.
2862if [ "$BATCH" = "y" ] || [ "$ONETRACK" = "y" ]; then
2863 echo NEXTTRACK
2864fi
2865
65f3ebb1 2866# Execute the user-defined post_read funtion before ejecting CD
2867post_read
2868
c9c2ca27 2869# We are now finished with the cdrom - it can be safely ejected. Note that
2870# abcde will not have completed yet.
2871if [ "$EJECTCD" = "y" ] && [ -x $(which $EJECT) ]; then
2872 # We check if the disk we are processing is actually the disk inside the
2873 # CD tray. If not, we do not eject the CD, since it might be so that the
2874 # user ejected it manually.
2875 #CURRENTTRACKINFO=$($CDDISCID $CDROM)
2876 #if if [ "$?" != "1" ] && [ "$CURRENTTRACKINFO" = "$TRACKINFO" ] ; then
2877 # More FreeBSD bits.
2878 if [ X"$(uname)" = X"FreeBSD" ] ; then
2879 # FreeBSD eject uses the EJECT environment variable to name the CDROM
2880 # but in this script EJECT is in the envionment and names the program
2881 eject=$EJECT
2882 unset EJECT
2883 # The FreeBSD eject needs "adc0" not "/dev/adc0c"
2884 cd="$(echo $CDROM | sed -e 's=.*/==;s=[a-h]$==;')"
2885 $eject $EJECTOPTS $cd
2886 elif [ X"$(uname)" = X"Darwin" ] ; then
2887 disktool -e ${CDROM#/dev/} 0
2888 else
2889 $EJECT $EJECTOPTS $CDROM
2890 fi
2891 #fi
2892fi
2893
2894) | (
2895
99d009fa 2896## Do we need to pre-process
2897#if [ x"$PREPROCESS" = "x" ] ; then
2898# cat
2899#else
2900# for PRETRACKNUM in $TRACKQUEUE
2901# do
2902# read GOAHEAD
2903# if [ "$GOAHEAD" = "NO" ]; then break; fi
2904# PREPROCEED=
2905# until [ $PREPROCEED ]
2906# do
2907# if checkstatus readtrack-$PRETRACKNUM; then PREPROCEED=y; break; fi
2908# # all locations are working, wait and try again later
2909# if [ ! $PREPROCEED ]; then sleep 3; fi
2910# done
2911# ( do_preprocess $PRETRACKNUM
2912# echo "NEXTTRACK"
2913# ) &
2914# done
2915#fi
2916#
2917#) | (
2918
c9c2ca27 2919# In batch mode, we want all tracks to be read first.
2920if [ "$BATCH" = "y" ]; then
2921 read GOAHEAD # For blocking - will contain either "NO" or "NEXTTRACK"
2922 if [ "$GOAHEAD" = "NO" ]; then break; fi
2923 for LASTTRACK in $TRACKQUEUE; do :; done
2924 if checkstatus readtrack-$LASTTRACK; then
2925 if [ "$DONORMALIZE" = "y" ]; then
2926 if checkstatus normalizetrack-$LASTTRACK; then :; else do_batch_normalize; fi
2927 if checkerrors batch-normalize; then exit; fi
2928 fi
2929 if [ "$DOENCODE" = "y" ]; then
2930 if checkstatus encodetrack-$LASTTRACK; then :; else do_batch_encode; fi
2931 if checkerrors batch-encode; then exit; fi
2932 fi
2933 fi
2934fi
2935
2936# If we are using ONETRACK, we can proceed with the normal encoding using just the $FIRSTTRACK as TRACKQUEUE
2937if [ "$ONETRACK" = "y" ] ; then
bef9c2d8 2938 TRACKQUEUE="$FIRSTTRACK"
c9c2ca27 2939 TRACKS="$FIRSTTRACK"
2940fi
2941
2942# Do the encoding, including parallelization of remote encoding
2943# Figure out where each track is going to be encoded
2944ENCODELOCATIONS="$(echo $REMOTEHOSTS | tr , ' ')"
2945if [ "$MAXPROCS" != "0" ]; then
2946 for NUM in $(f_seq_row 1 "$MAXPROCS")
2947 do
2948 ENCODELOCATIONS="$ENCODELOCATIONS %local$NUM%"
2949 done
2950fi
2951# Strip whitespace
2952ENCODELOCATIONS=$(echo $ENCODELOCATIONS)
2953for UTRACKNUM in $TRACKQUEUE
2954do
2955 # Wait for our cue
2956 read GOAHEAD # For blocking - will contain either "NO" or "NEXTTRACK"
2957 if [ "$GOAHEAD" = "NO" ]; then break; fi
2958 # find out where this track is to be encoded
49c04b11 2959 if [ "$DOENCODE" = "y" -a "$USEPIPES" != "y" ]; then
c9c2ca27 2960 # Make sure we have a place to encode this, if not, exit stage right
2961 if [ -z "$ENCODELOCATIONS" ]; then
2962 continue
2963 fi
2964 PROCEED=
2965 until [ $PROCEED ]
2966 do
2967 for LOCATION in $ENCODELOCATIONS
2968 do
2969 PREVIOUSTRACK="$(checkstatus encodetracklocation-$LOCATION)"
2970 # check first if a track has ever been assigned to this location
2971 if [ -z "$PREVIOUSTRACK" ]; then PROCEED=y; break; fi
2972 # If it errored out, rebuild $ENCODELOCATIONS without this location in it
2973 if checkerrors encodetrack-$PREVIOUSTRACK; then
2974 for TEMPLOCATION in $ENCODELOCATIONS
2975 do
2976 if [ "$TEMPLOCATION" != "$LOCATION" ]; then
2977 TEMPENCODELOCATIONS="$TEMPENCODELOCATIONS $TEMPLOCATION"
2978 fi
2979 done
2980 ENCODELOCATIONS=$(echo $TEMPENCODELOCATIONS)
2981 ABORT=y
2982 PROCEED=y
2983 break
2984 fi
2985 # We're still here, this location must have been previously assigned,
2986 # and last completed without error - check if it's done with the
2987 # previous track yet
2988 if checkstatus encodetrack-$PREVIOUSTRACK; then PROCEED=y; break; fi
2989 done
2990 # all locations are working, wait and try again later
2991 if [ ! $PROCEED ]; then sleep 3; fi
2992 done
2993 # Record the location we're about to encode the next track at
2994 echo "encodetracklocation-$LOCATION=$UTRACKNUM" >> "$ABCDETEMPDIR/status"
2995 fi
2996 # Don't proceed with the rest of the loop if we can't encode
2997 if [ "$ABORT" ]; then continue; fi
2998 # Set TRACKNUM, TRACKNAME
2999 if [ -e "$CDDBDATA" ]; then
3000 if [ "$ONETRACK" = "y" ]; then
3001 TRACKNAME="$DALBUM"
3002 TRACKNUM="$FIRSTTRACK"
3003 splitvarious
3004 else
3005# TRACKNUM=$(printf %0.${TRACKNUMPADDING}d $(expr ${UTRACKNUM} + 0))
3006 TRACKNUM=$UTRACKNUM
eb1474fe 3007 CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
3008 TRACKNAME="$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')"
c9c2ca27 3009 splitvarious
3010 fi
3011 fi
3012 # You can't encode a file which needs to be normalized before finishing
3013 # You can't tag a file before it's finished encoding -
3014 # thus all of this is backgrounded together
3015 (
3016 if [ "$DONORMALIZE" = "y" ]; then
3017 if checkstatus readtrack-$UTRACKNUM; then
3018 if checkstatus normalizetrack-$UTRACKNUM; then :; else do_normalize $UTRACKNUM; fi
3019 fi
3020 fi
c7ea851d 3021 if [ "$DOENCODE" = "y" -a "$USEPIPES" != "y" ]; then
c9c2ca27 3022 if checkstatus readtrack-$UTRACKNUM; then
3023 #if checkstatus encodetrack-$UTRACKNUM; then :; else do_encode $UTRACKNUM $LOCATION; fi
3024 if [ "$DONORMALIZE" = "y" ]; then
3025 if checkstatus normalizetrack-$UTRACKNUM; then
3026 if checkstatus encodetrack-$UTRACKNUM; then :; else do_encode $UTRACKNUM $LOCATION $OUTPUT; fi
3027 fi
3028 else
3029 if checkstatus encodetrack-$UTRACKNUM; then :; else do_encode $UTRACKNUM $LOCATION $OUTPUT; fi
3030 fi
3031 fi
3032 fi
efc6de2f 3033 if [ "$DOTAG" = "y" ]; then
3034 if checkstatus encodetrack-$UTRACKNUM; then
c9c2ca27 3035 if checkstatus tagtrack-$UTRACKNUM; then :; else do_tag $UTRACKNUM; fi
3036 fi
b82e2042 3037 # Lets tag the cue file
3038 if checkstatus cleancuefile >/dev/null; then :; else
3039 if checkstatus cuefile >/dev/null ; then
3040 do_cleancue
3041 fi
3042 fi
c9c2ca27 3043 fi
3044 if [ "$DOMOVE" = "y" ]; then
3045 if checkstatus tagtrack-$UTRACKNUM; then
3046 if checkstatus movetrack-$UTRACKNUM; then :; else do_move $UTRACKNUM; fi
3047 fi
3048 fi
3049 ) &
3050done
79c8f4d0 3051
79c8f4d0 3052
c9c2ca27 3053# Go through it again and make sure there's no distmp3 stragglers, otherwise
3054# we'll delete the files they're working on
c7ea851d 3055## FIXME ## Check also for files which are encoded using PIPEs.
3056if [ "$DOENCODE" = "y" -a "$USEPIPES" != "y" ]; then
c9c2ca27 3057 PROCEED=
3058 until [ $PROCEED ]
3059 do
3060 PROCEED=y
3061 for LOCATION in $ENCODELOCATIONS
3062 do
3063 CHECKTRACK="$(checkstatus encodetracklocation-$LOCATION)"
3064 # "How can he give us a status update, if he's DEAD?"
3065 if checkstatus encodetrack-$CHECKTRACK; then
3066 continue
3067 fi
3068 # Nothing to see here please go quietly back to your homes
3069 if [ -z "$CHECKTRACK" ]; then continue; fi
3070 # You're still here? Maybe there is something...
3071 if checkstatus encodetrack-$CHECKTRACK; then :; else PROCEED= ; break; fi
3072 done
3073 # hold up
3074 if [ ! $PROCEED ]; then sleep 5; fi
3075 done
3076fi
3077# If the above didn't catch the stragglers, this will
3078wait
3079# Check to see if run_command logged any errors
3080if [ -f "$ABCDETEMPDIR/errors" ]; then
3081 echo "The following commands failed to run:"
3082 cat "$ABCDETEMPDIR/errors"
3083 # Don't clean up
3084 DOCLEAN=n
3085fi
3086if [ "$KEEPWAVS" = "y" ];then
3087 # Don't clean up
3088 DOCLEAN=n
3089fi
3090if [ "$DOCLEAN" = "y" ]; then
3091 # Wipe all the evidence
3092 # Gimme gimme gimme some more time!
3093 sleep 5
3094 rm -rf "$ABCDETEMPDIR"
3095 echo "Finished."
3096else
3097 echo "Finished. Not cleaning $ABCDETEMPDIR."
3098fi
3099)
3100exit 0
d5200e72 3101
3102# b:is_bash
3103# vim:tabstop=4