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