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