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