#!/bin/sh
# Copyright (c) 1998-2001 Robert Woodcock <rcw@debian.org>
# This code is hereby licensed for public consumption under either the
# GNU GPL v2 or greater, or Larry Wall's Artistic license - your choice.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# 
# Copyright for this work is to expire January 1, 2010, after which it
# shall be public domain.

VERSION=1.9.8

usage ()
{
echo "This is abcde v$VERSION."
echo "Usage: abcde [options] [tracks]"
echo "Options:"
echo "-a    Specify which actions to perform (cddb,read,encode,tag,move,playlist,clean)"
echo "-c    Specify a configuration file (overrides system and user config files)"
echo "-C    Specify a discid to resume from (only needed if you no longer have the cd)"
echo "-d    Specify CDROM device to grab"
echo "-D    Debugging mode (equivalent to sh -x abcde)"
echo "-h    This help information"
echo "-j    Number of encoder processes to run at once"
echo "-l    Use low disk space algorithm"
echo "-n    No lookup. Don't query CDDB, just create and use template"
echo "-N    Noninteractive. Never prompt for anything"
echo "-o    Output file type - either \"ogg\" or \"mp3\""
echo "-r    [host1,host2...] Remote encode on these hosts as well as the local machine"
echo "-x    Eject CD after all tracks are read"
echo "Tracks is a space-delimited list of tracks to grab."
echo "Ranges specified with hyphens are allowed."
}

# run_command [blurb] [command...]
# Runs a command, silently if necessary, keeps the output in a logfile,
# erases the logfile if the command was run successfully (exit status 0)
# and updates the status file with the blurb if necessary
run_command ()
{
	BLURB="$1"
	shift
	# See if this is supposed to be silent
	if [ "$(checkstatus encode-output)" = "loud" ]; then
		"$@" >&2
	else
		"$@" >/dev/null 2>&1
	fi
	if [ "$BLURB" != "" ]; then
		echo $BLURB >> $ABCDETEMPDIR/status
	fi
}

# checkstatus [blurb]
# Returns "0" if the blurb was found, returns 1 if it wasn't
# Puts the blurb content, if available, on stdout.
# Otherwise, returns "".
checkstatus ()
{
	# Take the last line in the status file if there's multiple matches
	BLURB="$(grep $1 $ABCDETEMPDIR/status | tail -1)"
	grep $1 $ABCDETEMPDIR/status >/dev/null
	case "$?" in
	0)
		# Matches found
		# See if there's a = in it
		if [ "$(echo $BLURB | grep -c =)" != "0" ]; then
			echo "$(echo $BLURB | cut -f2- -d=)"
		fi
		return 0
		;;
	1)
		# No matches found
		return 1
		;;
	2)
		# Grep failed
		return 2
		;;
	esac
}

# relpath() and slash() are Copyright (c) 1999 Stuart Ballard and
# distributed under the terms of the GNU GPL v2 or later, at your option

