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