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