#!/bin/bash
#
#	TCDR
#	Version: 2.0
#	Author: Szilard Torok <kobold2@yahoo.com>
#	Thu Nov 14 00:18:11 CET 2002
#	Motto: "Some of this works for me."
#
#####

# TCDR Copyright (C) 2002 Szilard Torok <kobold2@yahoo.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# 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
#
# If you would like to negotiate alternate licensing terms, you may do
# so by contacting the author: Szilard Torok <kobold2@yahoo.com>


######### Software detection: ###################################
detect_sw(){
	if [ $(basename $0) != tcdr ]	;then echo "This script should be named tcdr <mv $(basename $0) tcdr>."		;exit 1 ;fi
	if type -p cdir		;then CDIR=$(type -p cdir)		;else echo "cdir not found!"		;exit 1 ;fi
	if type -p cdrdao	;then CDRDAO=$(type -p cdrdao)		;else echo "cdrdao not found!"		;exit 1 ;fi
	if type -p cdrecord	;then CDRECORD=$(type -p cdrecord)	;else echo "cdrecord not found!"	;exit 1 ;fi
	if type -p isoinfo	;then ISOINFO=$(type -p isoinfo)	;else echo "isoinfo not found!"		;exit 1 ;fi
	if type -p mkisofs	;then MKISOFS=$(type -p mkisofs)	;else echo "mkisofs not found!"		;exit 1 ;fi
	if type -p dialog	;then DIALOG="$(type -p dialog) --backtitle TCDR_Console_Frontend_v2.0 --aspect 75";else echo "dialog not found!" ;exit 1 ;fi
	if type -p mkzftree	;then MKZFTREE=$(type -p mkzftree)	;fi
	if type -p readcd	;then READCD=$(type -p readcd)		;fi

	#################################################################
	# To set a program for a given task, change the order of "if's",
	# that is, make it appear first in the appropriate
	# if..elif...else...fi construct.
	# Or just let tcdr set the variables as it finds the programs.
	######### Rippers: ##############################################
	NONE="None!"
	echo -n "cd  ripper: "
	if type -p cdparanoia ;then
		CDRIPPER="$(type -p cdparanoia)"
	elif type -p cdda2wav ;then
		CDRIPPER="$(type -p cdda2wav)"
	elif type -p dagrab ;then		# dagrab can't produce image
		CDRIPPER="$(type -p dagrab)"	# files, only separate wavs.
	else	CDRIPPER="$NONE"
	fi >/dev/null 2>&1
	echo "$CDRIPPER"
	######### MP3 Encoders: #########################################
	echo -n "mp3 encoder: "
	if type -p bladeenc ;then
		MP3ENCODER="$(type -p bladeenc)"
	elif type -p lame ;then
		MP3ENCODER="$(type -p lame)"
	elif type -p toolame ;then
		MP3ENCODER="$(type -p toolame)"
	else	MP3ENCODER="$NONE"
	fi >/dev/null 2>&1
	echo "$MP3ENCODER"
	######### MP3 Decoders: #########################################
	echo -n "mp3 decoder: "
	if type -p mpg123 ;then
		MP3DECODER="$(type -p mpg123)"
	elif type -p madplay ;then
		MP3DECODER="$(type -p madplay)"
	elif type -p mpg321 ;then
		MP3DECODER="$(type -p mpg321)"
	elif type -p maplay ;then
		MP3DECODER="$(type -p maplay)"
	elif type -p splay ;then
		MP3DECODER="$(type -p splay)"
	elif type -p amp ;then
		MP3DECODER="$(type -p amp)"
	elif type -p lame ;then
		MP3DECODER="$(type -p lame)"
	elif type -p mp3blaster ;then
		MP3DECODER="$(type -p mp3blaster)"
	else	MP3DECODER="$NONE"
	fi >/dev/null 2>&1
	echo "$MP3DECODER"
	######### MP3 Players: ##########################################
	echo -n "mp3 player: "
	if type -p mpg123 ;then
		MP3PLAYER="$(type -p mpg123)"
	elif type -p mpg321 ;then
		MP3PLAYER="$(type -p mpg321)"
	elif type -p madplay ;then
		MP3PLAYER="$(type -p madplay)"
	elif type -p maplay ;then
		MP3PLAYER="$(type -p maplay)"
	elif type -p splay ;then
		MP3PLAYER="$(type -p splay)"
	elif type -p lame ;then
		MP3PLAYER="$(type -p lame)"
	elif type -p amp ;then
		MP3PLAYER="$(type -p amp)"
	elif type -p freeamp ;then
		MP3PLAYER="$(type -p freeamp)"
	elif type -p mp3blaster ;then
		MP3PLAYER="$(type -p mp3blaster)"
	else	MP3PLAYER="$NONE"
	fi >/dev/null 2>&1
	echo "$MP3PLAYER"
	######### OGG Encoders: #########################################
	echo -n "ogg encoder: "
	if type -p oggenc ;then
		OGGENCODER="$(type -p oggenc)"
	else	OGGENCODER="$NONE"
	fi >/dev/null 2>&1
	echo "$OGGENCODER"
	######### OGG Decoders: #########################################
	echo -n "ogg decoder: "
	if type -p oggdec ;then
		OGGDECODER="$(type -p oggdec)"
	elif type -p ogg123 ;then
		OGGDECODER="$(type -p ogg123)"
	else	OGGDECODER="$NONE"
	fi >/dev/null 2>&1
	echo "$OGGDECODER"
	######### OGG Players: ##########################################
	echo -n "ogg player: "
	if type -p ogg123 ;then
		OGGPLAYER="$(type -p ogg123)"
	elif type -p oggdec ;then
		OGGPLAYER="$(type -p oggdec)"
	elif type -p mp3blaster ;then
		OGGPLAYER="$(type -p mp3blaster)"
	else	OGGPLAYER="$NONE"
	fi >/dev/null 2>&1
	echo "$OGGPLAYER"
	######### RAW Recorder: #########################################
	echo -n "raw recorder: "
	if type -p sox ;then
		RAWRECORDER="$(type -p sox)"
	elif type -p rawrec ;then
		RAWRECORDER="$(type -p rawrec)"
	elif type -p brec ;then
		RAWRECORDER="$(type -p brec)"
	else	RAWRECORDER="$NONE"
	fi >/dev/null 2>&1
	echo "$RAWRECORDER"
	######### RAW to WAV Converters: ################################
	echo -n "raw to wav converter: "
	if type -p sox ;then
		RAW2WAVCONVERTER="$(type -p sox)"
	elif type -p wav2cdr ;then
		RAW2WAVCONVERTER="$(type -p wav2cdr)"
	else	RAW2WAVCONVERTER="$NONE"
	fi >/dev/null 2>&1
	echo "$RAW2WAVCONVERTER"
	######### RAW Players: ##########################################
	echo -n "raw player: "
	if type -p sox ;then
		RAWPLAYER="$(type -p sox)"
	elif type -p rawplay ;then
		RAWPLAYER="$(type -p rawplay)"
	elif type -p tplay ;then
		RAWPLAYER="$(type -p tplay)"
	else	RAWPLAYER="$NONE"
	fi >/dev/null 2>&1
	echo "$RAWPLAYER"
	######### WAV Recorder: #########################################
	echo -n "wav recorder: "
	if type -p sox ;then
		WAVRECORDER="$(type -p sox)"
	elif type -p wavrec ;then
		WAVRECORDER="$(type -p wavrec)"
	elif type -p brec ;then
		WAVRECORDER="$(type -p brec)"
	elif type -p yarec ;then
		WAVRECORDER="$(type -p yarec)"
	elif type -p wavr ;then
		WAVRECORDER="$(type -p wavr)"
	else	WAVRECORDER="$NONE"
	fi >/dev/null 2>&1
	echo "$WAVRECORDER"
	######### WAV to RAW Converters: ################################
	echo -n "wav to raw converter: "
	if type -p sox ;then
		WAV2RAWCONVERTER="$(type -p sox)"
	elif type -p wav2cdr ;then
		WAV2RAWCONVERTER="$(type -p wav2cdr)"
	else	WAV2RAWCONVERTER="$NONE"
	fi >/dev/null 2>&1
	echo "$WAV2RAWCONVERTER"
	######### WAV Players: ##########################################
	echo -n "wav player: "
	if type -p sox ;then
		WAVPLAYER="$(type -p sox)"
	elif type -p splay ;then
		WAVPLAYER="$(type -p splay)"
	elif type -p wavp ;then
		WAVPLAYER="$(type -p wavp)"
	elif type -p wavplay ;then
		WAVPLAYER="$(type -p wavplay)"
	elif type -p rawplay ;then
		WAVPLAYER="$(type -p rawplay)"
	elif type -p tplay ;then
		WAVPLAYER="$(type -p tplay)"
	elif type -p bplay ;then
		WAVPLAYER="$(type -p bplay)"
	elif type -p yarec ;then
		WAVPLAYER="$(type -p yarec)"
	else	WAVPLAYER="$NONE"
	fi >/dev/null 2>&1
	echo "$WAVPLAYER"
	######### CD Players: ###########################################
	echo -n "cd  player: "
	if type -p workbone ;then
		CDPLAYER="$(type -p workbone) -d"
	elif type -p cdcd ;then
		CDPLAYER="$(type -p cdcd) -d"
	elif type -p cdp ;then
		CDPLAYER="$(type -p cdp) -c"
	elif type -p sadp ;then
		CDPLAYER="$(type -p sadp) -f 0 -O a -C"
	elif type -p gcd ;then
		CDPLAYER="$(type -p gcd)"
	elif type -p mcd ;then
		CDPLAYER="$(type -p mcd)"
	elif type -p groovycd ;then		# "/dev/cdrom" is hardwired into groovycd,
		CDPLAYER="$(type -p groovycd)"	# so this is just a placeholder for now.
	else	CDPLAYER="$NONE"
	fi >/dev/null 2>&1
	echo "$CDPLAYER"
	######### Mixers: ###############################################
	echo -n "mixer: "
	if type -p aumix ;then
		MIXER="$(type -p aumix)"
	elif type -p nmixer ;then
		MIXER="$(type -p nmixer)"
	elif type -p cam ;then
		MIXER="$(type -p cam)"
	elif type -p rexima ;then
		MIXER="$(type -p rexima)"
	elif type -p mp3blaster ;then
		MIXER="$(type -p mp3blaster)"
	else	MIXER="$NONE"
	fi >/dev/null 2>&1
	echo "$MIXER"

	SOX="$(type -p sox)"
}
######## Variables: ###########################################################
	declare -a CDROM

	declare -a CDRW

	declare -a MI

	declare -a PR
	PR[0]="/mnt/images/"
	PR[1]="img.iso"
	PR[2]="$(date +%Y-%m-%d_%H:%M:%S_Volume_ID)"
	PR[3]="you@mail.net"
	PR[4]="/mnt/images/sound/"
	PR[5]="/mnt/images/cdroot/"
	PR[6]="/dev/fd0"
	PR[7]="boot.img"
	PR[8]="boot/"
	PR[9]="boot.catalog"
	PR[10]="cdda.raw"
	PR[11]="cdda.wav"
	PR[12]="cdda.mp3"
	PR[13]="$(hostname)"
	PR[14]="driver=mmc_cdr driveropts=burnfree"
	PR[15]="$HOME/.xmms/xmms.m3u"

	declare -a PLA

	DD="/bin/dd"
	LSOF="/usr/sbin/lsof"
	MOUNT="/bin/mount"
	UMOUNT="/bin/umount"
	TRKNR=""
	NXTRK=""
	TTY="$(/usr/bin/tty|cut -f3 -d "/")"
	TOC=""
	TMPO="/tmp/tcdro.$$"
	TMP="/tmp/tcdr.$$"
	TCDRRC="/etc/tcdrrc"
	TCDRPATH="$(echo $0 | sed 's/tcdr$//')"
	NOOUTP="$1"
	
