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