#!/bin/sh
# Copyright (c) 1998,1999,2000 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.2

usage ()
{
echo "This is abcde v$VERSION."
echo "Usage: abcde [options] [tracks]"
echo "Options:"
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 [num] Low disk space handling option"
echo "      1 = normal parallelization"
echo "      2 = disk space conservation"
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 "-p    Create m3u playlist"
echo "-P    Only create m3u playlist (do no CD ripping)"
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
}

# do_tag [tracknumber]
# id3 tags a filename
# variables used:
# TRACKS, TRACKNAME, ID3, ID3OPTS, ID3COMMENT, DALBUM, DARTIST
do_tag ()
{
	# Ogg commenting is typically done by the encoder since it's at the
	# beginning of the stream
	if [ "$OUTPUTTYPE" = "mp3" ]; then
		run_command '' echo "Tagging track $1 of $TRACKS: $TRACKNAME..."
		run_command tagtrack-$1 $ID3 $ID3OPTS -c "$(eval echo ${ID3COMMENT})" \
			-A "$DALBUM" -a "$DARTIST" -t "$TRACKNAME" -T "$1" "$ABCDETEMPDIR/track$1.$OUTPUTTYPE"
	fi
}

# do_encode [tracknumber] [hostname]
# If no hostname is specified, encode locally
# variables used:
# TRACKS, TRACKNAME, 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" != "" ]; then
			run_command encodetrack-$1 $DISTMP3 $DISTMP3OPTS $2 "$IN" "$OUT"
		else
			case "$ENCODERSYNTAX" in
			lame|gogo) run_command encodetrack-$1 nice $ENCNICE $ENCODER $ENCODEROPTS "$IN" "$OUT" ;;
			bladeenc|l3enc) run_command encodetrack-$1 nice $ENCNICE $ENCODER "$IN" "$OUT" $ENCODEROPTS ;;
			mp3enc) run_command encodetrack-$1 nice $ENCNICE $ENCODER -if "$IN" -of "$OUT" $ENCODEROPTS ;;
			esac
		fi
		;;
	ogg)
		case "$ENCODERSYNTAX" in
		vorbize) run_command encodetrack-$1 nice $ENCNICE $ENCODER $ENCODEROPTS -w "$OUT" -a "$DARTIST" -t "$TRACKNAME" -l "$DALBUM" "$IN" ;;
		oggenc) run_command encodetrack-$1 nice $ENCNICE $ENCODER $ENCODEROPTS "$IN" -o "$OUT" -a "$DARTIST" -t "$TRACKNAME" -l "$DALBUM" ;;
		esac
		;;
	esac
	rm -f "$IN"
}

# do_move [tracknumber]
# Deduces the outfile from environment variables
# Creates directory if necessary
# variables used:
# TRACKNUM, TRACKNAME, DALBUM, DARTIST, OUTPUTFORMAT
do_move ()
{
	# Create ALBUMFILE, ARTISTFILE, TRACKFILE
	# Munge filenames as follows: 
	# ' ' -> '_'
	# '/' -> '_'
	# ''' -> ''
	# '?' -> ''
	# Eat control characters
	ALBUMFILE=$(mungefilename $DALBUM)
	ARTISTFILE=$(mungefilename $DARTIST)
	TRACKFILE=$(mungefilename $TRACKNAME)
	# Supported variables for OUTPUTFORMAT are ALBUMFILE, ARTISTFILE, TRACKFILE, and TRACKNUM.
	OUTPUTFILE=$(eval echo $OUTPUTFORMAT)
	# Check that the directory for OUTPUTFILE exists, if it doesn't, create it
	OUTPUTFILEDIR=$(dirname "$OUTPUTFILE")
	if [ \! -d "$OUTPUTFILEDIR" ]; then
		mkdir -p "$OUTPUTFILEDIR"
	fi
	run_command movetrack-$1 mv "$ABCDETEMPDIR/track$1.$OUTPUTTYPE" "$OUTPUTFILE"
}