######### Startup functions: #################################################
startscr(){
	# pasted from CD-Writing-HOWTO <winni@xpilot.org>:
	echo -n "Checking modules..."
	[ $(whoami) = 'root' ] || echo "You must be root to execute the commands."
	$CDRECORD -scanbus > /dev/null 2>&1
	if ! (pidof kerneld || test -f "/proc/sys/kernel/modprobe"); then
		echo "Neither kerneld nor kmod are running to automatically load modules".
	fi
	report_no_autoload(){
		echo "Ensure the module $1 is loaded automatically next time."
	}
	if test ! -f "/proc/scsi/scsi"; then
		report_no_autoload scsi_mod && insmod scsi_mod
	fi
	if ! grep "^........ sg_" /proc/ksyms > /dev/null; then
		report_no_autoload sg && insmod sg
	fi
	if ! grep "^........ sr_" /proc/ksyms > /dev/null; then
		report_no_autoload sr_mod && insmod sr_mod
	fi
	if ! grep "^........ loop_" /proc/ksyms > /dev/null; then
		report_no_autoload loop && insmod loop
	fi
	if ! grep iso9660 /proc/filesystems > /dev/null; then
		report_no_autoload iso9660 && insmod iso9660
	fi
	# echo "The following is only needed for IDE/ATAPI CD-writers."
	if ! grep ide-scsi /proc/ide/drivers > /dev/null; then
		report_no_autoload ide-scsi && insmod ide-scsi
	fi
	$CDRECORD -scanbus > /dev/null 2>&1
}
detect_scsi(){
	SCSICDNR=$(awk '{print $5}' /proc/scsi/sg/devices|wc -l|tr -d " ")
	echo "Detecting devices...  $SCSICDNR SCSI CD-ROMs found."

	declare -a SR0
	declare -a SR1

	SR0[3]="$(awk 'BEGIN{RS="\\n"} {if ( NR == 1 ) print $2","$3","$4}' /proc/scsi/sg/devices)"
	SR0[1]="$(awk 'BEGIN{RS="\\n"} {if ( NR == 1 ) print $1" "$2" "$3}' /proc/scsi/sg/device_strs)"
	SR1[3]="$(awk 'BEGIN{RS="\\n"} {if ( NR == 2 ) print $2","$3","$4}' /proc/scsi/sg/devices)"
	SR1[1]="$(awk 'BEGIN{RS="\\n"} {if ( NR == 2 ) print $1" "$2" "$3}' /proc/scsi/sg/device_strs)"

	if [ $(awk '/Can write CD-R:/{print $4}' /proc/sys/dev/cdrom/info) -eq "1" ] ;then
		CDRW[0]="/dev/$(awk '/drive name:/{print $3}' /proc/sys/dev/cdrom/info)"
		CDRW[6]="$(awk '/drive speed:/{print $3}' /proc/sys/dev/cdrom/info)"
		if [ ${CDRW[0]} == "/dev/sr0" ] ;then
			CDRW[1]="${SR0[1]}"
			CDRW[3]="${SR0[3]}"
		elif [ ${CDRW[0]} == "/dev/sr1" ] ;then
			CDRW[1]="${SR1[1]}"
			CDRW[3]="${SR1[3]}"
		fi
	elif [ $(awk '/Can write CD-R:/{print $4}' /proc/sys/dev/cdrom/info) -eq "0" ] ;then
		CDROM[0]="/dev/$(awk '/drive name:/{print $3}' /proc/sys/dev/cdrom/info)"
		CDROM[6]="$(awk '/drive speed:/{print $3}' /proc/sys/dev/cdrom/info)"
		if [ ${CDROM[0]} == "/dev/sr0" ] ;then
			CDROM[1]="${SR0[1]}"
			CDROM[3]="${SR0[3]}"
		elif [ ${CDROM[0]} == "/dev/sr1" ] ;then
			CDROM[1]="${SR1[1]}"
			CDROM[3]="${SR1[3]}"
		fi
	fi

	if [ $(awk '/Can write CD-R:/{print $5}' /proc/sys/dev/cdrom/info) -eq "1" ] ;then
		CDRW[0]="/dev/$(awk '/drive name:/{print $4}' /proc/sys/dev/cdrom/info)"
		CDRW[6]="$(awk '/drive speed:/{print $4}' /proc/sys/dev/cdrom/info)"
		if [ ${CDRW[0]} == "/dev/sr0" ] ;then
			CDRW[1]="${SR0[1]}"
			CDRW[3]="${SR0[3]}"
		elif [ ${CDRW[0]} == "/dev/sr1" ] ;then
			CDRW[1]="${SR1[1]}"
			CDRW[3]="${SR1[3]}"
		fi
	elif [ $(awk '/Can write CD-R:/{print $5}' /proc/sys/dev/cdrom/info) -eq "0" ] ;then
		CDROM[0]="/dev/$(awk '/drive name:/{print $4}' /proc/sys/dev/cdrom/info)"
		CDROM[6]="$(awk '/drive speed:/{print $4}' /proc/sys/dev/cdrom/info)"
		if [ ${CDROM[0]} == "/dev/sr0" ] ;then
			CDROM[1]="${SR0[1]}"
			CDROM[3]="${SR0[3]}"
		elif [ ${CDROM[0]} == "/dev/sr1" ] ;then
			CDROM[1]="${SR1[1]}"
			CDROM[3]="${SR1[3]}"
		fi
	fi

	grep ${CDROM[0]} /etc/fstab && CDROM[2]=$(grep ${CDROM[0]} /etc/fstab |awk '{print $2}') || CDROM[2]=$(grep /dev/$(namei ${CDROM[0]}|tail -1|awk '{print $2}') /etc/fstab |awk '{print $2}')
	grep ${CDRW[0]} /etc/fstab && CDRW[2]=$(grep ${CDRW[0]} /etc/fstab |awk '{print $2}') || CDRW[2]=$(grep /dev/$(namei ${CDRW[0]}|tail -1|awk '{print $2}') /etc/fstab |awk '{print $2}')

	if [ $SCSICDNR -lt "2" ] ;then
		CDROM[0]="${CDRW[0]}"
		CDROM[1]="${CDRW[1]}"
		CDROM[2]="${CDRW[2]}"
		CDROM[3]="${CDRW[3]}"
	fi
	
	[ $SCSICDNR -lt "1" ] && detect_ide
}
detect_ide(){
	[ -f /proc/ide/ide0/hda/media ] && HDA=$(cat /proc/ide/ide0/hda/media)
	[ -f /proc/ide/ide0/hdb/media ] && HDB=$(cat /proc/ide/ide0/hdb/media)
	[ -f /proc/ide/ide1/hdc/media ] && HDC=$(cat /proc/ide/ide1/hdc/media)
	[ -f /proc/ide/ide1/hdd/media ] && HDD=$(cat /proc/ide/ide1/hdd/media)
	[ -f /proc/ide/ide2/hde/media ] && HDE=$(cat /proc/ide/ide2/hde/media)
	[ -f /proc/ide/ide2/hdf/media ] && HDF=$(cat /proc/ide/ide2/hdf/media)
	[ -f /proc/ide/ide3/hdg/media ] && HDG=$(cat /proc/ide/ide3/hdg/media)
	[ -f /proc/ide/ide3/hdh/media ] && HDH=$(cat /proc/ide/ide3/hdh/media)

	[ "$HDA" == "cdrom" ] && grep "CD-ROM" /proc/ide/ide0/hda/model >/dev/null && CDROM[0]="/dev/hda" && CDROM[1]=$(cat /proc/ide/ide0/hda/model)
	[ "$HDB" == "cdrom" ] && grep "CD-ROM" /proc/ide/ide0/hdb/model >/dev/null && CDROM[0]="/dev/hdb" && CDROM[1]=$(cat /proc/ide/ide0/hdb/model)
	[ "$HDC" == "cdrom" ] && grep "CD-ROM" /proc/ide/ide1/hdc/model >/dev/null && CDROM[0]="/dev/hdc" && CDROM[1]=$(cat /proc/ide/ide1/hdc/model)
	[ "$HDD" == "cdrom" ] && grep "CD-ROM" /proc/ide/ide1/hdd/model >/dev/null && CDROM[0]="/dev/hdd" && CDROM[1]=$(cat /proc/ide/ide1/hdd/model)
	[ "$HDE" == "cdrom" ] && grep "CD-ROM" /proc/ide/ide2/hde/model >/dev/null && CDROM[0]="/dev/hde" && CDROM[1]=$(cat /proc/ide/ide2/hde/model)
	[ "$HDF" == "cdrom" ] && grep "CD-ROM" /proc/ide/ide2/hdf/model >/dev/null && CDROM[0]="/dev/hdf" && CDROM[1]=$(cat /proc/ide/ide2/hdf/model)
	[ "$HDG" == "cdrom" ] && grep "CD-ROM" /proc/ide/ide3/hdg/model >/dev/null && CDROM[0]="/dev/hdg" && CDROM[1]=$(cat /proc/ide/ide3/hdg/model)
	[ "$HDH" == "cdrom" ] && grep "CD-ROM" /proc/ide/ide3/hdh/model >/dev/null && CDROM[0]="/dev/hdh" && CDROM[1]=$(cat /proc/ide/ide3/hdh/model)

	[ "$HDA" == "cdrom" ] && grep "CD-RW" /proc/ide/ide0/hda/model >/dev/null && CDRW[0]="/dev/hda" && CDRW[1]=$(cat /proc/ide/ide0/hda/model)
	[ "$HDB" == "cdrom" ] && grep "CD-RW" /proc/ide/ide0/hdb/model >/dev/null && CDRW[0]="/dev/hdb" && CDRW[1]=$(cat /proc/ide/ide0/hdb/model)
	[ "$HDC" == "cdrom" ] && grep "CD-RW" /proc/ide/ide1/hdc/model >/dev/null && CDRW[0]="/dev/hdc" && CDRW[1]=$(cat /proc/ide/ide1/hdc/model)
	[ "$HDD" == "cdrom" ] && grep "CD-RW" /proc/ide/ide1/hdd/model >/dev/null && CDRW[0]="/dev/hdd" && CDRW[1]=$(cat /proc/ide/ide1/hdd/model)
	[ "$HDE" == "cdrom" ] && grep "CD-RW" /proc/ide/ide2/hde/model >/dev/null && CDRW[0]="/dev/hde" && CDRW[1]=$(cat /proc/ide/ide2/hde/model)
	[ "$HDF" == "cdrom" ] && grep "CD-RW" /proc/ide/ide2/hdf/model >/dev/null && CDRW[0]="/dev/hdf" && CDRW[1]=$(cat /proc/ide/ide2/hdf/model)
	[ "$HDG" == "cdrom" ] && grep "CD-RW" /proc/ide/ide3/hdg/model >/dev/null && CDRW[0]="/dev/hdg" && CDRW[1]=$(cat /proc/ide/ide3/hdg/model)
	[ "$HDH" == "cdrom" ] && grep "CD-RW" /proc/ide/ide3/hdh/model >/dev/null && CDRW[0]="/dev/hdh" && CDRW[1]=$(cat /proc/ide/ide3/hdh/model)

	if [ -f /proc/sys/dev/cdrom/info ]
	then
		CD1=$(awk '/drive name:/{print $3}' /proc/sys/dev/cdrom/info)
		if [ $(awk '/Can write CD-R:/{print $4}' /proc/sys/dev/cdrom/info) -eq 1 ]
			then	CDRWL="/dev/$CD1"
			else	CDROM[0]="/dev/$CD1"
		fi
		if [ $(awk '/Can write CD-RW:/{print $4}' /proc/sys/dev/cdrom/info) -eq 1 ]
			then	CDRWL="/dev/$CD1"
			else	CDROM[0]="/dev/$CD1"
		fi

		CD1=$(awk '/drive name:/{print $4}' /proc/sys/dev/cdrom/info)
		if [ $(awk '/Can write CD-R:/{print $5}' /proc/sys/dev/cdrom/info) -eq 1 ]
			then	CDRWL="/dev/$CD1"
			else	CDROM[0]="/dev/$CD1"
		fi
		if [ $(awk '/Can write CD-RW:/{print $5}' /proc/sys/dev/cdrom/info) -eq 1 ]
			then	CDRWL="/dev/$CD1"
			else	CDROM[0]="/dev/$CD1"
		fi
	fi

	if [ -L $CDRWL ]
		then	CDRW[0]="/dev/$(/bin/ls -l "$CDRWL"|awk '{print $11}')" && CDRW[2]=$(grep "${CDRW[0]}" /etc/fstab|awk '{print $2}')
		else	CDRW[0]="$CDRWL" && CDRW[2]=$(grep "${CDRW[0]}" /etc/fstab|awk '{print $2}')
	fi

	if [ -L /dev/cdrom ]
		then	CDROM[2]=$(grep "/dev/cdrom" /etc/fstab|awk '{print $2}')
		else	CDROM[2]=$(grep "$CDROM" /etc/fstab|awk '{print $2}')
	fi

	CDRW[3]=$($CDRECORD -scanbus 2>/dev/null |grep $(echo "${CDRW[1]}" | awk '{print $3}')|awk '{print $1}')
	CDROM[3]=$($CDRECORD -scanbus 2>/dev/null |grep $(echo "${CDROM[1]}" | awk '{print $3}')|awk '{print $1}')
}
ckpath(){
	[ -d ${PR[0]} ] || mkdir --verbose ${PR[0]}
	[ -d ${PR[4]} ] || mkdir --verbose ${PR[4]}
	[ -d ${PR[5]} ] || mkdir --verbose ${PR[5]}
	rm -f /tmp/tcdr*
}
splash(){
	echo -n "Closing trays and unmounting: "
	if ! eject -t "${CDROM[0]}" ;then
		echo "${CDROM[0]} cannnot be opened"
		exit 1
	fi
	grep -q "${CDROM[2]}" /etc/mtab && $UMOUNT ${CDROM[0]}
	echo -n "${CDROM[0]}."

	if ! eject -t "${CDRW[0]}" ;then
		echo "${CDRW[0]} cannnot be opened"
		exit 1
	fi
	grep -q "${CDRW[2]}" /etc/mtab && $UMOUNT ${CDRW[0]}
	echo -n "${CDRW[0]}."

	[ "$NOOUTP" -eq "1" ] 2>/dev/null && return 0
	di "${CDRW[3]}"

	(cat <<EOF
	------------ CD-ROM: ------------
	vendor & model:        ${CDROM[1]}
	device file:           ${CDROM[0]}
	mount point:           ${CDROM[2]}
	SCSI BUS,TARGET,LUN:   ${CDROM[3]}
	drive reading speed:   ${CDROM[6]}

	------------ CD-RW: -------------
	vendor & model:        ${CDRW[1]}
	device file:           ${CDRW[0]}
	mount point:           ${CDRW[2]}
	SCSI BUS,TARGET,LUN:   ${CDRW[3]}
	drive reading speed:   ${CDRW[6]}
	medium writing speed:  ${MI[3]}

	--------- Medium Status: ---------
${CDROM[0]}: $($CDIR -d ${CDROM[0]})

${CDRW[0]}:  $($CDIR -d ${CDRW[0]})
EOF
	) >$TMPO
	$DIALOG --textbox $TMPO 23 65
	rm -f $TMPO
}
######### Medium checking functions: #########################################
di(){
	eject -t ${CDRW[0]}
	$CDRDAO disk-info --device "$1" --driver generic-mmc > $TMPO 2>&1
	MI[0]="$(awk 'BEGIN{FS=": "}/CD-RW                :/{print $2}' $TMPO)"
	MI[1]="$(awk 'BEGIN{FS=" " }/Total Capacity       :/{print $5}' $TMPO|tr -d "(")"
	MI[2]="$(awk 'BEGIN{FS=": "}/CD-R medium          :/{print $2}' $TMPO)"
	MI[3]="$(awk 'BEGIN{FS=" " }/Recording Speed      :/{print $6}' $TMPO|tr -d "X")"
	MI[4]="$(awk 'BEGIN{FS=": "}/CD-R empty           :/{print $2}' $TMPO)"
	MI[5]="$(awk 'BEGIN{FS=": "}/Toc Type             :/{print $2}' $TMPO)"
	MI[6]="$(awk 'BEGIN{FS=": "}/Sessions             :/{print $2}' $TMPO)"
	MI[7]="$(awk 'BEGIN{FS=": "}/Last Track           :/{print $2}' $TMPO)"
	MI[8]="$(awk 'BEGIN{FS=": "}/Appendable           :/{print $2}' $TMPO)"
	MI[9]="$(awk 'BEGIN{FS=" " }/Start of last session:/{print $5}' $TMPO)"
	MI[10]="$(awk 'BEGIN{FS=" "}/Start of new session :/{print $6}' $TMPO)"
	MI[11]="$(awk 'BEGIN{FS=" "}/Remaining Capacity   :/{print $5}' $TMPO|tr -d "(")"
	[ -f ${PR[0]}${PR[1]} ] && MI[12]="$($ISOINFO -d -i ${PR[0]}${PR[1]}|awk 'BEGIN{FS=": "}/Volume size is:/{print $2}')"
}
is_r(){
	[ ${MI[0]} == "no" ] 2>/dev/null
}
is_rw(){
	[ ${MI[0]} == "yes" ] 2>/dev/null
}
is_blank(){
	[ ${MI[4]} == "yes" ] 2>/dev/null
}
is_app(){
	[ "${MI[8]}" == "yes" ] 2>/dev/null
}
is_r_and_blank(){
	if is_r && is_blank
		then	return 0
		else	return 1
	fi
}
is_rw_and_blank(){
	if is_rw && is_blank
		then	return 0
		else	return 1
	fi
}
is_app_and_r(){
	if is_app && is_r
		then	return 0
		else	return 1
	fi
}
is_app_and_rw(){
	if is_app && is_rw
		then	return 0
		else	return 1
	fi
}
is_r_and_full(){
	if is_r && ! is_blank
		then	return 0
		else	return 1
	fi
}
is_rw_and_full(){
	if is_rw && ! is_blank
		then	return 0
		else	return 1
	fi
}
fit2blank(){
	[ -f ${PR[0]}${PR[1]} ] || return 0
	[ ${MI[1]} -gt ${MI[12]} ] 2>/dev/null && return 0 || return 1
}
fit2multi(){
	[ -f ${PR[0]}${PR[1]} ] || return 0
	[ ${MI[11]} -gt ${MI[12]} ] 2>/dev/null && return 0 || return 1
}
fit_if_blanked(){
	[ -f ${PR[0]}${PR[1]} ] || return 0
	if	is_rw
	then
		if [ -z "${MI[1]}" ] ;then
			MI[1]="332800"
		fi
		FREESPACE="$(( ${MI[1]} - ${MI[12]} ))"
		[ $FREESPACE -gt 0 ] 2>/dev/null && return 0 || return 1
	fi
}
fit_if_unclose(){
	[ -f ${PR[0]}${PR[1]} ] || return 0
	if [ "${MI[1]}" = "" ] ;then
		MI[1]="332800"
	fi
	CDS="$($ISOINFO -d -i ${CDRW[0]}|awk 'BEGIN{FS=": "}/Volume size is:/{print $2}')"
	IMGS="$($ISOINFO  -d -i ${PR[0]}${PR[1]}|awk 'BEGIN{FS=": "}/Volume size is:/{print $2}')"
	RESULT="$(( ${MI[1]} - $CDS - $IMGS ))"
	[ $RESULT -gt 0 ] && return 0 || return 1
}
is_audio(){
	( $CDIR -d "$1" |grep -q "nodisc" ) 2>/dev/null && return 1
	( $CDIR -d "$1" |grep -qvi "DATA" ) 2>/dev/null && return 0
}
is_data(){
	if $CDIR -d "$1" |grep -q "datadisc"
		then return 0
		else return 1
	fi 2>/dev/null
}
is_mix(){
	if $CDIR -d "$1" |grep -q "DATA"
		then	return 0
		else	return 1
	fi 2>/dev/null

}
is_gracetime(){
	$CDRECORD --help 2>$TMPO
	if grep -q "gracetime=" $TMPO ;then
		PR[14]="gracetime=3 driver=mmc_cdr driveropts=burnfree"
		return 0
	else
		PR[14]="driver=mmc_cdr driveropts=burnfree"
		return 1
	fi
}
is_overburn(){
	$CDRECORD --help 2>$TMPO
	if grep -q "\-overburn" $TMPO ;then
		PR[14]="-overburn driver=mmc_cdr driveropts=burnfree"
		return 0
	else
		$DIALOG --msgbox "$($CDRECORD --version) doesn't support overburning. Install cdrecord 1.11a01 or later." 7 70
		return 1
	fi
}
is_ziso(){
	if [ -z "$MKZFTREE" ] ;then
		$DIALOG --msgbox "mkzftree is required for this operation. Install zisofs-tools and retry (Linux only)." 7 70
		return 1
	else
		return 0
	fi
	$MKISOFS --help 2>$TMPO
	if grep -q "\-transparent-compression" $TMPO ;then
		return 0
	else
		$DIALOG --msgbox "$($MKISOFS --version) doesn't support transparent compression. Install cdrtools 1.11a20 or later." 7 70
		return 1
	fi
}
ckspace(){
	di "${CDRW[3]}"
	case "$1" in
		a)	if is_rw ;then
				while ! is_blank ; do
					$DIALOG --beep --menu "Medium about to be burned is not empty." 9 55 2\
						"B "	" Blank the whole disk    "\
						"I "	" Insert a blank medium   "\
					2>$TMP || return 1
					case $(cat $TMP) in
						"B ")	blankfast || return 1
						;;
						"I ")	ejectr || return 1
						;;
					esac
					di "${CDRW[3]}"
				done
				return 0
			elif is_r ;then
				while ! is_blank ; do
					di "${CDRW[3]}"
					$DIALOG --beep --yesno "Medium about to be burned is a used CD-R.  Insert a blank one?" 7 70 && ejectr || return 1
				done
				return 0
			fi
			while ! is_audio ${CDROM[0]}
			do
				ejecta || return 1
			done
			return 0
		;;
		d)	if is_rw ;then
				while ! is_blank ; do
					$DIALOG --beep --menu "Medium about to be burned is not empty." 9 55 2\
						"B "	" Blank the whole disk    "\
						"I "	" Insert a blank medium   "\
					2>$TMP || return 1
					case $(cat $TMP) in
						"B ")	blankfast || return 1
						;;
						"I ")	ejectr || return 1
						;;
					esac
					di "${CDRW[3]}"
				done
				return 0
			elif is_r ;then
				while ! is_blank ; do
					di "${CDRW[3]}"
					$DIALOG --beep --yesno "Medium about to be burned is a used CD-R.  Insert a blank one?" 7 70 && ejectr || return 1
				done
				return 0
			fi
			while ! is_data ${CDROM[0]}
			do
				ejectm || return 1
			done
			return 0
		;;
		m)	if is_rw ;then
				while ! is_blank ; do
					$DIALOG --beep --menu "Medium about to be burned is not empty." 9 55 2\
						"B "	" Blank the whole disk    "\
						"I "	" Insert a blank medium   "\
					2>$TMP || return 1
					case $(cat $TMP) in
						"B ")	blankfast || return 1
						;;
						"I ")	ejectr || return 1
						;;
					esac
					di "${CDRW[3]}"
				done
				return 0
			elif is_r ;then
				while ! is_blank ; do
					di "${CDRW[3]}"
					$DIALOG --beep --yesno "Medium about to be burned is a used CD-R.  Insert a blank one?" 7 70 && ejectr || return 1
				done
				return 0
			fi
		;;
		*)	:
		;;
	esac
	noisofm || return 1
	if is_r ;then
		if is_blank ;then
			if fit2blank ;then
				echo 0 >$TMP
			else
				echo 1 >$TMP
			fi
		else
			if is_app ;then
				if fit2multi ;then
					echo 0 >$TMP
				else
					echo 2 >$TMP
				fi
			else
				echo 3 >$TMP
			fi
		fi
	elif is_rw ;then
		if is_blank ;then
			if fit2blank ;then
				echo 0 >$TMP
			else
				echo 4 >$TMP
			fi
		else
			if is_app ;then
				if fit2multi ;then
					echo 0 >$TMP
				else
					echo 5 >$TMP
				fi
			else
				if fit_if_unclose ;then
					echo 6 >$TMP
				elif fit_if_blanked ;then
					echo 7 >$TMP
				else
					echo 8 >$TMP
				fi
			fi
		fi
	fi
	case $(cat $TMP) in

		"0")	return 0
			;;
		"1")	$DIALOG --menu \
				"The medium is a blank CD-R, but ${PR[0]}${PR[1]} is\
				too large.  You could insert a larger capacity medium\
				or attempt to overburn the medium.  Alternatively you\
				could consider creating a new, compressed ZISO image\
				(mkzftree required - Linux only)." 15 65 4 \
				"I "	"  Insert another medium           "\
				"O "	"  Overburn the medium             "\
				"Z "	"  Generate and write a ZISO image "\
				"W "	"  CD-RW medium Info <${CDRW[0]}>          "\
 			2>$TMP || return 1
			case $(cat $TMP) in
				"I ")	ejectr ${CDRW[0]} || return 1
					ckspace
					;;
				"O ")	overburn || return 1
					dirsub || return 1
					;;
				"Z ")	mkziso && cdrec || return 1
					;;
				"W ")	diskinfo ${CDRW[0]} ${CDRW[3]} ${CDRW[2]}
					ckspace
					;;
			esac
			;;
		"2")	$DIALOG --menu \
				"The medium is an appendable CD-R, but ${PR[0]}${PR[1]} is\
				too large.  You could attempt overburing the medium or create\
				a new, compressed ZISO image in multi mode (mkzftree required)\
				or insert a new *appendable* medium." 15 65 4 \
				"I "	" Insert another appendable medium       "\
				"O "	" Overburn the medium (~= 12 extra megs) "\
				"Z "	" Generate and write a ZISO image        "\
				"W "	" CD-RW medium Info <${CDRW[0]}>  "\
			2>$TMP || return 1
			case $(cat $TMP) in
				"I ")	ejectr ${CDRW[0]} || return 1
					ckspace
					;;
				"O ")	overburn || return 1
					dirsub || return 1
					;;
				"Z ")	msmkziso && cdrec -multi || return 1
					;;
				"W ")	diskinfo ${CDRW[0]} ${CDRW[3]} ${CDRW[2]}
					ckspace
					;;
			esac
			;;
		"3")	$DIALOG --yesno	"The medium is a written-and-closed used CD-R (read-only). Insert a blank one?" 7 70 && ejectr || return 1
			;;
		"4")	$DIALOG --menu \
				"The medium is a blank CD-RW, but ${PR[0]}${PR[1]} is\
				too large.  You could insert a larger capacity medium\
				or attempt to overburn the medium.  Alternatively you\
				could consider creating a new, compressed ZISO image.\
				mkzftree is required for that (Linux only)." 15 65 4 \
				"I "	"  Insert another medium                  "\
				"O "	"  Overburn the medium (~= 12 extra megs) "\
				"Z "	"  Generate and write a ZISO image        "\
				"W "	"  CD-RW medium Info <${CDRW[0]}>  "\
 			2>$TMP || return 1
			case $(cat $TMP) in
				"I ")	ejectr ${CDRW[0]} || return 1
					ckspace
					;;
				"O ")	overburn || return 1
					dirsub || return 1
					;;
				"Z ")	mkziso && cdrec || return 1
					;;
				"W ")	diskinfo ${CDRW[0]} ${CDRW[3]} ${CDRW[2]}
					ckspace
					;;
			esac
			;;
		"5")	$DIALOG --menu \
				"The medium is an appendable CD-RW, but ${PR[0]}${PR[1]}\
				is too large to fit.  You could attempt to overburn it or\
				to free up some space by deleting the last track or session.\
				Alternatively you could insert another *appendable* medium\
				or choose to blank the whole medium.  Finally you could write\
				a ZISO image in multi-session mode." 19 65 7 \
			"T "	" Track                                   "\
			"S "	" Blank last session                      "\
			"B "	" Blank the whole disk                    "\
			"I "	" Insert another appendable medium        "\
			"O "	" Overburn the medium (~= 12 extra megs)  "\
			"Z "	" Generate and write a ZISO image (multi) "\
			"W "	" CD-RW medium Info <${CDRW[0]}>            "\
			2>$TMP || return 1
			case $(cat $TMP) in
				"T ")	blanktrack || return 1
					;;
				"S ")	blanklastsession || return 1
					;;
				"B ")	blankfast || return 1
					;;
				"I ")	ejectr ${CDRW[0]} || return 1
					ckspace || return 1
					;;
				"O ")	overburn || return 1
					dirsub
					;;
				"Z ")	msmkziso && cdrec -multi || return 1
					dirsub
					;;
				"W ")	diskinfo ${CDRW[0]} ${CDRW[3]} ${CDRW[2]}
					ckspace
					;;
			esac
			;;
		"6")	$DIALOG --menu \
				"The medium is a CLOSED CD-RW, but ${PR[0]}${PR[1]} would\
				fit after blanking or unclosing.\
				NOTE: unclosing never worked for me, but if you want to\
				try it, this is what will happen: the last session will\
				be (hopefully) unclosed and 'Multi-session' menu will\
				be started.\n\
				You should then choose:	Session > Yes > Rewrite it.\n\n\
				I recommend to blank or to use a blank/appendable medium." 20 65 5\
				"B "	"  Blank fast                    "\
				"U "	"  Unclose                       "\
				"I "	"  Insert another medium         "\
				"W "	"  CD-RW medium Info <${CDRW[0]}> "\
			2>$TMP || return 1
			case $(cat $TMP) in
				"B ")	blankfast || return 1
					;;
				"U ")	blankunclose || return 1
					msd2cd
					;;
				"I ")	ejectr ${CDRW[0]} || return 1
					ckspace
					;;
				"W ")	diskinfo ${CDRW[0]} ${CDRW[3]} ${CDRW[2]}
					ckspace
					;;
			esac
			;;
		"7")	$DIALOG --yesno	"The medium is a CLOSED CD-RW, but ${PR[0]}${PR[1]} would fit if blanked.\n\
					Do it?" 8 45 && blankfast || return 1
			;;
		"8")	$DIALOG --menu "The medium is a WRITTEN-AND-CLOSED CD-RW and ${PR[0]}${PR[1]} would be too large even after blanking." 12 65 4\
			"I "	"  Insert a larger capacity medium       "\
			"O "	"  Blank and overburn (~= 12 extra megs) "\
			"Z "	"  Blank and write A ZISO image          "\
			"W "	"  CD-RW medium Info <${CDRW[0]}> "\
			2>$TMP || return 1
			case $(cat $TMP) in
				"I ")	ejectr ${CDRW[0]} || return 1
					ckspace
					;;
				"O ")	blankfast || return 1
					overburn || return 1
					;;
				"Z ")	blankfast || return 1
					mkziso || return 1
					cdrec || return 1
					return 1
					;;
				"W ")	diskinfo ${CDRW[0]} ${CDRW[3]} ${CDRW[2]}
					ckspace
					;;
			esac
			;;
	esac
}
######### Core functions: ####################################################
mkiso(){
	cdrootemptym || return 1
	echo "$MKISOFS -JlLrV ${PR[2]} -P ${PR[3]} -p ${PR[3]} -sysid ${PR[13]} -o ${PR[0]}${PR[1]} ${PR[5]}" > $TMPO 2>&1
	($MKISOFS -JlLrV ${PR[2]} -P ${PR[3]} -p ${PR[3]} -sysid ${PR[13]} -o ${PR[0]}${PR[1]} ${PR[5]} >> $TMPO 2>&1 && echo -n ) &
	tailbox $MKISOFS
}
msmkiso(){
	cdrootemptym || return 1
	[ "$1" = "c" ] && ckspace
	NXTRK=$($CDRECORD -msinfo dev=${CDRW[3]})
	echo "$MKISOFS -rJlL -o ${PR[0]}${PR[1]} -C $NXTRK -M ${CDRW[0]} ${PR[5]}" > $TMPO 2>&1
	($MKISOFS -rJlL -o ${PR[0]}${PR[1]} -C $NXTRK -M ${CDRW[0]} ${PR[5]} >> $TMPO 2>&1 && echo -n ) &
	tailbox $MKISOFS
}
mkziso(){
	is_ziso || return 1
	cdrootemptym || return 1
	$MKISOFS --help 2>$TMPO
	cd ${PR[0]}
	$DIALOG --infobox "\n          Running mkzftree..." 5 45
	$MKZFTREE ${PR[5]} ${PR[0]}Z
	echo "$MKISOFS -rJlLz -V ${PR[2]} -P ${PR[3]} -p ${PR[3]} -sysid ${PR[13]} -o ${PR[0]}${PR[1]} ${PR[0]}Z" > $TMPO 2>&1
	($MKISOFS -rJlLz -V ${PR[2]} -P ${PR[3]} -p ${PR[3]} -sysid ${PR[13]} -o ${PR[0]}${PR[1]} ${PR[0]}Z >> $TMPO 2>&1 && echo -n ) &
	tailbox $MKISOFS
	$DIALOG --infobox "\n             Removing compressed tree..." 5 55
	rm -rf ${PR[0]}Z
}
msmkziso(){
	is_ziso || return 1
	cdrootemptym || return 1
	$MKISOFS --help 2>$TMPO
	[ "$1" = "c" ] && ckspace
	NXTRK=$($CDRECORD -msinfo dev=${CDRW[3]})
	$DIALOG --infobox "\n          Running mkzftree..." 5 45
	$MKZFTREE ${PR[5]} ${PR[0]}Z
	echo "$MKISOFS -rJlLz -o ${PR[0]}${PR[1]} -C $NXTRK -M ${CDRW[0]} ${PR[0]}Z" > $TMPO 2>&1
	($MKISOFS -rJlLz -o ${PR[0]}${PR[1]} -C $NXTRK -M ${CDRW[0]} ${PR[0]}Z >> $TMPO 2>&1 && echo -n ) &
	tailbox $MKISOFS
}
cdrec(){
	is_gracetime
	ckspace cdrec && \
	if $DIALOG  --yesno "       Test (dummy) mode run?" 6 40 ;then
		echo "$CDRECORD -dummy dev=${CDRW[3]} ${PR[14]} -eject $1 ${PR[0]}${PR[1]}" > $TMPO 2>&1
		($CDRECORD -dummy dev=${CDRW[3]} ${PR[14]} -eject $1 ${PR[0]}${PR[1]} >> $TMPO 2>&1 && echo -n ) &
		tailbox $CDRECORD
	else
		echo "$CDRECORD dev=${CDRW[3]} ${PR[14]} -eject $1 ${PR[0]}${PR[1]}" > $TMPO 2>&1
		($CDRECORD dev=${CDRW[3]} ${PR[14]} -eject $1 ${PR[0]}${PR[1]} >> $TMPO 2>&1 && echo -n ) &
		tailbox $CDRECORD
	fi
}
overburn(){
	is_overburn || return 1
	is_gracetime
	ckspace cdrec && \
	if $DIALOG  --yesno "       Test (dummy) mode run?\n     This is highly recommended." 6 40;then
		echo "$CDRECORD -dummy -dao -v dev=${CDRW[3]} ${PR[14]} -eject $1 ${PR[0]}${PR[1]}" > $TMPO 2>&1
		($CDRECORD -dummy -dao -v dev=${CDRW[3]} ${PR[14]} -eject $1 ${PR[0]}${PR[1]} >> $TMPO 2>&1 && echo -n ) &
		tailbox $CDRECORD
	else
		echo "$CDRECORD -dao -v dev=${CDRW[3]} ${PR[14]} -eject $1 ${PR[0]}${PR[1]}" > $TMPO 2>&1
		($CDRECORD -dao -v dev=${CDRW[3]} ${PR[14]} -eject $1 ${PR[0]}${PR[1]} >> $TMPO 2>&1 && echo -n ) &
		tailbox $CDRECORD
	fi
}
rtocdao(){
	$DIALOG --infobox " Extracting TOC from ${CDROM[0]} to ${PR[4]}${PR[10]}.toc.\n                    Please wait..." 6 65
	$CDRDAO read-toc --device ${CDROM[3]} --driver generic-mmc --datafile ${PR[4]}$1 $TMP.toc > $TMPO 2>&1
	$CDRDAO show-toc $TMP.toc > $TMPO 2>&1 && outp TOC
	cp $TMP.toc ${PR[4]}$1.toc 2>/dev/null
}
dao(){
	ckspace "$3" || return 1
	if $DIALOG --yesno "       Test (dummy) mode run?" 6 40 ;then
		waitm
		echo "$CDRDAO simulate --force --device ${CDRW[3]} --driver generic-mmc --speed ${MI[3]} --buffers 240 $1 --eject -n -v 2 $2" > $TMPO
		$CDRDAO simulate --force --device ${CDRW[3]} --driver generic-mmc --speed ${MI[3]} --buffers 240 $1 --eject -n -v 2 $2 >> $TMPO 2>&1 && outp DAO_Simulation
	else
		waitm
		echo "$CDRDAO write --force --device ${CDRW[3]} --driver generic-mmc --speed ${MI[3]} --buffers 240 $1 --eject -n -v 2 $2" > $TMPO
		$CDRDAO write --force --device ${CDRW[3]} --driver generic-mmc --speed ${MI[3]} --buffers 240 $1 --eject -n -v 2 $2 >> $TMPO 2>&1 && outp DAO_Write
	fi
}
daocopy(){
	if [ ${CDROM[3]} = ${CDRW[3]} ] ;then
		$DIALOG  --msgbox "   There needs to be two devices present for CD to CD copy:\n          a source device and a destination device.\n\nCreate an ISO image first and then insert a blank medium in\n      the burner drive and write the ISO image on it." 11 65
		return 1
	fi
	ckspace "$3" || return 1
	if $DIALOG --yesno "       Test (dummy) mode run?" 6 40 ;then
		waitm
		echo "$CDRDAO copy --simulate --force --source-device ${CDROM[3]} --source-driver generic-mmc --device ${CDRW[3]} --driver generic-mmc --speed ${MI[3]} --buffers 240 --eject -n -v 2 $1" > $TMPO
		$CDRDAO copy --simulate --force --source-device ${CDROM[3]} --source-driver generic-mmc --device ${CDRW[3]} --driver generic-mmc --speed ${MI[3]} --buffers 240 --eject -n -v 2 $1 >> $TMPO 2>&1 && outp DAO_Simulation
	else
		waitm
		echo "$CDRDAO copy --force --source-device ${CDROM[3]} --source-driver generic-mmc --device ${CDRW[3]} --driver generic-mmc --speed ${MI[3]} --buffers 240 --eject -n -v 2 $1" > $TMPO
		$CDRDAO copy --force --source-device ${CDROM[3]} --source-driver generic-mmc --device ${CDRW[3]} --driver generic-mmc --speed ${MI[3]} --buffers 240 --eject -n -v 2 $1 >> $TMPO 2>&1 && outp DAO_Copy
	fi
}
######### Directory menu: ####################################################
dir2i(){
	if [ -f ${PR[0]}${PR[1]} ] ;then
		$DIALOG --menu\
			"          ${PR[0]}${PR[1]} exists." 9 55 2\
			"U " " Use it       "\
			"O " " Overwrite it "\
			2>$TMP || return 1
		case $(cat $TMP) in
			"U " )	return 0
				;;
			"O " )	inpvid || return 1
				mkiso
				;;
		esac
	else
		inpvid || return 1
		mkiso
	fi
}
dir2i2cd(){
	dir2i || return 1
	cdrec
}
######### Data CD menu: ######################################################
cd2i_dd(){
	if [ -f ${PR[0]}${PR[1]} ] ;then
		$DIALOG --menu\
			"          ${PR[0]}${PR[1]} exists." 9 55 2\
			"U " " Use it       "\
			"O " " Overwrite it "\
			2>$TMP || return 1
		case $(cat $TMP) in
			"U " )	return 0
				;;
			"O " )	($DD if=${CDROM[0]} of=${PR[0]}${PR[1]} > $TMPO 2>&1 && echo -n ) &
				tailbox $DD
				;;
		esac
	else
		($DD if=${CDROM[0]} of=${PR[0]}${PR[1]} > $TMPO 2>&1 && echo -n ) &
		tailbox $DD
	fi
}
cd2i_rcd(){
	if [ -f ${PR[0]}${PR[1]} ] ;then
		$DIALOG --menu\
			"          ${PR[0]}${PR[1]} exists." 9 55 2\
			"U " "Use it"\
			"O " "Overwrite it"\
			2>$TMP || return 1
		case $(cat $TMP) in
			"U " )	return 0
				;;
			"O " )	$DIALOG --infobox "\nCloning ${CDRW[0]} to ${PR[0]}${PR[1]}..." 5 60
				$READCD -clone dev=${CDROM[3]} f=${PR[0]}${PR[1]} > $TMPO 2>&1 && outp readcd
				;;
		esac
	else
		($READCD dev=${CDROM[3]} f=${PR[0]}${PR[1]} > $TMPO 2>&1 && echo -n ) &
		tailbox $READCD
	fi
}
i2dcd(){
	cdrec
}
dcd2cd(){
	daocopy " " " " d
}
dcd2cdr(){
	daocopy --on-the-fly " " d
}
######### Mixed mode CD menu: ################################################
mixcd2cd(){
	[ "$NOOUTP" -eq "1" ] 2>/dev/null || \
	$DIALOG --yesno \
		" CD Extra is a multi session CD with audio *and* data tracks.  The \
		place for audio tracks is in ${PR[4]} in .raw or .wav format and \
		the contents of the data track should be in ${PR[5]}.  Writing \
		takes two sessions; audio goes first (don't take out the medium from the \
		tray after the first eject).\nThe medium used must be blank, or appendable. \
		The medium will remain appendable for later sessions.\nTo mount the \
		data track use <mount ... -tiso9660 -osession=2>." 14 70 || return 1
	cd ${PR[4]}
	eject -t ${CDRW[0]}
	mktoc *.wav || mktoc *.raw || return 1
	dao --multi $TMP.toc "m"
	eject -t ${CDRW[0]}
	NXTRK=$($CDRECORD -msinfo dev=${CDRW[3]})
	if [ $1 = "z" ] ;then
		is_ziso || return 1
		cd ${PR[0]}
		$DIALOG --infobox "\n          Running mkzftree..." 5 45
		$MKZFTREE ${PR[5]} ${PR[0]}Z
		echo "$MKISOFS -rJlLv -V ${PR[2]} -P ${PR[3]} -p ${PR[3]} -sysid ${PR[13]} -o ${PR[0]}${PR[1]} -C $NXTRK ${PR[0]}Z" 2>$TMPO
		($MKISOFS -rJlLz -V ${PR[2]} -P ${PR[3]} -p ${PR[3]} -sysid ${PR[13]} -o ${PR[0]}${PR[1]} -C $NXTRK ${PR[0]}Z 2>>$TMPO && echo -n ) &
		tailbox $MKISOFS
	else
		echo "$MKISOFS -JlLrV ${PR[2]} -P ${PR[3]} -p ${PR[3]} -sysid ${PR[13]} -o ${PR[0]}${PR[1]} -C $NXTRK ${PR[5]}" 2>$TMPO
		($MKISOFS -JlLrV ${PR[2]} -P ${PR[3]} -p ${PR[3]} -sysid ${PR[13]} -o ${PR[0]}${PR[1]} -C $NXTRK ${PR[5]} 2>>$TMPO && echo -n ) &
		tailbox $MKISOFS
	fi
	cdrec -multi || return 1
}
######### Multi session CD menu: #############################################
msd2cd(){
	[ "$NOOUTP" -eq "1" ] 2>/dev/null || \
	$DIALOG --yesno \
		"Multi session CDs contain data tracks only.  The tracks are written \
		one at a time (one at a session), but all the data is accessible \
		after a normal mount.\n\
		To burn a session, there must be either some files in \
		${PR[5]} or an existing ISO image at ${PR[0]}${PR[1]}. \
		An existing ISO image may be used for the first session *only*.\n\
		The medium must be blank, or appendable.  If you are appending a new \
		session to an appendable mixed mode CD (CD Extra), the mounting \
		command has the form of <mount ... -tiso9660 -osession=2>.\n\
		The medium will remain appendable for later sessions." 15 73 || return 1

	if [ -f ${PR[0]}${PR[1]} ] ;then
		TRKNR=$(( $( $CDIR -d ${CDRW[0]}|awk '{print $2}' |tail -1)+1 ))
		if [ "$TRKNR" -eq "1" ] ;then
			$DIALOG --radiolist "   Existing ISO image ${PR[0]}${PR[1]} found." 9 55 2\
				1 " Use it       " off\
				2 " Overwrite it " on 2>$TMP
			case $(cat $TMP) in
				"1" )	cdrec -multi || return 1;;
				"2" )	mkiso c || return 1
					cdrec -multi || return 1;;
			esac
		elif [ "$TRKNR" -gt "1" ] ;then
			$DIALOG --radiolist "   Existing ISO image ${PR[0]}${PR[1]} found." 9 55 2\
				1 " Use it       " off\
				2 " Overwrite it " on 2>$TMP
			case $(cat $TMP) in
				"1" )	cdrec -multi || return 1;;
				"2" )	msmkiso || return 1
					cdrec -multi || return 1;;
			esac
		fi
	else
		TRKNR=$(( $( $CDIR -d ${CDRW[0]}|awk '{print $2}' |tail -1)+1 ))
		if [ "$TRKNR" -eq "1" ] ;then
			mkiso c || return 1
			cdrec -multi || return 1
		elif [ "$TRKNR" -gt "1" ] ;then
			msmkiso c || return 1
			cdrec -multi || return 1
		fi
	fi
}
msdclose(){
	msmkiso c || return 1
	cdrec || return 1
}
msdcdinfo(){
	($CDRECORD -vv -toc dev=${CDRW[3]} >$TMPO && echo -n ) &
	tailbox $CDRECORD
}
######### Boot CD menu: ######################################################
bootcd(){
	[ "$NOOUTP" -eq "1" ] 2>/dev/null || \
	$DIALOG --yesno \
		"Making a bootable CD takes two runs of mkisofs.\n First, a\
		regular ISO image is created from ${PR[5]}. After that, an\
		image file of a bootable floppy disk is inserted.  That is\
		what will be burned onto the CD.\n It is important to set up\
		a generic CD-ROM driver on the floppy.\nIf you don't have a\
		floppy-image, but do have a bootable floppy, choose the second\
		menupoint first, or download an image from the Internet and\
		place it in ${PR[5]}${PR[8]}${PR[7]}.\nI tested this with a DOS\
		boot image only." 15 70 || return 1
	dir2i || return 1
	$DIALOG --inputbox "   Enter boot image file name relative to ${PR[5]}${PR[8]}:" 8 75 "${PR[7]}" 2>$TMPO || return 1
	PR[7]=$(cat $TMPO)
	echo "$MKISOFS -JlLrV ${PR[2]} -P ${PR[3]} -p ${PR[3]} -sysid ${PR[13]} -b ${PR[8]}${PR[7]} -c ${PR[8]}${PR[9]} -o ${PR[0]}${PR[1]} ${PR[5]}" > $TMPO 2>&1
	($MKISOFS -JlLrV ${PR[2]} -P ${PR[3]} -p ${PR[3]} -sysid ${PR[13]} -b ${PR[8]}${PR[7]} -c ${PR[8]}${PR[9]} -o ${PR[0]}${PR[1]} ${PR[5]} >> $TMPO 2>&1 && echo -n ) &
	tailbox $MKISOFS
	cdrec
}
fd2bootimg(){
	[ -d ${PR[5]}${PR[8]} ] || mkdir ${PR[5]}${PR[8]}
	$DIALOG --yesno "         Insert a bootable 1.44 floppy disk (with a\n           generic CD-ROM driver) into <${PR[6]}>." 6 65 || return 1
	($DD if=${PR[6]} of=${PR[5]}${PR[8]}${PR[7]} bs=10k count=144 > $TMPO 2>&1 && echo -n ) &
	tailbox $DD
}
######### Audio CD to Audio CD Copy: #########################################
acd2acd_t(){
	is_audio ${CDROM[0]} || ejecta || return 1
	daocopy " " " " "a"
}
acd2acd(){
	is_audio ${CDROM[0]} || ejecta || return 1
	daocopy --on-the-fly " " "a"
}
acd2acd_cdparanoia(){
	noswm ripper $CDRIPPER Ripping || return 1
	$CDRIPPER -q -R -d ${CDROM[0]} 1-
	rtocdao ${PR[10]}
	dao " " ${PR[10]}.toc "a"
}
acd2acd_cdda2wav(){
	echo "$CDRIPPER -I cooked_ioctl -s -x -b 16 -r 44100 -O raw -C guess -E big -D ${CDROM[0]} -t 1+$NUMTRACK ${PR[4]}${PR[10]}" > $TMPO 2>&1
	$CDRIPPER -I cooked_ioctl -s -x -b 16 -r 44100 -O raw -C guess -E big -D ${CDROM[0]} -t 1+$NUMTRACK ${PR[4]}${PR[10]} >> $TMPO 2>&1 && outp AudioCD_2_AudioCD
	rtocdao ${PR[10]}
	dao " " ${PR[10]}.toc "a"
}
######### Audio CD to RAW Image: #############################################
acd2raw(){
	rm -f $TMPO
	cd ${PR[4]}
	is_audio ${CDROM[0]} || ejecta || return 1
	infocd || return 1
	noswm ripper $CDRIPPER Ripping || return 1
	(case $(basename $CDRIPPER) in
		cdda2wav)	$CDRIPPER -I cooked_ioctl -s -x -b 16 -r 44100 -O raw -C guess -E big -D ${CDROM[0]} -t 1+$NUMTRACK ${PR[4]}${PR[10]}
		;;
		cdparanoia)	$CDRIPPER -R -d ${CDROM[0]} 1-
		;;
		*)	nim
		;;
	esac) >> $TMPO 2>&1 && echo -n  && outp AudioCD_2_RAW
	rtocdao ${PR[10]}
}
######### Audio CD to WAV Image: #############################################
acd2wav(){
	rm -f $TMPO
	cd ${PR[4]}
	is_audio ${CDROM[0]} || ejecta || return 1
	infocd || return 1
	noswm ripper $CDRIPPER Ripping || return 1
	(case $(basename $CDRIPPER) in
		cdda2wav)	$CDRIPPER -I cooked_ioctl -s -x -b 16 -r 44100 -O wav -D ${CDROM[0]} -t 1+$NUMTRACK ${PR[4]}${PR[11]}
		;;
		cdparanoia)	$CDRIPPER -w -d ${CDROM[0]} 1-
		;;
		*)	nim
		;;
	esac) >> $TMPO 2>&1 && echo -n  && outp AudioCD_2_WAV
	rtocdao ${PR[11]}
}
######### Audio CD to WAVs: ##################################################
acd2wavs(){
	rm -f $TMPO
	cd ${PR[4]}
	is_audio ${CDROM[0]} || ejecta || return 1
	infocd || return 1
	noswm ripper $CDRIPPER Ripping || return 1
	(case $(basename $CDRIPPER) in
		cdda2wav)	$CDRIPPER -B -I cooked_ioctl -s -x -b 16 -r 44100 -O wav -D ${CDROM[0]} -H track
		;;
		cdparanoia)	$CDRIPPER -B -w -d ${CDROM[0]} 1- wav
		;;
		dagrab)		$CDRIPPER -d ${CDROM[0]} -a
		;;
		*)	nim
		;;
	esac) >> $TMPO 2>&1 && echo -n  && outp AudioCD_2_WAVs
}
######### Audio CD to MP3 Files: #############################################
acd2mp3(){
	rm -f $TMPO
	cd ${PR[4]}
	is_audio ${CDROM[0]} || ejecta || return 1
	infocd || return 1
	acd2wavs || return 1
	noswm encoder $MP3ENCODER Encoding || return 1
	(case $(basename $MP3ENCODER) in
		bladeenc) $MP3ENCODER -192 -progress=5 track*.wav
		;;
		lame)	for i in track*.wav ;do
				$MP3ENCODER --preset cd $i $i.mp3
			done
		;;
		toolame) for i in track*.wav ;do
				$MP3ENCODER  $i
			done
		;;
		*)	nim
		;;
	esac) >> $TMPO 2>&1 && echo -n  && outp AudioCD_2_MP3
}
######### Audio CD to OGG Files: #############################################
acd2ogg(){
	rm -f $TMPO
	cd ${PR[4]}
	is_audio ${CDROM[0]} || ejecta || return 1
	infocd || return 1
	acd2wavs || return 1
	noswm encoder $OGGENCODER Encoding || return 1
	(case $(basename $OGGENCODER) in
		oggenc)	$OGGENCODER -b 192 track*.wav
		;;
		*)	nim
		;;
	esac) >> $TMPO 2>&1 && echo -n  && outp AudioCD_2_OggVorbis
}
######### RAW to CD-R/CD-RW: #################################################
raw2cd(){
	if [ -r ${PR[4]}*.raw.toc ]
	then
		TOC=$(/bin/ls ${PR[4]}*.raw.toc)
		if ! $DIALOG --menu\
			"               TOC file $TOC found." 10 50 3\
			"V "	" View it          "\
			"U "	" Use it           "\
			"C "	" Create a new one "\
			2> $TMP
			then	return 1
		fi
		case $(cat $TMP) in
			"V ")	$DIALOG --textbox "$TOC" 22 65
				raw2cd
				;;
			"U ")	dao " " "$TOC"
				;;
			"C ")	mktoc *.raw
				dao " " $TMP.toc
				;;
		esac
	else
		mktoc *.raw
		dao " " $TMP.toc "a" || return 1
	fi
}
######### RAW to WAV Converting: #############################################
raw2wav(){
	cd ${PR[4]}
	if ! [ "$1" = "nofsel" ] ;then
		fsel raw || return 1
	else
		/bin/ls -1 ${PR[4]}*.raw >$TMP
	fi
	noswm converter $RAW2WAVCONVERTER Converting || return 1
	rm -f $TMPO
	(case $(basename $RAW2WAVCONVERTER) in
		sox)	for i in $(cat $TMP) ;do
				$RAW2WAVCONVERTER -V -t raw -s -w -c2 -r 44100 -x  "$i" "$i".wav
			done
		;;
		wav2cdr) for i in $(cat $TMP) ;do
				$RAW2WAVCONVERTER --quiet --inraw -I --towav "$i" "$i"
				mv -f "$i".* "$i".wav
			done
		;;
		*)	nim
		;;
	esac) >> $TMPO 2>&1 && echo -n  && outp RAW_2_WAV
}
######### RAW to RAW Converting (Swap byte order): ###########################
swapraw(){
	cd ${PR[4]}
	/bin/ls -1 *.raw |awk '{print $1 " " NR " on"}' >$TMPO
	$DIALOG --separate-output --checklist "Select raw files to swap:" 14 70 7 $(cat $TMPO) 2>$TMP || return 1
	$DIALOG --infobox "\n          Swapping byte order..." 5 45
	(for i in $(cat $TMP) ;do
		$SOX -V -t raw -c2 -r 44100 -s -w "$i" -t raw -c2 -r 44100 -s -w -x "$i".swapped.raw >> $TMPO 2>&1
	done) && echo -n  &
	tailbox $SOX
}
######### WAV to CD-R/CD-RW: #################################################
wav2cd(){
	if [ -r ${PR[4]}*.wav.toc ]
	then
		TOC=$(/bin/ls ${PR[4]}*.wav.toc)
		if ! $DIALOG --menu\
			"               TOC file $TOC found." 10 50 3\
			"V "	" View it          "\
			"U "	" Use it           "\
			"C "	" Create a new one "\
			2> $TMP
			then	return 1
		fi
		case $(cat $TMP) in
			"V ")	$DIALOG --textbox "$TOC" 22 65
				wav2cd
				;;
			"U ")	dao " " "$TOC"
				;;
			"C ")	mktoc *.wav
				dao " " $TMP.toc
				;;
		esac
	else
		mktoc *.wav
		dao " " $TMP.toc "a" || return 1
	fi
}
######### WAV to RAW Converting: #############################################
wav2raw(){
	cd ${PR[4]}
	if ! [ "$2" = "nofsel" ] ;then
		fsel wav || return 1
	else
		/bin/ls -1 ${PR[4]}*.wav >$TMP
	fi
	noswm converter $WAV2RAWCONVERTER Converting || return 1
	rm -f $TMPO
	(case $(basename $WAV2RAWCONVERTER) in
		sox)	for i in $(cat $TMP) ;do
				$WAV2RAWCONVERTER -V -t wav "$i" -t raw -s -w -c2 -r 44100 -x "$i".raw
			done
		;;
		wav2cdr) for i in $(cat $TMP) ;do
				$WAV2RAWCONVERTER --quiet --toraw -O "$i" "$i".raw
				mv -f "$i".* "$i".raw
			done
		;;
		*)	nim
		;;
	esac) >> $TMPO 2>&1 && echo -n  && outp WAV_2_RAW
}
######### OGG Encoding: ######################################################
oggencode(){
	cd ${PR[4]}
	(case "$1" in
		raw)	if $DIALOG --yesno "          Swap raw byte order?" 6 45 ;then
				swapraw || return 1
				noswm encoders $OGGENCODER Encoding || return 1
				for i in $(cat $TMP) ;do
					$OGGENCODER -r -B 16 -C 2 -R 44100 -b 192 "$i".swapped.raw && rm -f "$i".swapped.raw
				done
			else
				/bin/ls -1 *."$1" |awk '{print $1 " " NR " on"}' >$TMPO
				$DIALOG --separate-output --checklist "Select $1 files to encode:" 14 70 7 $(cat $TMPO) 2>$TMP || return 1
				noswm encoders $OGGENCODER Encoding || return 1
				for i in $(cat $TMP) ;do
					$OGGENCODER -r -B 16 -C 2 -R 44100 -b 192 "$i"
				done
			fi
		;;
		wav)	/bin/ls -1 *."$1" |awk '{print $1 " " NR " on"}' >$TMPO
			$DIALOG --separate-output --checklist "Select $1 files to encode:" 14 70 7 $(cat $TMPO) 2>$TMP || return 1
			noswm encoders $OGGENCODER Encoding || return 1
			for i in $(cat $TMP) ;do
				$OGGENCODER -b 192 "$i"
			done
		;;
	esac)> $TMPO 2>&1 && echo -n  && outp OggVorbis_encoding
}
######### OGG Decoding: ######################################################
oggdecode(){
	cd ${PR[4]}
	if ! [ "$2" = "nofsel" ] ;then
		fsel ogg || return 1
	else
		/bin/ls -1 ${PR[4]}*.ogg >$TMP
	fi
	noswm decoders $OGGDECODER Decoding || return 1
	(case "$1" in
		raw)	case $(basename $OGGDECODER) in
				ogg123)	for i in $(cat $TMP) ;do
						$OGGDECODER -d "$1" -o byteorder:big -f "$i"."$1" "$i"
					done
				;;
				oggdec)	for i in $(cat $TMP) ;do
						$OGGDECODER -b 16 -R 1 -e 1 -o "$i"."$1" "$i"
					done
				;;
				lame)	for i in $(cat $TMP) ;do
						$OGGDECODER --decode -t -x "$i" "$i"."$1"
					done
				;;
				*)	nim
				;;
			esac
		;;
		wav)	case $(basename $OGGDECODER) in
				ogg123)	for i in $(cat $TMP) ;do
						$OGGDECODER -d $1 -f "$i"."$1" "$i"
					done
				;;
				oggdec)	for i in $(cat $TMP) ;do
						$OGGDECODER "$i"
					done
				;;
				lame)	for i in $(cat $TMP) ;do
						$OGGDECODER --decode "$i"
					done
				;;
				*)	nim
				;;
			esac
		;;
	esac)> $TMPO 2>&1 && echo -n  && outp OggVorbis_decoding
}
######### MP3 Encoding: ######################################################
mp3enc(){
	nosfilesm $1 || return 1
	cd ${PR[4]}
	/bin/ls -1 *."$1" |awk '{print $1 " " NR " on"}' >$TMPO
	$DIALOG --separate-output --checklist "Select $1 files to encode:" 14 70 7 $(cat $TMPO) 2>$TMP || return 1
	noswm encoders $MP3ENCODER Encoding || return 1
	(case "$1" in
		raw)	case "$(basename $MP3ENCODER)" in
				bladeenc) for i in $(cat $TMP) ;do
						$MP3ENCODER -192 -progress=5 -rawbyteorder=BIG "$i"
					done
				;;
				lame)	for i in $(cat $TMP) ;do
						$MP3ENCODER -S -r -s 44.1 -m j --preset cd "$i"
					done
				;;
				toolame) for i in $(cat $TMP) ;do
						$SOX -V -t "$1" -c 2 -r 44100 -w -s -x "$i" "$i".wav
						$MP3ENCODER "$i".wav "$i".mp3
						rm -f "$i".wav
					done
				;;
				*)	nim
				;;
			esac
		;;
		wav)	case "$(basename $MP3ENCODER)" in
				bladeenc) for i in $(cat $TMP) ;do
						$MP3ENCODER -192 -progress=5 "$i"
					done
				;;
				lame)	for i in $(cat $TMP) ;do
						$MP3ENCODER -S --preset cd "$i" "$i".mp3
					done
				;;
				toolame) for i in $(cat $TMP) ;do
						$MP3ENCODER -x "$i"
					done
				;;
				*)	nim
				;;
			esac
		;;
	esac)> $TMPO 2>&1 && echo -n  && outp MP3_encoder
}
######### MP3 Decoding: ######################################################
mp3dec(){
	cd ${PR[4]}
	if ! [ "$2" = "nofsel" ] ;then
		fsel mp3 || return 1
	else
		/bin/ls -1 ${PR[4]}*.mp3 >$TMP
	fi
	noswm decoders $MP3DECODER Decoding || return 1
	(case "$1" in
		raw)	case "$(basename $MP3DECODER)" in
				mpg123)	for i in $(cat $TMP) ;do
						$MP3DECODER -s -w - "$i" |$SOX -V -t cdr - "$i"."$1"
					done &
				;;
				mpg321)	for i in $(cat $TMP) ;do
						$MP3DECODER -s -w - "$i" |$SOX -V -t cdr - "$i"."$1"
					done &
				;;
				amp)	for i in $(cat $TMP) ;do
						$MP3DECODER -c "$i" - | $SOX -V -t "$1" -c2 -w -s -r 44100 - -t raw -c2 -r 44100 -w -s -x "$i"."$1"
					done
				;;
				lame)	for i in $(cat $TMP) ;do
						$MP3DECODER -S --decode -t -x "$i" "$i"."$1"
					done &
				;;
				madplay) for i in $(cat $TMP) ;do
						$MP3DECODER -o "$1":- "$i" |$SOX -V -t cdr -s -w -c2 -r 44100 - "$i"."$1"
					done &
				;;
				maplay)	for i in $(cat $TMP) ;do
						$MP3DECODER -q -s "$i" |$SOX -V -t cdr -s -w -c2 -r 44100 - "$i"."$1"
					done &
				;;
				splay)	for i in $(cat $TMP) ;do
						$MP3DECODER "$i" -d - |$SOX -V -t cdr -s -w -c2 -r 44100 - "$i"."$1"
					done &
				;;
				*)	nim
				;;
			esac
		;;
		wav)	case "$(basename $MP3DECODER)" in
				mpg123)	for i in $(cat $TMP) ;do
						$MP3DECODER -w "$i"."$1" "$i"
					done &
				;;
				mpg321)	for i in $(cat $TMP) ;do
						$MP3DECODER -w "$i"."$1" "$i"
					done &
				;;
				amp)	for i in $(cat $TMP) ;do
						$MP3DECODER -c -w "$i" "$i"."$1"
					done
				;;
				lame)	for i in $(cat $TMP) ;do
						$MP3DECODER -S --decode "$i"
					done &
				;;
				madplay) for i in $(cat $TMP) ;do
						$MP3DECODER "$i" -o wave:"$i"."$1"
					done &
				;;
				maplay)	for i in $(cat $TMP) ;do
						$MP3DECODER -q -s "$i" |$SOX -V -t raw -s -w -c2 -r 44100 - -t "$1" "$i"."$1"
					done &
				;;
				splay)	for i in $(cat $TMP) ;do
						$MP3DECODER "$i" -d - |$SOX -V -t raw -s -w -c2 -r 44100 - -t "$1" "$i"."$1"
					done &
				;;
				mp3blaster) $MP3DECODER
				;;
				*)	nim
				;;
			esac
		;;
	esac)> $TMPO 2>&1 && tailbox $MP3DECODER
}
######### M3U Functions: #####################################################
getm3u(){
	if [ -r ${PR[15]} ] ;then
		$DIALOG --menu\
			"          Playlist Selection:" 9 65 2\
			"X "	" Xmms default:      ${PR[15]} "\
			"S "	" Select manually                         "\
		2>$TMP || return 1
		case $(cat $TMP) in
			"X ")	echo "${PR[15]}" >$TMP
			;;
			"S ")	$DIALOG --fselect /mnt/images/sound/*.m3u 20 75 2>$TMP || return 1
			;;
		esac
	else
		$DIALOG --fselect /mnt/images/sound/*.m3u 20 75 2>$TMP || return 1
	fi
	PLF="$(cat $TMP)"
	if [ -r $PLF ] ;then
		j="0"
		for i in $(sed '/^#/d' "$PLF") ;do
			EXT=$(echo "$i"|awk 'BEGIN{FS="."}{print $NF}'|tr "[:upper:]" "[:lower:]" )
			if [ "$EXT" = "raw" ] || [ "$EXT" = "wav" ] || [ "$EXT" = "mp3" ] || [ "$EXT" = "ogg" ] ;then
				if [ "$EXT" = "wav" ] ;then
					PLA[$j]="$i"
				elif [ "$EXT" = "mp3" ] ;then
					PLA[$j]="$i"
				elif [ "$EXT" = "ogg" ] ;then
					PLA[$j]="$i"
				fi
				j="$(( j + 1 ))"
			fi
		done
	fi
}
m3u2acd(){
	m3u2wav || return 1
	mktoc *.wav || return 1
	dao " " $TMP.toc "a" || return 1
}
m3u2cd(){
	j="0"
	while ! [ "${PLA[$j]}" = "" ] ;do
		cp -f ${PLA[$j]} ${PR[5]}
		j="$(( j + 1 ))"
	done
	dir2i2cd || return 1
}
m3u2raw(){
	cd ${PR[4]}
	rm -f ${PR[4]}*.raw
	j="0"
	while ! [ "${PLA[$j]}" = "" ] ;do
		cp -f ${PLA[$j]} ${PR[4]}
		j="$(( j + 1 ))"
	done
	if ((/bin/ls ${PR[4]}*.mp3) || (/bin/ls ${PR[4]}*.MP3))>/dev/null 2>&1 ;then
		mp3dec raw nofsel || return 1
	fi
	if ((/bin/ls ${PR[4]}*.ogg) || (/bin/ls ${PR[4]}*.OGG))>/dev/null 2>&1 ;then
		oggdecode raw nofsel || return 1
	fi
}
m3u2wav(){
	cd ${PR[4]}
	rm -f ${PR[4]}*.wav
	j="0"
	while ! [ "${PLA[$j]}" = "" ] ;do
		cp -f ${PLA[$j]} ${PR[4]}
		j="$(( j + 1 ))"
	done
	if ((/bin/ls ${PR[4]}*.mp3) || (/bin/ls ${PR[4]}*.MP3))>/dev/null 2>&1 ;then
		mp3dec wav nofsel || return 1
	fi
	if ((/bin/ls ${PR[4]}*.ogg) || (/bin/ls ${PR[4]}*.OGG))>/dev/null 2>&1 ;then
		oggdecode wav nofsel || return 1
	fi
}
######### Start / Pause / Stop Playing Audio Files: ##########################
startplay(){
	cd ${PR[4]}
	nosfilesm $1 || return 1
	callsp || return 1
	/bin/ls -1 ${PR[4]}*."$1" | awk '{print $1 " " NR " on"}' >$TMPO
	$DIALOG --separate-output --checklist "Select files to play:" 14 70 7 $(cat $TMPO) 2>$TMP || return 1
	case "$1" in
		raw)	case $(basename $RAWPLAYER) in
				sox)	for i in $(cat $TMP);do
						$RAWPLAYER -t $1 -s -w -c2 -r 44100 -x "$i" -t ossdsp -w -s /dev/dsp || return 1
					done &
				;;
				rawplay) for i in $(cat $TMP);do
						$RAWPLAYER -B 262144 -c 2 -d /dev/dsp -f s16_be -s 44100 -j 0 "$i" || return 1
					done &
				;;
				tplay)	for i in $(cat $TMP);do
						$RAWPLAYER -f -b 16 -s 44100 -e linear -S 0 -D /dev/dsp -x "$i" || return 1
					done &
				;;
				*)	nim
				;;
			esac
		;;
		wav)	case $(basename $WAVPLAYER) in
				wavp)	for i in $(cat $TMP);do
						$WAVPLAYER $i	>/dev/null 2>&1	|| return 1
					done &
				;;
				wavplay) for i in $(cat $TMP);do
						$WAVPLAYER -q -S -b 16 -s 44100 "$i" >/dev/null 2>&1 || return 1
					done &
				;;
				sox)	for i in $(cat $TMP);do
						$WAVPLAYER -t $1 "$i" -t ossdsp -w -s /dev/dsp || return 1
					done &
				;;
				splay)	for i in $(cat $TMP);do
						cat "$i"|$WAVPLAYER -W	|| return 1
					done &
				;;
				rawplay) for i in $(cat $TMP);do
						$WAVPLAYER rawplay -B 262144 -c 2 -d /dev/dsp -f s16_le -s 44100 -j 0 || return 1
					done &
				;;
				tplay)	for i in $(cat $TMP);do
						$WAVPLAYER -f -b 16 -s 44100 -e linear -S 0 -D /dev/dsp "$i" || return 1
					done &
				;;
				bplay)	for i in $(cat $TMP);do
						$WAVPLAYER -S -s 44100 -b 16 "$i" >/dev/null 2>&1 || return 1
					done &
				;;
				yarec)	for i in $(cat $TMP);do
						$WAVPLAYER -r 44100 -b 16 -c 2 "$i" || return 1
					done
				;;
				*)	nim
				;;
			esac
		;;
		mp3)	case $(basename $MP3PLAYER) in
				mp3blaster) $MP3PLAYER || return 1
				;;
				lame)	for i in $(cat $TMP);do
						$MP3PLAYER --quiet --decode "$i" - | $SOX -t wav - -t ossdsp -w -s /dev/dsp >/dev/null 2>&1 || return 1 &
					done
				;;
				freeamp) clear;$MP3PLAYER $(cat $TMP) || return 1
				;;
				mpg123)	cat $TMP | $MP3PLAYER -q -@ - || return 1 &
				;;
				mpg321)	cat $TMP | $MP3PLAYER -q -@ - || return 1 &
				;;
				amp)	for i in $(cat $TMP);do
						$MP3PLAYER -q "$i" || return 1 &
					done
				;;
				madplay) $MP3PLAYER -Q $(cat $TMP) || return 1 &
				;;
				maplay)	$MP3PLAYER -q -@ $TMP || return 1 &
				;;
				splay)	$MP3PLAYER -l $TMP || return 1 &
				;;
				*)	nim
				;;
			esac
		;;
		ogg)	case $(basename $OGGPLAYER) in
				ogg123)	$OGGPLAYER -@ $TMP || return 1 &
				;;
				oggdec)	for i in $(cat $TMP);do
						$OGGPLAYER -b 16 -R 1 -e 1 "$i" -o - |$SOX -t raw -s -w -c2 -r 44100 -x - -t ossdsp -w -s /dev/dsp > /dev/null 2>&1 || return 1 &
					done
				;;
				lame)	for i in $(cat $TMP);do
						$OGGPLAYER --decode "$i" - |$SOX -t wav - -t ossdsp -w -s /dev/dsp >/dev/null 2>&1 || return 1 &
					done
				;;
				*)	nim
				;;
			esac
		;;
	esac
	read
}
callsp(){	
	if $LSOF /dev/dsp >/dev/null ;then
		stopplay $RAWPLAYER || return 1
		stopplay $WAVPLAYER || return 1
		stopplay $MP3PLAYER || return 1
		stopplay $OGGPLAYER || return 1
		cd ${PR[4]}
	fi
}
stopplay(){
	if pidof "$1" >/dev/null 2>&1
	then
		case $(ps -t $TTY -o pid,fname,ruser,stat|grep $(basename $1)|grep $(whoami)|head -1|awk '{print $4}'|tr -d "<") in
			S|R)	if ! $DIALOG --menu\
					"     Running $(basename $1) process" 9 40 2\
					"K "	"Kill        <SIGKILL> "\
					"S "	"Stop        <SIGSTOP> "\
					2> $TMP
				then
					return 1
				fi
			;;
			T)	if ! $DIALOG --menu\
					"     Stopped $(basename $1) process." 9 40 2\
					"K "	"Kill        <SIGKILL> "\
					"C "	"Continue    <SIGCONT> "\
					2> $TMP
				then
					return 1
				fi
			;;
		esac
		case $(cat $TMP) in
			"K ")	kill -9  $($LSOF /dev/dsp|head -2|tail -1|awk '{print $2}') >/dev/null 2>&1 &
				sleep 1 && return 0
			;;
			"S ")	kill -19 $($LSOF /dev/dsp|head -2|tail -1|awk '{print $2}') >/dev/null 2>&1 &
				return 1
			;;
			"C ")	kill -18 $($LSOF /dev/dsp|head -2|tail -1|awk '{print $2}') >/dev/null 2>&1 &
				return 1
			;;
		esac
	fi
}
########## Start Recordings: #################################################
startrec(){
	cd ${PR[4]}
	$DIALOG --infobox "\n  Recording from /dev/dsp to ${PR[4]}rec.$1\n             (44100 Hz/16 bit/2 channel).\n\n            Hit Ctrl-C to stop recording." 8 60
	case "$1" in
		raw)
			case "$(basename $RAWRECORDER)" in
				sox)	$RAWRECORDER -V -t ossdsp -w -s /dev/dsp -t $1 -s -w -c2 -r 44100 -x ${PR[4]}rec.$1 > $TMPO 2>&1
				;;
				brec)	$RAWRECORDER -r -S -b 16 -s 44100 ${PR[4]}rec.$1 > $TMPO 2>&1
				;;
				rawrec)	$RAWRECORDER -s 44100 -c 2 rec.raw > $TMPO 2>&1
				;;
				*)	nim
				;;
			esac
		;;
		wav)	case "$(basename $WAVRECORDER)" in
				sox)	$WAVRECORDER -V -t ossdsp -w -s /dev/dsp -t $1 -c2 -r 44100 -s ${PR[4]}rec.$1 > $TMPO 2>&1
				;;
				wavr)	$WAVRECORDER -f ${PR[4]}rec.$1 -r 44100 -c 2 -d 16 -s m > $TMPO 2>&1
				;;
				wavrec)	$WAVRECORDER -S -s 44100 -b ${PR[4]}rec.$1 > $TMPO 2>&1
				;;
				brec)	$WAVRECORDER -w -S -b 16 -s 44100 ${PR[4]}rec.$1 > $TMPO 2>&1
				;;
				yarec)	echo -n \'Q + Enter\' to stop.
					$WAVRECORDER -f -b 16 -r 44100 -c 2 ${PR[4]}rec.$1 > $TMPO 2>&1
					rm -f ${PR[4]}yarec.tmp
				;;
				*)	nim
				;;
			esac
		;;
	esac && outp Recording
}
########## Miscellaneous Menu: ###############################################
blankfull(){
	eject -t "${CDRW[0]}"
	di "${CDRW[3]}"
	if	is_r
	then	$DIALOG --beep --msgbox "   The CD is not rewritable." 6 35 && return 1
	elif	is_blank
	then	$DIALOG --beep --msgbox "   The CD is already empty or blanked." 6 45 && return 1
	elif	is_rw_and_full
	then	if $DIALOG --yesno "            Blank fully?" 6 40
		then	is_gracetime
			echo "$CDRECORD dev=${CDRW[3]} ${PR[14]} blank=all -force" > $TMPO 2>&1
			($CDRECORD dev=${CDRW[3]} ${PR[14]} blank=all -force >> $TMPO 2>&1 && echo -n ) &
			tailbox $CDRECORD
		else	return 1
		fi
	fi
}
blankfast(){
	eject -t "${CDRW[0]}"
	di "${CDRW[3]}"
	if	is_r
	then	$DIALOG --beep --msgbox "     The CD is not rewritable." 6 40 && return 1
	elif	is_blank
	then	$DIALOG --beep --msgbox "   The CD is already empty or blanked." 6 45 && return 1
	elif	is_rw_and_full
	then	if $DIALOG --yesno "            Blank fast?" 6 40 ;then
			is_gracetime
			echo "$CDRECORD dev=${CDRW[3]} ${PR[14]} blank=fast -force" > $TMPO 2>&1
			($CDRECORD dev=${CDRW[3]} ${PR[14]} blank=fast -force >> $TMPO 2>&1 && echo -n ) &
			tailbox $CDRECORD || return 1
		else
			return 1
		fi
	fi
}
blanktrack(){
	eject -t "${CDRW[0]}"
	is_gracetime
	echo "$CDRECORD dev=${CDRW[3]} ${PR[14]} blank=track -eject" > $TMPO 2>&1 
	($CDRECORD dev=${CDRW[3]} ${PR[14]} blank=track -eject >> $TMPO 2>&1 && echo -n ) &
	tailbox $CDRECORD
	eject -t "${CDRW[0]}"
}
unreservetrack(){
	eject -t "${CDRW[0]}"
	is_gracetime
	echo "$CDRECORD dev=${CDRW[3]} ${PR[14]} blank=unreserve -eject" > $TMPO 2>&1
	($CDRECORD dev=${CDRW[3]} ${PR[14]} blank=unreserve -eject >> $TMPO 2>&1 && echo -n  || return 1) &
	tailbox $CDRECORD
	eject -t "${CDRW[0]}"
}
blanktracktail(){
	eject -t "${CDRW[0]}"
	is_gracetime
	echo "$CDRECORD  dev=${CDRW[3]} ${PR[14]} blank=trtail -eject" > $TMPO 2>&1
	($CDRECORD dev=${CDRW[3]} ${PR[14]} blank=trtail -eject >> $TMPO 2>&1 && echo -n  || return 1) &
	tailbox $CDRECORD
	eject -t "${CDRW[0]}"
}
blankunclose(){
	eject -t "${CDRW[0]}"
	is_gracetime
	echo "$CDRECORD dev=${CDRW[3]} ${PR[14]} blank=unclose -eject" > $TMPO 2>&1
	($CDRECORD dev=${CDRW[3]} ${PR[14]} blank=unclose -eject >> $TMPO 2>&1 && echo -n  || return 1) &
	tailbox $CDRECORD
	eject -t "${CDRW[0]}"
}
blanklastsession(){
	eject -t "${CDRW[0]}"
	is_gracetime
	echo "$CDRECORD dev=${CDRW[3]} ${PR[14]} blank=session -eject" > $TMPO 2>&1
	($CDRECORD dev=${CDRW[3]} ${PR[14]} blank=session -eject >> $TMPO 2>&1 && echo -n ) &
	tailbox $CDRECORD
	eject -t "${CDRW[0]}"
}
fixate(){
	eject -t "${CDRW[0]}"
	di "${CDRW[3]}"
	if	is_blank
	then	$DIALOG --beep --msgbox "       The CD is empty or blanked." 6 45 && return 1
	elif	! is_app
	then	$DIALOG --beep --msgbox "     The CD is not open (appendable)." 6 45 && return 1
	elif	is_app
	then	if $DIALOG --yesno "       Fixate (write TOC)?" 6 40
		then	is_gracetime
			echo "$CDRECORD -fix dev=${CDRW[3]} ${PR[14]}" > $TMPO 2>&1
			($CDRECORD -fix dev=${CDRW[3]} ${PR[14]} >> $TMPO 2>&1 && echo -n ) &
			tailbox $CDRECORD
		else	return 1
		fi
	fi
}
cleanup(){
	$DIALOG --checklist "     Select items for <rm -rf>" 16 50 9\
		1 "${PR[0]}${PR[1]}" on\
		2 "${PR[5]}*" off\
		3 "${PR[4]}*" on\
		4 "${PR[4]}audio.cddb" off\
		5 "${PR[4]}cdda.inf" off\
		6 "${PR[4]}*.raw" off\
		7 "${PR[4]}*.wav" off\
		8 "${PR[4]}*.mp3" off\
		9 "${PR[4]}*.ogg" off\
		2> $TMPO

	grep -q "1" $TMPO && rm -rf ${PR[0]}${PR[1]}
	grep -q "2" $TMPO && rm -rf ${PR[5]}*
	grep -q "3" $TMPO && rm -rf ${PR[4]}*
	grep -q "4" $TMPO && rm -rf ${PR[4]}audio.cddb
	grep -q "5" $TMPO && rm -rf ${PR[4]}cdda.inf
	grep -q "6" $TMPO && rm -rf ${PR[4]}*.raw
	grep -q "7" $TMPO && rm -rf ${PR[4]}*.wav
	grep -q "8" $TMPO && rm -rf ${PR[4]}*.mp3
	grep -q "9" $TMPO && rm -rf ${PR[4]}*.ogg
}
diskinfo(){
	echo -ne "----------------------------\n<cdir> output:\n----------------------------\n" >$TMPO
	$CDIR -d $1 >>$TMPO
	echo -ne "\n----------------------------\n<isoinfo> output:\n----------------------------\n" >>$TMPO
	$ISOINFO -d -i $1 >> $TMPO
	if [ "$?" -eq "123" ] ;then
		echo "No medium in $1" >>$TMPO
		outp "$1"
		return 1
	fi
	echo -ne "\n----------------------------\n<cdrdao disk-info> output:\n----------------------------\n" >>$TMPO
	$CDRDAO disk-info --device $2 --driver generic-mmc  >> $TMPO 2>&1
	echo -ne "\n----------------------------\n<cdrecord -toc> output:\n----------------------------\n" >>$TMPO
	$CDRECORD -v -toc dev=$2 >>$TMPO 2>/dev/null
	echo -ne "\n----------------------------\nroot directory:\n----------------------------\n" >>$TMPO
	$MOUNT $1 $3 2>/dev/null &&\
	ls -l $3 >>$TMPO &&\
	$UMOUNT $1
	outp "$1"
}
show_sw(){
	$DIALOG --infobox "\n      Getting version info..." 5 40
	(
	echo "-----"
	if type -p cdir;then cdir -V >/dev/null 2>$TMP ;head -1 $TMP;fi
	echo "-----"
	if type -p cdrdao;then cdrdao 2>$TMP ;head -2 $TMP |tail -1;fi
	echo "-----"
	if type -p cdrecord;then cdrecord --version;fi
	echo "-----"
	if type -p isoinfo ;then isoinfo --version;fi
	echo "-----"
	if type -p mkisofs ;then mkisofs --version;fi
	echo "-----"
	if type -p dialog ;then dialog --version 2>$TMP;cat $TMP;fi
	echo "-----"
	if type -p mkzftree ;then mkzftree --version 2>$TMP;cat $TMP;fi
	echo "-----"
	if type -p readcd ;then readcd --version;fi
	echo "-----"
	if type -p amp ;then amp -v;fi
	echo "-----"
	if type -p aumix ;then aumix -h|grep -i "aumix ";fi
	echo "-----"
	if type -p bladeenc ;then bladeenc |head -2|tail -1;fi
	echo "-----"
	if type -p bplay ;then :;fi
	echo "-----"
	if type -p brec ;then :;fi
	echo "-----"
	if type -p cam ;then :;fi
	echo "-----"
	if type -p cdcd ;then cdcd -v|head -1;fi
	echo "-----"
	if type -p cdda2wav ;then cdda2wav -version 2>$TMP ;cat $TMP;fi
	echo "-----"
	if type -p cdp ;then :;fi
	echo "-----"
	if type -p cdparanoia ;then cdparanoia --version 2>$TMP ;head -1 $TMP;fi
	echo "-----"
	if type -p dagrab ;then dagrab >/dev/null 2>./ide;head -2 ./ide|tail -1;fi
	echo "-----"
	if type -p freeamp ;then freeamp --help|head -1;fi
	echo "-----"
	if type -p gcd ;then strings $(type -p gcd) |grep "gcd "|tail -1;fi
	echo "-----"
	if type -p groovycd ;then groovycd -v;fi
	echo "-----"
	if type -p lame ;then lame --version|head -1;fi
	echo "-----"
	if type -p madplay ;then madplay --version 2>/dev/null;fi
	echo "-----"
	if type -p maplay ;then :;fi
	echo "-----"
	if type -p mcd ;then strings $(type -p mcd) |grep version;fi
	echo "-----"
	if type -p mp3blaster ;then mp3blaster -v;fi
	echo "-----"
	if type -p mpg123 ;then mpg123 -V|head -1;fi
	echo "-----"
	if type -p mpg321 ;then mpg321 -V|head -1;fi
	echo "-----"
	if type -p nmixer ;then strings $(type -p nmixer) |grep -i "NMixer"|grep -v compiled|tr -d "<>";fi
	echo "-----"
	if type -p ogg123 ;then ogg123 -V 2>$TMP ;head -1 $TMP;fi
	echo "-----"
	if type -p oggdec ;then :;fi
	echo "-----"
	if type -p oggenc ;then oggenc --version 2>$TMP ;cat $TMP;fi
	echo "-----"
	if type -p rawplay ;then rawplay --version|head -1;fi
	echo "-----"
	if type -p rawrec ;then rawrec --version|head -1;fi
	echo "-----"
	if type -p rexima ;then strings $(type -p rexima)|grep -i "rexima v";fi
	echo "-----"
	if type -p sadp ;then sadp --help|head -2|tail -1;fi
	echo "-----"
	if type -p sox ;then sox -h 2>$TMP ; grep -i version $TMP;fi
	echo "-----"
	if type -p splay ;then splay|head -1;fi
	echo "-----"
	if type -p toolame ;then toolame 2>ide ;grep -i "version" ./ide;fi
	echo "-----"
	if type -p tplay ;then tplay -v;fi
	echo "-----"
	if type -p wav2cdr ;then wav2cdr --version|grep -i "version";fi
	echo "-----"
	if type -p wavp ;then :;fi
	echo "-----"
	if type -p wavplay ;then wavplay -V|grep -i "version"|head -1;fi
	echo "-----"
	if type -p wavr ;then :;fi
	echo "-----"
	if type -p wavrec ;then wavrec -V|grep -i "version"|head -1;fi
	echo "-----"
	if type -p workbone ;then workbone -v|grep -i "version"|head -1;fi
	echo "-----"
	if type -p yarec ;then :;fi
	echo "-----"
	) >$TMPO
	$DIALOG --title "Detected software supported by TCDR" --textbox $TMPO 22 70
}
src_tcdrrc(){
	. $TCDRRC
}
######### Utility Functions: #################################################
infocd(){
	is_audio ${CDROM[0]} || ejecta || return 1
	NUMTRACK=$($CDIR -d ${CDROM[0]} |head -1 |awk '{print $(NF-1) }')
	if	is_mix ${CDROM[0]}
	then	( $DIALOG --beep --yesno "   This is a mixed mode CD (audio + data track)\n                     Continue?" 8 55 && return 0 || return 1 )
	fi
}
mktoc(){
	cd ${PR[4]}
	echo "CD_ROM_XA" >$TMP.toc
	for i in "$@" ; do
		echo "" >>$TMP.toc
		if [ ! -r "$i" ] ;then
			$DIALOG --beep --msgbox "   Can't read ${PR[4]}$i." 6 60
			return 1
		elif [ ! -s "$i" ] ;then
			$DIALOG --beep --msgbox "   Size of ${PR[4]}$i is 0." 6 60
			return 1
		fi
		echo "TRACK AUDIO" >>$TMP.toc
		echo "AUDIOFILE \"$i\" 0" >>$TMP.toc
	done
	$CDRDAO show-toc $TMP.toc > $TMPO 2>&1 && outp TOC
}
inpvid(){
	$DIALOG --inputbox "Enter Volume ID" 8 65 "${PR[2]}" 2>$TMPO && PR[2]=$(cat $TMPO) || return 1
}
fsel(){
	nosfilesm "$1" || return 1
	/bin/ls -1 *."$1" | awk '{print $1 " " NR " on"}' >$TMPO
	$DIALOG --separate-output --checklist "Select "$1" files to decode:" 14 70 7 $(cat $TMPO) 2>$TMP || return 1
}
ejectm(){
	eject ${CDROM[0]} ;$DIALOG --yesno "Insert the master CD into ${CDROM[1]}" 5 70 || return 1
	eject -t ${CDROM[0]}
}
ejecta(){
	eject ${CDROM[0]}
	$DIALOG --yesno "Insert the audio CD into ${CDROM[1]}" 6 70 || return 1
	eject -t ${CDROM[0]}
}
ejectr(){
	eject ${CDRW[0]} ;$DIALOG --yesno "Insert a writeable CD-R/CD-RW medium into ${CDRW[1]}" 5 70 || return 1
	eject -t ${CDRW[0]}
}
tailbox(){
	[ "$NOOUTP" -eq "1" ] 2>/dev/null && return 0
	$DIALOG --tailbox $TMPO 22 75 || kill -9 $(pidof $1)
}
outp(){
	[ "$NOOUTP" -eq "1" ] 2>/dev/null && return 0
	$DIALOG --title "$1" --beep --textbox $TMPO 22 77 || return 1
	rm -f $TMPO
}
waitm(){
	$DIALOG --infobox "\n  You may watch die blinkink lichts..." 5 50
}
noswm(){
	if [ $2 == "$NONE" ] ;then
		$DIALOG --msgbox "No $1 found" 6 50
		return 1
	else
		if [ ! -z $3 ] ;then
			$DIALOG --infobox "\n  $3..." 5 50
		fi
		return 0
	fi
}
nim(){
	$DIALOG --msgbox "               Not implemented." 6 50
}
nosfilesm(){
	if ! ls ${PR[4]}*.$1 > /dev/null 2>&1 ;then
		$DIALOG --msgbox "No .$1 files found in ${PR[4]}" 6 65
		return 1
	else
		return 0
	fi
}
noisofm(){
	if [ ! -f ${PR[0]}${PR[1]} ] ;then
		$DIALOG --msgbox "No ${PR[1]} file found in ${PR[0]}" 6 65
		return 1
	else
		return 0
	fi
}
cdrootemptym(){

	if [ -z $(/bin/ls ${PR[5]}*) ] ;then
		$DIALOG --msgbox "No files found in ${PR[5]}" 6 65
		return 1
	else
		return 0
	fi 2>/dev/null
}
######### Submenus: ##########################################################
dirsub(){
	if ! $DIALOG --menu\
		"      FS Directory Operations <${PR[5]}>" 11 77 4\
		"I "  "Directory -> ISO                 <mkisofs>                      "\
		"C "  "Directory -> ISO  -> CD-R/CD-RW  <mkisofs | cdrecord>           "\
		"Z "  "Directory -> ZISO                <mkzftree| mkisofs -z>         "\
		"T "  "Directory -> ZISO -> CD-R/CD-RW  <mkzftree|mkisofs -z|cdrecord> "\
		2> $TMP
	then mainm
	fi
	case $(cat $TMP) in
		"I ")	dir2i
			dirsub
			;;
		"C ")	dir2i2cd
			dirsub
			;;
		"Z ")	if [ -f ${PR[0]}${PR[1]} ] ;then
				$DIALOG --menu\
					"${PR[0]}${PR[1]} exists." 9 65 2\
					"1" "Use it"\
					"2" "Overwrite it"\
					2>$TMP || dirsub
				case $(cat $TMP) in
					"1" )	:
						;;
					"2" )	inpvid || dirsub
						mkziso || dirsub
						;;
				esac
			else
				inpvid || dirsub
				mkziso || dirsub
			fi
			dirsub
			;;
		"T ")	if [ -f ${PR[0]}${PR[1]} ] ;then
				$DIALOG --menu\
					"${PR[0]}${PR[1]} exists." 9 65 2\
					"1" "Use it"\
					"2" "Overwrite it"\
					2>$TMP || dirsub
				case $(cat $TMP) in
					"1" )	:
						;;
					"2" )	inpvid || dirsub
						mkziso || dirsub
						;;
				esac
			else
				inpvid || dirsub
				mkziso || dirsub
			fi
			cdrec
			dirsub
			;;
	esac
}
dcdsub(){
	if ! $DIALOG --menu\
		"       Data CD Options (${CDROM[0]}  ->  ${CDRW[0]})" 13 77 6\
        	"D " "Data CD   ->  ISO                   <dd>                       "\
        	"R " "Data CD   ->  ISO                   <readcd> (try if dd fails) "\
        	"I " "ISO/ZISO  ->  CD-R/CD-RW            <cdrecord>                 "\
        	"B " "ISO/ZISO  ->  Overburn CD-R/CD-RW   <cdrecord -dao -overburn>  "\
	        "C " "Data CD   ->  CD-R/CD-RW            <cdrdao copy>              "\
        	"O " "Data CD   ->  CD-R/CD-RW            <cdrdao copy --on-the-fly> "\
		2> $TMP
	then mainm
	fi
	case $(cat $TMP) in
		"D ")	cd2i_dd
			dcdsub
			;;
		"R ")	cd2i_rcd
			dcdsub
			;;
		"I ")	i2dcd
			dcdsub
			;;
		"B ")	overburn
			dcdsub
			;;
		"C ")	dcd2cd
			dcdsub
			;;
		"O ")	dcd2cdr
			dcdsub
			;;
	esac
}
mixcdsub(){
	if ! $DIALOG --menu\
		"         Mixed Mode (CD Extra/CD+) Options" 10 75 3\
        	"X "	"Mixed Mode CD                ->          CD-R/CD-RW      "\
        	"Z "	"Compressed Mixed Mode CD     ->          CD-R/CD-RW      "\
        	"I "	"Display track info on medium in ${CDRW[1]} "\
		2> $TMP
	then mainm
	fi
	case $(cat $TMP) in
		"X ")	mixcd2cd
			mixcdsub
			;;
		"Z ")	mixcd2cd z
			mixcdsub
			;;
		"I ")	msdcdinfo
			mixcdsub
			;;
	esac
}
msdsub(){
	if ! $DIALOG --menu\
		"      Multi-session Data CD Options" 11 75 4\
 		"S " "Session       ->   MS CD-R/CD-RW         <mkisofs | cdrecord> "\
		"L " "Last session  ->   Close MS CD-R/CD-RW   <mkisofs | cdrecord> "\
 		"I " "Display track info on medium in ${CDRW[1]} "\
		2> $TMP
	then mainm
	fi
	case $(cat $TMP) in
		"S ")	msd2cd
			msdsub
			;;
		"L ")	msdclose
			msdsub
			;;
		"I ")	msdcdinfo
			msdsub
			;;
	esac
}
bootcdsub(){
	if ! $DIALOG --menu\
		"      Bootable data CD options (El Torito)" 10 77 3\
        	"B " "Directory and ${PR[7]}  ->  CD-R/CD-RW     <mkisofs | cdrecord> "\
        	"F " "Bootable floppy         ->  ${PR[8]}${PR[7]}  <dd>                 "\
		2> $TMP
	then mainm
	fi
	case $(cat $TMP) in
		"B ")	bootcd
			bootcdsub
			;;
		"F ")	fd2bootimg
			bootcdsub
			;;
	esac
}
acdsub(){
	if ! $DIALOG --menu\
		"                    Audio CD ripping options.\n            Target directory:  <${PR[4]}>" 18 75 10\
		"C "  "Audio CD  ->  CD-R/CD-RW     <cdrdao CD Copy>            "\
		"O "  "Audio CD  ->  CD-R/CD-RW     <cdrdao CD Copy On the Fly> "\
		"A "  "Audio CD  ->  CD-R/CD-RW     <$(basename $CDRIPPER) | cdrdao>       "\
		"S "  "Audio CD  ->  Separate WAVs  <$(basename $CDRIPPER)>                "\
		"R "  "Audio CD  ->  RAW image      <$(basename $CDRIPPER) + toc>          "\
		"W "  "Audio CD  ->  WAV image      <$(basename $CDRIPPER) + toc>          "\
		"M "  "Audio CD  ->  MP3 tracks     <$(basename $CDRIPPER) | $(basename $MP3ENCODER)>     "\
		"G "  "Audio CD  ->  OGG tracks     <$(basename $CDRIPPER) | $(basename $OGGENCODER)>     "\
		"P "  "CD Player                    <$(basename $CDPLAYER)>                  "\
		"X "  "Mixer                        <$(basename $MIXER)>                     "\
		2> $TMP
	then
		mainm
	fi
	case $(cat $TMP) in
		"C ")	cd ${PR[4]}
			acd2acd_t
			acdsub
			;;
		"O ")	acd2acd
			acdsub
			;;
		"A ")	cd ${PR[4]}
			is_audio ${CDROM[0]} || ejecta || acdsub
			infocd || acdsub
			acd2acd_$(basename $CDRIPPER)
			acdsub
			;;
		"S ")	acd2wavs
			acdsub
			;;
		"R ")	acd2raw
			acdsub
			;;
		"W ")	acd2wav
			acdsub
			;;
		"M ")	acd2mp3
			acdsub
			;;
		"G ")	acd2ogg
			acdsub
			;;
		"P ")	clear
			$CDPLAYER ${CDRW[0]}
			acdsub
			;;
		"X ")	$MIXER
			acdsub
			;;
	esac
}
rawsub(){
	rm -f $TMPO
	if ! $DIALOG --menu\
		"         RAW/CDR Options (${PR[4]}*.raw)" 15 70 8\
		"C "  "RAW/CDR    ->    CD-R/CD-RW             <cdrdao>   "\
		"W "  "RAW/CDR    ->    WAV                    <$(basename $RAW2WAVCONVERTER)>      "\
		"M "  "RAW/CDR    ->    MP3                    <$(basename $MP3ENCODER)> "\
		"O "  "RAW/CDR    ->    OGG                    <$(basename $OGGENCODER)> "\
		"P "  "Player                                  <$(basename $RAWPLAYER)>      "\
		"S "  "RAW        ->    RAW  (Swap byte order) <$(basename $SOX)>    "\
		"R "  "Record from /dev/dsp                    <$(basename $RAWRECORDER)>    "\
		"X "  "Mixer                                   <$(basename $MIXER)>    "\
		2> $TMP
	then	
		mainm
	fi
	case $(cat $TMP) in
		"C ")	raw2cd
			rawsub
			;;
		"W ")	nosfilesm raw || rawsub
			noswm converter $RAW2WAVCONVERTER Converting || rawsub
			raw2wav
			rawsub
			;;
		"M ")	mp3enc raw
			rawsub
			;;
		"O ")	oggencode raw
			rawsub
			;;
		"P ")	noswm players $RAWPLAYER || rawsub
			startplay raw
			rawsub
			;;
		"S ")	swapraw
			rawsub
			;;
		"R ")	startrec raw
			rawsub
			;;
		"X ")	$MIXER
			rawsub
			;;
	esac
}
wavsub(){
	rm -f $TMPO
	if ! $DIALOG --menu\
		"           WAV Options (${PR[4]}*.wav)" 14 65 7\
		"C "  "WAV  ->  CD-R/CD-RW            <cdrdao>   "\
		"R "  "WAV  ->  RAW                   <$(basename $WAV2RAWCONVERTER)>      "\
		"M "  "WAV  ->  MP3                   <$(basename $MP3ENCODER)> "\
		"O "  "WAV  ->  OGG                   <$(basename $OGGENCODER)> "\
		"P "  "Player                         <$(basename $WAVPLAYER)>      "\
		"S "  "Record from /dev/dsp           <$(basename $WAVRECORDER)>      "\
		"X "  "Mixer                          <$(basename $MIXER)>    "\
		2> $TMP
	then
		mainm
	fi
	case $(cat $TMP) in
		"C ")	nosfilesm wav || wavsub
			wav2cd
			wavsub
			;;
		"R ")	nosfilesm wav || wavsub
			wav2raw
			wavsub
			;;
		"M ")	mp3enc wav
			wavsub
			;;
		"O ")	oggencode wav
			wavsub
			;;
		"P ")	noswm players $WAVPLAYER || wavsub
			startplay wav
			wavsub
			;;
		"S ")	noswm recorders $WAVRECORDER Recording || wavsub
			startrec wav
			wavsub
			;;
		"X ")	$MIXER
			wavsub
			;;
	esac
}
mp3sub(){
	rm -f $TMPO
	if ! $DIALOG --menu\
		"         MP3 Options (${PR[4]}*.mp3)" 12 65 5\
		"C "  "MP3    ->    CD-R/CD-RW     <$(basename $MP3DECODER) | cdrdao> "\
		"R "  "MP3    ->    RAW            <$(basename $MP3DECODER) | sox>    "\
		"W "  "MP3    ->    WAV            <$(basename $MP3DECODER)>          "\
		"P "  "Player                      <$(basename $MP3PLAYER)>          "\
		"X "  "Mixer                       <$(basename $MIXER)>           "\
		2> $TMP
	then
		mainm
	fi
	case $(cat $TMP) in
		"C ")	nosfilesm mp3 || mp3sub
			noswm decoders $MP3DECODER Decoding || mp3sub
			mp3dec wav || mp3sub
			mktoc *.wav || mp3sub
			dao " " $TMP.toc "a" || mp3sub
			mp3sub
			;;
		"R ")	mp3dec raw
			mp3sub
			;;
		"W ")	mp3dec wav
			mp3sub
			;;
		"P ")	noswm players $MP3PLAYER || mp3sub
			startplay mp3
			mp3sub
			;;
		"X ")	$MIXER
			mp3sub
			;;
	esac
}
oggsub(){
	rm -f $TMPO
	if ! $DIALOG --menu\
		"        OGG Options (${PR[4]}*.ogg)" 12 65 5\
		"C "  "OGG    ->    CD-R/CD-RW        <$(basename $OGGDECODER) | cdrdao> "\
		"R "  "OGG    ->    RAW               <$(basename $OGGDECODER)>          "\
		"W "  "OGG    ->    WAV               <$(basename $OGGDECODER)>          "\
		"P "  "Player                         <$(basename $OGGPLAYER)>          "\
		"X "  "Mixer                          <$(basename $MIXER)>           "\
		2> $TMP
	then
		mainm
	fi
	case $(cat $TMP) in
		"C ")	nosfilesm ogg || oggsub
			noswm decoders $OGGDECODER Decoding || oggsub
			oggdecode wav || oggsub
			mktoc *.wav || oggsub
			dao " " $TMP.toc "a"
			oggsub
			;;
		"R ")	nosfilesm ogg || oggsub
			oggdecode raw
			oggsub
			;;
		"W ")	nosfilesm ogg || oggsub
			oggdecode wav
			oggsub
			;;
		"P ")	noswm players $OGGPLAYER || oggsub
			startplay ogg
			oggsub
			;;
		"X ")	$MIXER
			oggsub
			;;
	esac
}
m3usub(){
	if ! $DIALOG --menu\
		"       Playlist Options" 12 65 5\
		"C "  "M3U    ->   Audio CD-R/CD-RW             <cdrdao> "\
		"D "  "M3U    ->   Data CD-R/CD-RW  <mkisofs | cdrecord> "\
		"R "  "M3U    ->   RAW                                   "\
		"W "  "M3U    ->   WAV                                   "\
		"M "  "Create M3U playlist file from the contents of...  "\
		2> $TMP
	then
		mainm
	fi
	unset PLA
	case $(cat $TMP) in
		"C ")	getm3u || m3usub
			m3u2acd
			m3usub
		;;
		"D ")	getm3u || m3usub
			m3u2cd
			m3usub
		;;
		"R ")	getm3u || m3usub
			m3u2raw
			m3usub
		;;
		"W ")	getm3u || m3usub
			m3u2wav
			m3usub
		;;
		"M ")	$DIALOG --menu\
				"           Create M3U playlist file from the contents of:" 10 55 3\
				"S "	" ${PR[4]}    "\
				"C "	" ${PR[5]}    "\
				"O "	" Browse...   "\
			2>$TMP || m3usub
			case $(cat $TMP) in
				"S ")	/bin/ls -1 ${PR[4]}*.wav ${PR[4]}*.mp3 ${PR[4]}*.ogg 2>/dev/null >$TMPO
					awk 'BEGIN{print "#EXTM3U"}{print "#EXTINF:\n"$1}' $TMPO > $TMP
					$DIALOG --inputbox "Enter M3U playlist filename:" 8 65 "${PR[4]}.m3u" 2>$TMPO || m3usub
					cp -f $TMP $(cat $TMPO)
				;;
				"C ")	/bin/ls -1 ${PR[5]}*.wav ${PR[5]}*.mp3 ${PR[5]}*.ogg 2>/dev/null >$TMPO
					awk 'BEGIN{print "#EXTM3U"}{print "#EXTINF:\n"$1}' $TMPO > $TMP
					$DIALOG --inputbox "Enter M3U playlist filename:" 8 65 "${PR[5]}.m3u" 2>$TMPO || m3usub
					cp -f $TMP $(cat $TMPO)
				;;
				"O ")	$DIALOG --fselect "$HOME/" 20 75 2>$TMP || m3usub
					/bin/ls -1 $(cat $TMP)*.wav $(cat $TMP)*.mp3 $(cat $TMP)*.ogg 2>/dev/null >$TMPO
					awk 'BEGIN{print "#EXTM3U"}{print "#EXTINF:\n"$1}' $TMPO > $TMP
					$DIALOG --inputbox "Enter M3U playlist filename:" 8 65 "$HOME/.m3u" 2>$TMPO || m3usub
					cp -f $TMP $(cat $TMPO)
				;;
			esac
			m3usub
		;;
	esac
}
miscsub(){
	if ! $DIALOG --menu\
		"       Miscellaneous Options" 20 65 13\
		"F "  "Blank all data on the CD-RW in <${CDRW[0]}>  (full) "\
		"B "  "Blank PMA, TOC, pregap on the CD-RW in <${CDRW[0]}> "\
		"T "  "Blank track  on the CD-RW in <${CDRW[0]}>           "\
		"N "  "Unreserve track on the CD-RW in <${CDRW[0]}>        "\
		"A "  "Blank a track tail on the CD-RW in <${CDRW[0]}>     "\
		"U "  "Unclose last session on the CD-RW in <${CDRW[0]}>   "\
		"S "  "Blank last session on the CD-RW in <${CDRW[0]}>     "\
		"C "  "Close (fixate) medium in <${CDRW[0]}>               "\
		"D "  "File Cleanup                                       "\
		"R "  "CD-ROM medium Info  <${CDROM[0]}>                     "\
		"W "  "CD-RW medium Info  <${CDRW[0]}>                     "\
		"I "  "Installed Software                                 "\
		"L "  "(Re)Load $TCDRRC                               "\
		2> $TMP
	then
		mainm
	fi
	case $(cat $TMP) in
		"F ")	blankfull
			miscsub
			;;
		"B ")	blankfast
			miscsub
			;;
		"T ")	blanktrack
			miscsub
			;;
		"N ")	unreservetrack
			miscsub
			;;
		"A ")	blanktracktail
			miscsub
			;;
		"U ")	blankunclose
			miscsub
			;;
		"S ")	blanklastsession
			miscsub
			;;
		"C ")	fixate
			miscsub
			;;
		"D ")	cleanup
			miscsub
			;;
		"R ")	diskinfo ${CDROM[0]} ${CDROM[3]} ${CDROM[2]}
			miscsub
			;;
		"W ")	diskinfo ${CDRW[0]} ${CDRW[3]} ${CDRW[2]}
			miscsub
			;;
		"I ")	show_sw
			miscsub
			;;
		"L ")	cd $TCDRPATH
			src_tcdrrc
			splash
			miscsub
			;;
	esac
}
######### Main menu: #########################################################
mainm(){
	if ! $DIALOG --menu\
		"          Source Medium     ->    Destination Medium       " 20 75 13\
		"I "	"  Directory         ->    CD-R/CD-RW/ISO/ZISO               "\
        	"D "	"  Data CD / ISO     ->    CD-R/CD-RW/ISO/ZISO  +  Overburn  "\
		"X "	"  Mixed mode CD     ->    CD-R/CD-RW                        "\
		"S "	"  Add a session     ->    Multi-session CD-R/CD-RW          "\
		"B "	"  Bootable image    ->    El Torito boot CD-R/CD-RW         "\
		"A "	"  Audio CD          ->    CD-R/CD-RW/RAW/WAV/MP3            "\
		"R "	"  RAW/CDR           ->    CD-R/CD-RW/WAV/MP3                "\
		"W "	"  WAV               ->    CD-R/CD-RW/RAW/MP3                "\
		"P "	"  MP3               ->    CD-R/CD-RW/RAW/WAV                "\
		"O "	"  OGG               ->    CD-R/CD-RW/RAW/WAV                "\
		"L "	"  M3U               ->    CD-R/CD-RW/RAW/WAV                "\
		"M "	"  Miscellaneous           Blank / Fixate / Cleanup / Info   "\
		"T "	"  Reload TCDR             Developer friendly interface 8^)  "\
		2> $TMP
	then
		callsp || mainm
		rm -f $TMP $TMPO
		#reset
		setleds +num
		exit 0
	fi
	case $(cat $TMP) in
		"I ")	dirsub
			;;
		"D ")	dcdsub
			;;
		"X ")	mixcdsub
			;;
		"S ")	msdsub
			;;
		"B ")	bootcdsub
			;;
		"A ")	acdsub
			;;
		"R ")	rawsub
			;;
		"W ")	wavsub
			;;
		"P ")	mp3sub
			;;
		"O ")	oggsub
			;;
		"L ")	m3usub
			;;
		"M ")	miscsub
			;;
		"T ")	. $TCDRPATH$(basename $0)
			;;
	esac
}
######### Main: ##############################################################

	detect_sw
	startscr
	detect_scsi
#	src_tcdrrc		# If auto-detection fails, uncomment this line and edit /etc/tcdrrc
	ckpath
	splash
	mainm

	exit $?

######### TCDR End.

