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