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