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