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