# Function to determine if a word contains a slash.
slash ()
{
	case "$1" in
	*/*) return 0;;
	*) return 1;;
	esac
}

# Function to give the relative path from one file to another.
# Usage: relpath fromfile tofile
# eg relpath music/Artist/Album.m3u music/Artist/Album/Song.mp3
# (the result would be Album/Song.mp3)
# Output is relative path to $2 from $1 on stdout

# This code has the following restrictions:
# Multiple ////s are not collapsed into single /s, with strange effects.
# Absolute paths and ../s are handled wrong in FR (but they work in TO)
# If FR is a directory it must have a trailing /

relpath ()
{
	FR="$1"
	TO="$2"
	
	case "$TO" in
	/*) ;; # No processing is needed for absolute paths
	*)
		# Loop through common prefixes, ignoring them.
		while slash "$FR" && [ "$(echo "$FR" | cut -d/ -f1)" = "$(echo "$TO" | cut -d/ -f1)" ]
		do
			FR="$(echo "$FR" | cut -d/ -f2-)"
			TO="$(echo "$TO" | cut -d/ -f2-)"
		done
		# Loop through directory portions left in FR, adding appropriate ../s.
		while slash "$FR"
		do
			FR="$(echo "$FR" | cut -d/ -f2-)"
			TO="../$TO"
		done
	esac
	
	echo $TO
}

# This code splits the a Various Artist track name from one of the following
# forms:
#
#  forward:      Artist / Track
#  forward-dash: Artist - Track
#  reverse:      Track / Artist
#  reverse-dash: Track - Artist
#  colon:        Artist: Track
#
# variables used:
# VARIOUSARTISTS, VARIOUSARTISTSTYLE, TRACKNAME, TRACKARTIST
splitvarious ()
{
#	set +e
	if [ "$VARIOUSARTISTS" = "y" ]; then
		case "$VARIOUSARTISTSTYLE" in
		forward)
			DTITLEARTIST="$(echo $TRACKNAME | sed 's- / -~-g')"
			TRACKARTIST="$(echo $DTITLEARTIST | cut -f1 -d~)"
			TRACKNAME="$(echo $DTITLEARTIST | cut -f2 -d~)"
			;;
		forward-dash)
			DTITLEARTIST="$(echo $TRACKNAME | sed 's, - ,~,g')"
			TRACKARTIST="$(echo $DTITLEARTIST | cut -f1 -d~)"
			TRACKNAME="$(echo $DTITLEARTIST | cut -f2 -d~)"
			;;
		reverse)
			DTITLEARTIST="$(echo $TRACKNAME | sed 's- / -~-g')"
			TRACKARTIST="$(echo $DTITLEARTIST | cut -f2 -d~)"
			TRACKNAME="$(echo $DTITLEARTIST | cut -f1 -d~)"
			;;
		reverse-dash)
			DTITLEARTIST="$(echo $TRACKNAME | sed 's, - ,~,g')"
			TRACKARTIST="$(echo $DTITLEARTIST | cut -f2 -d~)"
			TRACKNAME="$(echo $DTITLEARTIST | cut -f1 -d~)"
			;;
		colon)
			DTITLEARTIST="$(echo $TRACKNAME | sed 's-: -~-g')"
			TRACKARTIST="$(echo $DTITLEARTIST | cut -f1 -d~)"
			TRACKNAME="$(echo $DTITLEARTIST | cut -f2 -d~)"
			;;
		esac
	else
		TRACKARTIST=$DARTIST
	fi
}

# do_tag [tracknumber]
# id3 tags a filename
# variables used:
# TRACKS, TRACKNAME, TRACKARTIST, TAGGER, TAGGEROPTS, COMMENT, DALBUM, DARTIST
# (and temporarily) ID3TAGV
do_tag ()
{
	COMMENTOUTPUT="$(eval echo ${COMMENT})"
	run_command '' echo "Tagging track $1 of $TRACKS: $TRACKNAME..."
	if [ "$OUTPUTTYPE" = "mp3" ]; then
		# Amazingly, id3 and id3v2 have identical -a, -A, -c, -t, and -T switches
		# ...except id3v2's -c is completely broken and I'm not
		# gonna sit around waiting for it to be fixed...
		if [ "$ID3TAGV" = "2" ]; then
			run_command tagtrack-$1 $TAGGER $TAGGEROPTS -A "$DALBUM" -a "$TRACKARTIST" \
				-t "$TRACKNAME" -T "$1" "$ABCDETEMPDIR/track$1.$OUTPUTTYPE"
		else
			run_command tagtrack-$1 $TAGGER $TAGGEROPTS -c "COMMENTOUTPUT" \
				-A "$DALBUM" -a "$TRACKARTIST" -t "$TRACKNAME" -T "$1" "$ABCDETEMPDIR/track$1.$OUTPUTTYPE"
		fi
	else
		# vorbiscomment can't do in-place modification, mv the file first
		mv "$ABCDETEMPDIR/track$1.$OUTPUTTYPE" "$ABCDETEMPDIR/track$1.uncommented.$OUTPUTTYPE"
		(
		# These are from http://www.xiph.org/ogg/vorbis/doc/v-comment.html
		echo ARTIST=$TRACKARTIST
		echo ALBUM=$DALBUM
		echo TITLE=$TRACKNAME
		echo TRACKNUMBER=$1
		if [ "$(eval echo ${COMMENT})" != "" ]; then
			echo $DESCRIPTION=$COMMENTOUTPUT;
		fi
		) | run_command tagtrack-$1 $VORBISCOMMENT -w \
			"$ABCDETEMPDIR/track$1.uncommented.$OUTPUTTYPE" "$ABCDETEMPDIR/track$1.$OUTPUTTYPE"
		rm -f "$ABCDETEMPDIR/track$1.uncommented.$OUTPUTTYPE"
	fi
}

# do_encode [tracknumber] [hostname]
# If no hostname is specified, encode locally
# variables used:
# TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, DALBUM, DARTIST, ENCNICE
do_encode ()
{
	IN="$ABCDETEMPDIR/track$1.wav"
	OUT="$ABCDETEMPDIR/track$1.$OUTPUTTYPE"
	run_command '' echo "Encoding track $1 of $TRACKS: $TRACKNAME..."
	case "$OUTPUTTYPE" in
	mp3)
		if [ "$2" = "%local%" ]; then
			case "$ENCODERSYNTAX" in
			lame|gogo) run_command encodetrack-$1 nice $ENCNICE $ENCODER $ENCODEROPTS "$IN" "$OUT" ;;
			bladeenc|l3enc|xingmp3enc) run_command encodetrack-$1 nice $ENCNICE $ENCODER "$IN" "$OUT" $ENCODEROPTS ;;
			mp3enc) run_command encodetrack-$1 nice $ENCNICE $ENCODER -if "$IN" -of "$OUT" $ENCODEROPTS ;;
			esac
		else
			run_command encodetrack-$1 $DISTMP3 $DISTMP3OPTS $2 "$IN" "$OUT" >/dev/null 2>&1
		fi
		;;
	ogg)
		case "$ENCODERSYNTAX" in
		vorbize) run_command encodetrack-$1 nice $ENCNICE $ENCODER $ENCODEROPTS -w "$OUT" "$IN" ;;
		oggenc) run_command encodetrack-$1 nice $ENCNICE $ENCODER $ENCODEROPTS "$IN" -o "$OUT" ;;
		esac
		;;
	esac
	rm -f "$IN"
}

# do_move [tracknumber]
# Deduces the outfile from environment variables
# Creates directory if necessary
# variables used:
# TRACKNUM, TRACKNAME, TRACKARTIST, DALBUM, OUTPUTFORMAT
do_move ()
{
	# Create ALBUMFILE, ARTISTFILE, TRACKFILE
	# Munge filenames as follows: 
	# ' ' -> '_'
	# '/' -> '_'
	# ''' -> ''
	# '?' -> ''
	# Eat control characters
	ALBUMFILE=$(mungefilename "$DALBUM")
	ARTISTFILE=$(mungefilename "$TRACKARTIST")
	TRACKFILE=$(mungefilename "$TRACKNAME")
	# Supported variables for OUTPUTFORMAT are ALBUMFILE, ARTISTFILE, TRACKFILE, and TRACKNUM.
	if [ "$VARIOUSARTISTS" = "y" ]; then
		OUTPUTFILE=$(eval echo $VAOUTPUTFORMAT)
	else
		OUTPUTFILE=$(eval echo $OUTPUTFORMAT)
	fi

	# Check that the directory for OUTPUTFILE exists, if it doesn't, create it
	OUTPUTFILEDIR=$(dirname "$OUTPUTDIR/$OUTPUTFILE")
	if [ \! -d "$OUTPUTFILEDIR" ]; then
		mkdir -p "$OUTPUTFILEDIR"
	fi
	run_command movetrack-$1 mv "$ABCDETEMPDIR/track$1.$OUTPUTTYPE" "$OUTPUTDIR/$OUTPUTFILE"
}

# do_playlist
# Create the playlist if wanted
# Variables used:
# PLAYLISTFORMAT, PLAYLISTDATAPREFIX, OUTPUTDIR, 
do_playlist ()
{
	# Create a playlist file for the playlist data to go into, wiping it out if
	# it exists already
	for LASTTRACK in $TRACKQUEUE; do :; done
	# get the number of digits to pad TRACKNUM with - we'll use this later
	TRACKNUMPADDING=$(echo -n $LASTTRACK | wc -c | tr -d ' ')
	ALBUMFILE=$(mungefilename "$DALBUM")
	ARTISTFILE=$(mungefilename "$DARTIST")
	PLAYLISTFILE=$(eval echo $PLAYLISTFORMAT)
	FINALPLAYLISTDIR=$(dirname "$OUTPUTDIR/$PLAYLISTFILE")
	if [ \! -e "$FINALPLAYLISTDIR" ]; then
		mkdir -p "$FINALPLAYLISTDIR"
	fi
	rm -f "$OUTPUTDIR/$PLAYLISTFILE"
	touch "$OUTPUTDIR/$PLAYLISTFILE"
	for UTRACKNUM in $TRACKQUEUE
	do
		# Shares some code with do_move since the filenames have to match
		CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
		TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -1 | cut -f2 -d= | tr -d \[:cntrl:\])
		splitvarious
		TRACKFILE=$(mungefilename "$TRACKNAME")
		ARTISTFILE=$(mungefilename "$TRACKARTIST")
		TRACKNUM=$(printf %0.${TRACKNUMPADDING}d ${UTRACKNUM})
		OUTPUTFILE=$(eval echo $OUTPUTFORMAT)
		if [ "$PLAYLISTDATAPREFIX" != "" ]; then
			echo -n $PLAYLISTDATAPREFIX >> "$OUTPUTDIR/$PLAYLISTFILE"
		fi
		relpath "$PLAYLISTFILE", "$OUTPUTFILE" >> "$OUTPUTDIR/$PLAYLISTFILE"
	done
	echo "playlistcomplete" >> "$ABCDETEMPDIR/status"
}

# do_discid
# This essentially the start of things
do_discid ()
{
	# Query the CD to get the track info, unless the user specified -C
	if [ "$DISCID" = "" ]; then
		echo -n "Getting CD track info... "
		TRACKINFO=$($CDDISCID $CDROM)
		# Make sure there's a CD in there by checking cd-discid's return code
		if [ "$?" = "1" ]; then
			echo "abcde error: CD could not be read. Perhaps there's no CD in the drive?" >&2
			exit 1
		fi
	else
		TRACKINFO=$(cat abcde.$DISCID/discid)
	fi
	
	# Get a full enumeration of tracks, sort it, and put it in the TRACKQUEUE.
	# This needs to be done now because a section of the resuming code will need
	# it later.
	
	TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
	
	if [ "$TRACKQUEUE" = "" ]; then
		echo -n "Grabbing entire CD - tracks: "
		X=0
		while [ "$X" != "$TRACKS" ]
		do
			X=$(expr $X + 1)
			TRACKQUEUE=$(echo "$TRACKQUEUE" $X)
		done
		echo $TRACKQUEUE
	else
		# User-supplied track queue.
		# Weed out non-numbers, whitespace, then sort and weed out duplicates
		TRACKQUEUE=$(echo $TRACKQUEUE | sed 's-[^0-9 ]--g' | tr ' ' '\n' | grep -v ^$ | sort -n | uniq | tr '\n' ' ' | sed 's- $--g')
		echo Grabbing tracks: "$TRACKQUEUE"
	fi
	
	for LASTTRACK in $TRACKQUEUE; do :; done
	# get the number of digits to pad TRACKNUM with - we'll use this later
	TRACKNUMPADDING=$(echo -n $LASTTRACK | wc -c | tr -d ' ')

	QUEUEDTRACKS=$(echo $TRACKQUEUE | wc -w | tr -d ' ')

	# We have the discid, create a temp directory after it to store all the temp
	# info
	
	ABCDETEMPDIR="$WAVOUTPUTDIR/abcde.$(echo $TRACKINFO | cut -f1 -d' ')"
	if [ -e "$ABCDETEMPDIR" ]; then
		echo -n "abcde: attempting to resume from $ABCDETEMPDIR"
		# It already exists, see if it's a directory
		if [ \! -d "$ABCDETEMPDIR" ]; then
			# This is a file/socket/fifo/device/etc, not a directory
			# Complain and exit
			echo >&2
			echo "abcde: file $ABCDETEMPDIR already exists and does not belong to abcde." >&2
			echo "Please investigate, remove it, and rerun abcde." >&2
			exit 1
		fi
		echo -n .
		# It's a directory, let's see if it's owned by us
		if [ \! -O "$ABCDETEMPDIR" ]; then
			# Nope, complain and exit
			echo >&2
			echo "abcde: directory $ABCDETEMPDIR already exists and is not owned by you." >&2
			echo "Please investigate, remove it, and rerun abcde." >&2
			exit 1
		fi
		echo .
		# See if it's populated
		if [ \! -f "$ABCDETEMPDIR/discid" ]; then
			# Wipe and start fresh
			echo "abcde: $ABCDETEMPDIR/discid not found. Abcde must remove and recreate" >&2
			echo -n "this directory to continue. Continue? [y/n] " >&2
			if [ "$INTERACTIVE" = "y" ]; then
				read ANSWER
			else
				echo y >&2
				ANSWER=y
			fi
			if [ "$ANSWER" != "y" ]; then
				exit 1
			fi
			rm -rf "$ABCDETEMPDIR" || exit 1
			mkdir "$ABCDETEMPDIR"
			if [ "$?" -gt "0" ]; then
				# Directory already exists or could not be created
				echo "abcde: Temp directory $ABCDETEMPDIR could not be created." >&2
				exit 1
			fi
		fi
	else
		# We are starting from scratch
		mkdir "$ABCDETEMPDIR"
		if [ "$?" -gt "0" ]; then
			# Directory already exists or could not be created
			echo "abcde: Temp directory $ABCDETEMPDIR could not be created." >&2
			exit 1
		fi
		cat /dev/null > "$ABCDETEMPDIR/status"
	fi
	
	# Create the discid file
	echo "$TRACKINFO" > "$ABCDETEMPDIR/discid"

	# Determine what actions are to be done from $ACTIONS and set the
	# following environment variables for them:
	DOCDDB=n
	DOREAD=n
	DOENCODE=n
	DOTAG=n
	DOMOVE=n
	DOPLAYLIST=n
	DOCLEAN=n

	for ACTION in $(echo $ACTIONS | tr , \ )
	do
		case $ACTION in
		cddb) DOCDDB=y;;
		read) DOREAD=y;;
		encode) DOENCODE=y; DOREAD=y;;
		tag) DOTAG=y; DOREAD=y; DOENCODE=y; DOCDDB=y;;
		move) DOMOVE=y; DOTAG=y; DOREAD=y; DOENCODE=y; DOCDDB=y;;
		playlist) DOPLAYLIST=y;;
		clean) DOCLEAN=y;;
		esac
	done
}

# do_cddbquery
do_cddbquery ()
{
	# Perform CDDB query if it hasn't already been done
	if checkstatus ^cddb-querycomplete; then :; else
		if [ "$CDDBAVAIL" = "n" ]; then
			ERRORCODE=no_query
		else
			CDDBUSER=$(echo $HELLOINFO | cut -f1 -d'@')
			CDDBHOST=$(echo $HELLOINFO | cut -f2- -d'@')
			$CDDBTOOL query $CDDBURL $CDDBUSER $CDDBHOST $TRACKINFO > "$ABCDETEMPDIR/cddbquery"
			ERRORCODE=$?
			case $ERRORCODE in
				0)  # success
				;;
				12|13|14)
					# no match found in database,
					# wget error, or user requested not to use CDDB
					# Make up an error code (503) that abcde
					# will recognize in do_cddbread
					# and compensate by making a template
					echo 503 > "$ABCDETEMPDIR/cddbquery"
				;;
				*) # strange and unknown error
					echo ERRORCODE=$ERRORCODE
					echo "abcde: $CDDBTOOL returned unknown error code"
				;;
			esac
		fi
		echo cddb-querycomplete >> $ABCDETEMPDIR/status
	fi
}

# do_cddbread
do_cddbread ()
{
	# If it's not to be used, generate a template.
	# Then, display it (or them) and let the user choose/ edit it
	if checkstatus ^cddb-readcomplete; then :; else
		# If CDDB is to be used, interpret the query results and read all
		# the available entries.
		rm -f "$ABCDETEMPDIR/cddbchoices"
		CDDBCHOICES=1 # Overridden by multiple matches
		RESPONSECODE="$(head -1 $ABCDETEMPDIR/cddbquery | cut -f1 -d' ')"
		case "$RESPONSECODE" in
		200)
			# One exact match, retrieve it
			# 200 [section] [discid] [artist] / [title]
			if checkstatus ^cddb-read-1-complete; then :; else
				echo -n "Retrieving 1 CDDB match..." >> "$ABCDETEMPDIR/cddbchoices"
				$CDDBTOOL read $CDDBURL $CDDBUSER $CDDBHOST $(cut -f2,3 -d' ' "$ABCDETEMPDIR/cddbquery") > "$ABCDETEMPDIR/cddbread.1"
				echo "done." >> "$ABCDETEMPDIR/cddbchoices"
				echo cddb-read-1-complete >> $ABCDETEMPDIR/status
				echo cddb-choice=1 >> $ABCDETEMPDIR/status
			fi
			# List out disc title/author and contents
			echo ---- $(cut '-d ' -f4- "$ABCDETEMPDIR/cddbquery") ---- >> "$ABCDETEMPDIR/cddbchoices"
			for TRACK in $(seq 1 $TRACKS)
			do
				echo $TRACK: $(grep ^TTITLE$(expr $TRACK - 1)= $ABCDETEMPDIR/cddbread.1 | cut -f2- -d= | tr -d \\r\\n) >> "$ABCDETEMPDIR/cddbchoices"
			done
			echo >> "$ABCDETEMPDIR/cddbchoices"
			;;
		202|403|409|503)
			# No match
			case "$RESPONSECODE" in
			202) echo "No CDDB match." >> "$ABCDETEMPDIR/cddbchoices" ;;
			403|409) echo "CDDB entry is corrupt, or the handshake failed." >> "$ABCDETEMPDIR/cddbchoices" ;;
			503) echo "CDDB unavailable." >> "$ABCDETEMPDIR/cddbchoices" ;;
			esac
			$CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.1"
			# List out disc title/author and contents of template
			echo ---- Unknown Artist / Unknown Album ---- >> "$ABCDETEMPDIR/cddbchoices"
			for TRACK in $(seq 1 $TRACKS)
			do
				echo $TRACK: $(grep ^TTITLE$(expr $TRACK - 1)= $ABCDETEMPDIR/cddbread.1 | cut -f2- -d= | tr -d \\r\\n) >> "$ABCDETEMPDIR/cddbchoices"
			done
			echo >> "$ABCDETEMPDIR/cddbchoices"
			echo cddb-read-1-complete >> $ABCDETEMPDIR/status
			echo cddb-choice=1 >> $ABCDETEMPDIR/status
			;;
		210|211)
			# Multiple exact, (possibly multiple) inexact matches
			IN=
			if [ "$RESPONSECODE" = "211" ]; then IN=in; fi
			if [ "$(wc -l < $ABCDETEMPDIR/cddbquery | tr -d ' ')" -eq 3 ]; then
				echo "One ${IN}exact match:" >> "$ABCDETEMPDIR/cddbchoices"
				tail +2 $ABCDETEMPDIR/cddbquery | head -1 >> "$ABCDETEMPDIR/cddbchoices"
	                        echo cddb-choice=1 >> $ABCDETEMPDIR/status
			else
				echo "Multiple ${IN}exact matches:" >> "$ABCDETEMPDIR/cddbchoices"
			fi
			grep -v ^[.]$ "$ABCDETEMPDIR/cddbquery" | ( X=0
			read DISCINFO # eat top line
			while read DISCINFO
			do
				X=$(expr $X + 1)
				if checkstatus ^cddb-read-$X-complete; then :; else
					$CDDBTOOL read $CDDBURL $CDDBUSER $CDDBHOST $(echo $DISCINFO | cut -f1,2 -d' ') > "$ABCDETEMPDIR/cddbread.$X"
					echo cddb-read-$X-complete >> $ABCDETEMPDIR/status
				fi
				# List out disc title/author and contents
				echo \#$X: ---- "$DISCINFO" ---- >> "$ABCDETEMPDIR/cddbchoices"
				for TRACK in $(seq 1 $TRACKS)
				do
					echo $TRACK: $(grep ^TTITLE$(expr $TRACK - 1)= $ABCDETEMPDIR/cddbread.$X | cut -f2- -d= | tr -d \\r\\n) >> "$ABCDETEMPDIR/cddbchoices"
				done
				echo >> "$ABCDETEMPDIR/cddbchoices"
			done )
			CDDBCHOICES=$(expr $(cat "$ABCDETEMPDIR/cddbquery" | wc -l) - 2)
			;;
		esac	
	fi
	echo "cddb-readcomplete" >> "$ABCDETEMPDIR/status"
}

# do_cddbedit
do_cddbedit ()
{
	if [ "$INTERACTIVE" = "y" ]; then
		# Display the $ABCDETEMPDIR/cddbchoices file created above
		# Pick a pager so that if the tracks overflow the screen the user can still view everything
		if [ -r "$ABCDETEMPDIR/cddbchoices" ]; then
			if checkstatus ^cddb-choice >/dev/null; then
				# Make sure user sees this so they can edit it if they want to
				cat "$ABCDETEMPDIR/cddbchoices"
			else
				# The user has a choice to make, display the info in a pager if necessary
				if [ $(cat "$ABCDETEMPDIR/cddbchoices" | wc -l) -ge 24 ]; then
					# Use the debian sensible-pager wrapper to pick the pager
					# user has requested via their $PAGER environment variable
					if [ -x "/usr/bin/sensible-pager" ]; then
						/usr/bin/sensible-pager "$ABCDETEMPDIR/cddbchoices"
					elif [ -x "$PAGER" ]; then
						# That failed, try to load the preferred editor, starting
						# with their PAGER variable
						$PAGER "$ABCDETEMPDIR/cddbchoices"
						# If that fails, check for less
					elif [ -x /usr/bin/less ]; then
						/usr/bin/less "$ABCDETEMPDIR/cddbchoices"
						# more should be on all UNIX systems
					elif [ -x /bin/more ]; then
						/bin/more "$ABCDETEMPDIR/cddbchoices"
					else
						# No bananas, just cat the thing
						cat "$ABCDETEMPDIR/cddbchoices" >&2
					fi
				else
					# It's all going to fit in one page, cat it
					cat "$ABCDETEMPDIR/cddbchoices" >&2
				fi
		
				# I'll take CDDB read #3 for $400, Alex
				echo -n "Which entry would you like abcde to use? [1-$CDDBCHOICES]: " >&2
				read CDDBCHOICE
				# Make sure we get a valid choice
				CDCHOICENUM=$(echo $CDDBCHOICE | xargs printf %d 2>/dev/null)
			        while [ $CDCHOICENUM -lt 1 ] || [ $CDCHOICENUM -gt $CDDBCHOICES ]; do
					echo "Invalid selection. Please choose a number between 1 and $CDDBCHOICES." >&2
					echo -n "Selection [1-$CDDBCHOICES]: " >&2
					read CDDBCHOICE
					CDCHOICENUM=$(echo $CDDBCHOICE | xargs printf %d 2>/dev/null)
				done
				echo "Selected: #$CDCHOICENUM ($(grep ^DTITLE= $ABCDETEMPDIR/cddbread.$CDCHOICENUM | cut -f2- -d= | tr -d \\r\\n))" >&2
				echo "cddb-choice=$CDCHOICENUM" >> "$ABCDETEMPDIR/status"
			fi
		fi
	else
		# We're noninteractive - pick the first choice.
		if [ -r "$ABCDETEMPDIR/cddbchoices" ]; then
			if checkstatus ^cddb-choice >/dev/null; then
				cat "$ABCDETEMPDIR/cddbchoices"
			fi
			CDCHOICENUM=1
			echo "Selected: #$CDCHOICENUM ($(grep ^DTITLE= $ABCDETEMPDIR/cddbread.$CDCHOICENUM | cut -f2- -d= | tr -d \\r\\n))" >&2
			echo "cddb-choice=1" >> "$ABCDETEMPDIR/status"
		fi
	fi
	
	# sanity check
	if checkstatus ^cddb-choice >/dev/null; then :; else
		echo "abcde: internal error: cddb-choice not recorded." >&2
		exit 1
	fi
	CDDBDATA="$ABCDETEMPDIR/cddbread.$(checkstatus ^cddb-choice)"
	
	echo -n "Edit selected CDDB data? [y/n] (n): " >&2
	if [ "$INTERACTIVE" = "y" ]; then
		read EDITCDDB
	else
		echo n >&2
		EDITCDDB=n
	fi
	if [ "$EDITCDDB" = "y" ]; then
		# Use the debian sensible-editor wrapper to pick the editor that the
		# user has requested via their $EDITOR environment variable
		if [ -x "/usr/bin/sensible-editor" ]; then
			/usr/bin/sensible-editor "$CDDBDATA"
		elif [ -x "$EDITOR" ]; then
			# That failed, try to load the preferred editor, starting
			# with their EDITOR variable
			$EDITOR "$CDDBDATA"
		# If that fails, check for a vi
		elif [ -x /usr/bin/vi ]; then
			/usr/bin/vi "$CDDBDATA"
		# ae should be on all debian systems
		elif [ -x /bin/ae ]; then
			/bin/ae "$CDDBDATA"
		# bomb out
		else
			echo "No editor available. Check your EDITOR environment variable." >&2
		fi
		# delete editor backup file if it exists
		if [ -w "$CDDBDATA~" ]; then
			rm -f "$CDDBDATA~"
		fi
	fi	
	
	# Some heuristics first. Look at Disc Title, and if it starts with
	# "Various", then we'll assume Various Artists
	if [ "$(grep ^DTITLE= "$CDDBDATA" | cut -f2 -d= | egrep -ci '^(various|soundtrack)')" != "0" ]; then
		echo "Looks like a Multi-Artist CD" >&2
		VARIOUSARTISTS=y
	else
		echo -n "Is the CD multi-artist? [y/n] (n): " >&2
		if [ "$INTERACTIVE" = "y" ]; then
			read VARIOUSARTISTS
		else
			echo n >&2
			VARIOUSARTISTS=n
		fi
	fi
	if [ "$VARIOUSARTISTS" = "y" ]; then
		# Set a default
		DEFAULTSTYLE=1
		# Need NUMTRACKS before cddb-tool will return it:
		NUMTRACKS=$(grep -E '^TTITLE[0-9]+=' "$CDDBDATA" | wc -l)
		if [ "$(grep -c "^TTITLE.*\/" "$CDDBDATA")" -gt "$(expr $NUMTRACKS / 2 )" ]; then
			# More than 1/2 tracks contain a "/", so guess forward
			DEFAULTSTYLE=1
		elif [ "$(grep -c "^TTITLE.*\-" "$CDDBDATA")" -gt "$(expr $NUMTRACKS / 2 )" ]; then
			# More than 1/2 contain a "-", so guess forward-dash
			DEFAULTSTYLE=2
		fi

		echo "1) Artist / Title" >&2
		echo "2) Artist - Title" >&2
		echo "3) Title / Artist" >&2
		echo "4) Title - Artist" >&2
		echo "5) Artist: Title" >&2
		echo "6) This is a single-artist CD" >&2
		echo -n "Which style of multiple artist entries is it? [1-6] ($DEFAULTSTYLE): " >&2
		if [ "$INTERACTIVE" = "y" ]; then
			read VARIOUSARTISTSTYLE
		else
			echo $DEFAULTSTYLE >&2
			VARIOUSARTISTSTYLE=$DEFAULTSTYLE
		fi
		VARIOUSARTISTSTYLE=$(echo $VARIOUSARTISTSTYLE | xargs printf %d)
		# If they press Enter, then the default style (0) was chosen
		while [ $VARIOUSARTISTSTYLE -lt 0 ] || [ $VARIOUSARTISTSTYLE -gt 6 ]; do
			echo "Invalid selection. Please choose a number between 1 and 6."
			echo -n "Selection [1-6]: "


			read VARIOUSARTISTSTYLE
			VARIOUSARTISTSTYLE=$(echo $VARIOUSARTISTSTYLE | xargs printf %d)
		done
		if [ "$VARIOUSARTISTSTYLE" = "0" ]; then
			VARIOUSARTISTSTYLE=$DEFAULTSTYLE
		fi
		echo "Selected: $VARIOUSARTISTSTYLE"
		case "$VARIOUSARTISTSTYLE" in
		1) # Artist / Title
			VARIOUSARTISTSTYLE=forward
			;;
		2) # Artist - Title
			VARIOUSARTISTSTYLE=forward-dash
			;;
		3) # Title / Artist
			VARIOUSARTISTSTYLE=reverse
			;;
		4) # Title - Artist
			VARIOUSARTISTSTYLE=reverse-dash
			;;
		5) # Artist: Title
			VARIOUSARTISTSTYLE=colon
			;;
		6) # Single Artist
			VARIOUSARTISTS=n
			;;
		esac
	fi
	
	#	This is temporarily commented out until I have a chance to mess with it a
	#	little more. --rcw 8/31/1999
	#
	#	In the meantime if someone else wants to fiddle with it be my guest.
	#	Please report all issues with the submission code to rcw@debian.org.
	#	--rcw 12/31/1999
	#
	#	# submit the modified file, if they want
	#	if [ "$NOSUBMIT" != "y" ]; then
	#		echo -n "Do you want to submit this entry to $CDDBSUBMIT? [y|N] "
	#		read YESNO
	#		while [ "$YESNO" != "y" ] && [ "$YESNO" != "n" ] && [ "$YESNO" != "Y" ] && [ "$YESNO" != "N" ]
	#		do
	#			echo -n 'Invalid selection. Please answer "y" or "n": '
	#			read YESNO
	#		done
	#		if [ "$YESNO" = "y" ] || [ "$YESNO" = "Y" ]; then
	#			echo -n "Sending..."
	#			$CDDBTOOL send "$CDDBDATA" $CDDBSUBMIT
	#			echo "done."
	#		fi
	#	fi
}

# do_cdread [tracknumber]
# 
do_cdread ()
{
	# The commands here don't go through run_command because they're never supposed to be silenced
	UTRACKNUM=$1
	CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
	WAVDATA="$ABCDETEMPDIR/track$UTRACKNUM.wav"
	OUTDATA="$ABCDETEMPDIR/track$UTRACKNUM.$OUTPUTTYPE"
	if [ -r "$CDDBDATA" ]; then
		TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -1 | cut -f2 -d= | tr -d \[:cntrl:\])
		echo "Grabbing track $UTRACKNUM: $TRACKNAME..." >&2
	else
		echo "Grabbing track $UTRACKNUM..." >&2
	fi
	case "$CDROMREADERSYNTAX" in
		cdparanoia) nice $READNICE $CDROMREADER -d $CDROM $UTRACKNUM "$WAVDATA" >&2 ;;
		cdda2wav) nice $READNICE $CDROMREADER -H -D $CDROM -t $UTRACKNUM "$WAVDATA" >&2 ;;
		debug) nice $READNICE $CDROMREADER -d $CDROM -w $UTRACKNUM-[:1] "$WAVDATA" >&2 ;;
	esac
	echo readtrack-$UTRACKNUM >> "$ABCDETEMPDIR/status"
}

# Start of execution

# Builtin defaults
CDDBURL="http://freedb.freedb.org/~cddb/cddb.cgi"
HELLOINFO="$(whoami)@$(hostname)"
INTERACTIVE=y
CDROM=/dev/cdrom
CDROMREADERSYNTAX=cdparanoia
OUTPUTTYPE=ogg
ENCODERSYNTAX=default
OUTPUTFORMAT='${ARTISTFILE}/${TRACKFILE}.$OUTPUTTYPE'
VAOUTPUTFORMAT=${OUTPUTFORMAT}
PLAYLISTFORMAT='${ARTISTFILE}_-_${ALBUMFILE}.m3u'
PLAYLISTDATAPREFIX=''
COMMENT=''
ID3TAGV=2
ENCNICE=10
READNICE=10
VARIOUSARTISTS=n
VARIOUSARTISTSTYLE=forward

# program paths - defaults to checking your $PATH
LAME=lame
GOGO=gogo
BLADEENC=bladeenc
L3ENC=l3enc
XINGMP3ENC=xingmp3enc
MP3ENC=mp3enc
VORBIZE=vorbize
OGGENC=oggenc
ID3=id3
ID3V2=id3v2
CDPARANOIA=cdparanoia
CDDA2WAV=cdda2wav
WGET=wget
CDDISCID=cd-discid
CDDBTOOL=cddb-tool
EJECT=eject
DISTMP3=distmp3
VORBISCOMMENT=vorbiscomment

# Options for programs called from abcde
LAMEOPTS=
GOGOOPTS=
BLADEENCOPTS=
L3ENCOPTS=
XINGMP3ENCOPTS=
MP3ENCOPTS=
VORBIZEOPTS=
OGGENCOPTS=
ID3OPTS=
ID3V2OPTS=
CDPARANOIAOPTS=
CDDA2WAVOPTS=
WGETOPTS=
CDDBTOOLOPTS=
EJECTOPTS=
DISTMP3OPTS=

# Default to one process if -j isn't specified
MAXPROCS=1

# List of actions to perform - by default, run to completion
ACTIONS=cddb,read,encode,tag,move,clean

# User-redefinable functions
# Custom filename munging:
mungefilename ()
{
	echo "$@" | sed s,:,\ -,g | tr \ / __ | tr -d \'\? | tr -d \[:cntrl:\]
}

# If CDDBAVAIL is set to n, no CDDB read is done
# If USEID3 is set to n, no ID3 tagging is done
CDDBAVAIL=y
USEID3=y

if [ "$OUTPUTDIR" = "" ]; then
	OUTPUTDIR=$(pwd)
fi

if [ "$WAVOUTPUTDIR" = "" ]; then
	WAVOUTPUTDIR="$OUTPUTDIR"
fi

# Load system defaults
if [ -r /etc/abcde.conf ]; then
	. /etc/abcde.conf
fi
# Load user preference defaults
if [ -r $HOME/.abcde.conf ]; then
	. $HOME/.abcde.conf
fi

# Parse command line options
while getopts a:c:C:d:Dhj:lnNo:r:x opt ; do
	case "$opt" in
		a) ACTIONS="$OPTARG" ;;
		c) . "$OPTARG" ;;
		C) DISCID="$OPTARG" ;;
		d) CDROM="$OPTARG" ;;
		D) set -x ;;
		j) MAXPROCS="$OPTARG" ;;
		h) usage; exit ;;
		l) LOWDISK=y ;;
		n) CDDBAVAIL="n" ;;
		N) INTERACTIVE="n" ;;
		o) OUTPUTTYPE="$OPTARG" ;;
		r) REMOTEHOSTS="$OPTARG" ;;
		x) EJECTCD="y" ;;
		?) usage; exit ;;
	esac
done

shift $(($OPTIND - 1))

while [ $# -gt 0 ]; do
	# Range parsing code courtesy of Vincent Ho
	RSTART=$(echo $1 | cut -f1 -d-)
	REND=$(echo $1 | cut -f2 -d-)
	if [ "$RSTART" = "$REND" ]; then 
		NEWTRACKS="$RSTART"
	else
		NEWTRACKS=$(seq -s ' ' $RSTART $REND)
	fi
	TRACKQUEUE=$(echo "$TRACKQUEUE" "$NEWTRACKS")

	shift
done

# Decide which CDROM reader we're gonna use
case "$CDROMREADERSYNTAX" in
	cdparanoia|debug)
		CDROMREADER="$CDPARANOIA"
		CDROMREADEROPTS="$CDPARANOIAOPTS"
		;;
	cdda2wav)
		CDROMREADER="$CDDA2WAV"
		CDROMREADEROPTS="$CDDA2WAVOPTS"
		;;
esac

# If nothing has been specified, use oggenc for oggs and lame for mp3s
if [ "$ENCODERSYNTAX" = "default" ]; then
	if [ "$OUTPUTTYPE" = "ogg" ]; then
		ENCODERSYNTAX=oggenc
	else
		ENCODERSYNTAX=lame
	fi
fi

# decide which encoder
case "$ENCODERSYNTAX" in
	lame)
		ENCODEROPTS="$LAMEOPTS"
		ENCODER="$LAME"
		;;
	gogo)
		ENCODEROPTS="$GOGOOPTS"
		ENCODER="$GOGO"
		;;
	bladeenc)
		ENCODEROPTS="$BLADEENCOPTS"
		ENCODER="$BLADEENC"
		;;
	l3enc)
		ENCODEROPTS="$L3ENCOPTS"
		ENCODER="$L3ENC"
		;;
	xingmp3enc)
		ENCODEROPTS="$XINGMP3ENCOPTS"
		ENCODER="$XINGMP3ENC"
		;;
	mp3enc)
		ENCODEROPTS="$MP3ENCOPTS"
		ENCODER="$MP3ENC"
		;;
	vorbize)
		ENCODEROPTS="$VORBIZEOPTS"
		ENCODER="$VORBIZE"
		;;
	oggenc)
		ENCODEROPTS="$OGGENCOPTS"
		ENCODER="$OGGENC"
		;;
esac

# and which tagger

if [ "$ID3TAGV" = "1" ]; then
	TAGGER="$ID3"
	TAGGEROPTS="$ID3OPTS"
else
	TAGGER="$ID3V2"
	TAGGEROPTS="$ID3V2OPTS"
fi

# Clean up nice options (either use '-n NICELEVEL or -NICELEVEL')

if [ "$ENCNICE" != "" ]; then
	ENCNICE=-$ENCNICE
fi
if [ "$READNICE" != "" ]; then
	READNICE=-$READNICE
fi


# Don't check for stuff if it's not needed
if [ "$REMOTEHOSTS" != "" ]; then NEEDDISTMP3=y; fi	
if [ "$OUTPUTTYPE" = "mp3" ]; then NEEDTAGGER=y; fi
if [ "$OUTPUTTYPE" = "ogg" ]; then NEEDCOMMENTER=y; fi

# Make sure a buncha things exist
for X in $CDROMREADER $CDDISCID ${NEEDTAGGER+$TAGGER} $ENCODER $WGET \
	${NEEDDISTMP3+$DISTMP3} ${NEEDCOMMENTER+$VORBISCOMMENT}
do
	# Cut off the command-line options we just added in
	X=$(echo $X | cut -d' ' -f2)
	if [ "$(which $X)" = "" ]; then
		echo "abcde error: $X is not in your path." >&2
		exit 1
	elif [ \! -x $(which $X) ]; then
		echo "abcde error: $X is not executable." >&2
		exit 1
	fi 
done

CDROMREADER="$CDROMREADER $CDROMREADEROPTS"
CDDBTOOL="$CDDBTOOL $CDDBTOOLOPTS"

# One thousand lines in, we can start doing stuff with things

# List of valid actions: cddb,playlist,read,encode,tag,move

do_discid # Get ABCDETEMPDIR created and status file initialized

if [ "$DOCDDB" = "y" ]; then
	do_cddbquery
	do_cddbread
	do_cddbedit

	eval $($CDDBTOOL parse "$CDDBDATA")
fi

# Export needed things so they can be read in this subshell
export CDDBTOOL ABCDETEMPDIR TRACKQUEUE LOWDISK EJECTCD EJECT EJECTOPTS
export CDROM CDDBDATA REMOTEHOSTS MAXPROCS

# Create playlist if needed (backgroundable) and start reading in tracks
(
if [ "$DOPLAYLIST" = "y" ]; then
	echo Creating playlist... >&2
	do_playlist
fi

# For the lowdisk option, only one program is running at once so the encoder
# can be unsilenced right away.
if [ "$LOWDISK" = "y" ]; then
	echo "encode-output=loud" >> "$ABCDETEMPDIR/status"
fi

for UTRACKNUM in $TRACKQUEUE
do
	if [ "$DOREAD" = "y" ]; then
		if checkstatus ^readtrack-$UTRACKNUM; then :; else do_cdread $UTRACKNUM; fi
		echo NEXTTRACK # Get the encoder machine churning again
		if [ "$LOWDISK" = "y" ] && [ "$DOENCODE" = "y" ]; then
			until checkstatus encodetrack-$UTRACKNUM; do sleep 2; done
		fi
	fi
done

# Now that we're done the encoding can be loud again.
echo "encode-output=loud" >> "$ABCDETEMPDIR/status"

# We are now finished with the cdrom - it can be safely ejected. Note that
# abcde will not have completed yet.
if [ "$EJECTCD" = "y" ]; then
	$EJECT $EJECTOPTS $CDROM
fi
) | (
# Do the encoding, including parallelization of remote encoding
# Figure out where each track is going to be encoded
ENCODELOCATIONS=""
for REMOTEHOST in $(echo $REMOTEHOSTS | tr , ' ')
do
	ENCODELOCATIONS="$ENCODELOCATIONS $REMOTEHOST"
done
for NUM in $(seq 1 "$MAXPROCS")
do
	ENCODELOCATIONS="$ENCODELOCATIONS %local%"
done
# Strip whitespace
ENCODELOCATIONS=$(echo $ENCODELOCATIONS)
for UTRACKNUM in $TRACKQUEUE
do
	read FOO # For blocking only - $FOO never gets used
	# FIXME: bashisms (arrays)
	# find out where this track is to be encoded
	if [ "$DOENCODE" = "y" ]; then
		PROCEED=
		until [ $PROCEED ]
		do
			for LOCATION in $ENCODELOCATIONS
			do
				PREVIOUSTRACK="$(checkstatus ^encodetracklocation-$LOCATION)"
				# check first if a track has ever been assigned to this location
				if [ "$PREVIOUSTRACK" = "" ]; then PROCEED=y; break; fi
				# We're still here, this location must have been previously assigned,
				# check if it's done with the previous track yet
				if checkstatus ^encodetrack-$PREVIOUSTRACK; then PROCEED=y; break; fi
			done
			# all locations are working, wait and try again later
			if [ \! $PROCEED ]; then sleep 5; fi
		done
		echo "encodetracklocation-$LOCATION=$UTRACKNUM" >> "$ABCDETEMPDIR/status"
	fi
	# Set TRACKNUM, TRACKNAME
	if [ -e "$CDDBDATA" ]; then
		TRACKNUM=$(printf %0.${TRACKNUMPADDING}d ${UTRACKNUM})
	        CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
		TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -1 | cut -f2 -d= | tr -d \[:cntrl:\])
		splitvarious
	fi
	if [ "$LOCATION" = "%local%" ]; then
		if [ "$DOENCODE" = "y" ]; then
			if checkstatus ^encodetrack-$UTRACKNUM; then :; else do_encode $UTRACKNUM $LOCATION; fi
		fi
		if [ "$DOTAG" = "y" ]; then
			if checkstatus ^tagtrack-$UTRACKNUM; then :; else do_tag $UTRACKNUM; fi
		fi
		if [ "$DOMOVE" = "y" ]; then
			if checkstatus ^movetrack-$UTRACKNUM; then :; else do_move $UTRACKNUM; fi
		fi
	else
		# You can't tag a file before it's finished encoding -
		# thus all of this is backgrounded together
		(
		if [ "$DOENCODE" = "y" ]; then
			if checkstatus ^encodetrack-$UTRACKNUM; then :; else do_encode $UTRACKNUM $LOCATION; fi
		fi
		if [ "$DOTAG" = "y" ]; then
			if checkstatus ^tagtrack-$UTRACKNUM; then :; else do_tag $UTRACKNUM; fi
		fi
		if [ "$DOMOVE" = "y" ]; then
			if checkstatus ^movetrack-$UTRACKNUM; then :; else do_move $UTRACKNUM; fi
		fi
		) &
	fi
done
# Go through it again and make sure there's no distmp3 stragglers, otherwise
# we'll delete the files they're working on
if [ "$DOENCODE" = "y" ]; then
	PROCEED=
	until [ $PROCEED ]
	do
		PROCEED=y
		for LOCATION in $ENCODELOCATIONS
		do
			CHECKTRACK="$(checkstatus ^encodetracklocation-$LOCATION)"
			# Nothing to see here please go quietly back to your homes
			if [ "$CHECKTRACK" = "" ]; then continue; fi
			if checkstatus ^encodetrack-$CHECKTRACK; then :; else PROCEED=; break; fi
		done
		# hold up
		if [ \! $PROCEED ]; then sleep 5; fi
	done
fi
echo Finished.
if [ "$DOCLEAN" = "y" ]; then
	# Wipe all the evidence
	rm -rf "$ABCDETEMPDIR"
fi
)
exit 0
