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