# do_playlist
# Create the playlist if wanted
# Variables used:
# PLAYLIST, PLAYLISTFORMAT, PLAYLISTDATAPREFIX, OUTPUTDIR, 
do_playlist ()
{
	# Create a playlist file for the playlist data to go into, wiping it out if
	# it exists already
	if [ "$PLAYLIST" = "y" ]; then
		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 | xargs)
		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:\])
			TRACKFILE=$(mungefilename $TRACKNAME)
			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
	fi
	echo "playlistcomplete" >> "$ABCDETEMPDIR/status"
}

# do_discid
# This essentially the start of things
do_discid ()
{
	# Query the CD to get the track info
	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?"
		exit 1
	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
		TRACKQUEUE=$((for X in $TRACKQUEUE; do echo $X; done) | sort -n | uniq | xargs)
		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 | xargs)

	QUEUEDTRACKS=$(echo $TRACKQUEUE | wc -w | xargs)

	# We have the discid, create a temp directory after it to store all the temp
	# info
	
	ABCDETEMPDIR="$OUTPUTDIR/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
			echo "abcde: file $ABCDETEMPDIR already exists and does not belong to abcde."
			echo "Please investigate, remove it, and rerun abcde."
			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
			echo "abcde: directory $ABCDETEMPDIR already exists and is not owned by you."
			echo "Please investigate, remove it, and rerun abcde."
			exit 1
		fi
		echo -n .
		# See if it's populated
		if [ \! -f "$ABCDETEMPDIR/discid" ]; then
			# Wipe and start fresh
			echo
			echo "abcde: $ABCDETEMPDIR/discid not found. Abcde must remove and recreate"
			echo -n "this directory to continue. Continue? [y/n] "
			if [ "$INTERACTIVE" = "y" ]; then
				read ANSWER
			else
				echo y
				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 -n "abcde: Temp directory $ABCDETEMPDIR could not be created."
				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 -n "abcde: Temp directory $ABCDETEMPDIR could not be created."
			exit 1
		fi
		cat /dev/null > "$ABCDETEMPDIR/status"
	fi
	
	# Create the discid file
	echo "$TRACKINFO" > "$ABCDETEMPDIR/discid"
}

# 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, Multiple inexact matches
			WORD=exact
			if [ "$RESPONSECODE" = "211" ]; then WORD=inexact; fi
			if [ "$(wc -l $ABCDETEMPDIR/cddbquery | xargs)" = "3" ]; then
				echo "One $WORD match:" >> "$ABCDETEMPDIR/cddbchoices"
				tail +2 $ABCDETEMPDIR/cddbquery | head -1 >> "$ABCDETEMPDIR/cddbchoices"
			else
				echo "Multiple $WORD 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 | xargs) -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-$NUMCDCHOICES]: " >&2
					read CDDBCHOICE
					CDCHOICENUM=$(echo $CDDBCHOICE | xargs printf %d 2>/dev/null)
				done
				echo "Selected: \#$CDCHOICENUM ($(grep ^DTITLE= $ABCDETEMPDIR/cddbread.$CDCHOICENUM | cut -f2- -d=))" >&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=))" >&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): "
	if [ "$INTERACTIVE" = "y" ]; then
		read EDITCDDB
	else
		echo n
		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	
	
	#	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)
	TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -1 | cut -f2 -d= | tr -d \[:cntrl:\])
	WAVDATA="$ABCDETEMPDIR/track$UTRACKNUM.wav"
	OUTDATA="$ABCDETEMPDIR/track$UTRACKNUM.$OUTPUTTYPE"
	echo "Grabbing track $UTRACKNUM: $TRACKNAME..." >&2
	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://cddb.cddb.com/~cddb/cddb.cgi"
HELLOINFO="$(whoami)@$(hostname)"
INTERACTIVE=y
CDROM=/dev/cdrom
CDROMREADERSYNTAX=cdparanoia
OUTPUTTYPE=ogg
ENCODERSYNTAX=oggenc
OUTPUTFORMAT='${ARTISTFILE}/${TRACKFILE}.$OUTPUTTYPE'
PLAYLISTFORMAT='${ARTISTFILE}_-_${ALBUMFILE}.m3u'
PLAYLISTDATAPREFIX=''
ID3COMMENT=''
ID3TAGV=2
ENCNICE=10
READNICE=10

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

