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