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