# Options for programs called from abcde
LAMEOPTS=
GOGOOPTS=
BLADEENCOPTS=
L3ENCOPTS=
MP3ENCOPTS=
VORBIZEOPTS=
OGGENCOPTS=
ID3OPTS=
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

# 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:d:Dehj:lnNo:r:vV:x opt ; do
	case "$opt" in
		a) ACTIONS="$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

# and 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"
		;;
	mp3enc)
		ENCODEROPTS="$MP3ENCOPTS"
		ENCODER="$MP3ENC"
		;;
	vorbize)
		ENCODEROPTS="$VORBIZEOPTS"
		ENCODER="$VORBIZE"
		;;
	oggenc)
		ENCODEROPTS="$OGGENCOPTS"
		ENCODER="$OGGENC"
		;;
esac

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

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


if [ "$REMOTEHOSTS" != "" ]; then
	REMOTEENCODE=y
fi	
	
# Make sure a buncha things exist
for X in $CDROMREADER $CDDISCID $ID3 $ENCODER $WGET ${REMOTEENCODE+$DISTMP3}
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."
		exit 1
	elif [ \! -x $(which $X) ]; then
		echo "abcde error: $X is not executable."
		exit 1
	fi 
done

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

# Eight hundred lines in, we can start doing stuff with things

# This subthread sits on $ABCDETEMPDIR/status watching it grow
# It is responsible for all CDDB gathering and all CD reading
# It exits when the status file contains "^cleanup-and-exit"

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

do_discid # Get ABCDETEMPDIR created and status file initialized

do_cddbquery
do_cddbread
do_cddbedit

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

# 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
(
echo Creating playlist... >&2
do_playlist # &

# 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 checkstatus ^readtrack-$UTRACKNUM; then :; else do_cdread $UTRACKNUM; fi

	if [ "$LOWDISK" = "y" ]; then
		until checkstatus encodetrack-$UTRACKNUM; do sleep 2; done
	fi
	echo NEXTTRACK # Get the encoder machine churning again
done

# 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
NUMLOCATIONS=0
ENCODELOCATION=""
for REMOTEHOST in $(echo $REMOTEHOSTS | sed 's-,- -g')
do
	if [ "$NUMLOCATIONS" = "0" ]; then
		ENCODELOCATION=$REMOTEHOST
	else
		ENCODELOCATION=$ENCODELOCATION:$REMOTEHOST
	fi
	NUMLOCATIONS=$(expr $NUMLOCATIONS + 1)
done
NUMPROCS=0
while [ "$NUMPROCS" != "$MAXPROCS" ]
do
	NUMPROCS=$(expr $NUMPROCS + 1)
	ENCODELOCATION=$ENCODELOCATION:
	NUMLOCATIONS=$(expr $NUMLOCATIONS + 1)
done
for I in $(seq 1 $QUEUEDTRACKS)
do
	UTRACKNUM=$(echo $TRACKQUEUE | cut '-d ' -f$I)
	LOCATION=$(echo $ENCODELOCATIONS | cut -d: -f$(expr $(expr $I - 1) % $NUMLOCATIONS + 1))
	read FOO
	( # Set TRACKNUM, TRACKNAME
	TRACKNUM=$(printf %0.${TRACKNUMPADDING}d ${UTRACKNUM})
        CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
	TRACKNAME=$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -1 | cut -f2 -d= | tr -d \[:cntrl:\])
	if checkstatus ^encodetrack-$UTRACKNUM; then :; else do_encode $UTRACKNUM $LOCATION; fi
	if checkstatus ^tagtrack-$UTRACKNUM; then :; else do_tag $UTRACKNUM; fi
	if checkstatus ^movetrack-$UTRACKNUM; then :; else do_move $UTRACKNUM; fi
	)
done
)
echo Finished.
# Wipe all the evidence
rm -rf "$ABCDETEMPDIR"
exit 0