2 # Copyright (c) 1998-2001 Robert Woodcock <rcw@debian.org>
3 # Copyright (c) 2003-2004 Jesus Climent <jesus.climent@hispalinux.es>
4 # This code is hereby licensed for public consumption under either the
5 # GNU GPL v2 or greater, or Larry Wall's Artistic license - your choice.
7 # You should have received a copy of the GNU General Public License
8 # along with this program; if not, write to the Free Software
9 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
11 # Copyright for this work is to expire January 1, 2010, after which it
12 # shall be public domain.
18 echo "This is abcde v$VERSION."
19 echo "Usage: abcde [options] [tracks]"
21 echo "-1 Encode the whole CD in a single file"
22 echo "-a Actions to perform (cddb,read,normalize,encode,tag,move,playlist,clean)"
23 echo "-A Experimental actions (retag, transcode)"
24 echo "-b Batch mode: enable album normalization and nogap encoding"
25 echo "-c Specify a configuration file (overrides system and user config files)"
26 echo "-C Specify discid to resume from (only needed if you no longer have the cd)"
27 echo "-d Specify CDROM device to grab"
28 echo "-D Debugging mode (equivalent to sh -x abcde)"
29 echo "-f Force the use of a local CDDB entry. Use an empty template if not found"
30 echo "-h This help information"
31 #echo "-i Tag files while encoding, when possible (local only) -NWY-"
32 echo "-j Number of encoder processes to run at once"
33 echo "-k Keep the wav tracks for later use"
34 echo "-l Use low disk space algorithm"
35 echo "-L Use local CDDB storage directory"
36 echo "-n No lookup. Don't query CDDB, just create and use template"
37 echo "-N Noninteractive. Never prompt for anything"
38 echo "-m Modify playlist to include CRLF endings, to comply with some players"
39 echo "-o Output file type(s) (ogg,mp3,flac,spx,mpc). Defaults to ogg"
40 echo "-p Pad track numbers with 0's (if less than 10 tracks)"
41 echo "-r [host1,host2...] Also encode on these remote hosts"
42 echo "-R Add replaygain values to the tag info (ogg,flac)"
43 echo "-s Start the track numbering at a given number"
44 echo "-S Set the CD speed"
45 #echo "-t File types to preprocess (wav)"
46 #echo "-T Set postprocessing options"
47 echo "-v Show version number and exit"
48 echo "-V Be a bit more verbose about what is happening behind the scenes"
49 echo "-x Eject CD after all tracks are read"
50 echo "Tracks is a space-delimited list of tracks to grab."
51 echo "Ranges specified with hyphens are allowed."
54 # Funtions to replace the need of seq, which is too distribution dependant.
58 while [ $i -ne `expr $2 + 1` ]
68 while [ $i -ne `expr $2 + 1` ]
77 # Returns "0" if the blurb was found, returns 1 if it wasn't
78 # Puts the blurb content, if available, on stdout.
79 # Otherwise, returns "".
82 # Take the last line in the status file if there's multiple matches
84 BLURB=$(egrep $PATTERN "$ABCDETEMPDIR/status" | tail -n 1)
86 if [ -z "$BLURB" ]; then
91 # See if there's a = in it
92 if [ "$(echo $BLURB | grep -c =)" != "0" ]; then
93 echo "$(echo $BLURB | cut -f2- -d=)"
100 # Returns "0" if the blurb was found (meaning there was an error),
101 # returns 1 if it wasn't (yes this is a little backwards).
102 # Does not print the blurb on stdout.
103 # Otherwise, returns "".
106 if [ -e "$ABCDETEMPDIR/errors" ]; then :; else
109 # Take the last line in the status file if there's multiple matches
111 BLURB="$(egrep $PATTERN $ABCDETEMPDIR/errors | tail -n 1)"
113 if [ -z "$BLURB" ]; then
114 # negative, we did not have a negative...
117 # affirmative, we had a negative...
122 # run_command [blurb] [command...]
123 # Runs a command, silently if necessary, and updates the status file
128 # See if this is supposed to be silent
129 if [ "$(checkstatus encode-output)" = "loud" ]; then
133 # Special case for SMP, since
134 # encoder output is never displayed, don't mute echos
135 if [ -z "$BLURB" -a "$MAXPROCS" != "1" ]; then
145 if [ "$RETURN" = "2" ]; then
146 # File was already normalized.
151 if [ "$RETURN" != "0" ]; then
152 # Put an error in the errors file. For various reasons we
153 # can't capture a copy of the program's output but we can
154 # log what we attempted to execute and the error code
155 # returned by the program.
156 if [ "$BLURB" ]; then
159 echo "${TWEAK}returned code $RETURN: $@" >> "$ABCDETEMPDIR/errors"
160 return $RETURN # Do not pass go, do not update the status file
162 if [ "$BLURB" ]; then
163 echo $BLURB >> "$ABCDETEMPDIR/status"
167 # relpath() and slash() are Copyright (c) 1999 Stuart Ballard and
168 # distributed under the terms of the GNU GPL v2 or later, at your option
170 # Function to determine if a word contains a slash.
179 # Function to give the relative path from one file to another.
180 # Usage: relpath fromfile tofile
181 # eg relpath music/Artist/Album.m3u music/Artist/Album/Song.mp3
182 # (the result would be Album/Song.mp3)
183 # Output is relative path to $2 from $1 on stdout
185 # This code has the following restrictions:
186 # Multiple ////s are not collapsed into single /s, with strange effects.
187 # Absolute paths and ../s are handled wrong in FR (but they work in TO)
188 # If FR is a directory it must have a trailing /
196 /*) ;; # No processing is needed for absolute paths
198 # Loop through common prefixes, ignoring them.
199 while slash "$FR" && [ "$(echo "$FR" | cut -d/ -f1)" = "$(echo "$TO" | cut -d/ -f1)" ]
201 FR="$(echo "$FR" | cut -d/ -f2-)"
202 TO="$(echo "$TO" | cut -d/ -f2-)"
204 # Loop through directory portions left in FR, adding appropriate ../s.
207 FR="$(echo "$FR" | cut -d/ -f2-)"
215 # This code splits the a Various Artist track name from one of the following
218 # forward: Artist / Track
219 # forward-dash: Artist - Track
220 # reverse: Track / Artist
221 # reverse-dash: Track - Artist
222 # colon: Artist: Track
223 # trailing-paren: Artist (Track)
226 # VARIOUSARTISTS, VARIOUSARTISTSTYLE, TRACKNAME, TRACKARTIST
229 if [ "$VARIOUSARTISTS" = "y" ] && [ ! "$ONETRACK" = "y" ]; then
230 case "$VARIOUSARTISTSTYLE" in
232 DTITLEARTIST="$(echo $TRACKNAME | sed 's- / -~-g')"
233 TRACKARTIST="$(echo $DTITLEARTIST | cut -f1 -d~)"
234 TRACKNAME="$(echo $DTITLEARTIST | cut -f2 -d~)"
237 DTITLEARTIST="$(echo $TRACKNAME | sed 's, - ,~,g')"
238 TRACKARTIST="$(echo $DTITLEARTIST | cut -f1 -d~)"
239 TRACKNAME="$(echo $DTITLEARTIST | cut -f2 -d~)"
242 DTITLEARTIST="$(echo $TRACKNAME | sed 's- / -~-g')"
243 TRACKARTIST="$(echo $DTITLEARTIST | cut -f2 -d~)"
244 TRACKNAME="$(echo $DTITLEARTIST | cut -f1 -d~)"
247 DTITLEARTIST="$(echo $TRACKNAME | sed 's, - ,~,g')"
248 TRACKARTIST="$(echo $DTITLEARTIST | cut -f2 -d~)"
249 TRACKNAME="$(echo $DTITLEARTIST | cut -f1 -d~)"
252 DTITLEARTIST="$(echo $TRACKNAME | sed 's-: -~-g')"
253 TRACKARTIST="$(echo $DTITLEARTIST | cut -f1 -d~)"
254 TRACKNAME="$(echo $DTITLEARTIST | cut -f2 -d~)"
257 DTITLEARTIST="$(echo $TRACKNAME | sed 's,^\(.*\) (\(.*\)),\1~\2,')"
258 TRACKARTIST="$(echo $DTITLEARTIST | cut -f2 -d~)"
259 TRACKNAME="$(echo $DTITLEARTIST | cut -f1 -d~)"
262 elif [ "$ONETRACK" = "y" ]; then
263 TRACKARTIST="Various"
269 # do_tag [tracknumber]
270 # id3 tags a filename
272 # TRACKS, TRACKNAME, TRACKARTIST, TAGGER, TAGGEROPTS, VORBISCOMMENT, METAFLAC,
273 # COMMENT, DALBUM, DARTIST, CDYEAR, CDGENRE (and temporarily) ID3TAGV
276 COMMENTOUTPUT="$(eval echo ${COMMENT})"
277 run_command '' echo "Tagging track $1 of $TRACKS: $TRACKNAME..."
278 for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
282 # id3v2 v0.1.9 claims to have solved the -c bug, so we merge both id3 and id3v2
283 run_command tagtrack-$1 $TAGGER $TAGGEROPTS -c "$COMMENTOUTPUT" \
284 -A "$DALBUM" -a "$TRACKARTIST" -t "$TRACKNAME" -y "$CDYEAR" \
285 -g "$CDGENRE" -T "$1/$TRACKS" "$ABCDETEMPDIR/track$1.$OUTPUT"
288 case "$OGGENCODERSYNTAX" in
290 # vorbiscomment can't do in-place modification, mv the file first
291 if [ -f "$ABCDETEMPDIR/track$1.$OUTPUT" -a ! -f "$ABCDETEMPDIR/track$1.uncommented.$OUTPUT" ]; then
292 mv "$ABCDETEMPDIR/track$1.$OUTPUT" "$ABCDETEMPDIR/track$1.uncommented.$OUTPUT"
295 # These are from http://www.xiph.org/ogg/vorbis/doc/v-comment.html
296 echo ARTIST=$TRACKARTIST
298 echo TITLE=$TRACKNAME
299 if [ -n "$CDYEAR" ]; then
302 if [ -n "$CDGENRE" ]; then
303 echo GENRE="$CDGENRE"
306 echo CDDB=$CDDBDISCID
307 if [ "$(eval echo ${COMMENT})" != "" ]; then
308 case "$COMMENTOUTPUT" in
309 *=*) echo "$COMMENTOUTPUT";;
310 *) echo COMMENT="$COMMENTOUTPUT";;
313 ) | run_command tagtrack-$1 $VORBISCOMMENT -w \
314 "$ABCDETEMPDIR/track$1.uncommented.$OUTPUT" "$ABCDETEMPDIR/track$1.$OUTPUT"
315 # Doublecheck that the commented file was created successfully before wiping the original
316 if [ -f "$ABCDETEMPDIR/track$1.$OUTPUT" ]; then
317 rm -f "$ABCDETEMPDIR/track$1.uncommented.$OUTPUT"
319 mv "$ABCDETEMPDIR/track$1.uncommented.$OUTPUT" "$ABCDETEMPDIR/track$1.$OUTPUT"
326 echo ARTIST="$TRACKARTIST"
328 echo TITLE="$TRACKNAME"
329 if [ -n "$CDYEAR" ]; then
332 if [ -n "$CDGENRE" ]; then
333 echo GENRE="$CDGENRE"
336 echo CDDB=$CDDBDISCID
337 if [ "$(eval echo ${COMMENT})" != "" ]; then
338 case "$COMMENTOUTPUT" in
339 *=*) echo "$COMMENTOUTPUT";;
340 *) echo COMMENT="$COMMENTOUTPUT";;
343 ) | run_command tagtrack-$1 $METAFLAC --import-vc-from=- --no-utf8-convert "$ABCDETEMPDIR/track$1.$OUTPUT"
346 run_command tagtrack-$1 true
349 run_command tagtrack-$1 true
357 # OUTPUTTYPE, {FOO}ENCODERSYNTAX, ENCNICE, ENCODER, ENCODEROPTS
360 # The commands here don't go through run_command because they're never supposed to be silenced
361 echo "Batch encoding tracks: $TRACKQUEUE"
362 OUTPUT=$(echo $OUTPUTTYPE | grep "mp3" )
365 case "$MP3ENCODERSYNTAX" in
370 for UTRACKNUM in $TRACKQUEUE
372 TRACKFILES="$TRACKFILES track$UTRACKNUM.wav"
374 nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS --nogap $TRACKFILES
376 if [ "$RETURN" != "0" ]; then
377 echo "batch-encode: $ENCODER returned code $RETURN" >> errors
379 for UTRACKNUM in $TRACKQUEUE
381 echo encodetrack-$UTRACKNUM >> status
389 # Other encoders fall through to normal encoding as the tracks
390 # have not been entered in the status file.
393 # do_encode [tracknumber] [hostname]
394 # If no hostname is specified, encode locally
396 # TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
399 IN="$ABCDETEMPDIR/track$1.wav"
400 # We need IN to proceed.
401 if [ -s "$IN" ] ; then
402 for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
404 OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
405 run_command '' echo "Encoding track $1 of $TRACKS: $TRACKNAME..."
410 case "$MP3ENCODERSYNTAX" in
411 lame|gogo) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS "$IN" "$OUT" ;;
412 bladeenc) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS -quit "$IN" ;;
413 l3enc|xingmp3enc) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $MP3ENCODER "$IN" "$OUT" $MP3ENCODEROPTS ;;
414 mp3enc) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $MP3ENCODER -if "$IN" -of "$OUT" $MP3ENCODEROPTS ;;
418 run_command encodetrack-$OUTPUT-$1 nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" >/dev/null 2>&1
425 case "$OGGENCODERSYNTAX" in
426 vorbize) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $OGGENCODER $OGGENCODEROPTS -w "$OUT" "$IN" ;;
427 oggenc) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $OGGENCODER $OGGENCODEROPTS -o "$OUT" "$IN" ;;
431 run_command encodetrack-$OUTPUT-$1 nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" >/dev/null 2>&1
438 case "$FLACENCODERSYNTAX" in
439 flac) run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $FLACENCODER $FLACENCODEROPTS -o "$OUT" "$IN" ;;
445 echo "$DISTMP3 $DISTMP3OPTS $2 $IN $OUT >/dev/null 2>&1"
446 run_command encodetrack-$OUTPUT-$1 nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" > /dev/null 2>&1
451 if [ "$(eval echo ${COMMENT})" != "" ]; then
454 *) COMMENT="COMMENT=$COMMENT" ;;
456 COMMENT="--comment \"$COMMENT\""
458 # Quick hack to avoid tagging Ogg/Speex, since there is no other way to tag than inline tagging
459 if [ ! "$DOTAG" = "y" ]; then
460 run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $SPEEXENCODER $SPEEXENCODEROPTS --author "$TRACKARTIST" --title "$TRACKNAME" "$COMMENT" "$IN" "$OUT"
462 run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $SPEEXENCODER $SPEEXENCODEROPTS "$IN" "$OUT"
466 # MPP/MP+(Musepack) format (.mpc) is done locally, with inline
468 # I tried compiling the mppenc from corecodecs.org and got some
469 # errors, so I have not tried it myself.
470 ## FIXME ## Needs some cleanup to determine if an empty tag sent
471 ## FIXME ## to the encoder ends up empty.
472 run_command encodetrack-$OUTPUT-$1 nice $ENCNICE $MPPENCODER $MPPENCODEROPTS --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --track "$1" --genre "$CDGENRE" --year "$CDYEAR" --comment "$COMMENT" "$IN" "$OUT"
476 # Only remove .wav if the encoding succeeded
477 if checkerrors "encodetrack-(.{3,4})-$1"; then
478 run_command encodetrack-$1 false
480 run_command encodetrack-$1 true
481 if [ ! "$KEEPWAVS" = "y" ] ; then
486 if [ "$(checkstatus encode-output)" = "loud" ]; then
487 echo "HEH! The file we were about to encode disappeared:"
490 run_command encodetrack-$1 false
494 # do_preprocess [tracknumber]
496 # TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
499 # IN="$ABCDETEMPDIR/track$1.wav"
500 # # We need IN to proceed.
501 # if [ -s "$IN" ] ; then
502 # for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
504 # #OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
505 # run_command '' echo "Pre-processing track $1 of $TRACKS..."
506 # case "$POSTPROCESSFORMAT" in
508 # run_command preprocess-$OUTPUT-$1 nice $PRENICE $WAV_PRE $IF $OF ;;
510 # run_command preprocess-$OUTPUT-$1 nice $PRENICE $MP3_PRE $IF $OF ;;
512 # run_command preprocess-$OUTPUT-$1 nice $PRENICE $OGG_PRE $IF $OF ;;
514 # run_command preprocess-$OUTPUT-$1 nice $PRENICE $FLAC_PRE $IF $OF ;;
516 # run_command preprocess-$OUTPUT-$1 nice $PRENICE $SPX_PRE $IF $OF ;;
519 # # Only remove .wav if the encoding succeeded
520 # if checkerrors "preprocess-(.{3,4})-$1"; then
521 # run_command preprocess-$1 false
523 # run_command preprocess-$1 true
526 # if [ "$(checkstatus encode-output)" = "loud" ]; then
527 # echo "HEH! The file we were about to pre-process disappeared:"
530 # run_command preprocess-$1 false
535 # do_postprocess [tracknumber]
537 # TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
540 # for POSTPROCESSFORMAT in $(echo $POSTPROCESSFORMATS | tr , \ )
542 # IN="$ABCDETEMPDIR/track$1.$POSTPROCESSFORMAT"
543 # # We need IN to proceed.
544 # if [ -s "$IN" ] ; then
545 # #OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
546 # run_command '' echo "Post-processing track $1 of $TRACKS..."
547 # case "$POSTPROCESSFORMAT" in
549 # run_command postprocess-$OUTPUT-$1 nice $POSTNICE $MP3_POST $IF $OF ;;
551 # run_command postprocess-$OUTPUT-$1 nice $POSTNICE $OGG_POST $IF $OF ;;
553 # run_command postprocess-$OUTPUT-$1 nice $POSTNICE $FLAC_POST $IF $OF ;;
555 # run_command postprocess-$OUTPUT-$1 nice $POSTNICE $SPX_POST $IF $OF ;;
557 # # Only remove .wav if the encoding succeeded
558 # if checkerrors "postprocess-(.{3,4})-$1"; then
559 # run_command postprocess-$1 false
561 # run_command postprocess-$1 true
564 # if [ "$(checkstatus encode-output)" = "loud" ]; then
565 # echo "HEH! The file we were about to post-process disappeared:"
568 # run_command postprocess-$1 false
575 # MP3GAIN, MP3GAINOPTS, VORBISGAIN, VORBISGAINOPTS
578 # The commands here don't go through run_command because they're never supposed to be silenced
579 echo "Batch analizing gain in tracks: $TRACKQUEUE"
584 for UTRACKNUM in $TRACKQUEUE
586 MP3FILES="$TRACKFILES track$UTRACKNUM.mp3"
588 # XXX: Hard-coded batch option!
589 $NORMALIZER -b $NORMALIZEROPTS $TRACKFILES
591 if [ "$RETURN" != "0" ]; then
592 echo "batch-normalize: $NORMALIZER returned code $RETURN" >> errors
594 for UTRACKNUM in $TRACKQUEUE
596 echo normalizetrack-$UTRACKNUM >> status
604 # NORMALIZER, NORMALIZEROPTS
605 do_batch_normalize ()
607 # The commands here don't go through run_command because they're never supposed to be silenced
608 echo "Batch normalizing tracks: $TRACKQUEUE"
613 for UTRACKNUM in $TRACKQUEUE
615 TRACKFILES="$TRACKFILES track$UTRACKNUM.wav"
617 # XXX: Hard-coded batch option!
618 $NORMALIZER -b $NORMALIZEROPTS $TRACKFILES
620 if [ "$RETURN" != "0" ]; then
621 echo "batch-normalize: $NORMALIZER returned code $RETURN" >> errors
623 for UTRACKNUM in $TRACKQUEUE
625 echo normalizetrack-$UTRACKNUM >> status
631 # do_normalize [tracknumber]
633 # TRACKS, TRACKNAME, NORMALIZER, NORMALIZEROPTS
636 IN="$ABCDETEMPDIR/track$1.wav"
637 if [ -e "$IN" ] ; then
638 run_command '' echo "Normalizing track $1 of $TRACKS: $TRACKNAME..."
639 run_command normalizetrack-$1 $NORMALIZER $NORMALIZEROPTS "$IN"
641 if [ "$(checkstatus encode-output)" = "loud" ]; then
642 echo "HEH! The file we were about to normalize disappeared:"
645 run_command normalizetrack-$1 false "File $IN was not found"
649 # do_move [tracknumber]
650 # Deduces the outfile from environment variables
651 # Creates directory if necessary
653 # TRACKNUM, TRACKNAME, TRACKARTIST, DALBUM, OUTPUTFORMAT, CDGENRE, CDYEAR
656 for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
658 # Create ALBUMFILE, ARTISTFILE, TRACKFILE
659 # Munge filenames as follows:
664 # Eat control characters
665 ALBUMFILE=$(mungefilename "$DALBUM")
666 ARTISTFILE=$(mungefilename "$TRACKARTIST")
667 TRACKFILE=$(mungefilename "$TRACKNAME")
668 GENRE=$(mungegenre "$GENRE")
670 # If we want to start the tracks with a given number, we need to modify the
671 # TRACKNUM value before evaluation
672 if [ -n "$STARTTRACKNUMBER" ] ; then
673 # Get the trackpadding from the current track
674 CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
675 TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - 1 ))
677 TRACKNUM=${UTRACKNUM}
679 # Supported variables for OUTPUTFORMAT are GENRE, ALBUMFILE, ARTISTFILE,
680 # TRACKFILE, and TRACKNUM.
681 if [ "$VARIOUSARTISTS" = "y" ]; then
682 OUTPUTFILE=$(eval echo $VAOUTPUTFORMAT)
684 OUTPUTFILE=$(eval echo $OUTPUTFORMAT)
686 # Check that the directory for OUTPUTFILE exists, if it doesn't, create it
687 OUTPUTFILEDIR=$(dirname "$OUTPUTDIR/$OUTPUTFILE")
688 # mkdir -p shouldn't return an error if the directory already exists
689 mkdir -p "$OUTPUTFILEDIR"
690 run_command movetrack-$1 mv "$ABCDETEMPDIR/track$1.$OUTPUT" "$OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
695 # Create the playlist if wanted
697 # PLAYLISTFORMAT, PLAYLISTDATAPREFIX, VAPLAYLISTFORMAT, VAPLAYLISTDATAPREFIX,
698 # VARIOUSARTISTS, OUTPUTDIR
701 for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
703 # Create a playlist file for the playlist data to go into.
704 # We used to wipe it out if it existed. Now we request permision if interactive.
705 for LASTTRACK in $TRACKQUEUE; do :; done
706 ALBUMFILE=$(mungefilename "$DALBUM")
707 ARTISTFILE=$(mungefilename "$DARTIST")
708 GENRE=$(mungegenre "$GENRE")
709 if [ "$VARIOUSARTISTS" = "y" ] ; then
710 PLAYLISTFILE=$(eval echo $VAPLAYLISTFORMAT)
712 PLAYLISTFILE=$(eval echo $PLAYLISTFORMAT)
714 FINALPLAYLISTDIR=$(dirname "$OUTPUTDIR/$PLAYLISTFILE")
715 mkdir -p "$FINALPLAYLISTDIR"
716 if [ -s "$OUTPUTDIR/$PLAYLISTFILE" ]; then
717 #echo -n "Erase any existing playlist file? [y/n] (y): " >&2
718 echo -n "Erase, Append to, or Keep the existing playlist file? [e/a/k] (e): " >&2
719 if [ "$INTERACTIVE" = "y" ]; then
720 while [ "$DONE" != "y" ]; do
722 case $ERASEPLAYLIST in
723 e|E|a|A|k|K) DONE=y ;;
731 # Once we erase the playlist, we use append to create the new one.
732 [ "$ERASEPLAYLIST" = "e" -o "$ERASEPLAYLIST" = "E" ] && rm -f "$OUTPUTDIR/$PLAYLISTFILE" && ERASEPLAYLIST=a
734 # The playlist does not exist, so we can safelly use append to create the new list
737 if [ "$ERASEPLAYLIST" = "a" -o "$ERASEPLAYLIST" = "A" ]; then
738 touch "$OUTPUTDIR/$PLAYLISTFILE"
739 for UTRACKNUM in $TRACKQUEUE
741 # Shares some code with do_move since the filenames have to match
742 CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
743 TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -n 1 | cut -f2 -d= | tr -d \[:cntrl:\])
745 TRACKFILE=$(mungefilename "$TRACKNAME")
746 ARTISTFILE=$(mungefilename "$TRACKARTIST")
747 # If we want to start the tracks with a given number, we need to modify the
748 # TRACKNUM value before evaluation
749 if [ -n $STARTTRACKNUMBER ] ; then
750 # Get the trackpadding from the current track
751 CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
752 TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - 1 ))
754 TRACKNUM=${UTRACKNUM}
756 if [ "$VARIOUSARTISTS" = "y" ]; then
757 OUTPUTFILE=$(eval echo $VAOUTPUTFORMAT)
759 OUTPUTFILE=$(eval echo $OUTPUTFORMAT)
761 if [ "$VARIOUSARTISTS" = "y" ]; then
762 if [ "$VAPLAYLISTDATAPREFIX" ] ; then
763 echo ${VAPLAYLISTDATAPREFIX}$OUTPUTFILE.$OUTPUT >> "$OUTPUTDIR/$PLAYLISTFILE"
765 relpath "$PLAYLISTFILE", "$OUTPUTFILE.$OUTPUT" >> "$OUTPUTDIR/$PLAYLISTFILE"
768 if [ "$PLAYLISTDATAPREFIX" ]; then
769 echo ${PLAYLISTDATAPREFIX}$OUTPUTFILE.$OUTPUT >> "$OUTPUTDIR/$PLAYLISTFILE"
771 relpath "$PLAYLISTFILE", "$OUTPUTFILE.$OUTPUT" >> "$OUTPUTDIR/$PLAYLISTFILE"
776 ## this will convert the playlist to have CRLF line-endings, if specified
777 ## (some hardware players insist on CRLF endings)
778 if [ "$DOSPLAYLIST" = "y" ]; then
779 awk '{substr("\r",""); printf "%s\r\n", $0}' "$OUTPUTDIR/$PLAYLISTFILE" > "$ABCDETEMPDIR/PLAYLISTFILE.tmp"
780 # mv -f "$ABCDETEMPDIR/PLAYLISTFILE.tmp" "$OUTPUTDIR/$PLAYLISTFILE"
781 cat "$ABCDETEMPDIR/PLAYLISTFILE.tmp" | sed 's/\//\\/' > "$OUTPUTDIR/$PLAYLISTFILE"
783 echo "playlistcomplete" >> "$ABCDETEMPDIR/status"
788 # This essentially the start of things
791 # Query the CD to get the track info, unless the user specified -C
792 # or we are using some actions which do not need the CDDB data at all
793 #if [ ! X"$EXPACTIONS" = "X" ]; then
795 #elif [ -z "$DISCID" ]; then
796 if [ -z "$DISCID" ]; then
797 vecho -n "Getting CD track info... "
798 TRACKINFO=$($CDDISCID $CDROM)
799 # Make sure there's a CD in there by checking cd-discid's return code
800 if [ "$?" = "1" ]; then
801 echo "abcde error: CD could not be read. Perhaps there's no CD in the drive?" >&2
806 TRACKINFO=$(cat "$WAVOUTPUTDIR/abcde.$DISCID/discid")
809 # Get a full enumeration of tracks, sort it, and put it in the TRACKQUEUE.
810 # This needs to be done now because a section of the resuming code will need
813 # get the number of digits to pad TRACKNUM with - we'll use this later
814 # a CD can only hold 99 tracks, but since we support a feature for starting
815 # numbering the tracks from a given number, we might need to set it as a
816 # variable for the user to define... or obtain it somehow.
817 if [ "$PADTRACKS" = "y" ] ; then
821 ABCDETEMPDIR="$WAVOUTPUTDIR/abcde.$(echo $TRACKINFO | cut -f1 -d' ')"
822 if [ -z "$TRACKQUEUE" ]; then
823 if [ ! "$STRIPDATATRACKS" = "y" ]; then
824 case "$CDROMREADERSYNTAX" in
826 if [ "$WEHAVEACD" = "y" ]; then
827 vecho "Querying the CD for audio tracks..."
828 TRACKS=$( $CDROMREADER -Q 2>&1 | egrep '^[[:space:]]+[[:digit:]]' | tail -n 1 | awk '{print $1}' | tr -d "." | tr '\n' ' ' )
829 CDPARANOIAAUDIOTRACKS="$TRACKS"
831 if [ -f "$ABCDETEMPDIR/status" ] && checkstatus cdparanoia-audio-tracks ; then
832 TRACKS=$( cat $ABCDETEMPDIR/cdparanoia-audio-tracks )
834 TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
838 *) TRACKS=$(echo $TRACKINFO | cut -f2 -d' ') ;;
841 TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
843 echo -n "Grabbing entire CD - tracks: "
844 if [ ! "$PADTRACKS" = "y" ] ; then
845 TRACKNUMPADDING=$(echo -n $TRACKS | wc -c | tr -d ' ')
847 TRACKS=$(printf "%0.${TRACKNUMPADDING}d" $TRACKS)
849 while [ "$X" -ne "$TRACKS" ]
851 X=$(printf "%0.${TRACKNUMPADDING}d" $(expr $X + 1))
852 TRACKQUEUE=$(echo "$TRACKQUEUE" $X)
856 TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
857 # User-supplied track queue.
858 # Weed out non-numbers, whitespace, then sort and weed out duplicates
859 TRACKQUEUE=$(echo $TRACKQUEUE | sed 's-[^0-9 ]--g' | tr ' ' '\n' | grep -v ^$ | sort -n | uniq | tr '\n' ' ' | sed 's- $--g')
860 # Once cleaned, obtain the highest value in the trackqueue for number padding
861 for LASTTRACK in $TRACKQUEUE; do :; done
862 if [ ! "$PADTRACKS" = "y" ] ; then
863 TRACKNUMPADDING=$(echo -n $LASTTRACK | wc -c | tr -d ' ')
865 # Now we normalize the trackqueue
866 for TRACK in $TRACKQUEUE ; do
867 TRACKNUM=$(printf %0.${TRACKNUMPADDING}d $(expr ${TRACK} + 0 ))
868 PADTRACKQUEUE=$(echo $PADTRACKQUEUE $TRACKNUM)
870 TRACKQUEUE=$PADTRACKQUEUE
871 echo Grabbing tracks: "$TRACKQUEUE"
874 # for LASTTRACK in $TRACKQUEUE; do :; done
876 QUEUEDTRACKS=$(echo $TRACKQUEUE | wc -w | tr -d ' ')
878 # We have the discid, create a temp directory after it to store all the temp
881 if [ -e "$ABCDETEMPDIR" ]; then
882 echo -n "abcde: attempting to resume from $ABCDETEMPDIR"
883 # It already exists, see if it's a directory
884 if [ ! -d "$ABCDETEMPDIR" ]; then
885 # This is a file/socket/fifo/device/etc, not a directory
888 echo "abcde: file $ABCDETEMPDIR already exists and does not belong to abcde." >&2
889 echo "Please investigate, remove it, and rerun abcde." >&2
893 # It's a directory, let's see if it's owned by us
894 if [ ! -O "$ABCDETEMPDIR" ]; then
895 # Nope, complain and exit
897 echo "abcde: directory $ABCDETEMPDIR already exists and is not owned by you." >&2
898 echo "Please investigate, remove it, and rerun abcde." >&2
902 # See if it's populated
903 if [ ! -f "$ABCDETEMPDIR/discid" ]; then
904 # Wipe and start fresh
905 echo "abcde: $ABCDETEMPDIR/discid not found. Abcde must remove and recreate" >&2
906 echo -n "this directory to continue. Continue? [y/n] (n)" >&2
907 if [ "$INTERACTIVE" = "y" ]; then
913 if [ "$ANSWER" != "y" ]; then
916 rm -rf "$ABCDETEMPDIR" || exit 1
917 mkdir "$ABCDETEMPDIR"
918 if [ "$?" -gt "0" ]; then
919 # Directory already exists or could not be created
920 echo "abcde: Temp directory $ABCDETEMPDIR could not be created." >&2
924 # Everything is fine. Check for ^encodetracklocation-
925 # and encode-output entries in the status file and
926 # remove them. These are not relevant across sessions.
927 if [ -f "$ABCDETEMPDIR/status" ]; then
928 mv "$ABCDETEMPDIR/status" "$ABCDETEMPDIR/status.old"
929 grep -v ^encodetracklocation- < "$ABCDETEMPDIR/status.old" \
930 | grep -v ^encode-output > "$ABCDETEMPDIR/status"
932 # Remove old error messages
933 if [ -f "$ABCDETEMPDIR/errors" ]; then
934 rm -f "$ABCDETEMPDIR/errors"
938 # We are starting from scratch
939 mkdir "$ABCDETEMPDIR"
940 if [ "$?" -gt "0" ]; then
941 # Directory already exists or could not be created
942 echo "abcde: Temp directory $ABCDETEMPDIR could not be created." >&2
945 cat /dev/null > "$ABCDETEMPDIR/status"
948 # If we got the CDPARANOIA status and it is not recorded, save it now
949 ## FIXME ## ! is non-portable
950 if [ -n "$CDPARANOIAAUDIOTRACKS" ] && ! checkstatus cdparanoia-audio-tracks; then
951 if echo "$CDPARANOIAAUDIOTRACKS" >> "$ABCDETEMPDIR/cdparanoia-audio-tracks"; then
952 echo "cdparanoia-audio-tracks" >> "$ABCDETEMPDIR/status"
956 # Create the discid file
957 echo "$TRACKINFO" > "$ABCDETEMPDIR/discid"
959 # Determine what actions are to be done from $ACTIONS and set the
960 # following environment variables for them:
972 for ACTION in $(echo $ACTIONS | tr , \ )
977 normalize) DONORMALIZE=y; DOREAD=y;;
978 preprocess) DOPREPROCESS=y; DOREAD=y;;
979 encode) DOENCODE=y; DOREAD=y;;
980 postprocess) DOPREPROCESS=y; DOENCODE=y; DOREAD=y;;
981 tag) DOTAG=y; DOREAD=y; DOENCODE=y; DOCDDB=y;;
982 move) DOMOVE=y; DOTAG=y; DOREAD=y; DOENCODE=y; DOCDDB=y;;
983 playlist) DOCDDB=y; DOPLAYLIST=y;;
990 # Parses a CDDB file and outputs the title and the track names.
991 # Variables: CDDBFILE
995 # List out disc title/author and contents
996 if [ "$ONETRACK" = "y" ]; then
997 vecho "ONETRACK mode selected: displaying only the title of the CD..."
999 echo "---- $(grep DTITLE "${CDDBPARSEFILE}" | cut '-d=' -f2- | tr -d \\r\\n ) ----"
1000 if [ ! "$ONETRACK" = "y" ]; then
1001 for TRACK in $(f_seq_row 1 $TRACKS)
1003 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "${CDDBPARSEFILE}" | cut -f2- -d= | tr -d \\r\\n)"
1009 # Check for a local CDDB file, and report success
1012 if checkstatus cddb-readcomplete && checkstatus cddb-choice >/dev/null; then :; else
1014 CDDBLOCALSUCCESS="n"
1015 CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
1016 CDDBLOCALFILE="${CDDBLOCALDIR}/${CDDBDISCID}"
1019 # If the user has selected to check a local CDDB repo, we proceed with it
1020 if [ -r "${CDDBLOCALFILE}" ]; then
1021 # List out disc title/author and contents
1022 do_cddbparse "${CDDBLOCALFILE}"
1023 echo -n "Locally cached CDDB entry found, use it? [y/n] (y): "
1024 if [ "$INTERACTIVE" = "y" ]; then
1026 while [ "$USELOCALRESP" != "y" ] && [ "$USELOCALRESP" != "n" ] && [ "$USELOCALRESP" != "" ] ; do
1027 echo -n 'Invalid selection. Please answer "y" or "n": '
1030 [ x"$USELOCALRESP" = "x" ] && USELOCALRESP="y"
1034 if [ "$USELOCALRESP" = "y" ]; then
1035 #echo "Using local copy of CDDB data"
1036 cp "${CDDBLOCALFILE}" "$ABCDETEMPDIR/cddbread.1"
1037 echo 999 > "$ABCDETEMPDIR/cddbquery" # Assuming 999 isn't used by CDDB
1038 echo cddb-readcomplete >> "$ABCDETEMPDIR/status"
1039 do_cddbparse "${CDDBLOCALFILE}" > "$ABCDETEMPDIR/cddbchoices"
1040 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
1041 CDDBLOCALSUCCESS="y"
1043 #echo "Not using local copy of CDDB data"
1044 CDDBLOCALSUCCESS="n"
1046 elif [ "$FORCECDDBUSELOCAL"= "y" ]; then
1047 $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.0"
1048 echo 998 > "$ABCDETEMPDIR/cddbquery" # Assuming 998 isn't used by CDDB
1049 echo cddb-readcomplete >> "$ABCDETEMPDIR/status"
1050 do_cddbparse "${CDDBLOCALFILE}" > "$ABCDETEMPDIR/cddbchoices"
1051 echo cddb-choice=0 >> "$ABCDETEMPDIR/status"
1052 CDDBLOCALSUCCESS="y"
1054 CDDBLOCALSUCCESS="n"
1062 # Perform CDDB protocol version check if it hasn't already been done
1063 if checkstatus cddb-statcomplete; then :; else
1064 if [ "$CDDBAVAIL" = "n" ]; then
1066 echo 503 > "$ABCDETEMPDIR/cddbstat"
1069 CDDBUSER=$(echo $HELLOINFO | cut -f1 -d'@')
1070 CDDBHOST=$(echo $HELLOINFO | cut -f2- -d'@')
1071 while test $rc -eq 1 -a $CDDBPROTO -ge 3; do
1072 vecho "Checking CDDB server status..."
1073 $CDDBTOOL stat $CDDBURL $CDDBUSER $CDDBHOST $CDDBPROTO > "$ABCDETEMPDIR/cddbstat"
1074 RESPONSECODE=$(head -n 1 "$ABCDETEMPDIR/cddbstat" | cut -f1 -d' ')
1075 case "$RESPONSECODE" in
1076 210) # 210 OK, status information follows (until terminating `.')
1079 501|*) # 501 Illegal CDDB protocol level: <n>.
1080 CDDBPROTO=`expr $CDDBPROTO - 1`
1084 if test $rc -eq 1; then
1088 echo cddb-statcomplete >> "$ABCDETEMPDIR/status"
1096 CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
1097 CDDBLOCALFILE="${CDDBLOCALDIR}/${CDDBDISCID}"
1099 # Perform CDDB query if it hasn't already been done
1100 if checkstatus cddb-querycomplete; then :; else
1101 if [ "$CDDBAVAIL" = "n" ]; then
1103 echo 503 > "$ABCDETEMPDIR/cddbquery"
1104 # The default CDDBLOCALSUCCESS is "n"
1105 # This part will be triggered if the user CDDB repo does not
1106 # contain the entry, or if we are not trying to use the repo.
1108 vecho "Querying the CDDB server..."
1109 CDDBUSER=$(echo $HELLOINFO | cut -f1 -d'@')
1110 CDDBHOST=$(echo $HELLOINFO | cut -f2- -d'@')
1111 $CDDBTOOL query $CDDBURL $CDDBPROTO $CDDBUSER $CDDBHOST $TRACKINFO > "$ABCDETEMPDIR/cddbquery"
1117 # no match found in database,
1118 # wget/fetch error, or user requested not to use CDDB
1119 # Make up an error code (503) that abcde
1120 # will recognize in do_cddbread
1121 # and compensate by making a template
1122 echo 503 > "$ABCDETEMPDIR/cddbquery"
1124 *) # strange and unknown error
1125 echo ERRORCODE=$ERRORCODE
1126 echo "abcde: $CDDBTOOL returned unknown error code"
1130 echo cddb-querycomplete >> "$ABCDETEMPDIR/status"
1137 # If it's not to be used, generate a template.
1138 # Then, display it (or them) and let the user choose/edit it
1139 if checkstatus cddb-readcomplete; then :; else
1140 vecho "Obtaining CDDB results..."
1141 # If CDDB is to be used, interpret the query results and read all
1142 # the available entries.
1143 rm -f "$ABCDETEMPDIR/cddbchoices"
1144 CDDBCHOICES=1 # Overridden by multiple matches
1145 RESPONSECODE=$(head -n 1 "$ABCDETEMPDIR/cddbquery" | cut -f1 -d' ')
1146 case "$RESPONSECODE" in
1148 # One exact match, retrieve it
1149 # 200 [section] [discid] [artist] / [title]
1150 if checkstatus cddb-read-1-complete; then :; else
1151 echo -n "Retrieving 1 CDDB match..." >> "$ABCDETEMPDIR/cddbchoices"
1152 $CDDBTOOL read $CDDBURL $CDDBPROTO $CDDBUSER $CDDBHOST $(cut -f2,3 -d' ' "$ABCDETEMPDIR/cddbquery") > "$ABCDETEMPDIR/cddbread.1"
1153 echo "done." >> "$ABCDETEMPDIR/cddbchoices"
1154 echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
1155 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
1157 # List out disc title/author and contents
1158 echo ---- "$(cut '-d ' -f4- "$ABCDETEMPDIR/cddbquery")" ---- >> "$ABCDETEMPDIR/cddbchoices"
1159 for TRACK in $(f_seq_row 1 $TRACKS)
1161 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.1" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
1163 echo >> "$ABCDETEMPDIR/cddbchoices"
1167 case "$RESPONSECODE" in
1168 202) echo "No CDDB match." >> "$ABCDETEMPDIR/cddbchoices" ;;
1169 403|409) echo "CDDB entry is corrupt, or the handshake failed." >> "$ABCDETEMPDIR/cddbchoices" ;;
1170 503) echo "CDDB unavailable." >> "$ABCDETEMPDIR/cddbchoices" ;;
1172 $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.0"
1173 # List out disc title/author and contents of template
1174 echo ---- Unknown Artist / Unknown Album ---- >> "$ABCDETEMPDIR/cddbchoices"
1176 for TRACK in $(f_seq_row 1 $TRACKS)
1178 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.0" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
1180 echo >> "$ABCDETEMPDIR/cddbchoices"
1181 echo cddb-read-0-complete >> "$ABCDETEMPDIR/status"
1182 echo cddb-choice=0 >> "$ABCDETEMPDIR/status"
1185 # Multiple exact, (possibly multiple) inexact matches
1187 if [ "$RESPONSECODE" = "211" ]; then IN=in; fi
1188 if [ "$(wc -l < $ABCDETEMPDIR/cddbquery | tr -d ' ')" -eq 3 ]; then
1189 echo "One ${IN}exact match:" >> "$ABCDETEMPDIR/cddbchoices"
1190 tail -n +2 "$ABCDETEMPDIR/cddbquery" | head -n 1 >> "$ABCDETEMPDIR/cddbchoices"
1191 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
1193 echo "Multiple ${IN}exact matches:" >> "$ABCDETEMPDIR/cddbchoices"
1195 vecho -n "Retrieving multiple matches... "
1196 grep -v ^[.]$ "$ABCDETEMPDIR/cddbquery" | ( X=0
1197 read DISCINFO # eat top line
1201 if checkstatus cddb-read-$X-complete; then :; else
1202 $CDDBTOOL read $CDDBURL $CDDBPROTO $CDDBUSER $CDDBHOST $(echo $DISCINFO | cut -f1,2 -d' ') > "$ABCDETEMPDIR/cddbread.$X"
1203 echo cddb-read-$X-complete >> "$ABCDETEMPDIR/status"
1205 # List out disc title/author and contents
1206 echo \#$X: ---- "$DISCINFO" ---- >> "$ABCDETEMPDIR/cddbchoices"
1207 for TRACK in $(f_seq_row 1 $TRACKS)
1209 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.$X" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
1211 echo >> "$ABCDETEMPDIR/cddbchoices"
1214 CDDBCHOICES=$(expr $(cat "$ABCDETEMPDIR/cddbquery" | wc -l) - 2)
1218 for TRACK in $(f_seq_row 1 $TRACKS)
1220 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.1" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
1222 echo >> "$ABCDETEMPDIR/cddbchoices"
1223 echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
1224 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
1227 echo "cddb-readcomplete" >> "$ABCDETEMPDIR/status"
1234 if checkstatus cddb-edit >/dev/null; then
1235 CDDBDATA="$ABCDETEMPDIR/cddbread.$(checkstatus cddb-choice)"
1236 VARIOUSARTISTS="$(checkstatus variousartists)"
1237 VARIOUSARTISTSTYLE="$(checkstatus variousartiststyle)"
1240 if [ "$INTERACTIVE" = "y" ]; then
1241 # We should show the CDDB results both when we are not using the local CDDB repo
1242 # or when we are using it but we could not find a proper match
1243 if [ "$CDDBUSELOCAL" = "y" ] && [ ! "$CDDBLOCALSUCCESS" = "y" ] || [ ! "$CDDBUSELOCAL" = "y" ]; then
1244 # Display the $ABCDETEMPDIR/cddbchoices file created above
1245 # Pick a pager so that if the tracks overflow the screen the user can still view everything
1246 if [ -r "$ABCDETEMPDIR/cddbchoices" ]; then
1247 CDDBCHOICES=$(expr $(cat "$ABCDETEMPDIR/cddbquery" | wc -l) - 2)
1248 CHOICE=$(checkstatus cddb-choice)
1249 if [ -n "$CHOICE" ] ; then
1250 case $CDDBCHOICES in
1251 1) cat "$ABCDETEMPDIR/cddbchoices" ;;
1253 echo "Selected: #$CHOICE"
1254 do_cddbparse "$ABCDETEMPDIR/cddbread.$CHOICE"
1258 # The user has a choice to make, display the info in a pager if necessary
1259 if [ $(cat "$ABCDETEMPDIR/cddbchoices" | wc -l) -ge 24 ]; then
1260 # Use the debian sensible-pager wrapper to pick the pager
1261 # user has requested via their $PAGER environment variable
1262 if [ -x "/usr/bin/sensible-pager" ]; then
1263 /usr/bin/sensible-pager "$ABCDETEMPDIR/cddbchoices"
1264 elif [ -x "$PAGER" ]; then
1265 # That failed, try to load the preferred editor, starting
1266 # with their PAGER variable
1267 $PAGER "$ABCDETEMPDIR/cddbchoices"
1268 # If that fails, check for less
1269 elif [ -x /usr/bin/less ]; then
1270 /usr/bin/less -f "$ABCDETEMPDIR/cddbchoices"
1271 # more should be on all UNIX systems
1272 elif [ -x /bin/more ]; then
1273 /bin/more "$ABCDETEMPDIR/cddbchoices"
1275 # No bananas, just cat the thing
1276 cat "$ABCDETEMPDIR/cddbchoices" >&2
1279 # It's all going to fit in one page, cat it
1280 cat "$ABCDETEMPDIR/cddbchoices" >&2
1283 # I'll take CDDB read #3 for $400, Alex
1284 echo -n "Which entry would you like abcde to use (0 for none)? [0-$CDDBCHOICES]: " >&2
1286 # Make sure we get a valid choice
1287 CDCHOICENUM=$(echo $CDDBCHOICE | xargs printf %d 2>/dev/null)
1288 while [ $CDCHOICENUM -lt 0 ] || [ $CDCHOICENUM -gt $CDDBCHOICES ]; do
1289 echo "Invalid selection. Please choose a number between 1 and $CDDBCHOICES." >&2
1290 echo -n "Selection [0-$CDDBCHOICES]: " >&2
1292 CDCHOICENUM=$(echo $CDDBCHOICE | xargs printf %d 2>/dev/null)
1294 if [ "$CDCHOICENUM" = "0" ]; then
1295 vecho "Creating empty CDDB template..."
1297 $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > $ABCDETEMPDIR/cddbread.0
1299 echo "Selected: #$CDCHOICENUM ($(grep ^DTITLE= $ABCDETEMPDIR/cddbread.$CDCHOICENUM | cut -f2- -d= | tr -d \\r\\n))" >&2
1300 do_cddbparse "$ABCDETEMPDIR/cddbread.$CDCHOICENUM"
1302 echo "cddb-choice=$CDCHOICENUM" >> "$ABCDETEMPDIR/status"
1306 # We need some code to show the selected option when local repository is selected and we have found a match
1307 vecho "Using cached CDDB match..." >&2
1308 # Display the $ABCDETEMPDIR/cddbchoices file created above
1309 # Pick a pager so that if the tracks overflow the screen the user can still view everything
1310 if [ -r "$ABCDETEMPDIR/cddbchoices" ]; then
1311 CDDBCHOICES=$(expr $(cat "$ABCDETEMPDIR/cddbquery" | wc -l) - 2)
1312 CHOICE=$(checkstatus cddb-choice)
1313 if [ "$USELOCALRESP" = "y" ]; then :; else
1314 if [ -n "$CHOICE" ] ; then
1315 case $CDDBCHOICES in
1318 echo "Selected template."
1320 1) cat "$ABCDETEMPDIR/cddbchoices" ;;
1322 echo "Selected: #$CHOICE"
1323 do_cddbparse "$ABCDETEMPDIR/cddbread.$CHOICE"
1331 # We're noninteractive - pick the first choice.
1332 # But in case we run a previous instance and selected a choice, use it.
1333 if [ -r "$ABCDETEMPDIR/cddbchoices" ]; then
1334 # Show the choice if we are not using the locally stored one
1335 # or when the local search failed to find a match.
1336 PREVIOUSCHOICE=$(checkstatus cddb-choice)
1337 if [ "$CDDBUSELOCAL" = "y" ] && [ "$CDDBLOCALSUCCESS" = "n" ] || [ ! "$CDDBUSELOCAL" = "y" ]; then
1338 #if [ "$PREVIOUSCHOICE" ]; then
1339 cat "$ABCDETEMPDIR/cddbchoices"
1342 if [ ! -z "$PREVIOUSCHOICE" ] ; then
1343 CDCHOICENUM=$PREVIOUSCHOICE
1346 echo "cddb-choice=$CDCHOICENUM" >> "$ABCDETEMPDIR/status"
1348 echo "Selected: #$CDCHOICENUM ($(grep ^DTITLE= $ABCDETEMPDIR/cddbread.$CDCHOICENUM | cut -f2- -d= | tr -d \\r\\n))" >&2
1353 if checkstatus cddb-choice >/dev/null; then :; else
1354 echo "abcde: internal error: cddb-choice not recorded." >&2
1357 CDDBDATA="$ABCDETEMPDIR/cddbread.$(checkstatus cddb-choice)"
1358 echo -n "Edit selected CDDB data? [y/n] (" >&2
1359 if [ "$INTERACTIVE" = "y" ]; then
1360 if [ "$UNKNOWNDISK" = "y" ]; then
1363 [ "$EDITCDDB" != "n" ] && EDITCDDB=y
1372 if [ "$EDITCDDB" = "y" ]; then
1373 CDDBDATAMD5SUM=$($MD5SUM "$CDDBDATA" | cut -d " " -f 1);
1375 # Use the debian sensible-editor wrapper to pick the editor that the
1376 # user has requested via their $EDITOR environment variable
1377 if [ -x "/usr/bin/sensible-editor" ]; then
1378 /usr/bin/sensible-editor "$CDDBDATA"
1379 elif [ -n "$EDITOR" ]; then
1380 if [ -x $(which "${EDITOR%%\ *}") ]; then
1381 # That failed, try to load the preferred editor, starting
1382 # with their EDITOR variable
1383 eval $(echo "$EDITOR") "$CDDBDATA"
1385 # If that fails, check for a vi
1386 elif [ -x /usr/bin/vi ]; then
1387 /usr/bin/vi "$CDDBDATA"
1388 # nano should be on all (modern, i.e., sarge) debian systems
1389 elif [ -x /usr/bin/nano ]; then
1390 /usr/bin/nano "$CDDBDATA"
1391 # mg should be on all OpenBSD systems
1392 elif [ -x /usr/bin/mg ]; then
1393 /usr/bin/mg "$CDDBDATA"
1396 echo "No editor available. Check your EDITOR environment variable." >&2
1398 # delete editor backup file if it exists
1399 if [ -w "$CDDBDATA~" ]; then
1404 # Some heuristics first. Look at Disc Title, and if it starts with
1405 # "Various", then we'll assume Various Artists
1406 if [ "$(grep ^DTITLE= "$CDDBDATA" | cut -f2 -d= | egrep -ci '^(various|soundtrack|varios|sonora|ost)')" != "0" ]; then
1407 echo "Looks like a Multi-Artist CD" >&2
1410 echo -n "Is the CD multi-artist? [y/n] (n): " >&2
1411 if [ "$INTERACTIVE" = "y" ]; then
1418 if [ "$VARIOUSARTISTS" = "y" ] && [ ! "$ONETRACK" = "y" ]; then
1421 # Need NUMTRACKS before cddb-tool will return it:
1422 NUMTRACKS=$(egrep '^TTITLE[0-9]+=' "$CDDBDATA" | wc -l)
1423 if [ "$(grep -c "^TTITLE.*\/" "$CDDBDATA")" -gt "$(expr $NUMTRACKS / 2 )" ]; then
1424 # More than 1/2 tracks contain a "/", so guess forward
1426 elif [ "$(grep -c "^TTITLE.*\-" "$CDDBDATA")" -gt "$(expr $NUMTRACKS / 2 )" ]; then
1427 # More than 1/2 contain a "-", so guess forward-dash
1429 elif [ "$(grep -c "^TTITLE.*(.*)" "$CDDBDATA")" -gt "$(expr $NUMTRACKS / 2 )" ]; then
1430 # More than 1/2 contain something in parens, so guess trailing-paren
1434 echo "1) Artist / Title" >&2
1435 echo "2) Artist - Title" >&2
1436 echo "3) Title / Artist" >&2
1437 echo "4) Title - Artist" >&2
1438 echo "5) Artist: Title" >&2
1439 echo "6) Title (Artist)" >&2
1440 echo "7) This is a single-artist CD" >&2
1441 echo -n "Which style of multiple artist entries is it? [1-7] ($DEFAULTSTYLE): " >&2
1442 if [ "$INTERACTIVE" = "y" ]; then
1443 read VARIOUSARTISTSTYLE
1445 echo $DEFAULTSTYLE >&2
1446 VARIOUSARTISTSTYLE=$DEFAULTSTYLE
1448 VARIOUSARTISTSTYLE=$(echo 0$VARIOUSARTISTSTYLE | xargs printf %d)
1449 # If they press Enter, then the default style (0) was chosen
1450 while [ $VARIOUSARTISTSTYLE -lt 0 ] || [ $VARIOUSARTISTSTYLE -gt 7 ]; do
1451 echo "Invalid selection. Please choose a number between 1 and 7."
1452 echo -n "Selection [1-7]: "
1453 read VARIOUSARTISTSTYLE
1454 VARIOUSARTISTSTYLE=$(echo 0$VARIOUSARTISTSTYLE | xargs printf %d)
1456 if [ "$VARIOUSARTISTSTYLE" = "0" ]; then
1457 VARIOUSARTISTSTYLE=$DEFAULTSTYLE
1459 vecho "Selected: $VARIOUSARTISTSTYLE"
1460 case "$VARIOUSARTISTSTYLE" in
1462 VARIOUSARTISTSTYLE=forward
1465 VARIOUSARTISTSTYLE=forward-dash
1468 VARIOUSARTISTSTYLE=reverse
1471 VARIOUSARTISTSTYLE=reverse-dash
1474 VARIOUSARTISTSTYLE=colon
1477 VARIOUSARTISTSTYLE=trailing-paren
1485 echo "variousartists=$VARIOUSARTISTS" >> "$ABCDETEMPDIR/status"
1486 echo "variousartiststyle=$VARIOUSARTISTSTYLE" >> "$ABCDETEMPDIR/status"
1488 if [ "$EDITCDDB" = "y" ] && [ "$UNINTENTIONALLY_ANGER_THE_FREEDB_PEOPLE" = "y" ]; then
1489 if [ $CDDBDATAMD5SUM != "" ] && [ $CDDBDATAMD5SUM != $($MD5SUM "$CDDBDATA" | cut -d " " -f 1) ]; then
1490 # This works but does not have the necessary error checking
1491 # yet. If you are familiar with the CDDB spec
1492 # (see http://www.freedb.org/src/latest/DBFORMAT)
1493 # and can create an error-free entry on your own, then put
1494 # UNINTENTIONALLY_ANGER_THE_FREEDB_PEOPLE=y in your
1495 # abcde.conf to enable it. Put CDDBSUBMIT=email@address in
1496 # your abcde.conf to change the email address submissions are
1499 # submit the modified file, if they want
1500 if [ "$NOSUBMIT" != "y" ]; then
1501 echo -n "Do you want to submit this entry to $CDDBSUBMIT? [y/n] (n): "
1503 while [ "$YESNO" != "y" ] && [ "$YESNO" != "n" ] && [ "$YESNO" != "Y" ] && \
1504 [ "$YESNO" != "N" ] && [ "$YESNO" != "" ]
1506 echo -n 'Invalid selection. Please answer "y" or "n": '
1509 if [ "$YESNO" = "y" ] || [ "$YESNO" = "Y" ]; then
1510 echo -n "Sending..."
1511 $CDDBTOOL send "$CDDBDATA" $CDDBSUBMIT
1517 # Make sure the cache directory exists
1518 mkdir -p $CDDBLOCALDIR
1519 # Cache edited CDDB entry in the user's cddb dir
1520 if [ "$CDDBCOPYLOCAL" = "y" ] || [ "$COPYCDDBLOCAL" = "Y" ]; then
1521 cat "$CDDBDATA" | tail -n $(expr $(cat "$CDDBDATA" | wc -l ) - 1 ) > ${CDDBLOCALDIR}/$(echo "$TRACKINFO" | cut -d' ' -f1)
1524 echo "cddb-edit" >> "$ABCDETEMPDIR/status"
1527 # do_cdread_one [lasttrack] [firsttrack]
1529 # Reads the CD in a single track. Live performances, concerts, mixes,... benefit from this.
1532 # The commands here don't go through run_command because they're never supposed to be silenced
1533 # return codes need to be doublechecked anyway, however
1536 WAVDATA="$ABCDETEMPDIR/track$FIRSTTRACKNUMBER.wav"
1537 echo "Grabbing the CD to a single track..." >&2
1538 case "$CDROMREADERSYNTAX" in
1539 cdparanoia) nice $READNICE $CDROMREADER -d $CDROM "1-" "$WAVDATA" >&2 ;;
1541 if [ "$OSFLAVOUR" = "OSX" ] ; then
1542 # Hei, we have to unmount the device before running anything like cdda2wav in OSX
1543 disktool -u ${CDROM#/dev/} 0
1544 # Also, in OSX the cdrom device for cdda2wav changes...
1545 CDDA2WAVCDROM="IODVDServices"
1546 elif [ "$OSFLAVOUR" = "FBSD" ] ; then
1547 CDDA2WAVCDROM="$CDROMID"
1549 if [ "$CDROMID" = "" ]; then
1550 CDDA2WAVCDROM="$CDROM"
1552 CDDA2WAVCDROM="$CDROMID"
1555 nice $READNICE $CDROMREADER -D $CDDA2WAVCDROM -t 1+$LASTTRACKNUM "$WAVDATA" >&2
1557 dagrab) nice $READNICE $CDROMREADER -d $CDROM -f $WAVDATA -v $UTRACKNUM >&2 ;;
1559 # Find the track's mounted path
1560 REALTRACKNUM=$(expr $UTRACKNUM + 0)
1561 FILEPATH=$(mount | grep "$CDROM on" | sed 's/^[^ ]* on \(.*\) (.*/\1/')
1562 FILEPATH=$(find "$FILEPATH" | grep "/$REALTRACKNUM ");
1563 # If the file exists, copy it
1564 if [ -e "$FILEPATH" ] ; then
1565 nice $READNICE $CDROMREADER "$FILEPATH" "$WAVDATA" >&2
1569 debug) nice $READNICE $CDROMREADER -d $CDROM -w $UTRACKNUM-[:1] "$WAVDATA" >&2 ;;
1572 if [ "$RETURN" != "0" -o ! -s "$WAVDATA" ]; then
1573 # Thank goodness errors is only machine-parseable up to the
1574 # first colon, otherwise this woulda sucked
1575 if [ "$RETURN" = "0" -a ! -s "$WAVDATA" ]; then
1576 RETURN=73 # fake a return code as cdparanoia return 0 also on aborted reads
1578 echo "readtrack-$FIRSTTRACKNUMBER: $CDROMREADER returned code $RETURN" >> "$ABCDETEMPDIR/errors"
1581 echo readtrack-$FIRSTTRACKNUMBER >> "$ABCDETEMPDIR/status"
1585 # do_cdread [tracknumber]
1589 # The commands here don't go through run_command because they're never supposed to be silenced
1590 # return codes need to be doublechecked anyway, however
1592 CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
1593 WAVDATA="$ABCDETEMPDIR/track$UTRACKNUM.wav"
1594 OUTDATA="$ABCDETEMPDIR/track$UTRACKNUM.$OUTPUTTYPE"
1595 if [ -r "$CDDBDATA" ]; then
1596 TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -n 1 | cut -f2 -d= | tr -d \[:cntrl:\])
1597 echo "Grabbing track $UTRACKNUM: $TRACKNAME..." >&2
1599 echo "Grabbing track $UTRACKNUM..." >&2
1601 case "$CDROMREADERSYNTAX" in
1602 cdparanoia) nice $READNICE $CDROMREADER -d $CDROM $UTRACKNUM "$WAVDATA" >&2 ;;
1604 if [ "$OSFLAVOUR" = "OSX" ] ; then
1605 # Hei, we have to unmount the device before running anything like cdda2wav in OSX
1606 disktool -u ${CDROM#/dev/} 0
1607 # Also, in OSX the cdrom device for cdda2wav changes...
1608 CDDA2WAVCDROM="IODVDServices"
1609 elif [ "$OSFLAVOUR" = "FBSD" ] ; then
1610 CDDA2WAVCDROM="$CDROMID"
1612 if [ "$CDROMID" = "" ]; then
1613 CDDA2WAVCDROM="$CDROM"
1615 CDDA2WAVCDROM="$CDROMID"
1618 nice $READNICE $CDROMREADER -D $CDDA2WAVCDROM -t $UTRACKNUM "$WAVDATA" >&2
1620 dagrab) nice $READNICE $CDROMREADER -d $CDROM -f $WAVDATA -v $UTRACKNUM >&2 ;;
1622 # Find the track's mounted path
1623 REALTRACKNUM=$(expr $UTRACKNUM + 0)
1624 FILEPATH=$(mount | grep "$CDROM on" | sed 's/^[^ ]* on \([^(]*\) (.*/\1/')
1625 FILEPATH=$(find "$FILEPATH" | grep "/$REALTRACKNUM ");
1626 # If the file exists, copy it
1627 if [ -e "$FILEPATH" ] ; then
1628 nice $READNICE $CDROMREADER "$FILEPATH" "$WAVDATA" >&2
1632 debug) nice $READNICE $CDROMREADER -d $CDROM -w $UTRACKNUM-[:1] "$WAVDATA" >&2 ;;
1635 if [ "$RETURN" != "0" -o ! -s "$WAVDATA" ]; then
1636 # Thank goodness errors is only machine-parseable up to the
1637 # first colon, otherwise this woulda sucked
1638 if [ "$RETURN" = "0" -a ! -s "$WAVDATA" ]; then
1639 RETURN=73 # fake a return code as cdparanoia return 0 also on aborted reads
1641 echo "readtrack-$UTRACKNUM: $CDROMREADER returned code $RETURN" >> "$ABCDETEMPDIR/errors"
1644 echo readtrack-$UTRACKNUM >> "$ABCDETEMPDIR/status"
1649 # No values accepted, only uses env variables
1652 if "$CDSPEED" "$CDSPEEDOPTS" "$CDSPEEDVALUE" >/dev/null ; then
1653 vecho "Setting CD speed to ${CDSPEEDVALUE}x"
1655 echo "abcde: unable to set the device speed" >&2
1661 # vecho outputs a message if EXTRAVERBOSE is selected
1664 if [ x"$EXTRAVERBOSE" != "x" ]; then
1669 # Start of execution
1672 CDDBURL="http://freedb.freedb.org/~cddb/cddb.cgi"
1673 CDDBSUBMIT=freedb-submit@freedb.org
1675 HELLOINFO="$(whoami)@$(hostname)"
1677 CDROMREADERSYNTAX=cdparanoia
1679 ENCODERSYNTAX=default
1681 MP3ENCODERSYNTAX=default
1682 OGGENCODERSYNTAX=default
1683 FLACENCODERSYNTAX=default
1684 SPEEXENCODERSYNTAX=default
1685 MPPENCODERSYNTAX=default
1686 NORMALIZERSYNTAX=default
1688 OUTPUTFORMAT='${ARTISTFILE}-${ALBUMFILE}/${TRACKNUM}.${TRACKFILE}'
1689 # Use the following VAOUTPUTFORMAT to revert to 2.0.x VA format:
1690 #VAOUTPUTFORMAT=${OUTPUTFORMAT}
1691 VAOUTPUTFORMAT='Various-${ALBUMFILE}/${TRACKNUM}.${ARTISTFILE}-${TRACKFILE}'
1692 ONETRACKOUTPUTFORMAT='${ARTISTFILE}-${ALBUMFILE}/${ALBUMFILE}'
1693 VAONETRACKOUTPUTFORMAT='Various-${ALBUMFILE}/${ALBUMFILE}'
1694 PLAYLISTFORMAT='${ARTISTFILE}-${ALBUMFILE}.${OUTPUT}.m3u'
1695 PLAYLISTDATAPREFIX=''
1696 VAPLAYLISTFORMAT='${ARTISTFILE}-${ALBUMFILE}.${OUTPUT}.m3u'
1697 VAPLAYLISTDATAPREFIX=''
1705 VARIOUSARTISTSTYLE=forward
1709 CDDBLOCALDIR="$HOME/.cddb"
1712 # If using scsi devices, cdda2wav needs a CDROMID, instead of a device node
1713 # i.e. CDROMID="1,0,0"
1716 # program paths - defaults to checking your $PATH
1722 XINGMP3ENC=xingmp3enc
1736 CDPARANOIA=cdparanoia
1745 VORBISCOMMENT=vorbiscomment
1747 NORMALIZE=normalize-audio
1749 VORBISGAIN=vorbisgain
1751 # Options for programs called from abcde
1782 # Default to one process if -j isn't specified
1785 # List of actions to perform - by default, run to completion
1786 ACTIONS=cddb,read,encode,tag,move,clean
1788 # User-redefinable functions
1789 # Custom filename munging:
1792 echo "$@" | sed s,:,\ -,g | tr \ /\* __+ | tr -d \'\"\?\[:cntrl:\]
1795 # Custom genre munging:
1798 echo $CDGENRE | tr "[:upper:]" "[:lower:]"
1802 # Empty pre_read function, to be defined in the configuration file.
1808 # Asume fetch if under FreeBSD. curl is used for Mac OS X. wget is used for Linux/OpenBSD/NetBSD.
1809 # Let's use these checkings to determine the OS flavour, which will be used later
1810 if [ X$(uname) = "XFreeBSD" ] ; then
1814 elif [ X$(uname) = "XDarwin" ] ; then
1817 # We should have disktool in OSX, but let's be sure...
1819 elif [ X$(uname) = "XOpenBSD" ] ; then
1826 # If CDDBAVAIL is set to n, no CDDB read is done
1827 # If USEID3 is set to n, no ID3 tagging is done
1831 if [ -z "$OUTPUTDIR" ]; then
1835 if [ -z "$WAVOUTPUTDIR" ]; then
1836 WAVOUTPUTDIR="$OUTPUTDIR"
1839 # Load system defaults
1840 if [ -r /etc/abcde.conf ]; then
1843 # Load user preference defaults
1844 if [ -r $HOME/.abcde.conf ]; then
1848 # By this time, we need some HTTPGETOPTS already defined.
1849 # If the user has defined its own, we should not be empty.
1851 if [ "$HTTPGETOPTS" = "" ] ; then
1853 wget) HTTPGETOPTS="-q -O -";;
1854 curl) HTTPGETOPTS="-f -s";;
1855 fetch)HTTPGETOPTS="-q -o -";;
1856 *) echo "abcde warning: HTTPGET in non-standard and HTTPGETOPTS are not defined." >&2 ;;
1860 # If the CDROM has not been set yet, find a suitable one.
1861 # If this is a devfs system, default to /dev/cdroms/cdrom0
1862 # instead of /dev/cdrom
1863 if [ "$CDROM" = "" ] ; then
1864 if [ -e /dev/cdroms/cdrom0 ]; then
1865 CDROM=/dev/cdroms/cdrom0
1866 elif [ -e /dev/cdrom ]; then
1868 elif [ -e /dev/cd0c ]; then
1870 elif [ -e /dev/acd0c ]; then
1872 elif [ -e /dev/disk1 ]; then
1877 # Parse command line options
1878 #while getopts 1a:bc:C:d:Dhj:klLnNo:pr:S:t:T:vVx opt ; do
1879 while getopts 1a:A:bc:C:d:Dfhj:klLnNo:pr:Rs:S:vVx opt ; do
1882 a) ACTIONS="$OPTARG" ;;
1883 A) EXPACTIONS="$OPTARG";;
1885 c) if [ -e "$OPTARG" ] ; then . "$OPTARG" ; else echo "abcde error: config file \"$OPTARG\" cannot be found." >&2 ; exit 1 ; fi ;;
1886 C) DISCID="${OPTARG#abcde.}" ;;
1887 d) CDROM="$OPTARG" ;;
1890 f) FORCECDDBUSELOCAL=y ;;
1892 j) MAXPROCS="$OPTARG" ;;
1895 L) CDDBUSELOCAL="y" ;;
1897 N) INTERACTIVE="n" ;;
1899 o) OUTPUTTYPE="$OPTARG" ;;
1901 r) REMOTEHOSTS="$OPTARG" ;;
1903 s) STARTTRACKNUMBER="$OPTARG" ;;
1904 S) CDSPEEDVALUE="$OPTARG" ;;
1905 t) PREPROCESSFORMATS="$OPTARG"
1907 T) POSTPROCESSFORMATS="$OPTARG" ;;
1909 echo "This is abcde v$VERSION."
1910 echo "Usage: abcde [options] [tracks]"
1911 echo "abcde -h for extra help"
1914 V) EXTRAVERBOSE="y" ;;
1920 shift $(($OPTIND - 1))
1922 # Decide if we can continue. TO_REMOVE as soon as we find out about dagrab
1923 if [ "$ONETRACK" = "y" ] ; then
1924 case "$CDROMREADERSYNTAX" in
1925 dagrab|debug) echo "abcde error: ONETRACK reading is not suported with "$CDROMREADERSYNTAX" yet"
1928 if [ "$BATCH" = "y" ]; then
1929 echo "abcde error: BATCH mode is not compatible with ONETRACK mode"
1931 # It does not matter how many tracks we want. In ONETRACK mode we grab them all
1932 if [ $# -gt 0 ]; then
1933 vecho "ONETRACK mode selected: grabbing all tracks..."
1936 while [ $# -gt 0 ]; do
1937 # Range parsing code courtesy of Vincent Ho
1938 RSTART=$(echo $1 | cut -f1 -d-)
1939 REND=$(echo $1 | cut -f2 -d-)
1940 if [ "$RSTART" = "$REND" ]; then
1943 NEWTRACKS=$(f_seq_line $RSTART $REND)
1945 TRACKQUEUE=$(echo "$TRACKQUEUE" "$NEWTRACKS")
1950 # At this point a CDROM has to be defined, so we check it exists.
1951 if [ "$CDROM" != "" ] ; then
1952 if [ "$CDROMREADERSYNTAX" = "cdda2wav" ] && [ "$NEEDCDROMID" = "y" ] ; then
1953 if [ "$OSFLAVOUR" = "FBSD" ]; then
1954 if ! echo "$CDROMID" | grep "^[0-9],[0-9],[0-9]$" >/dev/null 2>&1 ; then
1955 echo "abcde error: CDROMID not in the right format for $CDROMREADERSYNTAX"
1956 echo "Use \"cdrecord -scanbus\" to obtain a adecuate ID an set CDROMID accordingly"
1960 elif [ ! -e $CDROM ] ; then
1961 echo "abcde error: CDROM device cannot be found." >&2
1965 echo "abcde error: CDROM has not been defined or cannot be found" >&2
1969 # Decide which CDROM reader we're gonna use
1970 case "$CDROMREADERSYNTAX" in
1972 CDROMREADER="$CDPARANOIA"
1973 CDROMREADEROPTS="$CDPARANOIAOPTS"
1976 CDROMREADER="$CDDA2WAV"
1977 CDROMREADEROPTS="$CDDA2WAVOPTS"
1980 CDROMREADER="$DAGRAB"
1981 CDROMREADEROPTS="$DAGRABOPTS"
1984 CDROMREADER="$CDDAFS"
1985 CDROMREADEROPTS="$CDDAFSOPTS"
1989 # There's only one normalize...
1990 case "$NORMALIZERSYNTAX" in
1992 NORMALIZER="$NORMALIZE"
1993 NORMALIZEROPTS="$NORMALIZEOPTS"
1997 # If nothing has been specified, use oggenc for oggs and lame for mp3s and flac for flacs and speexenc for speex and mppenc for mpps
1999 # Getting ready for multiple output changes
2000 for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
2003 ogg) [ "$OGGENCODERSYNTAX" = "default" ] && OGGENCODERSYNTAX=oggenc
2004 echo $ACTIONS | grep tag > /dev/null 2>&1 && NEEDCOMMENTER=y
2005 if [ "$REPLAYGAIN" = "y" ]; then NEEDVORBISGAIN=y; fi
2007 mp3) [ "$MP3ENCODERSYNTAX" = "default" ] && MP3ENCODERSYNTAX=lame
2008 echo $ACTIONS | grep tag > /dev/null 2>&1 && NEEDTAGGER=y
2010 flac) [ "$FLACENCODERSYNTAX" = "default" ] && FLACENCODERSYNTAX=flac
2011 echo $ACTIONS | grep tag > /dev/null 2>&1 && NEEDMETAFLAC=y
2013 spx) [ "$SPEEXENCODERSYNTAX" = "default" ] && SPEEXENCODERSYNTAX=speexenc ;;
2014 mpc) [ "$MPPENCODERSYNTAX" = "default" ] && MPPENCODERSYNTAX=mppenc ;;
2015 *) echo "abcde error: Invalid OUTPUTTYPE defined" >&2
2021 # decide which encoder
2022 case "$MP3ENCODERSYNTAX" in
2024 MP3ENCODEROPTS="$LAMEOPTS"
2028 MP3ENCODEROPTS="$GOGOOPTS"
2032 MP3ENCODEROPTS="$BLADEENCOPTS"
2033 MP3ENCODER="$BLADEENC"
2036 MP3ENCODEROPTS="$L3ENCOPTS"
2040 MP3ENCODEROPTS="$XINGMP3ENCOPTS"
2041 MP3ENCODER="$XINGMP3ENC"
2044 MP3ENCODEROPTS="$MP3ENCOPTS"
2045 MP3ENCODER="$MP3ENC"
2048 case "$OGGENCODERSYNTAX" in
2050 OGGENCODEROPTS="$VORBIZEOPTS"
2051 OGGENCODER="$VORBIZE"
2054 OGGENCODEROPTS="$OGGENCOPTS"
2055 OGGENCODER="$OGGENC"
2058 case "$FLACENCODERSYNTAX" in
2060 FLACENCODEROPTS="$FLACOPTS"
2064 case "$SPEEXENCODERSYNTAX" in
2066 SPEEXENCODEROPTS="$SPEEXENCOPTS"
2067 SPEEXENCODER="$SPEEXENC"
2070 case "$MPPENCODERSYNTAX" in
2072 MPPENCODEROPTS="$MPPENCOPTS"
2073 MPPENCODER="$MPPENC"
2079 if [ "$ID3TAGV" = "1" ]; then
2081 TAGGEROPTS="$ID3OPTS"
2084 TAGGEROPTS="$ID3V2OPTS"
2087 # Clean up nice options (either use '-n NICELEVEL or -NICELEVEL')
2089 if [ "$ENCNICE" ]; then
2090 ENCNICE="-n $ENCNICE"
2092 if [ "$READNICE" ]; then
2093 READNICE="-n $READNICE"
2095 if [ "$DISTMP3NICE" ]; then
2096 DISTMP3NICE="-n $DISTMP3NICE"
2099 # Don't check for stuff if it's not needed
2100 if [ "$REMOTEHOSTS" ]; then NEEDDISTMP3=y; fi
2101 if echo $ACTIONS | grep normalize > /dev/null 2>&1 ; then NEEDNORMALIZER=y; fi
2102 if [ "$EJECTCD" = "y" ]; then NEEDEJECT=y; fi
2103 if echo $ACTIONS | grep cddb > /dev/null 2>&1 ; then NEEDHTTPGET=y ; fi
2105 if [ X"$CDSPEEDVALUE" != "X" ]; then
2106 case "$CDROMREADERSYNTAX" in
2107 cdparanoia|debug) CDROMREADEROPTS="$CDPARANOIAOPTS -S $CDSPEEDVALUE" ;;
2113 # Make sure a buncha things exist
2114 for X in $CDROMREADER $CDDISCID ${NEEDTAGGER+$TAGGER} $MP3ENCODER \
2115 $OGGENCODER $FLACENCODER $SPEEXENCODER $MPPENCODER \
2116 ${NEEDHTTPGET+$HTTPGET} ${NEEDDISTMP3+$DISTMP3} \
2117 ${NEEDCOMMENTER+$VORBISCOMMENT} ${NEEDMETAFLAC+$METAFLAC} \
2118 ${NEEDNORMALIZER+$NORMALIZER} ${NEEDEJECT+$EJECT} \
2119 ${NEEDDISKTOOL+disktool} ${NEEDCDSPEED+$CDSPEED} \
2120 ${NEEDVORBISGAIN+$VORBISGAIN}
2122 # Cut off the command-line options we just added in
2123 X=$(echo $X | cut -d' ' -f2)
2124 if [ "$(which $X)" = "" ]; then
2125 echo "abcde error: $X is not in your path." >&2
2127 elif [ ! -x $(which $X) ]; then
2128 echo "abcde error: $X is not executable." >&2
2133 CDROMREADER="$CDROMREADER $CDROMREADEROPTS"
2134 CDDBTOOL="$CDDBTOOL $CDDBTOOLOPTS"
2135 HTTPGET="$HTTPGET $HTTPGETOPTS"
2137 # Here it used to say:
2138 # One thousand lines in, we can start doing stuff with things
2139 # Well, right now we are at line 2139 ;)
2141 # List of valid actions: cddb,read,normalize,encode,tag,playlist,move,clean
2142 # List of experimental actions: retag,transcode
2144 # Export needed things so they can be read in this subshell
2145 export CDDBTOOL ABCDETEMPDIR TRACKQUEUE LOWDISK EJECTCD EJECT EJECTOPTS
2146 export CDROM CDDBDATA REMOTEHOSTS MAXPROCS HTTPGET MD5SUM
2148 # User-definable function to set some things. Use it for
2149 # - closing the CD tray with eject -t
2150 # - set the CD speed value with eject -x
2151 vecho -n "Executing customizable pre-read function... "
2153 pre_read # Execute the user-defined pre-read funtion. Close the CD with it.
2157 do_discid # Get ABCDETEMPDIR created and status file initialized
2159 if [ "$DOCDDB" = "y" ]; then
2160 if [ $CDDBUSELOCAL = "y" ]; then
2163 ## FIXME ## ! is non-portable
2164 if ! checkstatus cddb-choice > /dev/null && [ ! "$CDDBLOCALSUCCESS" = "y" ] ; then
2171 eval $($CDDBTOOL parse "$CDDBDATA")
2174 # Before reading tracks, we set the speed of the device
2176 if [ X"$CDSPEEDVALUE" != "X" ]; then
2177 case "$CDROMREADERSYNTAX" in
2178 cdparanoia|debug) : ;;
2183 if [ "$STRIPDATATRACKS" = "y" ] && [ ! "$ONETRACK" = "y" ]; then
2184 case "$CDROMREADERSYNTAX" in
2186 # cdparanoia can query the CD, so let's process the TRACKQUEUE list with the results.
2187 if checkstatus cdparanoia-audio-tracks; then
2188 CDTRACKQUEUE=$( cat $ABCDETEMPDIR/cdparanoia-audio-tracks )
2191 vecho "Querying the CD to obtain a list of valid audio tracks..."
2192 $CDROMREADER -Q > $ABCDETEMPDIR/cdparanoia-query 2>&1
2193 # Obtain a list of valid audio tracks from the results of the query
2194 CDTRACKQUEUE=$( cat $ABCDETEMODIR/cdparanoia- | egrep '^[[:space:]]+[[:digit:]]' | awk '{print $1}' | tr -d "." | tr '\n' ' ' )
2196 # Obtain the track padding value from the before-processing list and pad the CD list
2197 TRACKNUMPADDING=$( echo $TRACKQUEUE | awk '{print $1}' | tr -d " \n" | wc -c )
2198 for TRACK in $CDTRACKQUEUE ; do
2199 TRACKNUM=$(printf %0.${TRACKNUMPADDING}d $(expr ${TRACK} + 0 ))
2200 PADNEWTRACKQUEUE=$(echo $PADNEWTRACKQUEUE $TRACKNUM)
2202 CDTRACKQUEUE=$PADNEWTRACKQUEUE
2203 # Now, compare if the values in the list are valid tracks in the CD
2204 for TRACK in $TRACKQUEUE; do
2205 if echo $CDTRACKQUEUE | grep $TRACK >/dev/null ; then
2206 NEWTRACKQUEUE="$NEWTRACKQUEUE $TRACK"
2209 TRACKQUEUE="$NEWTRACKQUEUE"
2214 # Create playlist if needed (backgroundable) and start reading in tracks
2218 if [ "$ONETRACK" = "y" ]; then :; else
2219 if [ "$DOPLAYLIST" = "y" ]; then
2220 echo Creating playlist... >&2
2225 # For the lowdisk option, only one program is running at once so the encoder
2226 # can be unsilenced right away.
2227 if [ "$LOWDISK" = "y" ] || [ "$ONETRACK" = "y" ]; then
2228 echo "encode-output=loud" >> "$ABCDETEMPDIR/status"
2231 if [ "$ONETRACK" = "y" ]; then
2232 FIRSTTRACK=$( echo $TRACKQUEUE | awk '{print $1}' )
2233 TRACKS="$FIRSTTRACK"
2234 for UTRACKNUM in $TRACKQUEUE; do :;done
2235 if checkstatus readtrack-$FIRSTTRACK; then :; else
2236 do_cdread_one $UTRACKNUM $FIRSTTRACK
2239 for UTRACKNUM in $TRACKQUEUE
2241 if [ "$DOREAD" = "y" ]; then
2242 if checkstatus readtrack-$UTRACKNUM; then :; else
2243 do_cdread $UTRACKNUM
2244 if [ "$?" != "0" ]; then
2245 # CD read failed - don't give the goahead to
2252 if [ "$BATCH" = "y" ]; then
2255 echo NEXTTRACK # Get the encoder machine churning again
2256 if [ "$DOREAD" = "y" ]; then
2257 if [ "$LOWDISK" = "y" ] && [ "$DOENCODE" = "y" ]; then
2258 until checkstatus encodetrack-$UTRACKNUM
2260 if checkerrors encodetrack-$UTRACKNUM; then
2271 # Now that we're done the encoding can be loud again -
2272 # if we're not using SMP.
2273 if [ "$MAXPROCS" = "1" ]; then
2274 echo "encode-output=loud" >> "$ABCDETEMPDIR/status"
2277 # All tracks read, start encoding.
2278 if [ "$BATCH" = "y" ] || [ "$ONETRACK" = "y" ]; then
2282 # We are now finished with the cdrom - it can be safely ejected. Note that
2283 # abcde will not have completed yet.
2284 if [ "$EJECTCD" = "y" ] && [ -x $(which $EJECT) ]; then
2285 # We check if the disk we are processing is actually the disk inside the
2286 # CD tray. If not, we do not eject the CD, since it might be so that the
2287 # user ejected it manually.
2288 #CURRENTTRACKINFO=$($CDDISCID $CDROM)
2289 #if if [ "$?" != "1" ] && [ "$CURRENTTRACKINFO" = "$TRACKINFO" ] ; then
2290 # More FreeBSD bits.
2291 if [ X"$(uname)" = X"FreeBSD" ] ; then
2292 # FreeBSD eject uses the EJECT environment variable to name the CDROM
2293 # but in this script EJECT is in the envionment and names the program
2296 # The FreeBSD eject needs "adc0" not "/dev/adc0c"
2297 cd="$(echo $CDROM | sed -e 's=.*/==;s=[a-h]$==;')"
2298 $eject $EJECTOPTS $cd
2299 elif [ X"$(uname)" = X"Darwin" ] ; then
2300 disktool -e ${CDROM#/dev/} 0
2302 $EJECT $EJECTOPTS $CDROM
2309 ## Do we need to pre-process
2310 #if [ x"$PREPROCESS" = "x" ] ; then
2313 # for PRETRACKNUM in $TRACKQUEUE
2316 # if [ "$GOAHEAD" = "NO" ]; then break; fi
2318 # until [ $PREPROCEED ]
2320 # if checkstatus readtrack-$PRETRACKNUM; then PREPROCEED=y; break; fi
2321 # # all locations are working, wait and try again later
2322 # if [ ! $PREPROCEED ]; then sleep 3; fi
2324 # ( do_preprocess $PRETRACKNUM
2332 # In batch mode, we want all tracks to be read first.
2333 if [ "$BATCH" = "y" ]; then
2334 read GOAHEAD # For blocking - will contain either "NO" or "NEXTTRACK"
2335 if [ "$GOAHEAD" = "NO" ]; then break; fi
2336 for LASTTRACK in $TRACKQUEUE; do :; done
2337 if checkstatus readtrack-$LASTTRACK; then
2338 if [ "$DONORMALIZE" = "y" ]; then
2339 if checkstatus normalizetrack-$LASTTRACK; then :; else do_batch_normalize; fi
2340 if checkerrors batch-normalize; then exit; fi
2342 if [ "$DOENCODE" = "y" ]; then
2343 if checkstatus encodetrack-$LASTTRACK; then :; else do_batch_encode; fi
2344 if checkerrors batch-encode; then exit; fi
2349 # If we are using ONETRACK, we can proceed with the normal encoding using just the $FIRSTTRACK as TRACKQUEUE
2350 if [ "$ONETRACK" = "y" ] ; then
2351 FIRSTTRACK=$( echo $TRACKQUEUE | awk '{print $1}')
2352 TRACKQUEUE=$FIRSTTRACK
2353 TRACKS="$FIRSTTRACK"
2356 # Do the encoding, including parallelization of remote encoding
2357 # Figure out where each track is going to be encoded
2358 ENCODELOCATIONS="$(echo $REMOTEHOSTS | tr , ' ')"
2359 if [ "$MAXPROCS" != "0" ]; then
2360 for NUM in $(f_seq_row 1 "$MAXPROCS")
2362 ENCODELOCATIONS="$ENCODELOCATIONS %local$NUM%"
2366 ENCODELOCATIONS=$(echo $ENCODELOCATIONS)
2367 for UTRACKNUM in $TRACKQUEUE
2370 read GOAHEAD # For blocking - will contain either "NO" or "NEXTTRACK"
2371 if [ "$GOAHEAD" = "NO" ]; then break; fi
2372 # find out where this track is to be encoded
2373 if [ "$DOENCODE" = "y" ]; then
2374 # Make sure we have a place to encode this, if not, exit stage right
2375 if [ -z "$ENCODELOCATIONS" ]; then
2381 for LOCATION in $ENCODELOCATIONS
2383 PREVIOUSTRACK="$(checkstatus encodetracklocation-$LOCATION)"
2384 # check first if a track has ever been assigned to this location
2385 if [ -z "$PREVIOUSTRACK" ]; then PROCEED=y; break; fi
2386 # If it errored out, rebuild $ENCODELOCATIONS without this location in it
2387 if checkerrors encodetrack-$PREVIOUSTRACK; then
2388 for TEMPLOCATION in $ENCODELOCATIONS
2390 if [ "$TEMPLOCATION" != "$LOCATION" ]; then
2391 TEMPENCODELOCATIONS="$TEMPENCODELOCATIONS $TEMPLOCATION"
2394 ENCODELOCATIONS=$(echo $TEMPENCODELOCATIONS)
2399 # We're still here, this location must have been previously assigned,
2400 # and last completed without error - check if it's done with the
2401 # previous track yet
2402 if checkstatus encodetrack-$PREVIOUSTRACK; then PROCEED=y; break; fi
2404 # all locations are working, wait and try again later
2405 if [ ! $PROCEED ]; then sleep 3; fi
2407 # Record the location we're about to encode the next track at
2408 echo "encodetracklocation-$LOCATION=$UTRACKNUM" >> "$ABCDETEMPDIR/status"
2410 # Don't proceed with the rest of the loop if we can't encode
2411 if [ "$ABORT" ]; then continue; fi
2412 # Set TRACKNUM, TRACKNAME
2413 if [ -e "$CDDBDATA" ]; then
2414 if [ "$ONETRACK" = "y" ]; then
2416 TRACKNUM="$FIRSTTRACK"
2419 # TRACKNUM=$(printf %0.${TRACKNUMPADDING}d $(expr ${UTRACKNUM} + 0))
2421 CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
2422 TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -n 1 | cut -f2 -d= | tr -d \[:cntrl:\])
2426 # You can't encode a file which needs to be normalized before finishing
2427 # You can't tag a file before it's finished encoding -
2428 # thus all of this is backgrounded together
2430 if [ "$DONORMALIZE" = "y" ]; then
2431 if checkstatus readtrack-$UTRACKNUM; then
2432 if checkstatus normalizetrack-$UTRACKNUM; then :; else do_normalize $UTRACKNUM; fi
2435 if [ "$DOENCODE" = "y" ]; then
2436 if checkstatus readtrack-$UTRACKNUM; then
2437 #if checkstatus encodetrack-$UTRACKNUM; then :; else do_encode $UTRACKNUM $LOCATION; fi
2438 if [ "$DONORMALIZE" = "y" ]; then
2439 if checkstatus normalizetrack-$UTRACKNUM; then
2440 if checkstatus encodetrack-$UTRACKNUM; then :; else do_encode $UTRACKNUM $LOCATION $OUTPUT; fi
2443 if checkstatus encodetrack-$UTRACKNUM; then :; else do_encode $UTRACKNUM $LOCATION $OUTPUT; fi
2447 if [ "$DOTAG" = "y" ]; then
2448 if checkstatus encodetrack-$UTRACKNUM; then
2449 if checkstatus tagtrack-$UTRACKNUM; then :; else do_tag $UTRACKNUM; fi
2452 if [ "$DOMOVE" = "y" ]; then
2453 if checkstatus tagtrack-$UTRACKNUM; then
2454 if checkstatus movetrack-$UTRACKNUM; then :; else do_move $UTRACKNUM; fi
2459 # Go through it again and make sure there's no distmp3 stragglers, otherwise
2460 # we'll delete the files they're working on
2461 if [ "$DOENCODE" = "y" ]; then
2466 for LOCATION in $ENCODELOCATIONS
2468 CHECKTRACK="$(checkstatus encodetracklocation-$LOCATION)"
2469 # "How can he give us a status update, if he's DEAD?"
2470 if checkstatus encodetrack-$CHECKTRACK; then
2473 # Nothing to see here please go quietly back to your homes
2474 if [ -z "$CHECKTRACK" ]; then continue; fi
2475 # You're still here? Maybe there is something...
2476 if checkstatus encodetrack-$CHECKTRACK; then :; else PROCEED= ; break; fi
2479 if [ ! $PROCEED ]; then sleep 5; fi
2482 # If the above didn't catch the stragglers, this will
2484 # Check to see if run_command logged any errors
2485 if [ -f "$ABCDETEMPDIR/errors" ]; then
2486 echo "The following commands failed to run:"
2487 cat "$ABCDETEMPDIR/errors"
2491 if [ "$KEEPWAVS" = "y" ];then
2495 if [ "$DOCLEAN" = "y" ]; then
2496 # Wipe all the evidence
2497 # Gimme gimme gimme some more time!
2499 rm -rf "$ABCDETEMPDIR"
2502 echo "Finished. Not cleaning $ABCDETEMPDIR."