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