Corrects syntax for the --comment tag with newer eyeD3.
[abcde.git] / abcde
1 #!/bin/bash
2 # Copyright (c) 1998-2001 Robert Woodcock <rcw@debian.org>
3 # Copyright (c) 2003-2006 Jesus Climent <jesus.climent@hispalinux.es>
4 # Copyright (c) 2009-2012 Colin Tuckley <colint@debian.org>
5 # Copyright (c) 2012      Steve McIntyre <93sam@@debian.org>
6 # This code is hereby licensed for public consumption under either the
7 # GNU GPL v2 or greater, or Larry Wall's Artistic license - your choice.
8 #
9 # You should have received a copy of the GNU General Public License along
10 # with this program; if not, write to the Free Software Foundation, Inc.,
11 # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
12 #
13 # Copyright for this work is to expire January 1, 2010, after which it
14 # shall be public domain.
15 #
16 # $Id$
17
18 VERSION='2.5.5-UNRELEASED'
19
20 usage ()
21 {
22 echo "This is abcde v$VERSION."
23 echo "Usage: abcde [options] [tracks]"
24 echo "Options:"
25 echo "-1     Encode the whole CD in a single file"
26 echo "-a <action1[,action2]...>"
27 echo "       Actions to perform:"
28 echo "       cddb,read,normalize,encode,tag,move,replaygain,playlist,clean"
29 #echo "-A     Experimental actions (retag, transcode)"
30 echo "-b     Enable batch normalization"
31 #echo "-B     Disable batch replaygain (do file by file)"
32 echo "-c <file>"
33 echo "       Specify a configuration file (overrides system and user config files)"
34 echo "-C <discid#>"
35 echo "       Specify discid to resume from (only needed if you no longer have the cd)"
36 echo "-d <device>"
37 echo "       Specify CDROM device to grab (flac uses a single-track flac file)"
38 echo "-D     Debugging mode (equivalent to sh -x abcde)"
39 echo "-e     Erase encoded track information from status file"
40 echo "-f     Force operations that otherwise are considered harmful. Read \"man abcde\""
41 echo "-g     Use \"lame --nogap\" for MP3 encoding. Disables low disk and pipes flags"
42 echo "-h     This help information"
43 #echo "-i    Tag files while encoding, when possible (local only) -NWY-"
44 echo "-j <#> Number of encoder processes to run at once (localhost)"
45 echo "-k     Keep the wav tracks for later use"
46 echo "-l     Use low disk space algorithm"
47 echo "-L     Use local CDDB storage directory"
48 echo "-m     Modify playlist to include CRLF endings, to comply with some players"
49 #echo "       WARNING: Deprecated. Use \"cue\" action"
50 #echo "-M     Create a CUE file"
51 echo "-n     No lookup. Don't query CDDB, just create and use template"
52 echo "-N     Noninteractive. Never prompt for anything"
53 echo "-o <type1[,type2]...>"
54 echo "       Output file type(s) (vorbis,mp3,flac,spx,mpc,wav,m4a,opus). Defaults to vorbis"
55 echo "-p     Pad track numbers with 0's (if less than 10 tracks)"
56 echo "-P     Use UNIX pipes to read+encode without wav files"
57 echo "-q <level>"
58 echo "       Set quality level (high,medium,low)"
59 echo "-r <host1[,host2]...>"
60 echo "       Also encode on these remote hosts"
61 echo "-s <field>"
62 echo "       Show fields from the CDDB info (year,genre)"
63 echo "-S <#> Set the CD speed"
64 echo "-t <#> Start the track numbering at a given number"
65 echo "-T <#> Same as -t but modifies tag numbering"
66 echo "-U     Do NOT use UNICODE (UTF8) tags and comments"
67 echo "-v     Show version number and exit"
68 echo "-V     Be a bit more verbose about what is happening behind the scenes"
69 echo "-x     Eject CD after all tracks are read"
70 echo "-w <comment>"
71 echo "       Add a comment to the CD tracks"
72 echo "-W <#> Concatenate CDs: -T #01 -w \"CD #\""
73 echo "-z     Use debug CDROMREADERSYNTAX option (needs cdparanoia)"
74 echo ""
75 echo "Tracks is a space-delimited list of tracks to grab."
76 echo "Ranges specified with hyphens are allowed (i.e., 1-5)."
77 echo ""
78 #echo "Double hyphens are used to concatenate tracks"
79 }
80
81 addstatus ()
82 {
83         echo "$@" >> "$ABCDETEMPDIR/status"
84 }
85
86 # log [level] [message]
87 #
88 # log outputs the right message in a common format
89 log ()
90 {
91         BLURB="$1"
92         shift
93         case $BLURB in
94                 error)   echo "[ERROR] abcde: $@" >&2 ;;
95                 warning) echo "[WARNING] $@" >&2 ;;
96                 info)    echo "[INFO] $@" ;;
97         esac
98 }
99
100 # Functions to replace the need of seq, which is too distribution dependent.
101 f_seq_row ()
102 {
103         i=$1
104         while [ $i -ne `expr $2 + 1` ]
105         do
106                 echo $i
107                 i=`expr $i + 1`
108         done
109 }
110
111 f_seq_line ()
112 {
113         i=$1
114         if echo $i | grep "[[:digit:]]" > /dev/null 2>&1 ; then
115                 while [ $i -ne `expr $2 + 1` ]
116                 do
117                         printf $i" "
118                         i=`expr $i + 1`
119                 done
120                 echo
121         else
122                 log error "syntax error while processing track numbers ($i)"
123                 exit 1
124         fi
125 }
126
127 # Functions to replace the need of awk {print $1} and {print $NF}
128 get_first()
129 {
130 if [ X"$1" = "X" ]; then
131         for first in `cat`; do
132                 break
133         done
134 else
135         first=$1
136 fi
137 echo $first
138 }
139
140 get_last()
141 {
142 if [ X"$1" = "X" ]; then
143         for stdin in `cat`; do
144                 last=$stdin
145         done
146 else
147         for last in $@ ; do :; done
148 fi
149 echo $last
150 }
151
152 # checkstatus [blurb]
153 # Returns "0" if the blurb was found, returns 1 if it wasn't
154 # Puts the blurb content, if available, on stdout.
155 # Otherwise, returns "".
156 checkstatus ()
157 {
158         # Take the last line in the status file if there's multiple matches
159         PATTERN="^$1(=.*)?$"
160         BLURB=$(grep -E $PATTERN "$ABCDETEMPDIR/status" | tail -n 1)
161
162         if [ -z "$BLURB" ]; then
163                 # No matches found
164                 return 1
165         else
166                 # Matches found
167                 # See if there's a = in it
168                 if [ "$(echo $BLURB | grep -c =)" != "0" ]; then
169                         echo "$(echo $BLURB | cut -f2- -d=)"
170                 fi
171                 return 0
172         fi
173 }
174
175 # checkwarnings [blurb]
176 # Returns "0" if the blurb was found (meaning there was an warning),
177 # returns 1 if it wasn't (yes this is a little backwards).
178 # Does not print the blurb on stdout.
179 # Otherwise, returns "".
180 checkwarnings ()
181 {
182         if [ -e "$ABCDETEMPDIR/warnings" ]; then :; else
183                 return 1
184         fi
185         # Take the last line in the status file if there's multiple matches
186         PATTERN="^$1(:.*)?$"
187         BLURB="$(grep -E $PATTERN "$ABCDETEMPDIR/warnings" | tail -n 1)"
188
189         if [ -z "$BLURB" ]; then
190                 # negative, we did not have a negative...
191                 return 1
192         else
193                 # affirmative, we had a negative...
194                 return 0
195         fi
196 }
197
198 # checkerrors [blurb]
199 # Returns "0" if the blurb was found (meaning there was an error),
200 # returns 1 if it wasn't (yes this is a little backwards).
201 # Does not print the blurb on stdout.
202 # Otherwise, returns "".
203 checkerrors ()
204 {
205         if [ -e "$ABCDETEMPDIR/errors" ]; then :; else
206                 return 1
207         fi
208         # Take the last line in the status file if there's multiple matches
209         PATTERN="^$1(:.*)?$"
210         BLURB="$(grep -E $PATTERN "$ABCDETEMPDIR/errors" | tail -n 1)"
211
212         if [ -z "$BLURB" ]; then
213                 # negative, we did not have a negative...
214                 return 1
215         else
216                 # affirmative, we had a negative...
217                 return 0
218         fi
219 }
220
221 # page [file]
222 # Finds the right pager in the system to display a file
223 page ()
224 {
225         PAGEFILE="$1"
226         # Use the debian sensible-pager wrapper to pick the pager
227         # user has requested via their $PAGER environment variable
228         if [ -x "/usr/bin/sensible-pager" ]; then
229                 /usr/bin/sensible-pager "$PAGEFILE"
230         elif [ -x "$PAGER" ]; then
231                 # That failed, try to load the preferred editor, starting
232                 # with their PAGER variable
233                 $PAGER "$PAGEFILE"
234                 # If that fails, check for less
235         elif [ -x /usr/bin/less ]; then
236                 /usr/bin/less -f "$PAGEFILE"
237                 # more should be on all UNIX systems
238         elif [ -x /bin/more ]; then
239                 /bin/more "$PAGEFILE"
240         else
241                 # No bananas, just cat the thing
242                 cat "$PAGEFILE" >&2
243         fi
244 }
245
246 # run_command [blurb] [command...]
247 # Runs a command, silently if necessary, and updates the status file
248 run_command ()
249 {
250         vvecho "$@"
251         BLURB="$1"
252         shift
253         # See if this is supposed to be silent
254         if [ "$(checkstatus encode-output)" = "loud" ]; then
255                 "$@" >&2
256                 RETURN=$?
257         else
258                 # Special case for SMP, since
259                 # encoder output is never displayed, don't mute echos
260                 if [ -z "$BLURB" -a "$MAXPROCS" != "1" ]; then
261                         "$@" >&2
262                         RETURN=$?
263                 else
264                         "$@" >/dev/null 2>&1
265                         RETURN=$?
266                 fi
267         fi
268         case "$1" in
269         normalize|normalize-audio)
270                 if [ "$RETURN" = "2" ]; then
271                         # File was already normalized.
272                         RETURN=0
273                 fi
274                 ;;
275         esac
276         if [ "$RETURN" != "0" ]; then
277                 # Put an error in the errors file. For various reasons we
278                 # can't capture a copy of the program's output but we can
279                 # log what we attempted to execute and the error code
280                 # returned by the program.
281                 if [ "$BLURB" ]; then
282                         TWEAK="$BLURB: "
283                 fi
284                 echo "${TWEAK}returned code $RETURN: $@" >> "$ABCDETEMPDIR/errors"
285                 return $RETURN # Do not pass go, do not update the status file
286         fi
287         if [ "$BLURB" ]; then
288                 echo $BLURB >> "$ABCDETEMPDIR/status"
289         fi
290 }
291
292 # relpath() and slash() are Copyright (c) 1999 Stuart Ballard and
293 # distributed under the terms of the GNU GPL v2 or later, at your option
294
295 # Function to determine if a word contains a slash.
296 slash ()
297 {
298         case "$1" in
299         */*) return 0;;
300         *) return 1;;
301         esac
302 }
303
304 # Function to give the relative path from one file to another.
305 # Usage: relpath fromfile tofile
306 # eg relpath music/Artist/Album.m3u music/Artist/Album/Song.mp3
307 # (the result would be Album/Song.mp3)
308 # Output is relative path to $2 from $1 on stdout
309
310 # This code has the following restrictions:
311 # Multiple ////s are not collapsed into single /s, with strange effects.
312 # Absolute paths and ../s are handled wrong in FR (but they work in TO)
313 # If FR is a directory it must have a trailing /
314
315 relpath ()
316 {
317         FR="$1"
318         TO="$2"
319
320         case "$TO" in
321         /*) ;; # No processing is needed for absolute paths
322         *)
323                 # Loop through common prefixes, ignoring them.
324                 while slash "$FR" && [ "$(echo "$FR" | cut -d/ -f1)" = "$(echo "$TO" | cut -d/ -f1)" ]
325                 do
326                         FR="$(echo "$FR" | cut -d/ -f2-)"
327                         TO="$(echo "$TO" | cut -d/ -f2-)"
328                 done
329                 # Loop through directory portions left in FR, adding appropriate ../s.
330                 while slash "$FR"
331                 do
332                         FR="$(echo "$FR" | cut -d/ -f2-)"
333                         TO="../$TO"
334                 done
335                 ;;
336         esac
337
338         echo $TO
339 }
340
341 new_checkexec ()
342 {
343         if [ ! "$@" = "" ]; then
344                 # Cut off any command-line option we added in
345                 X=$(echo $@ | cut -d' ' -f2)
346                 if [ "$(which $X)" = "" ]; then
347                         return 1
348                 elif [ ! -x $(which $X) ]; then
349                         return 2
350                 fi
351         fi
352         return 0
353 }
354
355 checkexec ()
356 {
357         if [ ! "$@" = "" ]; then
358                 # Cut off any command-line option we added in
359                 X=$(echo $@ | cut -d' ' -f2)
360                 # Test for built-in abcde.function
361                 [ "$X" != "${X#abcde.}" ] && type $X >/dev/null 2>&1 && return
362                 if [ "$(which $X)" = "" ]; then
363                         log error "$X is not in your path." >&2
364                         log info  "Define the full path to the executable if it exists on your system." >&2
365                         if [ -e /etc/debian_release ] ; then
366                                 case $X in
367                                         oggenc)         MISSING_PACKAGE=vorbis-tools ;;
368                                         lame|flac)      MISSING_PACKAGE=$X ;;
369                                 esac
370                                 log info "Hint: apt-get install $MISSING_PACKAGE" >&2
371                         fi
372                         exit 1
373                 elif [ ! -x "$(which $X)" ]; then
374                         log error "$X is not executable." >&2
375                         exit 1
376                 fi
377         fi
378 }
379
380 # diffentries <filename> <max_value> <entry1>,<entry2>
381 # max_value: the range of entries goes from 1 to <max_value>
382 diffentries ()
383 {
384         FILENAME=$1
385         shift
386         local CDDBDIFFCHOICES=$1
387         shift
388         local CDDBDIFFCHOICE="$@"
389         if [ ! X"$DIFF" = "X" ]; then
390                 PARSECHOICE1=$(echo $CDDBDIFFCHOICE | cut -d"," -f1 | xargs printf %d 2>/dev/null)
391                 PARSECHOICE2=$(echo $CDDBDIFFCHOICE | cut -d"," -f2 | xargs printf %d 2>/dev/null)
392                 if [ $PARSECHOICE1 -lt 1 ] || [ $PARSECHOICE1 -gt $CDDBDIFFCHOICES ] || \
393                    [ $PARSECHOICE2 -lt 1 ] || [ $PARSECHOICE2 -gt $CDDBDIFFCHOICES ] || \
394                    [ $PARSECHOICE1 -eq $PARSECHOICE2 ]; then
395                         echo "Invalid diff range. Please select two comma-separated numbers between 1 and $CDDBDIFFCHOICES" >&2
396                 else
397                         # We parse the 2 choices to diff, store them in temporary files and diff them.
398                         for PARSECHOICE in $(echo $CDDBDIFFCHOICE | tr , \ ); do
399                                 do_cddbparse "$ABCDETEMPDIR/$FILENAME.$PARSECHOICE" > "$ABCDETEMPDIR/$FILENAME.parsechoice.$PARSECHOICE"
400                         done
401                         echo "Showing diff between choices $PARSECHOICE1 and $PARSECHOICE2..." > "$ABCDETEMPDIR/$FILENAME.diff"
402                         $DIFF $DIFFOPTS "$ABCDETEMPDIR/$FILENAME.parsechoice.$PARSECHOICE1" "$ABCDETEMPDIR/$FILENAME.parsechoice.$PARSECHOICE2" >> "$ABCDETEMPDIR/$FILENAME.diff"
403                         if [ $(cat "$ABCDETEMPDIR/$FILENAME.diff" | wc -l) -ge 24 ]; then
404                                 page "$ABCDETEMPDIR/$FILENAME.diff"
405                         else
406                                 cat "$ABCDETEMPDIR/$FILENAME.diff" >&2
407                         fi
408                 fi
409         else
410                 echo "The diff program was not found in your path. Please choose a number between 0 and $CDDBDIFFCHOICES." >&2
411         fi
412 }
413
414 # getcddbinfo
415 # Finds an specific field from cddbinfo
416 getcddbinfo()
417 {
418         case $1 in
419         TRACKNAME1)
420                 TRACKNAME="$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -n 1 | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')"
421                 ;;
422         TRACKNAME)
423                 TRACKNAME="$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')"
424                 ;;
425         TRACK-INFO)
426                 grep ^EXTT$CDDBTRACKNUM= "$CDDBDATA" | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\\n/\n/g'
427                 ;;
428         esac
429 }
430
431 # gettracknum
432 # Get the track number we are going to use for different actions
433 gettracknum()
434 {
435         if [ -n "$STARTTRACKNUMBER" ] ; then
436                 # Get the trackpadding from the current track, also trim whitespace for MacOSX
437                 CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c | tr -d ' ')
438                 TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - $FIRSTTRACK ))
439         else
440                 TRACKNUM=${UTRACKNUM}
441         fi
442 }
443
444 # makeids
445 #
446 # Calculate cddb disc ids without requiring specialized helper programs.
447 # largely copied from cd-discid and musicbrainz examples.  some of the steps
448 # don't make sense, but they're necessary to match the ids generated by other
449 # programs.
450 #
451 ## FIXME ## Right now, we get 2 frames more than with cue2discid ??
452 # data@petit:~$ sh /tmp/cue2discid /home/data/tmp/flac/01.Roisin_Murphy--Ruby_Blue.flac
453 # processing offsetimes 00:00:00 04:47:10 08:20:37 11:46:46 17:45:36 21:41:57 27:32:21 32:03:73 35:39:28 38:27:33 43:50:38 44:42:34
454 # 980b4b0c 12 150 21685 37687 53146 80061 97782 124071 144448 160603 173208 197438 201334 2895
455 # data@petit:~$ metaflac --export-cuesheet-to=- /home/data/tmp/flac/01.Roisin_Murphy--Ruby_Blue.flac| python /home/data/sources/abcde/trunk/examples/cue2discid
456 # 980b4b0c 12 150 21685 37687 53146 80061 97782 124071 144448 160603 173208 197438 201334 2893
457 #
458 # Variables: OFFSETS, TRACKS, LEADOUT, [LEADIN]
459 makeids ()
460 {
461         if [ X"$LEADOUT" = "X" ]; then
462                 log warning "Error trying to calculate disc ids without lead-out information."
463                 exit 1
464         fi
465
466         # default to a two second lead-in
467         IDMAGICNUM=150
468         LEADIN=${LEADIN:=150}
469
470         # number of cdframes per second
471         CDFRAMES=75
472
473         # reset cddb checksum for cddb disc-id calululation
474         CDDBCKSUM=0
475
476         COOKEDOFFSETS=""
477         for OFFSET in $(echo $OFFSETS)
478         do
479                 COOKEDOFFSETS="${COOKEDOFFSETS} $(($OFFSET + $LEADIN))"
480
481                 OFFSETTIME=$(( ($OFFSET + $LEADIN) / $CDFRAMES  ))
482                 while [ $OFFSETTIME -gt 0 ]; do
483                         CDDBCKSUM=$(($CDDBCKSUM + $OFFSETTIME % 10))
484                         OFFSETTIME=$(($OFFSETTIME / 10))
485                 done
486
487         done
488
489         COOKEDOFFSETS="${COOKEDOFFSETS:1}"  # eat the leading space
490
491         PREGAP=$(($(echo $OFFSETS | cut -f1 -d' ')))
492         TOTALTIME=$(( (($LEADOUT + $LEADIN + $PREGAP) / $CDFRAMES) - (($LEADIN + $PREGAP) / $CDFRAMES)))
493
494         case "$CDDBMETHOD" in
495                 cddb)
496                         printf -v DISCID "%08lx" $(( ($CDDBCKSUM % 0xff) << 24 | $TOTALTIME << 8 | $TRACKS))
497                         ;;
498                 musicbrainz)
499                         # FIXME: don't assume the first track is 1
500                         echo "dasd: 1 $TRACKS $LEADIN $LEADOUT $OFFSETS "
501                         DISCID=$($MUSICBRAINZ --command calcid --discinfo 1 $TRACKS $LEADIN $LEADOUT $OFFSETS)
502                         ;;
503         esac
504
505         TRACKINFO="${DISCID} $((TRACKS)) ${COOKEDOFFSETS} $((($LEADOUT + $LEADIN + $IDMAGICNUM) / $CDFRAMES))"
506 }
507
508 do_replaygain()
509 {
510         if checkstatus replaygain; then :; else
511                 run_command "" echo "Adding replaygain information..."
512                 for TMPOUTPUT in $( echo $OUTPUTTYPE | tr , \ )
513                 do
514                         case $TMPOUTPUT in
515                                 vorbis|ogg)
516                                         OUTPUT=$OGGOUTPUTCONTAINER
517                                         ;;
518                                 opus)
519                                         OUTPUT=$OPUSOUTPUTCONTAINER
520                                         ;;
521                                 flac)
522                                         OUTPUT=$FLACOUTPUTCONTAINER
523                                         ;;
524                                 *)
525                                         OUTPUT=$TMPOUTPUT
526                                         ;;
527                         esac
528                         OUTPUTFILES=""
529                         REPLAYINDEX=0
530                         for UTRACKNUM in $TRACKQUEUE
531                         do
532                                 CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
533                                 getcddbinfo TRACKNAME
534                                 splitvarious
535                                 TRACKFILE="$(mungefilename "$TRACKNAME")"
536                                 ARTISTFILE="$(mungefilename "$TRACKARTIST")"
537                                 ALBUMFILE="$(mungefilename "$DALBUM")"
538                                 GENRE="$(mungegenre "$GENRE")"
539                                 YEAR=${CDYEAR:-$CDYEAR}
540                                 gettracknum
541                                 if [ "$ONETRACK" = "y" ]; then
542                                         if [ "$VARIOUSARTISTS" = "y" ]; then
543                                                 OUTPUTFILE="$(eval echo \""$VAONETRACKOUTPUTFORMAT"\")"
544                                         else
545                                                 OUTPUTFILE="$(eval echo \""$ONETRACKOUTPUTFORMAT"\")"
546                                         fi
547                                 else    
548                                         if [ "$VARIOUSARTISTS" = "y" ]; then
549                                                 OUTPUTFILE="$(eval echo \""$VAOUTPUTFORMAT"\")"
550                                         else
551                                                 OUTPUTFILE="$(eval echo \""$OUTPUTFORMAT"\")"
552                                         fi
553                                 fi
554                                 OUTPUTFILES[$REPLAYINDEX]="$OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
555                                 (( REPLAYINDEX = $REPLAYINDEX + 1 ))
556                         done
557                         case "$OUTPUT" in
558                                 flac)
559                                         run_command replaygain-flac nice $ENCNICE $METAFLAC $FLACGAINOPTS "${OUTPUTFILES[@]}"
560                                         #run_command replaygain-flac true
561                                         ;;
562                                 vorbis|ogg)
563                                         run_command replaygain-vorbis nice $ENCNICE $VORBISGAIN $VORBISGAINOPTS "${OUTPUTFILES[@]}"
564                                         ;;
565                                 mp3)
566                                         run_command replaygain-mp3 nice $ENCNICE $MP3GAIN $MP3GAINOPTS "${OUTPUTFILES[@]}"
567                                         ;;
568                                 mpc)
569                                         run_command replaygain-mpc nice $ENCNICE $MPPGAIN --auto "${OUTPUTFILES[@]}"
570                                         ;;
571                                 *);;
572                         esac
573                 done
574                 if checkerrors "replaygain-.{3,6}"; then :; else
575                         run_command replaygain true
576                 fi
577         fi
578 }
579
580 # This code splits the a Various Artist track name from one of the following
581 # forms:
582 #
583 #  forward:        Artist / Track
584 #  forward-dash:   Artist - Track
585 #  reverse:        Track / Artist
586 #  reverse-dash:   Track - Artist
587 #  colon:          Artist: Track
588 #  trailing-paren: Artist (Track)
589 #
590 # variables used:
591 # VARIOUSARTISTS, VARIOUSARTISTSTYLE, TRACKNAME, TRACKARTIST
592 splitvarious ()
593 {
594         if [ "$VARIOUSARTISTS" = "y" ] && [ ! "$ONETRACK" = "y" ]; then
595                 case "$VARIOUSARTISTSTYLE" in
596                 forward)
597                         DTITLEARTIST="$(echo "$TRACKNAME" | sed 's- / -~-g')"
598                         TRACKARTIST="$(echo "$DTITLEARTIST" | cut -f1 -d~)"
599                         TRACKNAME="$(echo "$DTITLEARTIST" | cut -f2 -d~)"
600                         ;;
601                 forward-dash)
602                         DTITLEARTIST="$(echo "$TRACKNAME" | sed 's, - ,~,g')"
603                         TRACKARTIST="$(echo "$DTITLEARTIST" | cut -f1 -d~)"
604                         TRACKNAME="$(echo "$DTITLEARTIST" | cut -f2 -d~)"
605                         ;;
606                 reverse)
607                         DTITLEARTIST="$(echo "$TRACKNAME" | sed 's- / -~-g')"
608                         TRACKARTIST="$(echo "$DTITLEARTIST" | cut -f2 -d~)"
609                         TRACKNAME="$(echo "$DTITLEARTIST" | cut -f1 -d~)"
610                         ;;
611                 reverse-dash)
612                         DTITLEARTIST="$(echo "$TRACKNAME" | sed 's, - ,~,g')"
613                         TRACKARTIST="$(echo "$DTITLEARTIST" | cut -f2 -d~)"
614                         TRACKNAME="$(echo "$DTITLEARTIST" | cut -f1 -d~)"
615                         ;;
616                 colon)
617                         DTITLEARTIST="$(echo "$TRACKNAME" | sed 's-: -~-g')"
618                         TRACKARTIST="$(echo "$DTITLEARTIST" | cut -f1 -d~)"
619                         TRACKNAME="$(echo "$DTITLEARTIST" | cut -f2 -d~)"
620                         ;;
621                 trailing-paren)
622                         DTITLEARTIST="$(echo "$TRACKNAME" | sed 's,^\(.*\) (\(.*\)),\1~\2,')"
623                         TRACKARTIST="$(echo "$DTITLEARTIST" | cut -f2 -d~)"
624                         TRACKNAME="$(echo "$DTITLEARTIST" | cut -f1 -d~)"
625                         ;;
626                 esac
627         elif [ "$VARIOUSARTISTS" = "y" ] && [ "$ONETRACK" = "y" ]; then
628                 TRACKARTIST="Various"
629         else
630                 TRACKARTIST="$DARTIST"
631         fi
632 }
633
634 do_getgenreid () {
635 local genre=$(echo "${@}" | tr '[A-Z]' '[a-z]')
636 local id=""
637         case ${genre} in
638                 "blues")                 id=0 ;;
639                 "classic rock")          id=1 ;;
640                 "country")               id=2 ;;
641                 "dance")                 id=3 ;;
642                 "disco")                 id=4 ;;
643                 "funk")                  id=5 ;;
644                 "grunge")                id=6 ;;
645                 "hip-hop")               id=7 ;;
646                 "jazz")                  id=8 ;;
647                 "metal")                 id=9 ;;
648                 "new age")               id=10 ;;
649                 "oldies")                id=11 ;;
650                 "other")                 id=12 ;;
651                 "pop")                   id=13 ;;
652                 "r&b")                   id=14 ;;
653                 "rap")                   id=15 ;;
654                 "reggae")                id=16 ;;
655                 "rock")                  id=17 ;;
656                 "techno")                id=18 ;;
657                 "industrial")            id=19 ;;
658                 "alternative")           id=20 ;;
659                 "ska")                   id=21 ;;
660                 "death metal")           id=22 ;;
661                 "pranks")                id=23 ;;
662                 "soundtrack")            id=24 ;;
663                 "euro-techno")           id=25 ;;
664                 "ambient")               id=26 ;;
665                 "trip-hop")              id=27 ;;
666                 "vocal")                 id=28 ;;
667                 "jazz+funk")             id=29 ;;
668                 "fusion")                id=30 ;;
669                 "trance")                id=31 ;;
670                 "classical")             id=32 ;;
671                 "instrumental")          id=33 ;;
672                 "acid")                  id=34 ;;
673                 "house")                 id=35 ;;
674                 "game")                  id=36 ;;
675                 "sound clip")            id=37 ;;
676                 "gospel")                id=38 ;;
677                 "noise")                 id=39 ;;
678                 "alt. rock")             id=40 ;;
679                 "bass")                  id=41 ;;
680                 "soul")                  id=42 ;;
681                 "punk")                  id=43 ;;
682                 "space")                 id=44 ;;
683                 "meditative")            id=45 ;;
684                 "instrum. pop")          id=46 ;;
685                 "instrum. rock")         id=47 ;;
686                 "ethnic")                id=48 ;;
687                 "gothic")                id=49 ;;
688                 "darkwave")              id=50 ;;
689                 "techno-indust.")        id=51 ;;
690                 "electronic")            id=52 ;;
691                 "pop-folk")              id=53 ;;
692                 "eurodance")             id=54 ;;
693                 "dream")                 id=55 ;;
694                 "southern rock")         id=56 ;;
695                 "comedy")                id=57 ;;
696                 "cult")                  id=58 ;;
697                 "gangsta")               id=59 ;;
698                 "top 40")                id=60 ;;
699                 "christian rap")         id=61 ;;
700                 "pop/funk"|"pop / funk") id=62 ;;
701                 "jungle")                id=63 ;;
702                 "native american")       id=64 ;;
703                 "cabaret")               id=65 ;;
704                 "new wave")              id=66 ;;
705                 "psychadelic")           id=67 ;;
706                 "rave")                  id=68 ;;
707                 "showtunes")             id=69 ;;
708                 "trailer")               id=70 ;;
709                 "lo-fi")                 id=71 ;;
710                 "tribal")                id=72 ;;
711                 "acid punk")             id=73 ;;
712                 "acid jazz")             id=74 ;;
713                 "polka")                 id=75 ;;
714                 "retro")                 id=76 ;;
715                 "musical")               id=77 ;;
716                 "rock & roll")           id=78 ;;
717                 "hard rock")             id=79 ;;
718                 "folk")                  id=80 ;;
719                 "folk/rock")             id=81 ;;
720                 "national folk")         id=82 ;;
721                 "swing")                 id=83 ;;
722                 "fusion")                id=84 ;;
723                 "bebob")                 id=85 ;;
724                 "latin")                 id=86 ;;
725                 "revival")               id=87 ;;
726                 "celtic")                id=88 ;;
727                 "bluegrass")             id=89 ;;
728                 "avantgarde")            id=90 ;;
729                 "gothic rock")           id=91 ;;
730                 "progress. rock")        id=92 ;;
731                 "psychadel. rock")       id=93 ;;
732                 "symphonic rock")        id=94 ;;
733                 "slow rock")             id=95 ;;
734                 "big band")              id=96 ;;
735                 "chorus")                id=97 ;;
736                 "easy listening")        id=98 ;;
737                 "acoustic")              id=99 ;;
738                 "humour")                id=100 ;;
739                 "speech")                id=101 ;;
740                 "chanson")               id=102 ;;
741                 "opera")                 id=103 ;;
742                 "chamber music")         id=104 ;;
743                 "sonata")                id=105 ;;
744                 "symphony")              id=106 ;;
745                 "booty bass")            id=107 ;;
746                 "primus")                id=108 ;;
747                 "porn groove")           id=109 ;;
748                 "satire")                id=110 ;;
749                 "slow jam")              id=111 ;;
750                 "club")                  id=112 ;;
751                 "tango")                 id=113 ;;
752                 "samba")                 id=114 ;;
753                 "folklore")              id=115 ;;
754                 "ballad")                id=116 ;;
755                 "power ballad")          id=117 ;;
756                 "rhythmic soul")         id=118 ;;
757                 "freestyle")             id=119 ;;
758                 "duet")                  id=120 ;;
759                 "punk rock")             id=121 ;;
760                 "drum solo")             id=122 ;;
761                 "a capella")             id=123 ;;
762                 "euro-house")            id=124 ;;
763                 "dance hall")            id=125 ;;
764                 "goa")                   id=126 ;;
765                 "drum & bass")           id=127 ;;
766                 "club-house")            id=128 ;;
767                 "hardcore")              id=129 ;;
768                 "terror")                id=130 ;;
769                 "indie")                 id=131 ;;
770                 "britpop")               id=132 ;;
771                 "negerpunk")             id=133 ;;
772                 "polsk punk")            id=134 ;;
773                 "beat")                  id=135 ;;
774                 "christian gangsta rap") id=136 ;;
775                 "heavy metal")           id=137 ;;
776                 "black metal")           id=138 ;;
777                 "crossover")             id=139 ;;
778                 "contemporary christian")id=140 ;;
779                 "christian rock")        id=141 ;;
780                 "merengue")              id=142 ;;
781                 "salsa")                 id=143 ;;
782                 "thrash metal")          id=144 ;;
783                 "anime")                 id=145 ;;
784                 "jpop")                  id=146 ;;
785                 "synthpop")              id=147 ;;
786                 "rock/pop"|"rock / pop") id=148 ;;
787                 *)                       return 1 ;;
788         esac
789 echo ${id}
790 return 0
791 }
792
793 # do_tag [tracknumber]
794 # id3 tags a filename
795 # variables used:
796 # TRACKS, TRACKNAME, TRACKARTIST, TAGGER, TAGGEROPTS, VORBISCOMMENT, METAFLAC, ATOMICPARSLEY
797 # COMMENT, DALBUM, DARTIST, CDYEAR, CDGENRE
798 do_tag ()
799 {
800         COMMENTOUTPUT="$(eval echo ${COMMENT})"
801         if [ -z "$COMMENTOUTPUT" ]; then
802                 COMMENTOUTPUT="$(getcddbinfo TRACK-INFO)"
803         fi
804         if [ "$CDDBMETHOD" = "cddb" ]; then
805                 CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
806         fi
807         run_command '' echo "Tagging track $1 of $TRACKS: $TRACKNAME..."
808         # If we want to start the tracks with a given number, we need to modify the
809         # TRACKNUM value before evaluation
810         if [ -n "$STARTTRACKNUMBERTAG" ] ; then
811                 gettracknum
812         fi
813         for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
814         do
815                 case "$OUTPUT" in
816                 mp3)
817                         # id3v2 v0.1.9 claims to have solved the -c bug, so we merge both id3 and id3v2
818                         GENREID=$(do_getgenreid "${CDGENRE}")
819                         # Set TPE2 in case we have a Various Artists rip.
820                         TPE2=""
821                         if [ "$VARIOUSARTISTS" = "y" ]; then
822                                 TPE2="Various"
823                         fi
824         
825                         case "$ID3SYNTAX" in
826                                 id3);;
827                                 eyed3)
828                                         # FIXME # track numbers in mp3 come with 1/10, so we cannot
829                                         # happily substitute them with $TRACKNUM
830                                         # FIXME as well! # Older versions of eyeD3 (< 0.7.0) expect
831                                         # --set-encoding and --set-text-frame so perhaps some version 
832                                         # sniffing would be useful. Might also be better to simply cut 
833                                         # ties with the older eyeD3... Andrew.
834                                         # eyeD3 --comment syntax is also different in < and >= 0.7.0
835                                         run_command tagtrack-$OUTPUT-$1 nice $ENCNICE $TAGGER $TAGGEROPTS \
836                                                 --comment="$COMMENTOUTPUT" -A "$DALBUM" \
837                                                 -a "$TRACKARTIST" -t "$TRACKNAME" -Y "$CDYEAR" \
838                                                 -G "$GENREID" -n "${TRACKNUM:-$1}" \
839                                                 ${TRACKNUM:+-N "$TRACKS"} \
840                                                 ${ENCODING:+--encoding="$ENCODING"} \
841                                                 ${TPE2:+--text-frame=TPE2:"$TPE2"} \
842                                                 "$ABCDETEMPDIR/track$1.$OUTPUT"
843                                         ;;
844                                 # FIXME # Still not activated...
845                                 id3ed)
846                                         run_command tagtrack-$OUTPUT-$1 nice $ENCNICE \
847                                                 $TAGGER $TAGGEROPTS -c "$COMMENTOUTPUT" \
848                                                 -a "$DALBUM" -n "$TRACKARTIST" -s "$TRACKNAME" \
849                                                 -y "$CDYEAR" -g "$GENREID" -k "${TRACKNUM:-$1}" \
850                                                 "$ABCDETEMPDIR/track$1.$OUTPUT"
851                                         ;;
852                                 *)
853                                         # FIXME # track numbers in mp3 come with 1/10, so we cannot
854                                         # happily substitute them with $TRACKNUM
855                                         run_command tagtrack-$OUTPUT-$1 nice $ENCNICE \
856                                                 $TAGGER $TAGGEROPTS -c "$COMMENTOUTPUT" \
857                                                 -A "$DALBUM" -a "$TRACKARTIST" -t "$TRACKNAME" \
858                                                 -y "$CDYEAR" -g "$GENREID" \
859                                                 -T "${TRACKNUM:-$1}/$TRACKS" \
860                                                 ${TPE2:+--TPE2 "$TPE2"} \
861                                                 "$ABCDETEMPDIR/track$1.$OUTPUT"
862                                         ;;
863                         esac
864                         ;;
865                 vorbis|ogg)
866                         case "$OGGENCODERSYNTAX" in
867                                 vorbize|oggenc)
868                                         # vorbiscomment can't do in-place modification, mv the file first
869                                         if [ -f "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER" -a ! -f "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER" ]; then
870                                                 mv "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER" "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER"
871                                         fi
872                                         (
873                                         # These are from
874                                         # http://www.xiph.org/ogg/vorbis/doc/v-comment.html
875
876                                         echo ARTIST="$TRACKARTIST"
877                                         echo ALBUM="$DALBUM"
878                                         echo TITLE="$TRACKNAME"
879                                         if [ -n "$CDYEAR" ]; then
880                                                 echo DATE="$CDYEAR"
881                                         fi
882                                         if [ -n "$CDGENRE" ]; then
883                                                 echo GENRE="$CDGENRE"
884                                         fi      
885                                         echo TRACKNUMBER=${TRACKNUM:-$1}
886                                         if [ -n "$DISCNUMBER" ]; then
887                                                 echo DISCNUMBER="$DISCNUMBER"
888                                         fi
889                                         echo CDDB=$CDDBDISCID
890                                         if [ "$(eval echo ${COMMENT})" != "" ]; then
891                                                 case "$COMMENTOUTPUT" in
892                                                         *=*) echo "$COMMENTOUTPUT";;
893                                                         *)   echo COMMENT="$COMMENTOUTPUT";;
894                                                 esac    
895                                         fi
896                                         ) | run_command tagtrack-$OUTPUT-$1 nice $ENCNICE \
897                                                 $VORBISCOMMENT $VORBISCOMMENTOPTS -w \
898                                                 "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER" "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER"
899                                         # Doublecheck that the commented file was created
900                                         # successfully before wiping the original
901                                         if [ -f "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER" ]; then
902                                                 rm -f "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER"
903                                         else
904                                                 mv "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER" "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER"
905                                         fi
906                                         ;;
907                         esac
908                         ;;
909                 opus)
910                         run_command tagtrack-$OUTPUT-$1 true
911                         ;;
912
913                 flac)
914                         (
915                         echo ARTIST="$TRACKARTIST"
916                         echo ALBUM="$DALBUM"
917                         echo TITLE="$TRACKNAME"
918                         if [ -n "$CDYEAR" ]; then
919                                 echo DATE="$CDYEAR"
920                         fi
921                         if [ -n "$CDGENRE" ]; then
922                                 echo GENRE="$CDGENRE"
923                         fi      
924                         echo TRACKNUMBER="${TRACKNUM:-$1}"
925                         if [ -n "$DISCNUMBER" ]; then
926                                 echo DISCNUMBER="$DISCNUMBER"
927                         fi
928                         echo CDDB="$CDDBDISCID"
929                         if [ "$(eval echo ${COMMENT})" != "" ]; then
930                                 case "$COMMENTOUTPUT" in
931                                         *=*) echo "$COMMENTOUTPUT";;
932                                         *)   echo COMMENT="$COMMENTOUTPUT";;
933                                 esac    
934                         fi
935                         ) | run_command tagtrack-$OUTPUT-$1 nice $ENCNICE $METAFLAC $METAFLACOPTS ${IMPORTCUESHEET:+--import-cuesheet-from="$ABCDETEMPDIR/$CUEFILE"} --import-tags-from=- "$ABCDETEMPDIR/track$1.$FLACOUTPUTCONTAINER"
936                         ;;
937                 spx)
938                         run_command tagtrack-$OUTPUT-$1 true
939                         ;;
940                 mpc)
941                         run_command tagtrack-$OUTPUT-$1 true
942                         ;;
943                 m4a)
944                         #  Use a temp-file of our choice. --overWrite seems to
945                         #  case core dumps with AtomicParsley 0.9.0
946                         ATOMICTEMPFILE="$ABCDETEMPDIR/track$1.m4a-atomic"
947
948                         VARIOUSBOOL="false"
949                         if [ "$VARIOUSARTISTS" = "y" ]; then
950                                 VARIOUSBOOL="true"
951                         fi
952
953                         #It has to be command file opts for AtomicParsley
954                         run_command tagtrack-$OUTPUT-$1 nice $ENCNICE $ATOMICPARSLEY "$ABCDETEMPDIR/track$1.m4a" --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --tracknum ${TRACKNUM:-$1} --year "$CDYEAR" --genre "$CDGENRE" --compilation $VARIOUSBOOL --comment "$COMMENTOUTPUT" --output $ATOMICTEMPFILE
955                         if [ -f $ATOMICTEMPFILE ]; then
956                                 mv "$ATOMICTEMPFILE" "$ABCDETEMPDIR/track$1.m4a"
957                         fi
958                         ;;
959                 wav)
960                         run_command tagtrack-$OUTPUT-$1 true
961                         ;;
962                 esac
963         done
964         if checkerrors "tagtrack-(.{3,6})-$1"; then :; else
965                 run_command tagtrack-$1 true
966         fi
967
968 }
969
970 # do_nogap_encode
971 # variables used:
972 # OUTPUTTYPE, {FOO}ENCODERSYNTAX, ENCNICE, ENCODER, ENCODEROPTS
973 do_nogap_encode ()
974 {
975         # The commands here don't go through run_command because they're never
976         # supposed to be silenced
977         echo "Encoding gapless MP3 tracks: $TRACKQUEUE"
978         for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
979         do
980                 case "$OUTPUT" in
981                 mp3)
982                         case "$MP3ENCODERSYNTAX" in
983                         lame|toolame)
984                                 (
985                                 cd "$ABCDETEMPDIR"
986                                 TRACKFILES=
987                                 for UTRACKNUM in $TRACKQUEUE
988                                 do
989                                         TRACKFILES="$TRACKFILES track$UTRACKNUM.wav"
990                                 done
991                                 nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS --nogap $TRACKFILES
992                                 RETURN=$?
993                                 if [ "$RETURN" != "0" ]; then
994                                         echo "nogap-encode: $ENCODER returned code $RETURN" >> errors
995                                 else
996                                         for UTRACKNUM in $TRACKQUEUE
997                                         do
998                                                 run_command encodetrack-$OUTPUT-$UTRACKNUM true
999                                                 #run_command encodetrack-$UTRACKNUM true
1000                                         done
1001                                 fi
1002                                 )
1003                                 ;;
1004                         esac
1005                         ;;
1006                 esac
1007         done            
1008         if checkerrors "nogap-encode"; then :; else
1009                 if [ ! "$KEEPWAVS" = "y" ] ; then
1010                         if [ ! "$KEEPWAVS" = "move" ] ; then
1011                                 rm -f "$IN"
1012                         fi
1013                 fi
1014         fi
1015         # Other encoders fall through to normal encoding as the tracks have not
1016         # been entered in the status file.
1017 }
1018
1019 # do_encode [tracknumber] [hostname]
1020 # If no hostname is specified, encode locally
1021 # variables used:
1022 # TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
1023 do_encode ()
1024 {
1025         if [ "$USEPIPES" = "y" ]; then
1026                 case "$OUTPUT" in
1027                         mp3)
1028                                 TEMPARG="PIPE_$MP3ENCODERSYNTAX"
1029                                 ;;
1030                         vorbis|ogg)
1031                                 TEMPARG="PIPE_$OGGENCODERSYNTAX"
1032                                 ;;
1033                         opus)
1034                                 TEMPARG="PIPE_$OPUSENCODERSYNTAX"
1035                                 ;;
1036                         flac)
1037                                 TEMPARG="PIPE_$FLACENCODERSYNTAX"
1038                                 ;;
1039                         spx)
1040                                 TEMPARG="PIPE_$SPEEXENCODER"
1041                                 ;;
1042                         mpc)
1043                                 TEMPARG="PIPE_$MPCENCODER"
1044                                 ;;
1045                         m4a)
1046                                 TEMPARG="PIPE_$AACENCODERSYNTAX"
1047                                 ;;
1048                 esac
1049                 IN="$( eval echo "\$$TEMPARG" )"
1050         else
1051                 IN="$ABCDETEMPDIR/track$1.wav"
1052                 case "$OUTPUT" in
1053                         mp3)
1054                                 case "$MP3ENCODERSYNTAX" in
1055                                         # FIXME # check if mp3enc needs -if for pipes
1056                                         # FIXME # I have not been able to find a working mp3enc binary
1057                                         mp3enc)
1058                                                 FILEARG="-if $IN"
1059                                                 ;;
1060                                         *)
1061                                                 FILEARG="$IN"
1062                                                 ;;
1063                                 esac
1064                                 ;;
1065                         *)
1066                                 FILEARG="$IN"
1067                                 ;;
1068                 esac
1069         fi
1070         # We need IN to proceed, if we are not using pipes.
1071         if [ -s "$IN" -o X"$USEPIPES" = "Xy" ] ; then
1072                 for TMPOUTPUT in $(echo $OUTPUTTYPE | tr , \ )
1073                 do
1074                         case "$TMPOUTPUT" in
1075                                 vorbis|ogg)
1076                                         OUTPUT=$OGGOUTPUTCONTAINER
1077                                         ;;
1078                                 opus)
1079                                         OUTPUT=$OPUSOUTPUTCONTAINER
1080                                         ;;
1081                                 flac)
1082                                         OUTPUT=$FLACOUTPUTCONTAINER
1083                                         ;;
1084                                 *)
1085                                         OUTPUT=$TMPOUTPUT
1086                                         ;;
1087                         esac
1088                         OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
1089                         if [ "$NOGAP" = "y" ] && checkstatus encodetrack-$OUTPUT-$1 ; then
1090                                 continue
1091                         fi
1092                         if [ X"$USEPIPES" = "Xy" ]; then
1093                                 RUN_COMMAND=""
1094                                 # We need a way to store the creation of the files when using PIPES
1095                                 RUN_COMMAND_PIPES="run_command encodetrack-$OUTPUT-$1 true"
1096                                 # When pipping it does not make sense to have a higher nice for
1097                                 # reading than for encoding, since it will be hold by the
1098                                 # encoding process. Setting an effective nice, to calm down a
1099                                 # bit the reading process.
1100                                 EFFECTIVE_NICE=$READNICE
1101                         else
1102                                 run_command '' echo "Encoding track $1 of $TRACKS: $TRACKNAME..."
1103                                 RUN_COMMAND="run_command encodetrack-$OUTPUT-$1"
1104                                 EFFECTIVE_NICE=$ENCNICE
1105                         fi
1106                         case "$OUTPUT" in
1107                         mp3)
1108                                 case "$2" in
1109                                 %local*%)
1110                                         case "$MP3ENCODERSYNTAX" in
1111                                         lame|toolame|gogo) $RUN_COMMAND nice $EFFECTIVE_NICE $MP3ENCODER $MP3ENCODEROPTS "$IN" "$OUT" ;;
1112                                         bladeenc) $RUN_COMMAND nice $EFFECTIVE_NICE $MP3ENCODER $MP3ENCODEROPTS -quit "$IN" ;;
1113                                         l3enc|xingmp3enc) $RUN_COMMAND nice $EFFECTIVE_NICE $MP3ENCODER "$IN" "$OUT" $MP3ENCODEROPTS ;;
1114                                         # FIXME # Relates to the previous FIXME since it might need the "-if" removed.
1115                                         mp3enc) $RUN_COMMAND nice $EFFECTIVE_NICE $MP3ENCODER -if "$IN" -of "$OUT" $MP3ENCODEROPTS ;;
1116                                         esac
1117                                         ;;
1118                                 *)
1119                                         $RUN_COMMAND nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" >/dev/null 2>&1
1120                                         ;;
1121                                 esac
1122                                 ;;
1123                         vorbis|ogg)
1124                                 case "$2" in
1125                                 %local*%)
1126                                         case "$OGGENCODERSYNTAX" in
1127                                         vorbize) $RUN_COMMAND nice $EFFECTIVE_NICE $OGGENCODER $OGGENCODEROPTS -w "$OUT" "$IN" ;;
1128                                         oggenc) $RUN_COMMAND nice $EFFECTIVE_NICE $OGGENCODER $OGGENCODEROPTS -o "$OUT" "$IN" ;;
1129                                         esac
1130                                         ;;
1131                                 *)
1132                                         $RUN_COMMAND nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" >/dev/null 2>&1
1133                                         ;;
1134                                 esac
1135                                 ;;
1136                         opus)
1137                                 case "$2" in
1138                                 %local*%)
1139                                         case "$OPUSENCODERSYNTAX" in
1140                                         opusenc) 
1141
1142                                             if [ "$(eval echo ${COMMENT})" != "" ]; then
1143                                                 case "$COMMENT" in
1144                                                     *=*) ;;
1145                                                      *)   COMMENT="COMMENT=$COMMENT" ;;
1146                                                 esac    
1147                                             fi
1148                                             # Tag the file at encode time, as it can't be done after encoding.
1149                                             if [ "$DOTAG" = "y" ]; then
1150                                               $RUN_COMMAND nice $EFFECTIVE_NICE $OPUSENCODER $OPUSENCODEROPTS --artist "$TRACKARTIST" --comment album="$DALBUM" --title "$TRACKNAME" --comment track="$1" --comment genre="$CDGENRE" --comment year="$CDYEAR" ${COMMENT:+--comment "$COMMENT"} "$IN" "$OUT"
1151                                             else
1152                                               $RUN_COMMAND nice $EFFECTIVE_NICE $OPUSENCODER $OPUSENCODEROPTS "$IN" "$OUT"  
1153                                             fi
1154                                               ;;
1155                                         esac
1156                                         ;;
1157                                 *)
1158                                         $RUN_COMMAND nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" >/dev/null 2>&1
1159                                         ;;
1160                                 esac
1161                                 ;;
1162                         flac)
1163                                 case "$2" in
1164                                 %local*%)
1165                                         case "$FLACENCODERSYNTAX" in
1166                                         flac) $RUN_COMMAND nice $EFFECTIVE_NICE $FLACENCODER -f $FLACENCODEROPTS -o "$OUT" "$IN" ;;
1167                                                 esac
1168                                                 ;;
1169                                         *)
1170                                                 vecho -n "DISTMP3:"
1171                                                 vecho "$DISTMP3 $DISTMP3OPTS $2 $IN $OUT >/dev/null 2>&1"
1172                                                 $RUN_COMMAND nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" > /dev/null 2>&1
1173                                         ;;
1174                                 esac
1175                                 ;;
1176                         spx)
1177                                 if [ "$(eval echo ${COMMENT})" != "" ]; then
1178                                         case "$COMMENT" in
1179                                                 *=*) ;;
1180                                                 *)   COMMENT="COMMENT=$COMMENT" ;;
1181                                         esac    
1182                                 fi
1183                                 # Tag the file at encode time, as it can't be done after encoding.
1184                                 if [ "$DOTAG" = "y" ]; then
1185                                         $RUN_COMMAND nice $EFFECTIVE_NICE $SPEEXENCODER $SPEEXENCODEROPTS --author "$TRACKARTIST" --title "$TRACKNAME" ${COMMENT:+--comment "$COMMENT"} "$IN" "$OUT"
1186                                 else
1187                                         $RUN_COMMAND nice $EFFECTIVE_NICE $SPEEXENCODER $SPEEXENCODEROPTS "$IN" "$OUT"
1188                                 fi
1189                                 ;;
1190                         mpc)    
1191                                 # MPP/MP+(Musepack) format (.mpc) is done locally, with inline
1192                                 # tagging. Uses mpcenc for Musepack SV8 encoding, changed from
1193                                 # Musepack SV7 which used mppenc....
1194                                 $RUN_COMMAND nice $EFFECTIVE_NICE $MPCENCODER $MPCENCODEROPTS --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --track "$1" --genre "$CDGENRE" --year "$CDYEAR" --comment "$COMMENT" "$IN" "$OUT"
1195                                 ;;
1196                         m4a)
1197                                 # Tag the file at encode time, as it can't be done after encoding.
1198                                 if [ "$AACENC" = "neroAacEnc" ] ; then
1199                                         DOTAG=y
1200                                         $RUN_COMMAND nice $ENCNICE $AACENC $AACENCOPTS -if "$IN" -of "$OUT"
1201                                                                         elif [ ! "$DOTAG" = "y" ]; then
1202                                         $RUN_COMMAND nice $EFFECTIVE_NICE $AACENCODER $AACENCODEROPTS --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --track "$1" --genre "$CDGENRE" --year "$CDYEAR" --comment "$COMMENT" -o "$OUT" "$IN"
1203                                         
1204                                 else
1205                                         $RUN_COMMAND nice $ENCNICE $AACENCODER $AACENCODEROPTS -o "$OUT" "$IN"
1206                                 fi
1207                                 ;;
1208                         wav)
1209                                 # In case of wav output we need nothing. Just keep the wavs.
1210                                 ;;
1211                         esac
1212                         $RUN_COMMAND_PIPES
1213                 done
1214                 # Only remove .wav if the encoding succeeded
1215                 if checkerrors "encodetrack-(.{3,6})-$1"; then :; else
1216                         run_command encodetrack-$1 true
1217                         if [ ! "$KEEPWAVS" = "y" ] ; then
1218                                 if [ ! "$KEEPWAVS" = "move" ] ; then
1219                                         rm -f "$IN"
1220                                 fi
1221                         fi
1222                 fi
1223         else
1224                 run_command "" echo "HEH! The file we were about to encode disappeared:"
1225                 run_command "" echo ">> $IN"
1226                 run_command encodetrack-$1 false
1227         fi
1228 }
1229
1230 # do_preprocess [tracknumber]
1231 # variables used:
1232 # TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
1233 #do_preprocess ()
1234 #{
1235 #       IN="$ABCDETEMPDIR/track$1.wav"
1236 #       # We need IN to proceed.
1237 #       if [ -s "$IN" ] ; then
1238 #               for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
1239 #               do
1240 #                       #OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
1241 #                       run_command '' echo "Pre-processing track $1 of $TRACKS..."
1242 #                       case "$POSTPROCESSFORMAT" in
1243 #                       all|wav*)
1244 #                               run_command preprocess-$OUTPUT-$1 nice $PRENICE $WAV_PRE $IF $OF ;;
1245 #                       mp3)
1246 #                               run_command preprocess-$OUTPUT-$1 nice $PRENICE $MP3_PRE $IF $OF ;;
1247 #                       ogg)
1248 #                               run_command preprocess-$OUTPUT-$1 nice $PRENICE $OGG_PRE $IF $OF ;;
1249 #                       flac)
1250 #                               run_command preprocess-$OUTPUT-$1 nice $PRENICE $FLAC_PRE $IF $OF ;;
1251 #                       spx)
1252 #                               run_command preprocess-$OUTPUT-$1 nice $PRENICE $SPX_PRE $IF $OF ;;
1253 #                       esac
1254 #               done
1255 #               # Only remove .wav if the encoding succeeded
1256 #               if checkerrors "preprocess-(.{3,4})-$1"; then
1257 #                       run_command preprocess-$1 false
1258 #               else
1259 #                       run_command preprocess-$1 true
1260 #               fi
1261 #       else
1262 #               if [ "$(checkstatus encode-output)" = "loud" ]; then
1263 #                       echo "HEH! The file we were about to pre-process disappeared:"
1264 #                       echo ">> $IN"
1265 #               fi
1266 #               run_command preprocess-$1 false
1267 #       fi
1268 #}
1269
1270
1271 # do_postprocess [tracknumber]
1272 # variables used:
1273 # TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
1274 #do_postprocess ()
1275 #{
1276 #       for POSTPROCESSFORMAT in $(echo $POSTPROCESSFORMATS | tr , \ )
1277 #       do
1278 #               IN="$ABCDETEMPDIR/track$1.$POSTPROCESSFORMAT"
1279 #               # We need IN to proceed.
1280 #               if [ -s "$IN" ] ; then
1281 #                       #OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
1282 #                       run_command '' echo "Post-processing track $1 of $TRACKS..."
1283 #                       case "$POSTPROCESSFORMAT" in
1284 #                               mp3)
1285 #                                       run_command postprocess-$OUTPUT-$1 nice $POSTNICE $MP3_POST $IF $OF ;;
1286 #                               ogg)
1287 #                                       run_command postprocess-$OUTPUT-$1 nice $POSTNICE $OGG_POST $IF $OF ;;
1288 #                               flac)
1289 #                                       run_command postprocess-$OUTPUT-$1 nice $POSTNICE $FLAC_POST $IF $OF ;;
1290 #                               spx)
1291 #                                       run_command postprocess-$OUTPUT-$1 nice $POSTNICE $SPX_POST $IF $OF ;;
1292 #                       esac
1293 #                       # Only remove .wav if the encoding succeeded
1294 #                       if checkerrors "postprocess-(.{3,4})-$1"; then
1295 #                               run_command postprocess-$1 false
1296 #                       else
1297 #                               run_command postprocess-$1 true
1298 #                       fi
1299 #               else
1300 #                       if [ "$(checkstatus encode-output)" = "loud" ]; then
1301 #                               echo "HEH! The file we were about to post-process disappeared:"
1302 #                               echo ">> $IN"
1303 #                       fi
1304 #                       run_command postprocess-$1 false
1305 #               fi
1306 #       done
1307 #}
1308
1309 # do_single_gain
1310 # variables used:
1311 # FIXME #
1312 do_single_gain ()
1313 {
1314 :
1315 }
1316
1317 # do_batch_gain
1318 # variables used:
1319 # MP3GAIN, MP3GAINOPTS, VORBISGAIN, VORBISGAINOPTS, MPPGAIN, MPPGAINOPTS
1320 # FIXME #
1321 do_batch_gain ()
1322 {
1323         # The commands here don't go through run_command because they're never supposed to be silenced
1324         echo "Batch analizing gain in tracks: $TRACKQUEUE"
1325         (
1326         cd "$ABCDETEMPDIR"
1327         BLURB=
1328         TRACKFILES=
1329         for UTRACKNUM in $TRACKQUEUE
1330         do
1331                 MP3FILES="$TRACKFILES track$UTRACKNUM.mp3"
1332         done
1333         # FIXME # Hard-coded batch option!
1334         $NORMALIZER -b $NORMALIZEROPTS $TRACKFILES
1335         RETURN=$?
1336         if [ "$RETURN" != "0" ]; then
1337                 echo "batch-normalize: $NORMALIZER returned code $RETURN" >> errors
1338         else
1339                 for UTRACKNUM in $TRACKQUEUE
1340                 do
1341                         echo normalizetrack-$UTRACKNUM >> status
1342                 done
1343         fi
1344         )
1345 }
1346
1347 # do_batch_normalize
1348 # variables used:
1349 # NORMALIZER, NORMALIZEROPTS
1350 do_batch_normalize ()
1351 {
1352         # The commands here don't go through run_command because they're never supposed to be silenced
1353         echo "Batch normalizing tracks: $TRACKQUEUE"
1354         (
1355         cd "$ABCDETEMPDIR"
1356         BLURB=
1357         TRACKFILES=
1358         for UTRACKNUM in $TRACKQUEUE
1359         do
1360                 TRACKFILES="$TRACKFILES track$UTRACKNUM.wav"
1361         done
1362         # XXX: Hard-coded batch option!
1363         $NORMALIZER -b $NORMALIZEROPTS $TRACKFILES
1364         RETURN=$?
1365         if [ "$RETURN" != "0" ]; then
1366                 echo "batch-normalize: $NORMALIZER returned code $RETURN" >> errors
1367         else
1368                 for UTRACKNUM in $TRACKQUEUE
1369                 do
1370                         echo normalizetrack-$UTRACKNUM >> status
1371                 done
1372         fi
1373         )
1374 }
1375
1376 # do_normalize [tracknumber]
1377 # variables used:
1378 # TRACKS, TRACKNAME, NORMALIZER, NORMALIZEROPTS
1379 do_normalize ()
1380 {
1381         IN="$ABCDETEMPDIR/track$1.wav"
1382         if [ -e "$IN" ] ; then
1383                 run_command '' echo "Normalizing track $1 of $TRACKS: $TRACKNAME..."
1384                 run_command normalizetrack-$1 $NORMALIZER $NORMALIZEROPTS "$IN"
1385         else
1386                 if [ "$(checkstatus encode-output)" = "loud" ]; then
1387                         echo "HEH! The file we were about to normalize disappeared:"
1388                         echo ">> $IN"
1389                 fi
1390                 run_command normalizetrack-$1 false "File $IN was not found"
1391         fi
1392 }
1393
1394 # do_move [tracknumber]
1395 # Deduces the outfile from environment variables
1396 # Creates directory if necessary
1397 # variables used:
1398 # TRACKNUM, TRACKNAME, TRACKARTIST, DALBUM, OUTPUTFORMAT, CDGENRE, CDYEAR
1399 do_move ()
1400 {
1401         for TMPOUTPUT in $(echo $OUTPUTTYPE | tr , \ )
1402         do
1403                 # For now, set OUTPUT as TMPOUTPUT, and then change it once we have
1404                 # defined the OUTPUTFILE:
1405                 OUTPUT="$TMPOUTPUT"
1406
1407                 # Create ALBUMFILE, ARTISTFILE, TRACKFILE
1408                 # Munge filenames as follows:
1409                 # ' ' -> '_'
1410                 # '/' -> '_'
1411                 # ''' -> ''
1412                 # '?' -> ''
1413                 # Eat control characters
1414                 ALBUMFILE="$(mungefilename "$DALBUM")"
1415                 ARTISTFILE="$(mungefilename "$TRACKARTIST")"
1416                 TRACKFILE="$(mungefilename "$TRACKNAME")"
1417                 GENRE="$(mungegenre "$GENRE")"
1418                 YEAR=${CDYEAR:-$CDYEAR}
1419                 # If we want to start the tracks with a given number, we need to modify
1420                 # the TRACKNUM value before evaluation
1421                 gettracknum
1422                 # Supported variables for OUTPUTFORMAT are GENRE, YEAR, ALBUMFILE,
1423                 # ARTISTFILE, TRACKFILE, and TRACKNUM.
1424                 if [ "$ONETRACK" = "y" ]; then
1425                         if [ "$VARIOUSARTISTS" = "y" ]; then
1426                                 OUTPUTFILE="$(eval echo \""$VAONETRACKOUTPUTFORMAT"\")"
1427                         else
1428                                 OUTPUTFILE="$(eval echo \""$ONETRACKOUTPUTFORMAT"\")"
1429                         fi
1430                 else    
1431                         if [ "$VARIOUSARTISTS" = "y" ]; then
1432                                 OUTPUTFILE="$(eval echo \""$VAOUTPUTFORMAT"\")"
1433                         else
1434                                 OUTPUTFILE="$(eval echo \""$OUTPUTFORMAT"\")"
1435                         fi
1436                 fi
1437                 if checkerrors "tagtrack-$OUTPUT-$1"; then :; else
1438                         # Once we know the specific output was successful, we can change
1439                         # the OUTPUT to the value containing the container
1440                         case $TMPOUTPUT in
1441                                 vorbis|ogg)
1442                                         OUTPUT=$OGGOUTPUTCONTAINER
1443                                         ;;
1444                                 opus)
1445                                         OUTPUT=$OPUSOUTPUTCONTAINER
1446                                         ;;
1447                                 flac)
1448                                         OUTPUT=$FLACOUTPUTCONTAINER
1449                                         ;;
1450                                 *)
1451                                         OUTPUT=$TMPOUTPUT
1452                                         ;;
1453                         esac
1454                         # Check that the directory for OUTPUTFILE exists, if it doesn't, create it
1455                         OUTPUTFILEDIR="$(dirname "$OUTPUTDIR/$OUTPUTFILE")"
1456                         case $OUTPUT in
1457                                 wav)
1458                                         if [ "$DOCLEAN" != "y" ] && [ "$FORCE" != "y" ]; then
1459                                                 # FIXME # introduce warnings?
1460                                                 :
1461                                         else
1462                                                 # mkdir -p shouldn't return an error if the directory already exists
1463                                                 mkdir -p "$OUTPUTFILEDIR"
1464                                                 run_command movetrack-$1 mv "$ABCDETEMPDIR/track$1.$OUTPUT" "$OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
1465                                                 if checkstatus movetrack-output-$OUTPUT; then :; else
1466                                                         run_command movetrack-output-$OUTPUT true
1467                                                 fi
1468                                         fi
1469                                         ;;
1470                                 *)
1471                                         # mkdir -p shouldn't return an error if the directory already exists
1472                                         mkdir -p "$OUTPUTFILEDIR"
1473                                         run_command movetrack-$1 mv "$ABCDETEMPDIR/track$1.$OUTPUT" "$OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
1474                                         if checkstatus movetrack-output-$OUTPUT; then :; else
1475                                                 run_command movetrack-output-$OUTPUT true
1476                                         fi
1477                                         ;;
1478                         esac
1479                         # Lets move the cue file
1480                         if CUEFILE=$(checkstatus cuefile) >/dev/null ; then
1481                                 if [ -r "$ABCDETEMPDIR/$CUEFILE" ]; then
1482                                         if checkstatus movecue-$OUTPUT; then :; else
1483                                                 # Silence the Copying output since it overlaps with encoding processes...
1484                                                 #run_command '' vecho "Copying cue file to its destination directory..."
1485                                                 if checkstatus onetrack >/dev/null ; then
1486                                                         case $OUTPUT in
1487                                                                 wav)
1488                                                                         if [ "$DOCLEAN" != "y" ] && [ "$FORCE" != "y" ]; then
1489                                                                                 # We dont have the dir, since it was not created before.
1490                                                                                 :
1491                                                                         else
1492                                                                                 run_command movecue-$OUTPUT cp "$ABCDETEMPDIR/$CUEFILE" "$OUTPUTDIR/$OUTPUTFILE.cue"
1493                                                                         fi
1494                                                                         ;;
1495                                                                 # NOTE: Creating a cue file with the 3-char-extension files is to comply with
1496                                                                 # http://brianvictor.tripod.com/mp3cue.htm#details
1497                                                                 [a-z0-9][a-z0-9][a-z0-9])
1498                                                                         run_command movecue-$OUTPUT cp "$ABCDETEMPDIR/$CUEFILE" "$OUTPUTDIR/$OUTPUTFILE.cue"
1499                                                                         ;;
1500                                                                 *)
1501                                                                         run_command movecue-$OUTPUT cp "$ABCDETEMPDIR/$CUEFILE" "$OUTPUTDIR/$OUTPUTFILE.$OUTPUT.cue"
1502                                                                         ;;
1503                                                         esac
1504                                                 else
1505                                                         run_command movecue-$OUTPUT cp "$ABCDETEMPDIR/$CUEFILE" "$OUTPUTFILEDIR/$CUEFILE"
1506                                                 fi
1507                                                 echo movecue-$OUTPUT >> "$ABCDETEMPDIR/status"
1508                                         fi
1509                                 fi
1510                         fi
1511                 fi
1512         done
1513 }
1514
1515 # do_playlist
1516 # Create the playlist if wanted
1517 # Variables used:
1518 # PLAYLISTFORMAT, PLAYLISTDATAPREFIX, VAPLAYLISTFORMAT, VAPLAYLISTDATAPREFIX,
1519 # VARIOUSARTISTS, OUTPUTDIR
1520 do_playlist ()
1521 {
1522         for TMPOUTPUT in $(echo $OUTPUTTYPE | tr , \ )
1523         do
1524                 case $TMPOUTPUT in
1525                         vorbis|ogg)
1526                                 OUTPUT=$OGGOUTPUTCONTAINER
1527                                 ;;
1528                         opus)
1529                                 OUTPUT=$OPUSOUTPUTCONTAINER
1530                                 ;;
1531                         flac)
1532                                 OUTPUT=$FLACOUTPUTCONTAINER
1533                                 ;;
1534                         *)
1535                                 OUTPUT=$TMPOUTPUT
1536                                 ;;
1537                 esac
1538                 # Create a playlist file for the playlist data to go into.
1539                 # We used to wipe it out if it existed. Now we request permision if interactive.
1540                 for LASTTRACK in $TRACKQUEUE; do :; done
1541                 ALBUMFILE="$(mungefilename "$DALBUM")"
1542                 ARTISTFILE="$(mungefilename "$DARTIST")"
1543                 GENRE="$(mungegenre "$GENRE")"
1544                 YEAR=${CDYEAR:-$CDYEAR}
1545                 if [ "$VARIOUSARTISTS" = "y" ] ; then
1546                         PLAYLISTFILE="$(eval echo "$VAPLAYLISTFORMAT")"
1547                 else
1548                         PLAYLISTFILE="$(eval echo "$PLAYLISTFORMAT")"
1549                 fi
1550                 FINALPLAYLISTDIR="$(dirname "$OUTPUTDIR/$PLAYLISTFILE")"
1551                 mkdir -p "$FINALPLAYLISTDIR"
1552                 if [ -s "$OUTPUTDIR/$PLAYLISTFILE" ]; then
1553                         echo -n "Erase, Append to, or Keep the existing playlist file? [e/a/k] (e): " >&2
1554                         if [ "$INTERACTIVE" = "y" ]; then
1555                                 while [ "$DONE" != "y" ]; do
1556                                         read ERASEPLAYLIST
1557                                         case $ERASEPLAYLIST in
1558                                                 e|E|a|A|k|K) DONE=y ;;
1559                                                 "") ERASEPLAYLIST=e ; DONE=y ;;
1560                                                 *) ;;
1561                                         esac
1562                                 done
1563                         else
1564                                 echo e >&2
1565                                 ERASEPLAYLIST=e
1566                         fi
1567                         # Once we erase the playlist, we use append to create the new one.
1568                         [ "$ERASEPLAYLIST" = "e" -o "$ERASEPLAYLIST" = "E" ] && rm -f "$OUTPUTDIR/$PLAYLISTFILE" && ERASEPLAYLIST=a
1569                 else
1570                         # The playlist does not exist, so we can safelly use append to create the new list
1571                         ERASEPLAYLIST=a
1572                 fi
1573                 if [ "$ERASEPLAYLIST" = "a" -o "$ERASEPLAYLIST" = "A" ]; then
1574                         touch "$OUTPUTDIR/$PLAYLISTFILE"
1575                         for UTRACKNUM in $TRACKQUEUE
1576                         do
1577                                 # Shares some code with do_move since the filenames have to match
1578                                 CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
1579                                 getcddbinfo TRACKNAME
1580                                 splitvarious
1581                                 TRACKFILE="$(mungefilename "$TRACKNAME")"
1582                                 ARTISTFILE="$(mungefilename "$TRACKARTIST")"
1583                                 ALBUMFILE="$(mungefilename "$DALBUM")"
1584                                 # If we want to start the tracks with a given number, we need to modify the
1585                                 # TRACKNUM value before evaluation
1586                                 gettracknum
1587                                 if [ "$VARIOUSARTISTS" = "y" ]; then
1588                                         OUTPUTFILE="$(eval echo \""$VAOUTPUTFORMAT\"")"
1589                                 else
1590                                         OUTPUTFILE="$(eval echo \""$OUTPUTFORMAT\"")"
1591                                 fi
1592                                 if [ "$VARIOUSARTISTS" = "y" ]; then
1593                                         if [ "$VAPLAYLISTDATAPREFIX" ] ; then
1594                                                 echo ${VAPLAYLISTDATAPREFIX}$OUTPUTFILE.$OUTPUT >> "$OUTPUTDIR/$PLAYLISTFILE"
1595                                         else
1596                                                 relpath "$PLAYLISTFILE", "$OUTPUTFILE.$OUTPUT" >> "$OUTPUTDIR/$PLAYLISTFILE"
1597                                         fi
1598                                 else
1599                                         if [ "$PLAYLISTDATAPREFIX" ]; then
1600                                                 echo ${PLAYLISTDATAPREFIX}$OUTPUTFILE.$OUTPUT >> "$OUTPUTDIR/$PLAYLISTFILE"
1601                                         else
1602                                                 relpath "$PLAYLISTFILE", "$OUTPUTFILE.$OUTPUT" >> "$OUTPUTDIR/$PLAYLISTFILE"
1603                                         fi
1604                                 fi
1605                         done
1606                 fi
1607                 ## this will convert the playlist to have CRLF line-endings, if specified
1608                 ## (some hardware players insist on CRLF endings)
1609                 if [ "$DOSPLAYLIST" = "y" ]; then
1610                         awk '{sub("\r$",""); printf "%s\r\n", $0}' "$OUTPUTDIR/$PLAYLISTFILE" > "$ABCDETEMPDIR/PLAYLISTFILE.tmp"
1611 #                       mv -f "$ABCDETEMPDIR/PLAYLISTFILE.tmp" "$OUTPUTDIR/$PLAYLISTFILE"
1612                         cat "$ABCDETEMPDIR/PLAYLISTFILE.tmp" | sed 's/\//\\/' > "$OUTPUTDIR/$PLAYLISTFILE"
1613                 fi
1614                 echo "playlistcomplete" >> "$ABCDETEMPDIR/status"
1615         done
1616 }
1617
1618 # abcde.cue2discid
1619 # This function reads a cuefile on stdin and writes an extended
1620 # cddb query on stdout.  Any PREGAP for track 1 is properly
1621 # handled, although cue files embedded in FLAC files do not
1622 # appear to properly store the PREGAP setting. :(
1623 abcde.cue2discid () {
1624
1625         cddb_sum () {
1626                 val=$1
1627                 ret=0
1628                 while [ $val -gt 0 ] ; do
1629                         ret=$(( $ret + ( $val % 10) ))
1630                         val=$(( $val / 10 ))
1631                 done
1632                 echo $ret
1633         }
1634
1635         msf2lba () {
1636                 OIFS="$IFS"
1637                 IFS=":"
1638                 set -- $1
1639                 IFS="$OIFS"
1640                 local first second third
1641                 first=$(expr ${1} + 0 )
1642                 second=$(expr ${2} + 0 )
1643                 third=$(expr ${3} + 0 )
1644
1645                 echo $(( ((($first * 60) + $second) * 75) + $third ))
1646         }
1647
1648         OFFSET=150
1649         PREGAP=0
1650         LEADOUT=0
1651         LEADIN=88200
1652         i=0
1653         N=0
1654         
1655         while read line ; do
1656                 set -- $line
1657                 case "$1" in
1658                 TRACK)  i=$(( i + 1 ))
1659                         ;;
1660                 INDEX)  if [ "$2" -eq 1 ] ; then
1661                                 LBA=$(msf2lba $3)
1662                                 START=$(( $LBA + $PREGAP + $OFFSET ))
1663                                 eval TRACK$i=$START
1664                                 X=$(cddb_sum $(( $START / 75 )) )
1665                                 N=$(( $N + $X ))
1666                         fi
1667                         ;;
1668                 PREGAP) PREGAP=$(msf2lba $2)
1669                         ;;
1670                 REM)    case "$2" in
1671                         FLAC__lead-out)
1672                                 LEADOUT=$(( $4 / 588 ))
1673                                 ;;
1674                         FLAC__lead-in)
1675                                 LEADIN=$(( $3 / 588 ))
1676                                 ;;
1677                         esac
1678                         ;;
1679                 esac
1680                 
1681         done
1682         
1683         TRACKS=$i
1684         LEADOUT=$(( $LEADOUT + $LEADIN ))
1685
1686         LENGTH=$(( $LEADOUT/75 - $TRACK1/75 ))
1687         DISCID=$(( ( $N % 255 ) * 2**24 | $LENGTH * 2**8 | $TRACKS ))
1688         printf "%08x %i" $DISCID $TRACKS
1689         
1690         j=1
1691         while [ $j -le $TRACKS ] ; do
1692                 eval echo -n "\" \$TRACK$j\""
1693                 j=$((j+1))
1694         done
1695         echo " $(( $LEADOUT / 75 ))"
1696 }
1697
1698 # abcde.mkcue
1699 # abcde.mkcue [--wholedisk]
1700 # This creates a cuefile directly from the extended discid information
1701 # The --wholedisk option controls whether we're ripping data from the
1702 # start of track one or from the start of the disk (usually, but not
1703 # always the same thing!)
1704 #
1705 # Track one leadin/pregap (if any) handeling:
1706 # --wholedisk specified:
1707 #   TRACK 01 AUDIO
1708 #     INDEX 00 00:00:00
1709 #     INDEX 01 <pregap value>
1710 #   Remaining track index values unchanged from disc TOC
1711 #
1712 # --wholedisk not specified
1713 #   TRACK 01 AUDIO
1714 #     PREGAP <pregap value>
1715 #     INDEX 01 00:00:00
1716 #   Remaining track index values offset by <pregap value>
1717 #
1718 # Variables used:
1719 # TRACKINFO
1720 abcde.mkcue () {
1721
1722         echomsf () {
1723                 printf "$1%02i:%02i:%02i\n" $(($2/4500)) $((($2/75)%60)) $(($2%75))
1724         }
1725
1726         local MODE DISCID TRACKS
1727         local i OFFSET LBA
1728         local CUEWAVFILE
1729
1730         if [ "$1" = --wholedisc ] ; then
1731                 MODE=INDEX
1732         else
1733                 MODE=PREGAP
1734         fi
1735
1736         vecho "One track is $ONETRACK"
1737         TRACKFILE="$(mungefilename "$TRACKNAME")"
1738         ARTISTFILE="$(mungefilename "$TRACKARTIST")"
1739         ALBUMFILE="$(mungefilename "$DALBUM")"
1740         if [ "$ONETRACK" = "y" ]; then
1741                 if [ "$VARIOUSARTISTS" = "y" ]; then
1742                         CUEWAVFILE="$(eval echo \""$VAONETRACKOUTPUTFORMAT"\" | sed -e 's@^.*/@@').$OUTPUT"
1743                 else
1744                         CUEWAVFILE="$(eval echo \""$ONETRACKOUTPUTFORMAT"\" | sed -e 's@^.*/@@').$OUTPUT"
1745                 fi
1746                 vecho "Cue wav file is $CUEWAVFILE"
1747         else
1748                 CUEWAVFILE="dummy.wav"
1749         fi
1750
1751         set -- $TRACKINFO
1752
1753         DISCID=$1
1754         TRACKS=$2
1755         shift 2
1756
1757         echo REM DISCID $DISCID
1758         echo FILE \""$CUEWAVFILE"\" WAVE
1759
1760         if [ $1 -ne 150 ] && [ $MODE = "PREGAP" ] ; then
1761                 OFFSET=$1
1762         else
1763                 OFFSET=150
1764         fi
1765
1766         i=1
1767         while [ $i -le "$TRACKS" ] ; do
1768                 LBA=$(( $1 - $OFFSET ))
1769                 printf "  TRACK %02i AUDIO\n" $i
1770                 if [ $i -eq 1 -a $1 -ne 150 ] ; then
1771                         if [ $MODE = PREGAP ] ; then
1772                                 echomsf "    PREGAP " $(($OFFSET-150))
1773                         else
1774                                 echo    "    INDEX 00 00:00:00"
1775                         fi
1776                 fi
1777                 echomsf "    INDEX 01 " $LBA
1778                 i=$(($i+1))
1779                 shift
1780         done
1781 }
1782
1783 # do_discid
1784 # This essentially the start of things
1785 do_discid ()
1786 {
1787         # Query the CD to get the track info, unless the user specified -C
1788         # or we are using some actions which do not need the CDDB data at all
1789         #if [ ! X"$EXPACTIONS" = "X" ]; then
1790         #       :
1791         #elif [ -z "$DISCID" ]; then
1792         if [ -z "$DISCID" ]; then
1793                 vecho -n "Getting CD track info... "
1794                 # In OSX, unmount the disc before a query
1795                 if [ "$OSFLAVOUR" = "OSX" ]; then
1796                         diskutil unmount ${CDROM#/dev/}
1797                 fi
1798                 case "$CDROMREADERSYNTAX" in
1799                         flac)
1800                                 if $METAFLAC $METAFLACOPTS --export-cuesheet-to=- "$CDROM" > /dev/null 2>&1 ; then
1801                                         case "$CUE2DISCID" in
1802                                                 # FIXME # right now we have 2 cue2discid internal
1803                                                 # implementations: builtin and abcde.cue2discid. Test
1804                                                 # both of them and decide which one we want to use.
1805                                                 builtin)
1806                                                         #vecho "Using builtin cue2discid implementation..."
1807                                                         CUESHEET="$(metaflac $METAFLACOPTS --export-cuesheet-to=- "$CDROM")"
1808
1809                                                         #TRACKS=$(echo $CUESHEET | grep -E "TRACK \+[[:digit:]]\+ \+AUDIO" |wc -l)
1810                                                         #TRACKS=0
1811                                                         OFFSETTIMES=( $(echo "$CUESHEET" | sed -n -e's/\ *INDEX 01\ \+//p' ) )
1812                                                         TRACKS=${#OFFSETTIMES[@]}
1813                                                         unset OFFSETS
1814                                                         #echo "processing offsetimes ${OFFSETTIMES[@]}"
1815                                                         for OFFSETTIME in ${OFFSETTIMES[@]}; do
1816                                                                 OFFSETS="$OFFSETS $(( 10#${OFFSETTIME:0:2} * 4500 + 10#${OFFSETTIME:3:2} * 75 + 10#${OFFSETTIME:6:2} ))"
1817                                                                 #OFFSETS[${#OFFSETS[*]}]=$(( 10#${OFFSETTIME:0:2} * 4500 + 10#${OFFSETTIME:3:2} * 75 + 10#${OFFSETTIME:6:2} ))
1818                                                         done
1819
1820                                                         LEADOUT=$(( $(echo "$CUESHEET" | grep lead-out | get_last) * 75 / 44100 ))
1821                                                         LEADIN=$(( $(echo "$CUESHEET" | grep lead-in | get_last) * 75 / 44100 ))
1822                                                         makeids
1823                                                 ;;
1824                                                 *)
1825                                                         #vecho "Using external python cue2discid implementation..."
1826                                                         TRACKINFO=$($METAFLAC $METAFLACOPTS --export-cuesheet-to=- "$CDROM" | $CUE2DISCID)
1827                                                 ;;
1828                                         esac
1829                                 else
1830                                         log error "the input flac file does not contain a cuesheet."
1831                                         exit 1
1832                                 fi
1833                                 ;;
1834 #                       cdparanoia|debug)
1835 #                               CDPARANOIAOUTPUT="$( $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" -Q --verbose 2>&1 )"
1836 #                               RET=$?
1837 #                               if [ ! "$RET" = "0" ];then
1838 #                                       log warning "something went wrong while querying the CD... Maybe a DATA CD?"
1839 #                               fi
1840 #
1841 #                               TRACKS="$(echo "$CDPARANOIAOUTPUT" | grep -E '^[[:space:]]+[[:digit:]]' | tail -n 1 | get_first | tr -d "." | tr '\n' ' ')"
1842 #                               CDPARANOIAAUDIOTRACKS="$TRACKS"
1843 #
1844 #                               LEADOUT="$(echo "$CDPARANOIAOUTPUT" | grep -Eo '^TOTAL[[:space:]]+([[:digit:]]+)' | get_last)"
1845 #                               OFFSETS="$(echo "$CDPARANOIAOUTPUT" | sed -n -e's/^ .* \([0-9]\+\) \[.*/\1/p')"
1846 #                               makeids
1847 #                               ;;
1848                         *)
1849                                 case "$CDDBMETHOD" in
1850                                         cddb) TRACKINFO=$($CDDISCID "$CDROM") ;;
1851                                         musicbrainz) TRACKINFO=$($MUSICBRAINZ --command id --device "$CDROM") ;;
1852                                 esac
1853                                 ;;
1854                 esac
1855                 # Make sure there's a CD in there by checking cd-discid's return code
1856                 if [ ! "$?" = "0" ]; then
1857                         if [ "$CDROMREADERSYNTAX" = "flac" ] ; then
1858                                 log error "cuesheet information from the flac file could not be read."
1859                                 log error "Perhaps the flac file does not contain a cuesheet?."
1860                                 exit 1
1861                         else
1862                                 log error "CD could not be read. Perhaps there's no CD in the drive?"
1863                                 exit 1
1864                         fi
1865                 fi
1866                 # In OSX, remount the disc again
1867                 if [ "$OSFLAVOUR" = "OSX" ]; then
1868                         diskutil mount ${CDROM#/dev/}
1869                 fi
1870                 WEHAVEACD=y
1871                 DISCID=$(echo $TRACKINFO | cut -f1 -d' ')
1872         else
1873                 TRACKINFO=$(cat "$WAVOUTPUTDIR/abcde.$DISCID/discid")
1874         fi
1875
1876         # Get a full enumeration of tracks, sort it, and put it in the TRACKQUEUE.
1877         # This needs to be done now because a section of the resuming code will need
1878         # it later.
1879
1880         # get the number of digits to pad TRACKNUM with - we'll use this later
1881         # a CD can only hold 99 tracks, but since we support a feature for starting
1882         # numbering the tracks from a given number, we might need to set it as a
1883         # variable for the user to define... or obtain it somehow.
1884         if [ "$PADTRACKS" = "y" ] ; then
1885                 TRACKNUMPADDING=2
1886         fi
1887
1888         ABCDETEMPDIR="$WAVOUTPUTDIR/abcde.$(echo $TRACKINFO | cut -f1 -d' ')"
1889         if [ -z "$TRACKQUEUE" ]; then
1890                 if [ ! "$STRIPDATATRACKS" = "n" ]; then
1891                         case "$CDROMREADERSYNTAX" in
1892                                 cdparanoia|debug)
1893                                         if [ "$WEHAVEACD" = "y" ]; then
1894                                                 vecho "Querying the CD for audio tracks..."
1895                                                 CDPARANOIAOUTPUT="$( $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" -Q --verbose 2>&1 )"
1896                                                 RET=$?
1897                                                 if [ ! "$RET" = "0" ];then
1898                                                         log warning "something went wrong while querying the CD... Maybe a DATA CD?"
1899                                                 fi
1900                                                 TRACKS="$(echo "$CDPARANOIAOUTPUT" | grep -E '^[[:space:]]+[[:digit:]]' | tail -n 1 | get_first | tr -d "." | tr '\n' ' ')"
1901                                                 CDPARANOIAAUDIOTRACKS="$TRACKS"
1902                                         else
1903                                                 # Previous versions of abcde would store the tracks on a file, instead of the status record.
1904                                                 if [ -f "$ABCDETEMPDIR/cdparanoia-audio-tracks" ]; then
1905                                                         echo cdparanoia-audio-tracks=$( cat "$ABCDETEMPDIR/cdparanoia-audio-tracks" ) >> "$ABCDETEMPDIR/status"
1906                                                         rm -f "$ABCDETEMPDIR/cdparanoia-audio-tracks"
1907                                                 fi
1908                                                 if [ -f "$ABCDETEMPDIR/status" ] && TRACKS=$(checkstatus cdparanoia-audio-tracks); then :; else
1909                                                         TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
1910                                                 fi
1911                                         fi
1912                                         ;;
1913                                 *)      TRACKS=$(echo $TRACKINFO | cut -f2 -d' ') ;;
1914                         esac
1915                 else
1916                         TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
1917                 fi
1918                 if echo "$TRACKS" | grep "[[:digit:]]" > /dev/null 2>&1 ;then :;else
1919                         log info "The disc does not contain any tracks. Giving up..."
1920                         exit 0
1921                 fi
1922                 echo -n "Grabbing entire CD - tracks: "
1923                 if [ ! "$PADTRACKS" = "y" ] ; then
1924                         TRACKNUMPADDING=$(echo -n $TRACKS | wc -c | tr -d ' ')
1925                 fi
1926                 TRACKS=$(printf "%0.${TRACKNUMPADDING}d" $TRACKS)
1927                 X=0
1928                 while [ "$X" -ne "$TRACKS" ]
1929                 do
1930                         X=$(printf "%0.${TRACKNUMPADDING}d" $(expr $X + 1))
1931                         TRACKQUEUE=$(echo $TRACKQUEUE $X)
1932                 done
1933                 echo $TRACKQUEUE
1934         else
1935                 TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
1936                 # User-supplied track queue.
1937                 # Weed out non-numbers, whitespace, then sort and weed out duplicates
1938                 TRACKQUEUE=$(echo $TRACKQUEUE | sed 's-[^0-9 ]--g' | tr ' ' '\n' | grep -v ^$ | sort -n | uniq | tr '\n' ' ' | sed 's- $--g')
1939                 # Once cleaned, obtain the highest value in the trackqueue for number padding
1940                 for LASTTRACK in $TRACKQUEUE; do :; done
1941                 if [ ! "$PADTRACKS" = "y" ] ; then
1942                         TRACKNUMPADDING=$(echo -n $LASTTRACK | wc -c | tr -d ' ')
1943                 fi
1944                 # Now we normalize the trackqueue
1945                 for TRACK in $TRACKQUEUE ; do
1946                         TRACKNUM=$(printf %0.${TRACKNUMPADDING}d $(expr ${TRACK} + 0 ))
1947                         PADTRACKQUEUE=$(echo $PADTRACKQUEUE $TRACKNUM)
1948                 done
1949                 TRACKQUEUE=$PADTRACKQUEUE
1950                 echo Grabbing tracks: "$TRACKQUEUE"
1951         fi
1952
1953         QUEUEDTRACKS=$(echo $TRACKQUEUE | wc -w | tr -d ' ')
1954
1955         # We have the discid, create a temp directory after it to store all the temp
1956         # info
1957
1958         if [ -e "$ABCDETEMPDIR" ]; then
1959                 echo -n "abcde: attempting to resume from $ABCDETEMPDIR"
1960                 # It already exists, see if it's a directory
1961                 if [ ! -d "$ABCDETEMPDIR" ]; then
1962                         # This is a file/socket/fifo/device/etc, not a directory
1963                         # Complain and exit
1964                         echo >&2
1965                         echo "abcde: file $ABCDETEMPDIR already exists and does not belong to abcde." >&2
1966                         echo "Please investigate, remove it, and rerun abcde." >&2
1967                         exit 1
1968                 fi
1969                 echo -n .
1970                 # It's a directory, let's see if it's writable by us
1971                 if [ ! -r "$ABCDETEMPDIR" ] || [ ! -w "$ABCDETEMPDIR" ] || [ ! -x "$ABCDETEMPDIR" ]; then
1972                         # Nope, complain and exit
1973                         echo >&2
1974                         echo "abcde: directory $ABCDETEMPDIR already exists and is not writeable." >&2
1975                         echo "Please investigate, remove it, and rerun abcde." >&2
1976                         exit 1
1977                 fi
1978                 echo .
1979                 # See if it's populated
1980                 if [ ! -f "$ABCDETEMPDIR/discid" ]; then
1981                         # Wipe and start fresh
1982                         echo "abcde: $ABCDETEMPDIR/discid not found. Abcde must remove and recreate" >&2
1983                         echo -n "this directory to continue. Continue [y/N]? " >&2
1984                         if [ "$INTERACTIVE" = "y" ]; then
1985                                 read ANSWER
1986                         else
1987                                 echo y >&2
1988                                 ANSWER=y
1989                         fi
1990                         if [ "$ANSWER" != "y" ]; then
1991                                 exit 1
1992                         fi
1993                         rm -rf "$ABCDETEMPDIR" || exit 1
1994                         mkdir -p "$ABCDETEMPDIR"
1995                         if [ "$?" -gt "0" ]; then
1996                                 # Directory already exists or could not be created
1997                                 echo "abcde: Temp directory $ABCDETEMPDIR could not be created." >&2
1998                                 exit 1
1999                         fi
2000                 else
2001                         # Everything is fine. Check for ^encodetracklocation-
2002                         # and encode-output entries in the status file and
2003                         # remove them. These are not relevant across sessions.
2004                         if [ -f "$ABCDETEMPDIR/status" ]; then
2005                                 mv "$ABCDETEMPDIR/status" "$ABCDETEMPDIR/status.old"
2006                                 grep -v ^encodetracklocation- < "$ABCDETEMPDIR/status.old" \
2007                                         | grep -v ^encode-output > "$ABCDETEMPDIR/status"
2008                         fi
2009                         # Remove old error messages
2010                         if [ -f "$ABCDETEMPDIR/errors" ]; then
2011                                 rm -f "$ABCDETEMPDIR/errors"
2012                         fi
2013                 fi
2014         else
2015                 # We are starting from scratch
2016                 mkdir -p "$ABCDETEMPDIR"
2017                 if [ "$?" -gt "0" ]; then
2018                         # Directory already exists or could not be created
2019                         echo "abcde: Temp directory $ABCDETEMPDIR could not be created." >&2
2020                         exit 1
2021                 fi
2022                 cat /dev/null > "$ABCDETEMPDIR/status"
2023                 # Store the abcde version in the status file.
2024                 echo "abcde-version=$VERSION" >> "$ABCDETEMPDIR/status"
2025         fi
2026         if [ X"$DOCUE" = "Xy" -a X"$WEHAVEACD" = "Xy" ]; then
2027                 if checkstatus cuefile > /dev/null 2>&1 ; then :; else
2028                         CUEFILE=cue-$(echo "$TRACKINFO" | cut -f1 -d' ').txt
2029                         vecho "Creating cue file..."
2030                         case $CDROMREADERSYNTAX in
2031                                 flac)
2032                                         if $METAFLAC --export-cuesheet-to=- "$CDROM" > "$ABCDETEMPDIR/$CUEFILE"; then
2033                                                 echo cuefile=$CUEFILE >> "$ABCDETEMPDIR/status"
2034                                         else
2035                                                 log warning "the input flac file does not contain a cuesheet."
2036                                         fi
2037                                         ;;
2038                                 *)
2039                                         if $CUEREADER $CUEREADEROPTS > "$ABCDETEMPDIR/$CUEFILE"; then
2040                                                 echo cuefile=$CUEFILE >> "$ABCDETEMPDIR/status"
2041                                         else
2042                                                 log warning "reading the CUE sheet is still considered experimental"
2043                                                 log warning "and there was a problem with the CD reading. abcde will continue,"
2044                                                 log warning "but consider reporting the problem to the abcde author"
2045                                         fi
2046                                         ;;
2047                         esac
2048                 fi
2049         fi
2050         # If we got the CDPARANOIA status and it is not recorded, save it now
2051         if [ -n "$CDPARANOIAAUDIOTRACKS" ]; then
2052                 if checkstatus cdparanoia-audio-tracks > /dev/null 2>&1; then :; else
2053                         echo cdparanoia-audio-tracks=$CDPARANOIAAUDIOTRACKS >> "$ABCDETEMPDIR/status"
2054                 fi
2055         fi
2056         
2057         # Create the discid file
2058         echo "$TRACKINFO" > "$ABCDETEMPDIR/discid"
2059         if checkstatus cddbmethod > /dev/null 2>&1 ; then :; else
2060                 echo "cddbmethod=$CDDBMETHOD" >> "$ABCDETEMPDIR/status"
2061         fi
2062 }
2063
2064 # do_cleancue
2065 # Create a proper CUE file based on the CUE file we created before.
2066 do_cleancue()
2067 {
2068         if CUEFILE_IN="$ABCDETEMPDIR"/$(checkstatus cuefile); then
2069                 CUEFILE_OUT=$CUEFILE_IN.out
2070                 ### FIXME ### checkstatus cddb
2071                 if [ -e "$CDDBDATA" ]; then
2072                         vecho "Adding metadata to the cue file..."
2073                         # FIXME It doesn't preserve spaces! Why?
2074                         # FIXME parse $track into PERFORMER and TITLE - abcde already has code for this?
2075                         n=1
2076                         echo "PERFORMER \"$DARTIST\"" >> "$CUEFILE_OUT"
2077                         echo "TITLE \"$DALBUM\"" >> "$CUEFILE_OUT"
2078                         # Set IFS to <newline> to prevent read from swallowing spaces and tabs
2079                         OIFS="$IFS"
2080                         IFS='
2081 '
2082                         cat "$CUEFILE_IN" | while read line
2083                         do
2084                                 if echo "$line" | grep "INDEX 01" > /dev/null 2>&1 ; then
2085 # FIXME # Possible patch: remove the line above, uncomment the 2 lines below.
2086 #                               echo "$line" >> "$CUEFILE_OUT"
2087 #                               if echo "$line" | grep "^[[:space:]]*TRACK" > /dev/null 2>&1 ; then
2088                                         eval track="\$TRACK$n"
2089                                         n=$(expr $n + 1)
2090                                         echo "    TITLE \"$track\"" >> "$CUEFILE_OUT"
2091                                 # When making a single-track rip, put the
2092                                 # actual file name into the file declaration
2093                                 # in the cue file so that it is usable by
2094                                 # music players and the like
2095                                 elif [ "$ONETRACK" = "y" ] &&
2096                                                 echo "$line" | grep '^FILE "dummy.wav" WAVE' > /dev/null 2>&1 ; then
2097
2098                                         TRACKFILE="$(mungefilename "$TRACKNAME")"
2099                                         ARTISTFILE="$(mungefilename "$TRACKARTIST")"
2100                                         ALBUMFILE="$(mungefilename "$DALBUM")"
2101
2102                                         if [ "$VARIOUSARTISTS" = "y" ]; then
2103                                                 OUTPUTFILE="$(eval echo \""$VAONETRACKOUTPUTFORMAT"\" | sed -e 's@^.*/@@').$OUTPUT"
2104                                         else
2105                                                 OUTPUTFILE="$(eval echo \""$ONETRACKOUTPUTFORMAT"\" | sed -e 's@^.*/@@').$OUTPUT"
2106                                         fi
2107
2108                                         echo "FILE \"$OUTPUTFILE\" WAVE" >> "$CUEFILE_OUT"
2109                                         continue
2110                                 fi
2111 # FIXME # If the lines above are uncommented, remove the line below.
2112                                 echo "$line" >> "$CUEFILE_OUT"
2113                         done
2114                         IFS="$OIFS"
2115                         mv "$CUEFILE_OUT" "$CUEFILE_IN"
2116                         echo "cleancuefile" >> "$ABCDETEMPDIR/status"
2117                 fi
2118         fi
2119 }
2120
2121 # do_cddbparse
2122 # Parses a CDDB file and outputs the title and the track names.
2123 # Variables: CDDBFILE
2124 do_cddbparse ()
2125 {
2126         CDDBPARSEFILE="$1"
2127         # List out disc title/author and contents
2128         if [ "$ONETRACK" = "y" ]; then
2129                 vecho "ONETRACK mode selected: displaying only the title of the CD..."
2130         fi
2131         echo "---- $(grep DTITLE "${CDDBPARSEFILE}" | cut '-d=' -f2- | tr -d \\r\\n ) ----"
2132         if [ X"$SHOWCDDBYEAR" = "Xy" ]; then
2133                 PARSEDYEAR=$(grep DYEAR "${CDDBPARSEFILE}" | cut '-d=' -f2-)
2134                 if [ ! X"$PARSEDYEAR" = "X" ]; then
2135                         echo "Year: $PARSEDYEAR"
2136                 fi
2137         fi
2138         if [ X"$SHOWCDDBGENRE" = "Xy" ]; then
2139                 PARSEDGENRE=$(grep DGENRE "${CDDBPARSEFILE}" | cut '-d=' -f2-)
2140                 if [ ! X"$PARSEDGENRE" = "X" ]; then
2141                         echo "Genre: $PARSEDGENRE"
2142                 fi
2143         fi
2144         if [ ! "$ONETRACK" = "y" ]; then
2145                 for TRACK in $(f_seq_row 1 $TRACKS)
2146                 do
2147                         echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "${CDDBPARSEFILE}" | cut -f2- -d= | tr -d \\r\\n)"
2148                 done
2149         fi
2150 }
2151
2152 # do_localcddb
2153 # Check for a local CDDB file, and report success
2154 do_localcddb ()
2155 {
2156         if checkstatus cddb-readcomplete && checkstatus cddb-choice >/dev/null; then :; else
2157         
2158                 CDDBLOCALSTATUS="notfound"
2159                 CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
2160                 USELOCALRESP="y"
2161
2162                 if [ "$CDDBLOCALRECURSIVE" = "y" ]; then
2163                         CDDBLOCALRESULTS="$(find ${CDDBLOCALDIR} -name "${CDDBDISCID}" -type f 2> /dev/null)"
2164                         if [ ! "${CDDBLOCALRESULTS}" = "" ]; then
2165                                 if   (( $(echo "${CDDBLOCALRESULTS}" | wc -l) == 1 )); then
2166                                         CDDBLOCALFILE="${CDDBLOCALRESULTS}"
2167                                         CDDBLOCALMATCH=single
2168                                 elif (( $(echo "${CDDBLOCALRESULTS}" | wc -l) > 1 )); then
2169                                         CDDBLOCALMATCH=multiple
2170                                 fi
2171                         else
2172                                 CDDBLOCALMATCH=none
2173                         fi
2174                 elif [ "$CDDBLOCALMATCH" = "none" ] && [ -r "${CDDBLOCALDIR}/${CDDBDISCID}" ]; then
2175                         CDDBLOCALFILE="${CDDBLOCALDIR}/${CDDBDISCID}"
2176                         CDDBLOCALMATCH=single
2177                 else
2178                         CDDBLOCALMATCH=none
2179                 fi
2180                 
2181                 # If the user has selected to check a local CDDB repo, we proceed with it
2182                 case $CDDBLOCALMATCH in
2183                         multiple)
2184                                 echo "Processing multiple matching CDDB entries..." > "$ABCDETEMPDIR/cddblocalchoices"
2185                                 X=0
2186                                 echo "$CDDBLOCALRESULTS" | while read RESULT ; do
2187                                         X=$(expr $X + 1)
2188                                         # List out disc title/author and contents
2189                                         CDDBLOCALREAD="$ABCDETEMPDIR/cddblocalread.$X"
2190                                         cat "$RESULT" > "${CDDBLOCALREAD}"
2191                                         {       
2192                                                 echo -n "#$X: "
2193                                                 do_cddbparse "${CDDBLOCALREAD}"
2194                                                 echo ""
2195                                                 ##FIXME## QUICK HACK !!!!
2196                                                 if [ ! "$INTERACTIVE" = "y" ]; then break ; fi
2197                                         } >> "$ABCDETEMPDIR/cddblocalchoices"
2198                                 done
2199                                 if [ $(cat "$ABCDETEMPDIR/cddblocalchoices" | wc -l) -ge 24 ] && [ "$INTERACTIVE" = "y" ]; then
2200                                         page "$ABCDETEMPDIR/cddblocalchoices"
2201                                 else
2202                                         # It's all going to fit in one page, cat it
2203                                         cat "$ABCDETEMPDIR/cddblocalchoices" >&2
2204                                 fi
2205                                 CDDBLOCALCHOICES=$( echo "$CDDBLOCALRESULTS" | wc -l )
2206                                 # Setting the choice to an impossible integer to avoid errors in the numeric comparisons
2207                                 CDDBLOCALCHOICENUM=-1
2208                                 if [ "$INTERACTIVE" = "y" ]; then
2209                                         while [ $CDDBLOCALCHOICENUM -lt 0 ] || [ $CDDBLOCALCHOICENUM -gt $CDDBLOCALCHOICES ]; do
2210                                                 echo -n "Locally cached CDDB entries found. Which one would you like to use (0 for none)? [0-$CDDBLOCALCHOICES]: " >&2
2211                                                 read CDDBLOCALCHOICE
2212                                                 [ x"$CDDBLOCALCHOICE" = "x" ] && CDDBLOCALCHOICE="1"
2213                                                 # FIXME # Introduce diff's
2214                                                 if echo $CDDBLOCALCHOICE | grep -E "[[:space:]]*[[:digit:]]+,[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
2215                                                         diffentries cddblocalread "$CDDBLOCALCHOICES" "$CDDBLOCALCHOICE"
2216                                                 elif echo $CDDBLOCALCHOICE | grep -E "[[:space:]]*[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
2217                                                         # Make sure we get a valid choice
2218                                                         CDDBLOCALCHOICENUM=$(echo $CDDBLOCALCHOICE | xargs printf %d 2>/dev/null)
2219                                                         if [ $CDDBLOCALCHOICENUM -lt 0 ] || [ $CDDBLOCALCHOICENUM -gt $CDDBLOCALCHOICES ]; then
2220                                                                 echo "Invalid selection. Please choose a number between 0 and $CDDBLOCALCHOICES." >&2
2221                                                         fi
2222                                                 fi
2223                                         done
2224                                 else
2225                                         ### FIXME ###
2226                                         #echo "Selected ..."
2227                                         CDDBLOCALRESP=y
2228                                         CDDBLOCALCHOICENUM=1
2229                                 fi
2230                                 if [ ! "$CDDBLOCALCHOICENUM" = "0" ]; then
2231                                         #echo "Using local copy of CDDB data"
2232                                         echo "# DO NOT ERASE THIS LINE! Added by abcde to imitate cddb output" > "$ABCDETEMPDIR/cddbread.1"
2233                                         cat "$ABCDETEMPDIR/cddblocalread.$CDDBLOCALCHOICENUM" >> "$ABCDETEMPDIR/cddbread.1"
2234                                         echo 999 > "$ABCDETEMPDIR/cddbquery" # Assuming 999 isn't used by CDDB
2235                                         echo cddb-readcomplete >> "$ABCDETEMPDIR/status"
2236                                         do_cddbparse "$ABCDETEMPDIR/cddbread.1" > "$ABCDETEMPDIR/cddbchoices"
2237                                         echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
2238                                         CDDBLOCALSTATUS="found"
2239                                 else
2240                                         #echo "Not using local copy of CDDB data"
2241                                         CDDBLOCALSTATUS="notfound"
2242                                 fi
2243                                 ;;
2244                         single)
2245                                 # List out disc title/author and contents
2246                                 do_cddbparse "${CDDBLOCALFILE}"
2247                                 #if [ "$CDROMREADERSYNTAX" = "flac" ] ; then
2248                                 #       echo -n "Embedded cuesheet entry found, use it [Y/n]? " >&2
2249                                 #else
2250                                         echo -n "Locally cached CDDB entry found, use it [Y/n]? " >&2
2251                                 #fi
2252                                 if [ "$INTERACTIVE" = "y" ]; then
2253                                         read USELOCALRESP
2254                                         while [ "$USELOCALRESP" != "y" ] && [ "$USELOCALRESP" != "n" ] && [ "$USELOCALRESP" != "" ] ; do
2255                                                 echo -n 'Invalid selection. Please answer "y" or "n": ' >&2
2256                                                 read USELOCALRESP
2257                                         done
2258                                         [ x"$USELOCALRESP" = "x" ] && USELOCALRESP="y"
2259                                 else
2260                                         echo "y" >&2
2261                                 fi
2262                                 if [ "$USELOCALRESP" = "y" ]; then
2263                                         #echo "Using local copy of CDDB data"
2264                                         echo "# DO NOT ERASE THIS LINE! Added by abcde to imitate cddb output" > "$ABCDETEMPDIR/cddbread.1"
2265                                         cat "${CDDBLOCALFILE}" >> "$ABCDETEMPDIR/cddbread.1"
2266                                         echo 999 > "$ABCDETEMPDIR/cddbquery" # Assuming 999 isn't used by CDDB
2267                                         echo cddb-readcomplete >> "$ABCDETEMPDIR/status"
2268                                         do_cddbparse "${CDDBLOCALFILE}" > "$ABCDETEMPDIR/cddbchoices"
2269                                         echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
2270                                         CDDBLOCALSTATUS="single"
2271                                 else
2272                                         #echo "Not using local copy of CDDB data"
2273                                         CDDBLOCALSTATUS="notfound"
2274                                 fi
2275                                 ;;
2276                         none)
2277                                 CDDBLOCALSTATUS="notfound"
2278                                 ;;
2279                 esac
2280         fi
2281 }
2282
2283 # do_cdtext
2284 # Try to read CD-Text from the drive using icedax / cdda2wav
2285 do_cdtext ()
2286 {
2287         if new_checkexec icedax; then
2288                 CDTEXT_READER=icedax
2289         elif new_checkexec cdda2wav; then
2290                 CDTEXT_READER=cdda2wav
2291     else
2292                 # Didn't find either, bail
2293                 return 0
2294         fi
2295
2296         if [ "$OSFLAVOUR" = "OSX" ] ; then
2297                 # Hei, we have to unmount the device before running anything like cdda2wav/icedax in OSX
2298                 diskutil unmount ${CDROM#/dev/}
2299                 # Also, in OSX the cdrom device for cdda2wav/icedax changes...
2300                 CDDA2WAVCDROM="IODVDServices"
2301         elif [ "$OSFLAVOUR" = "FBSD" ] ; then
2302                 CDDA2WAVCDROM="$CDROMID"
2303         else
2304                 if [ "$CDROMID" = "" ]; then
2305                         CDDA2WAVCDROM="$CDROM"
2306                 else
2307                         CDDA2WAVCDROM="$CDROMID"
2308                 fi
2309         fi
2310
2311         # Do we have CD-Text on the disc (and can the drive read it?)
2312         ${CDTEXT_READER} -J -N -D ${CDDA2WAVCDROM} > "$ABCDETEMPDIR/cd-text" 2>&1
2313         grep -q '^CD-Text: detected' "$ABCDETEMPDIR/cd-text"
2314         ERRORCODE=$?
2315         if [ $ERRORCODE -ne 0 ]; then
2316                 # No CD-Text found, bail
2317                 return 0
2318         fi
2319
2320         rm -f "$ABCDETEMPDIR/cddbchoices"
2321         CDDBCHOICES=1
2322         # Make an empty template
2323         $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.1"
2324         echo -n "Retrieved 1 CD-Text match..." >> "$ABCDETEMPDIR/cddbchoices"
2325         echo "done." >> "$ABCDETEMPDIR/cddbchoices"
2326         echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
2327         echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
2328         ATITLE=$(grep -e '^Album title:' "${ABCDETEMPDIR}/cd-text" | cut -c14- )
2329         echo "200 none ${ATITLE}" >> "$ABCDETEMPDIR/cddbquery"
2330         # List out disc title/author and contents
2331         echo ---- ${ATITLE} ---- >> "$ABCDETEMPDIR/cddbchoices"
2332         for TRACK in $(f_seq_row 1 $TRACKS)
2333         do
2334                 TRACKM1=$(($TRACK - 1))
2335                 TITLE="$(grep -E ^Track\ +$TRACK: "$ABCDETEMPDIR/cd-text" | tr -d \\r\\n | sed 's~^Track ..: .~~g;'"s~'$~~g")"
2336                 echo "$TRACK: $TITLE" >> "$ABCDETEMPDIR/cddbchoices"
2337                 sed "s~^TTITLE${TRACKM1}=.*~TTITLE${TRACKM1}=${TITLE}~" "$ABCDETEMPDIR/cddbread.1" > "$ABCDETEMPDIR/cddbread.new"
2338         mv -f "$ABCDETEMPDIR/cddbread.new" "$ABCDETEMPDIR/cddbread.1"
2339         done
2340         sed "s~^DTITLE=.*~DTITLE=${ATITLE}~" "$ABCDETEMPDIR/cddbread.1" > "$ABCDETEMPDIR/cddbread.new"
2341     mv -f "$ABCDETEMPDIR/cddbread.new" "$ABCDETEMPDIR/cddbread.1"
2342         echo >> "$ABCDETEMPDIR/cddbchoices"
2343         echo "cdtext-readcomplete" >> "$ABCDETEMPDIR/status"
2344 }
2345
2346 # do_musicbrainz
2347 # Work with the musicbrainz WS API, then transform the results here so
2348 # they look (very) like the results from CDDB. Maybe not the best way
2349 # to go, but it Works For Me (TM)
2350
2351 do_musicbrainz ()
2352 {
2353         if checkstatus musicbrainz-readcomplete; then :; else
2354                 vecho "Obtaining Musicbrainz results..."
2355                 # If MB is to be used, interpret the query results and read all
2356                 # the available entries.
2357                 rm -f "$ABCDETEMPDIR/cddbchoices"
2358                 CDDBCHOICES=1 # Overridden by multiple matches
2359                 MBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
2360                 ${MUSICBRAINZ} --command data --discid "$MBDISCID" --workdir "$ABCDETEMPDIR"
2361
2362                 # The helper script will write disc matches out to
2363                 # cddbread.*. Count how many we have
2364                 if [ ! -f "${ABCDETEMPDIR}/cddbread.1" ] ; then
2365                         # No matches. Use the normal cddb template for the user to
2366                         # fill in
2367                         echo "No Musicbrainz match." >> "$ABCDETEMPDIR/cddbchoices"
2368                         $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.0"
2369                         # List out disc title/author and contents of template
2370                         echo ---- Unknown Artist / Unknown Album ---- >> "$ABCDETEMPDIR/cddbchoices"
2371                         UNKNOWNDISK=y
2372                         for TRACK in $(f_seq_row 1 $TRACKS)
2373                         do
2374                                 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.0" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
2375                         done
2376                         echo >> "$ABCDETEMPDIR/cddbchoices"
2377                         echo cddb-read-0-complete >> "$ABCDETEMPDIR/status"
2378                         echo cddb-choice=0 >> "$ABCDETEMPDIR/status"
2379                         echo 503 > "$ABCDETEMPDIR/cddbquery"
2380                 else
2381                         # We have some matches
2382                         NUM_RESPONSES=$(echo "${ABCDETEMPDIR}"/cddbread.* | wc -w)
2383                         if [ "$NUM_RESPONSES" -eq 1 ] ; then
2384                                 # One exact match
2385                                 echo -n "Retrieved 1 Musicbrainz match..." >> "$ABCDETEMPDIR/cddbchoices"
2386                                 echo "done." >> "$ABCDETEMPDIR/cddbchoices"
2387                                 echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
2388                                 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
2389                                 ATITLE=$(grep -e '^DTITLE=' "${ABCDETEMPDIR}/cddbread.1" | cut -c8- )
2390                                 echo "200 none ${ATITLE}" >> "$ABCDETEMPDIR/cddbquery"
2391                                 # List out disc title/author and contents
2392                                 echo ---- ${ATITLE} ---- >> "$ABCDETEMPDIR/cddbchoices"
2393                                 for TRACK in $(f_seq_row 1 $TRACKS)
2394                                 do
2395                                         echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.1" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
2396                                 done
2397                                 echo >> "$ABCDETEMPDIR/cddbchoices"
2398                         else
2399                                 echo "210 Found exact matches, list follows (until terminating .)" > "$ABCDETEMPDIR/cddbquery"
2400                                 echo "Multiple Musicbrainz matches:" >> "$ABCDETEMPDIR/cddbchoices"
2401                                 for file in "$ABCDETEMPDIR"/cddbread.*
2402                                 do
2403                                         X=$(echo $file | sed 's/^.*cddbread\.//g')
2404                                         echo cddb-read-$X-complete >> "$ABCDETEMPDIR/status"
2405                                         ATITLE=$(grep -e '^DTITLE=' "${ABCDETEMPDIR}"/cddbread.$X | cut -c8- )
2406                                         echo "none ${ATITLE}" >> "$ABCDETEMPDIR/cddbquery"
2407                                         # List out disc title/author and contents
2408                                         echo "#$X: ---- ${ATITLE} ----" >> "$ABCDETEMPDIR/cddbchoices"
2409                                         for TRACK in $(f_seq_row 1 $TRACKS)
2410                                         do
2411                                                 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.$X" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
2412                                         done
2413                                         echo >> "$ABCDETEMPDIR/cddbchoices"
2414                                 done
2415                                 echo "." >> "$ABCDETEMPDIR/cddbquery"
2416                         fi
2417                 fi
2418                 echo "musicbrainz-readcomplete" >> "$ABCDETEMPDIR/status"
2419         fi
2420 }
2421
2422 # do_cddbstat
2423 do_cddbstat ()
2424 {
2425         # Perform CDDB protocol version check if it hasn't already been done
2426         if checkstatus cddb-statcomplete; then :; else
2427                 if [ "$CDDBAVAIL" = "n" ]; then
2428                         ERRORCODE=no_query
2429                         echo 503 > "$ABCDETEMPDIR/cddbstat"
2430                 else
2431                         rc=1
2432                         CDDBUSER=$(echo $HELLOINFO | cut -f1 -d'@')
2433                         CDDBHOST=$(echo $HELLOINFO | cut -f2- -d'@')
2434                         while test $rc -eq 1 -a $CDDBPROTO -ge 3; do
2435                                 vecho "Checking CDDB server status..."
2436                                 $CDDBTOOL stat $CDDBURL $CDDBUSER $CDDBHOST $CDDBPROTO > "$ABCDETEMPDIR/cddbstat"
2437                                 RESPONSECODE=$(head -n 1 "$ABCDETEMPDIR/cddbstat" | cut -f1 -d' ')
2438                                 case "$RESPONSECODE" in
2439                                 210)    # 210 OK, status information follows (until terminating `.')
2440                                         rc=0
2441                                         ;;
2442                                 501)  # 501 Illegal CDDB protocol level: <n>.
2443                                         CDDBPROTO=`expr $CDDBPROTO - 1`
2444                                         ;;
2445                                 *)      # Try a cddb query, since freedb2.org doesn't support the stat or ver commands
2446                                         # FreeDB TESTCD disc-id is used for query
2447                                         $CDDBTOOL query $CDDBURL $CDDBPROTO $CDDBUSER $CDDBHOST 03015501 1 296 344 > "$ABCDETEMPDIR/cddbstat"
2448                                         RESPONSECODE=$(head -n 1 "$ABCDETEMPDIR/cddbstat" | cut -f1 -d' ')
2449                                         case "$RESPONSECODE" in
2450                                                 2??)    # Server responded, everything seems OK
2451                                                         rc=0
2452                                                         ;;
2453                                                 *)      # unknown error
2454                                                         break
2455                                                         ;;
2456                                         esac
2457                                         ;;
2458                                 esac
2459                         done
2460                         if test $rc -eq 1; then
2461                                 CDDBAVAIL="n"
2462                         fi
2463                 fi
2464                 echo cddb-statcomplete >> "$ABCDETEMPDIR/status"
2465         fi
2466 }
2467
2468
2469 # do_cddbquery
2470 do_cddbquery ()
2471 {
2472         CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
2473         CDDBLOCALFILE="${CDDBLOCALDIR}/${CDDBDISCID}"
2474         
2475         # Perform CDDB query if it hasn't already been done
2476         if checkstatus cddb-querycomplete; then :; else
2477                 if [ "$CDDBAVAIL" = "n" ]; then
2478                         ERRORCODE=no_query
2479                         echo 503 > "$ABCDETEMPDIR/cddbquery"
2480                 # The default CDDBLOCALSTATUS is "notfound"
2481                 # This part will be triggered if the user CDDB repo does not
2482                 # contain the entry, or if we are not trying to use the repo.
2483                 else
2484                         vecho "Querying the CDDB server..."
2485                         CDDBUSER=$(echo $HELLOINFO | cut -f1 -d'@')
2486                         CDDBHOST=$(echo $HELLOINFO | cut -f2- -d'@')
2487                         $CDDBTOOL query $CDDBURL $CDDBPROTO $CDDBUSER $CDDBHOST $TRACKINFO > "$ABCDETEMPDIR/cddbquery"
2488                         ERRORCODE=$?
2489                         case $ERRORCODE in
2490                                 0)  # success
2491                                 ;;
2492                                 12|13|14)
2493                                         # no match found in database,
2494                                         # wget/fetch error, or user requested not to use CDDB
2495                                         # Make up an error code (503) that abcde
2496                                         # will recognize in do_cddbread
2497                                         # and compensate by making a template
2498                                         echo 503 > "$ABCDETEMPDIR/cddbquery"
2499                                 ;;
2500                                 *) # strange and unknown error
2501                                         echo ERRORCODE=$ERRORCODE
2502                                         echo "abcde: $CDDBTOOL returned unknown error code"
2503                                 ;;
2504                         esac
2505                 fi
2506                 echo cddb-querycomplete >> "$ABCDETEMPDIR/status"
2507         fi
2508 }
2509
2510 # do_cddbread
2511 do_cddbread ()
2512 {
2513         # If it's not to be used, generate a template.
2514         # Then, display it (or them) and let the user choose/edit it
2515         if checkstatus cddb-readcomplete; then :; else
2516                 vecho "Obtaining CDDB results..."
2517                 # If CDDB is to be used, interpret the query results and read all
2518                 # the available entries.
2519                 rm -f "$ABCDETEMPDIR/cddbchoices"
2520                 CDDBCHOICES=1 # Overridden by multiple matches
2521                 RESPONSECODE=$(head -n 1 "$ABCDETEMPDIR/cddbquery" | cut -f1 -d' ')
2522                 case "$RESPONSECODE" in
2523                 200)
2524                         # One exact match, retrieve it
2525                         # 200 [section] [discid] [artist] / [title]
2526                         if checkstatus cddb-read-1-complete; then :; else
2527                                 echo -n "Retrieving 1 CDDB match..." >> "$ABCDETEMPDIR/cddbchoices"
2528                                 $CDDBTOOL read $CDDBURL $CDDBPROTO $CDDBUSER $CDDBHOST $(cut -f2,3 -d' ' "$ABCDETEMPDIR/cddbquery") > "$ABCDETEMPDIR/cddbread.1"
2529                                 echo "done." >> "$ABCDETEMPDIR/cddbchoices"
2530                                 echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
2531                                 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
2532                         fi
2533                         # List out disc title/author and contents
2534                         echo ---- "$(cut '-d ' -f4- "$ABCDETEMPDIR/cddbquery")" ---- >> "$ABCDETEMPDIR/cddbchoices"
2535                         for TRACK in $(f_seq_row 1 $TRACKS)
2536                         do
2537                                 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.1" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
2538                         done
2539                         echo >> "$ABCDETEMPDIR/cddbchoices"
2540                         ;;
2541                 202|403|409|503)
2542                         # No match
2543                         case "$RESPONSECODE" in
2544                         202) echo "No CDDB match." >> "$ABCDETEMPDIR/cddbchoices" ;;
2545                         403|409) echo "CDDB entry is corrupt, or the handshake failed." >> "$ABCDETEMPDIR/cddbchoices" ;;
2546                         503) echo "CDDB unavailable." >> "$ABCDETEMPDIR/cddbchoices" ;;
2547                         esac
2548                         $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.0"
2549                         # List out disc title/author and contents of template
2550                         echo ---- Unknown Artist / Unknown Album ---- >> "$ABCDETEMPDIR/cddbchoices"
2551                         UNKNOWNDISK=y
2552                         for TRACK in $(f_seq_row 1 $TRACKS)
2553                         do
2554                                 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.0" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
2555                         done
2556                         echo >> "$ABCDETEMPDIR/cddbchoices"
2557                         echo cddb-read-0-complete >> "$ABCDETEMPDIR/status"
2558                         echo cddb-choice=0 >> "$ABCDETEMPDIR/status"
2559                         ;;
2560                 210|211)
2561                         # Multiple exact, (possibly multiple) inexact matches
2562                         IN=
2563                         if [ "$RESPONSECODE" = "211" ]; then IN=in; fi
2564                         if [ "$(wc -l < "$ABCDETEMPDIR/cddbquery" | tr -d ' ')" -eq 3 ]; then
2565                                 echo "One ${IN}exact match:" >> "$ABCDETEMPDIR/cddbchoices"
2566                                 tail -n +2 "$ABCDETEMPDIR/cddbquery" | head -n 1 >> "$ABCDETEMPDIR/cddbchoices"
2567                                                         echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
2568                         else
2569                                 echo "Multiple ${IN}exact matches:" >> "$ABCDETEMPDIR/cddbchoices"
2570                         fi
2571                         vecho -n "Retrieving multiple matches... "
2572                         grep -v ^[.]$ "$ABCDETEMPDIR/cddbquery" | ( X=0
2573                         read DISCINFO # eat top line
2574                         while read DISCINFO
2575                         do
2576                                 X=$(expr $X + 1)
2577                                 if checkstatus cddb-read-$X-complete; then :; else
2578                                         $CDDBTOOL read $CDDBURL $CDDBPROTO $CDDBUSER $CDDBHOST $(echo $DISCINFO | cut -f1,2 -d' ') > "$ABCDETEMPDIR/cddbread.$X"
2579                                         echo cddb-read-$X-complete >> "$ABCDETEMPDIR/status"
2580                                 fi
2581                                 # List out disc title/author and contents
2582                                 echo \#$X: ---- "$DISCINFO" ---- >> "$ABCDETEMPDIR/cddbchoices"
2583                                 for TRACK in $(f_seq_row 1 $TRACKS)
2584                                 do
2585                                         echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.$X" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
2586                                 done
2587                                 echo >> "$ABCDETEMPDIR/cddbchoices"
2588                         done )
2589                         vecho "done."
2590                         CDDBCHOICES=$(expr $(cat "$ABCDETEMPDIR/cddbquery" | wc -l) - 2)
2591                         ;;
2592                 999)
2593                         # Using local copy.
2594                         for TRACK in $(f_seq_row 1 $TRACKS)
2595                         do
2596                                 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.1" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
2597                         done
2598                         echo >> "$ABCDETEMPDIR/cddbchoices"
2599                         echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
2600                         echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
2601                         ;;
2602                 esac    
2603                 echo "cddb-readcomplete" >> "$ABCDETEMPDIR/status"
2604         fi
2605 }
2606
2607 # do_cddbedit
2608 do_cddbedit ()
2609 {
2610         if checkstatus cddb-edit >/dev/null; then
2611                 CDDBDATA="$ABCDETEMPDIR/cddbread.$(checkstatus cddb-choice)"
2612                 VARIOUSARTISTS="$(checkstatus variousartists)"
2613                 VARIOUSARTISTSTYLE="$(checkstatus variousartiststyle)"
2614                 return 0
2615         fi
2616         if [ "$INTERACTIVE" = "y" ]; then
2617                 # We should show the CDDB results both when we are not using the local CDDB repo
2618                 # or when we are using it but we could not find a proper match
2619                 if [ "$CDDBUSELOCAL" = "y" ] && [ "$CDDBLOCALSTATUS" = "notfound" ] || [ ! "$CDDBUSELOCAL" = "y" ]; then
2620                         # Display the $ABCDETEMPDIR/cddbchoices file created above
2621                         # Pick a pager so that if the tracks overflow the screen the user can still view everything
2622                         if [ -r "$ABCDETEMPDIR/cddbchoices" ]; then
2623                                 CDDBCHOICES=$(expr $(cat "$ABCDETEMPDIR/cddbquery" | wc -l) - 2)
2624                                 CHOICE=$(checkstatus cddb-choice)
2625                                 if [ -n "$CHOICE" ] ; then
2626                                         case $CDDBCHOICES in
2627                                                 -1) if head -1 "$ABCDETEMPDIR/cddbquery" | grep "^$" > /dev/null 2>&1 ; then
2628                                                                 log error "CDDB query failed!"
2629                                                                 exit 1
2630                                                         else
2631                                                                 cat "$ABCDETEMPDIR/cddbchoices"
2632                                                         fi
2633                                                         ;;
2634                                                 1) cat "$ABCDETEMPDIR/cddbchoices" ;;
2635                                                 *)
2636                                                 echo "Selected: #$CHOICE"
2637                                                 do_cddbparse "$ABCDETEMPDIR/cddbread.$CHOICE"
2638                                                 ;;
2639                                         esac
2640                                 else
2641                                         # The user has a choice to make, display the info in a pager if necessary
2642                                         if [ $(cat "$ABCDETEMPDIR/cddbchoices" | wc -l) -ge 24 ]; then
2643                                                 page "$ABCDETEMPDIR/cddbchoices"
2644                                         else
2645                                                 # It's all going to fit in one page, cat it
2646                                                 cat "$ABCDETEMPDIR/cddbchoices" >&2
2647                                         fi
2648                                         
2649                                         CDDBCHOICENUM=""
2650                                         # Setting the choice to an impossible integer to avoid errors in the numeric comparisons
2651                                         CDCHOICENUM=-1
2652                                         # I'll take CDDB read #3 for $400, Alex
2653                                         while [ $CDCHOICENUM -lt 0 ] || [ $CDCHOICENUM -gt $CDDBCHOICES ]; do
2654                                                 echo -n "Which entry would you like abcde to use (0 for none)? [0-$CDDBCHOICES]: " >&2
2655                                                 read CDDBCHOICE
2656                                                 [ X"$CDDBCHOICE" = "X" ] && CDDBCHOICE=1
2657                                                 if echo $CDDBCHOICE | grep -E "[[:space:]]*[[:digit:]]+,[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
2658                                                         if [ ! X"$DIFF" = "X" ]; then
2659                                                                 PARSECHOICE1=$(echo $CDDBCHOICE | cut -d"," -f1 | xargs printf %d 2>/dev/null)
2660                                                                 PARSECHOICE2=$(echo $CDDBCHOICE | cut -d"," -f2 | xargs printf %d 2>/dev/null)
2661                                                                 if [ $PARSECHOICE1 -lt 1 ] || [ $PARSECHOICE1 -gt $CDDBCHOICES ] || \
2662                                                                    [ $PARSECHOICE2 -lt 1 ] || [ $PARSECHOICE2 -gt $CDDBCHOICES ] || \
2663                                                                    [ $PARSECHOICE1 -eq $PARSECHOICE2 ]; then
2664                                                                         echo "Invalid diff range. Please select two comma-separated numbers between 1 and $CDDBCHOICES" >&2
2665                                                                 else
2666                                                                         # We parse the 2 choices to diff, store them in temporary files and diff them.
2667                                                                         for PARSECHOICE in $(echo $CDDBCHOICE | tr , \ ); do
2668                                                                                 do_cddbparse "$ABCDETEMPDIR/cddbread.$PARSECHOICE" > "$ABCDETEMPDIR/cddbread.parsechoice.$PARSECHOICE"
2669                                                                         done
2670                                                                         echo "Showing diff between choices $PARSECHOICE1 and $PARSECHOICE2..." > "$ABCDETEMPDIR/cddbread.diff"
2671                                                                         $DIFF $DIFFOPTS "$ABCDETEMPDIR/cddbread.parsechoice.$PARSECHOICE1" "$ABCDETEMPDIR/cddbread.parsechoice.$PARSECHOICE2" >> "$ABCDETEMPDIR/cddbread.diff"
2672                                                                         if [ $(cat "$ABCDETEMPDIR/cddbread.diff" | wc -l) -ge 24 ]; then
2673                                                                                 page "$ABCDETEMPDIR/cddbread.diff"
2674                                                                         else
2675                                                                                 cat "$ABCDETEMPDIR/cddbread.diff" >&2
2676                                                                         fi
2677                                                                 fi
2678                                                         else
2679                                                                 echo "The diff program was not found in your path. Please choose a number between 0 and $CDDBCHOICES." >&2
2680                                                         fi
2681                                                 elif echo $CDDBCHOICE | grep -E "[[:space:]]*[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
2682                                                         # Make sure we get a valid choice