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