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