#!/bin/bash

#    cdwrite 3.5.1 reads and writes CDs/DVDs and encodes audio data to MP3/OGG/FLAC
#    cdwrite: Copyright (C) 2001-2015 Cezary M. Kruk <c.kruk@bigfoot.com>
#    cdcopy:  Copyright (C) 1999 H. P. de Vries <h.p.d.vries@stud.tue.nl>
#
#    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 3 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, see <http://www.gnu.org/licenses/>.

# Configuration --------------------------------------------------------------

version="3.5.1"
timeline="2001-2015"

copyright="cdwrite $version by Cezary M. Kruk <c.kruk@bigfoot.com> ($timeline)"

reset
echo "$copyright"
echo "cdwrite: reads and writes CDs/DVDs and encodes audio data to MP3s/OGGs"
echo

if [ "$1" == "" ]
then
    echo -e "\033[4mIf the shell prompts you to type the password don't do it but read INSTALL file.\033[0m"
    echo -e "\033[4mFor the work of the shell the crucial is the usage of sudo and /etc/sudoers.\033[0m"
    echo
fi

path=`echo $0 | sed 's/cdwrite$//'`

local1=${path}.cdwriterc
local2=${path}cdwrite.conf
local3=$HOME/.cdwriterc
global1=/usr/local/etc/cdwrite.conf
global2=/etc/cdwrite.conf
global3=/opt/cdwrite/etc/cdwrite.conf

echo "Reading configuration. Please wait a while..."
echo

if [ -e $local1 ]
then
    . $local1
    configuration=$local1
elif [ -e $local2 ]
then
    . $local2
    configuration=$local2
elif [ -e $local3 ]
then
    . $local3
    configuration=$local3
elif [ -e $global1 ]
then
    . $global1
    configuration=$global1
elif [ -e $global2 ]
then
    . $global2
    configuration=$global2
elif [ -e $global3 ]
then
    . $global3
    configuration=$global3
else
    echo "There is no configuration file:"
    echo "    $local3,"
    echo "    $global1,"
    echo "    $global2,"
    echo "    nor $global3."
    exit
fi

if [ "$debug" == "on" ]
then
    reset="echo"
elif [ "$debug" == "off" ]
then
    reset="reset"
fi

tempfile=`mktemp $tempfile.XXXXXX`

if [ -f $tempfile ]
then
    rm $tempfile 2> /dev/null
    if [ -e $tempfile ]
    then
	echo "Cannot remove $tempfile."
	exit
    fi
fi

> $tempfile 2> /dev/null
if [ ! -f $tempfile ]
then
    echo "Cannot create $tempfile."
    exit
fi

# Defaults -------------------------------------------------------------------

device=$device1
reader=$reader1
writer=$writer1
number=$number1
driver=$driver1
dvd_rw=$dvd_rw1
speed=$speed1
buffers=$buffers1
cd_dir=$cd_dir1
dvd_dir=$dvd_dir1

# Commands -------------------------------------------------------------------

eject_media()
{
    if [ "$eject" == "on" ]
    then
	$eject_command 2> /dev/null
	$eject_command 2> /dev/null
    elif [ "$eject" == "off" ]
    then
	echo -n
    fi
}

display_message()
{
    echo
    echo "$message"
    $command
}

general_commands()
{
    # cdparanoia commands
    cdparanoia_test="cdparanoia -d $reader -Q"
    cdparanoia_read="cdparanoia -d $reader -B"

    # cdda2wav command
    if [ "$number" != "" ]
    then
	cdda2wav_indexes="$cdda2wav_call -v indices -B -J -D $number"
    else
	cdda2wav_indexes=""
    fi
    cdda2wav_read="cdda2wav -D $reader -x -paranoia -t"

    # cdrdao commands
    cdrdao_test="cdrdao disk-info --device $reader --driver $driver"
    cdrdao_write="$cdrdao_call write --device $writer --driver $driver --buffers $buffers --speed $speed"
    cdrdao_copy="$cdrdao_call copy --device $writer --driver $driver --buffers $buffers --speed $speed"

    # mkisofs command
    mkisofs_read_linux="$mkisofs_call -R"
    mkisofs_read_windows="$mkisofs_call -r -J -D"

    # cdrecord commands
    cdrecord_test_toc="cdrecord -toc dev=$reader"
    cdrecord_scan="$cdrecord_call dev=$reader -scanbus"
    cdrecord_test_msinfo="cdrecord -msinfo dev=$reader"
    cdrecord_write="$cdrecord_call -v dev=$writer"

    # growisofs commands
    if [ "$dvd_rw" != "" ]
    then
	growisofs_zero="$growisofs_call -Z $dvd_rw -R -J"
	growisofs_merge="$growisofs_call -M $dvd_rw -R -J"
	growisofs_iso="$growisofs_call -Z $dvd_rw=$iso_file"
	growisofs_erase="$growisofs_call -Z $dvd_rw=/dev/zero"
    else
	growisofs_zero=""
	growisofs_merge=""
	growisofs_iso=""
	growisofs_erase=""
    fi

    # dvd+rw-* commands
    if [ "$dvd_rw" != "" ]
    then
	dvd_rw_info_test="dvd+rw-mediainfo $dvd_rw"
	dvd_rw_blank="$dvdrw_format_call -blank $dvd_rw"
	dvd_rw_format="$dvdrw_format_call -force $dvd_rw"
#	cdrecord_blank_fast="$cdrecord_call -v dev=$dvd_rw speed=12 blank=fast"
#	cdrecord_blank_all="$cdrecord_call -v dev=$dvd_rw speed=12 blank=all"
    else
	dvd_rw_info_test=""
	dvd_rw_blank=""
	dvd_rw_format=""
#	cdrecord_blank_fast=""
#	cdrecord_blank_all=""
    fi
}

ripping_commands()
{
    # cdparanoia and cdda2wav encoding commands
    cdparanoia_encoding="cdparanoia -d $reader -B $track -"
    cdda2wav_encoding="cdda2wav -D $reader -x -paranoia -t $track -"
}

general_commands

# Tests ----------------------------------------------------------------------

exit="no"

echo

for program in cdda2wav
do
    if [ "`which $program 2> /dev/null`" == "" ]
    then
	echo "There is no \"$program\" program."
	echo "Install \"cdrtools\" or \"icedax\"  package."
	echo
	exit="yes"
    fi
done

for program in cdrecord
do
    if [ "`which $program 2> /dev/null`" == "" ]
    then
	echo "There is no \"$program\" program."
	echo "Install \"cdrtools\" or \"vodim\" package."
	echo
	exit="yes"
    fi
done

for program in mkisofs isoinfo
do
    if [ "`which $program 2> /dev/null`" == "" ]
    then
	echo "There is no \"$program\" program."
	echo "Install \"cdrtools\" or \"genisoimage\" package."
	echo
	exit="yes"
    fi
done

if [ "$dvd_rw" != "" ]
then
    for program in growisofs dvd+rw-format dvd+rw-mediainfo
    do
	if [ "`which $program 2> /dev/null`" == "" ]
	then
	    echo "There is no \"$program\" program."
	    echo "Install \"dvd+rw-tools\" package."
	    echo
	    exit="yes"
	fi
    done
fi

for program in oggenc
do
    if [ "`which $program 2> /dev/null`" == "" ]
    then
	echo "There is no \"$program\" program."
	echo "Install \"vorbis-tools\" package."
	echo
	exit="yes"
    fi
done

for program in cdda-player
do
    if [ "`which $program 2> /dev/null`" == "" ]
    then
	echo "There is no \"$program\" program."
	echo "Install \"libcdio\" or \"libcdio-utils\" package."
	echo
	exit="yes"
    fi
done

for program in cdparanoia cdrdao lame mp3gain mplayer perl gawk dialog
do
    if [ "`which $program 2> /dev/null`" == "" ]
    then
	echo "There is no \"$program\" program."
	echo "Install \"$program\" package."
	echo
	exit="yes"
    fi
done

if [ "$exit" == "yes" ]
then
    display_message
    rm -f $tempfile
    exit
fi

exit="yes"

for program in normalize normalize-audio
do
    if [ "`which $program 2> /dev/null`" != "" ]
    then
	exit="no"
    fi
done

if [ "$exit" == "yes" ]
then
    echo "There is no \"normalize\" nor \"normalize-audio\" program."
    echo "Install \"normalize\" or \"normalize-audio\" package."
    echo
    display_message
    rm -f $tempfile
    exit
fi

# Presets --------------------------------------------------------------------

backtitle="$copyright"
masterretval=0
retval=0

shopt -s expand_aliases
alias c='perl -e '\''$_="@ARGV"; print eval $_, "\n"'\'''

# Cleaning -------------------------------------------------------------------

for mounted_device in $cd_dir $dvd_dir
do
    if [ "`mount | grep $mounted_device`" != "" ]
    then
	$umount_call $mounted_device 2> /dev/null
    fi
done

if [ -e $tempfile ]
then
    rm -f $tempfile
fi

# Functions ------------------------------------------------------------------

main_menu()
{
    dialog --backtitle "$backtitle" \
    --help-button \
    --title "Main Menu" \
    --menu "Select the option:" 22 62 15 \
    1 "Preferences" \
    2 "Audio CD" \
    3 "Data CD" \
    4 "DVD-R/DVD-RW Disc" \
    5 "About cdwrite" \
    6 "Exit" \
    2> $tempfile
    masterretval=$?
    output=`cat $tempfile`
    if [ "`echo $output | grep 'HELP '`" != "" ]
    then
	cat << EOF > $tempfile

Dialog Usage

  Navigation
    to select an option use up and down arrow keys; to
    select OK, Cancel, or Help button use left and right
    arrow keys; to use a highlighted button press Enter.

  Checklists and radiolists
    to switch a checkbox [ ] or select a radiobox ( ) use
    the space key.

  Shortcuts
    to select an option from the list quickly use the
    first digit or the first letter of its name.

  Directory selection
    to select the directory write its name followed by /,
    for example: ./ is for the current directory, ./../
    is for the directory above the current directory, and
    /home/ is for /home directory; it is enough to write
    one or a few first letters of the name of the
    directory to highlight it and then press the space
    key to select that directory; to move the highlight
    use Tab and arrow keys.

  File selection
    to select the file write one or a few first letters
    of its name to highlight it and then press the space
    key to select that file; to move the highlight use
    Tab and arrow keys.
EOF
	dialog --backtitle "$backtitle" \
	--clear --title "Dialog Usage Help" $@ --textbox "$tempfile" 22 62
	retval=$?
	main_menu
    fi
}

preferences_submenu() # 1.
{
    dialog --backtitle "$backtitle" \
    --help-button \
    --title "Preferences Menu" \
    --menu "Select the option:" 22 62 15 \
    1 "Select device" \
    2 "Select speed" \
    3 "Scan SCSI devices" \
    4 "Umount device" \
    5 "Display config" \
    2> $tempfile
    retval=$?
    output=`cat $tempfile`
    if [ "`echo $output | grep 'HELP '`" != "" ]
    then
	cat << EOF > $tempfile

Preferences Menu

  1 Select device
    allows to select between up to five devices (first is
    default).

  2 Select speed
    allows to select recording speed from 1x to 56x (12x
    is default).

  3 Scan SCSI devices
    scans for all SCSI devices.

  4 Umount device
    umounts a mounted device -- an option useful in
    systems that mounts automatically inserted CD or DVD
    media.

  5 Display config
    displays the configuration settings.
EOF
	dialog --backtitle "$backtitle" \
	--clear --title "Preferences Menu Help" $@ --textbox "$tempfile" 22 62
	retval=$?
	preferences_submenu
    fi
}

audio_cd_submenu() # 2.
{
    dialog --backtitle "$backtitle" \
    --help-button \
    --title "Audio CD Menu" \
    --menu "Select the option:" 22 62 15 \
    1 "Audio CD media info" \
    2 "Read audio CD tracks" \
    3 "Write WAV files" \
    4 "Rename WAV files" \
    5 "Remove WAV, INF, and TOC files" \
    6 "Copy audio CD" \
    7 "Write BIN file" \
    8 "Remove BIN and TOC files" \
    9 "Encode WAV files or CD tracks" \
    10 "Normalize FLAC, MP3, OGG, and WAV files" \
    11 "Change gain (MP3 VBR or 192 and more kb/s)" \
    12 "Convert audio files" \
    13 "Play audio files" \
    14 "Play audio CD" \
    15 "Blank CD-RW disc" \
    2> $tempfile
    retval=$?
    output=`cat $tempfile`
    if [ "`echo $output | grep 'HELP '`" != "" ]
    then
	cat << EOF > $tempfile

Audio CD Menu

  1 Audio CD media info
    displays audio CD media information.

  2 Read audio CD tracks
    reads audio tracks from the CD as well as the indexes
    and CD-TEXT and writes them to WAV files and
    temporary files -- it can compile up to 9999 tracks
    into the same directory (by default it uses for
    ripping cdparanoia but it allows to use cdda2wav
    too).

        1 Read entire CD
        2 Read selected tracks

  3 Write WAV files
    writes WAV files from the current directory to the
    audio CD taking into consideration indexes and
    CD-TEXT if available.

  4 Rename WAV files
    renames WAV files using CDDB data; WAV files for
    renaming have to have the names compatible with the
    following standard: ????.wav where ???? is a
    four-digit number; renaming works for complete audio
    CDs.

  5 Remove WAV, INF, and TOC files
    removes all WAV, INF, and TOC files.

  6 Copy audio CD
    copies audio CD creating temporary audio BIN file
    instead of WAV files and using the same drive for
    reading and writing; in the case of the discs using
    Copy Control technology reading tracks to WAV files
    and writing WAV files to the audio CD gives better
    results.

  7 Write BIN file
    writes audio BIN file to the consecutive audio CDs;
    the user has to select the appropriate TOC file.

  8 Remove BIN and TOC files
    removes all BIN and TOC files.

  9 Encode WAV files or CD tracks
    encodes WAV files or audio CD to FLAC, MP3, or OGG
    files using cdparanoia or cdda2wav and flac, lame, or
    oggenc and offering different compressions as well as
    constant and variable bitrates; it can also use CDDB
    data and set ID3 tags (by default it uses cdparanoia,
    lame and variable bitrate preset --r3mix).

 10 Normalize FLAC, MP3, OGG, and WAV files
    adjusts volume levels of FLAC, MP3, OGG, and WAV
    files; MP3 and OGG files normalization relies on
    decoding and consecutive encoding of the audio files
    with the new bitrate so the sound quality of the
    resulting files can deteriorate (by default both MP3
    and OGG files normalization is disabled when the
    normalization is performed by normalize* program but
    it is enabled for MP3 files when the normalization is
    performed by mp3gain program).

 11 Change gain (MP3 VBR or 192 and more kb/s)
    changes the gain of the MP3 files -- it works with
    the files using constant 192 kb/s compression bitrate
    or the higher one as well as with the variable
    bitrates -- both using stereo mode instead of
    joint-stereo one; in the case of the overloaded files
    start from -1 dB and go down if necessary.

 12 Convert audio files
    converts audio files from APE, FLAC, M4A, MP3, MPC,
    OGG, WAV, or WMA formats to FLAC, MP3, OGG, or WAV
    formats using musica script (that option uses musica
    script settings for flac, lame, and oggenc
    compressions, bitrates, and qualities).

 13 Play audio files
    plays audio files in the following formats: APE,
    FLAC, M4A, MP3, MPC, OGG, WAV, and WMA.

 14 Play audio CD
    plays audio CDs using cdda-player or mplayer; the
    first plays CDs smoothly but with some devices does
    not produce the sound -- the second works with all
    devices but inserts gaps between the tracks (default
    player is mplayer for compatibility reasons).

 15 Blank CD-RW disc
    blanks CD-RW discs using one of the seven possible
    options (fast blanking is default).

        1 Blank the disc fast
        2 Blank the entire disc
        3 Blank the last track
        4 Unreserve a reserved track
        5 Blank the tail of the track
        6 Unclose the last session
        7 Blank the last session
EOF
	dialog --backtitle "$backtitle" \
	--clear --title "Audio CD Menu Help" $@ --textbox "$tempfile" 22 62
	retval=$?
	audio_cd_submenu
    fi
}

data_cd_submenu() # 3.
{
    dialog --backtitle "$backtitle" \
    --help-button \
    --title "Data CD Menu" \
    --menu "Select the option:" 22 62 15 \
    1 "Data CD media info" \
    2 "Create ISO image" \
    3 "Write ISO image" \
    4 "Read data CD" \
    5 "Link ISO file to $iso_file" \
    6 "Remove ISO image" \
    7 "Blank CD-RW disc" \
    2> $tempfile
    retval=$?
    output=`cat $tempfile`
    if [ "`echo $output | grep 'HELP '`" != "" ]
    then
	cat << EOF > $tempfile

Data CD Menu

  1 Data CD media info
    displays data CD media information.

  2 Create ISO image
    creates image.iso ISO image of the selected directory
    in single session or multi session mode following the
    symbolic links if required and setting the parameters
    for Linux or Windows compatibility (Linux
    compatibility is default).

        1 First session ISO image
        2 Next session ISO image

  3 Write ISO image
    writes image.iso ISO image from the current directory
    to the disc; the valid name of that image is
    image.iso so in order to burn the image using the
    other name symbolic link image.iso is required.

        1 Multi session writing mode
        2 Closing session writing mode (see: BUGS)

  4 Read data CD
    reads data CD and stores ISO image in image.iso file.

  5 Link ISO file to image.iso
    links ISO file for burning to image.iso file.

  6 Remove ISO image
    removes ISO image image.iso file.

  7 Blank CD-RW disc
    blanks CD-RW discs using one of the seven possible
    options (fast blanking is default).

        1 Blank the disc fast
        2 Blank the entire disc
        3 Blank the last track
        4 Unreserve a reserved track
        5 Blank the tail of the track
        6 Unclose the last session
        7 Blank the last session
EOF
	dialog --backtitle "$backtitle" \
	--clear --title "Data CD Menu Help" $@ --textbox "$tempfile" 22 62
	retval=$?
	data_cd_submenu
    fi
}

dvdr_dvdrw_disc_submenu() # 4.
{
    dialog --backtitle "$backtitle" \
    --help-button \
    --title "DVD-R/DVD-RW Disc Menu" \
    --menu "Select the option:" 22 62 15 \
    1 "DVD media info" \
    2 "Write first session" \
    3 "Write next session" \
    4 "Write ISO image" \
    5 "Read data DVD" \
    6 "Link ISO file to $iso_file" \
    7 "Remove ISO image" \
    8 "Blank DVD disc" \
    9 "Format DVD disc" \
    10 "Erase DVD disc" \
    2> $tempfile
    retval=$?
    output=`cat $tempfile`
    if [ "`echo $output | grep 'HELP '`" != "" ]
    then
	cat << EOF > $tempfile

DVD-R/DVD-RW Disc Menu

  1 DVD media info
    displays DVD media information.

  2 Write first session
    writes the first session on DVD media; with DVD-RW
    media it overwrites the previous sessions so blanking
    the disc is not required.

  3 Write next session
    writes next session on DVD media.

  4 Write ISO image
    writes ISO image on DVD media; the valid name of that
    image is image.iso so in order to burn the image
    using the other name symbolic link image.iso is
    required.

  5 Read data DVD
    reads data DVD and stores ISO image in image.iso
    file.

  6 Link ISO file to image.iso
    links ISO file for burning to image.iso file.

  7 Remove ISO image
    removes ISO image image.iso file.

  8 Blank DVD disc
    blanks DVD media fast.

  9 Format DVD disc
    formats DVD media fast.

 10 Erase DVD disc
    erases all DVD media data.
EOF
	dialog --backtitle "$backtitle" \
	--clear --title "DVD-R/DVD-RW Disc Menu Help" $@ --textbox "$tempfile" 22 62
	retval=$?
	dvdr_dvdrw_disc_submenu
    fi
}

about_cdwrite_submenu() # 5.
{
    cat << EOF > $tempfile

cdwrite is a console shell for reading, writing, and
copying audio CDs as well as for creating and writing ISO
images on CD media and data or ISO images on DVD media.
It can also encode WAV files or audio CDs to FLAC, MP3,
or OGG files, using optionally CDDB data and setting ID3
tags.

Users that prefer command line interfaces can use cdwr.*,
cdencode, and settags scripts instead of cdwrite.

To use cdwrite and the scripts the user have to have the
basic understanding of the processes of reading and
writing CD and DVD media as well as of the processes of
encoding audio data or at least he or she have to want to
get to know these processes during the work with cdwrite.

The program is distributed under the terms of the GNU
General Public License version 3.

The initial version of cdwrite was based on cdcopy by H.
P. de Vries.
EOF
    dialog --backtitle "$backtitle" \
    --clear --title "About cdwrite" $@ --textbox "$tempfile" 22 62
    retval=$?
}

select_device() # 1.1.
{
	device_old="$device"
	> $tempfile
	echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
	echo --title \"Device\" \\ >> $tempfile
	echo --radiolist \"Select device:\" 22 62 15 \\ >> $tempfile
	if [ "$device1" != "" ]
	then
	    echo -n "\"$device1\" \" \" " >> $tempfile
	    if [ "$device" == "$device1" ]
	    then
		echo -n "on " >> $tempfile
	    else
		echo -n "off " >> $tempfile
	    fi
	    echo 2\> $tempfile \\ >> $tempfile
	fi
	if [ "$device2" != "" ]
	then
	    echo -n "\"$device2\" \" \" " >> $tempfile
	    if [ "$device" == "$device2" ]
	    then
		echo -n "on " >> $tempfile
	    else
		echo -n "off " >> $tempfile
	    fi
	    echo 2\> $tempfile \\ >> $tempfile
	fi
	if [ "$device3" != "" ]
	then
	    echo -n "\"$device3\" \" \" " >> $tempfile
	    if [ "$device" == "$device3" ]
	    then
		echo -n "on " >> $tempfile
	    else
		echo -n "off " >> $tempfile
	    fi
	    echo 2\> $tempfile \\ >> $tempfile
	fi
	if [ "$device4" != "" ]
	then
	    echo -n "\"$device4\" \" \" " >> $tempfile
	    if [ "$device" == "$device4" ]
	    then
		echo -n "on " >> $tempfile
	    else
		echo -n "off " >> $tempfile
	    fi
	    echo 2\> $tempfile \\ >> $tempfile
	fi
	if [ "$device5" != "" ]
	then
	    echo -n "\"$device5\" \" \" " >> $tempfile
	    if [ "$device" == "$device5" ]
	    then
		echo -n "on " >> $tempfile
	    else
		echo -n "off " >> $tempfile
	    fi
	    echo 2\> $tempfile \\ >> $tempfile
	fi
	chmod u+x $tempfile
	$tempfile
	chmod u-x $tempfile
	retval=$?
	output=`cat $tempfile`
	device=`echo $output | sed 's/"//g'`
	if [ "$device" == "" ]
	then
	    device="$device_old"
	fi
	devices
}

devices()
{
	if [ "$device" == "$device1" ]
	then
	    reader=$reader1
	    writer=$writer1
	    number=$number1
	    driver=$driver1
	    dvd_rw=$dvd_rw1
	    speed=$speed1
	    buffers=$buffers1
	    cd_dir=$cd_dir1
	    dvd_dir=$dvd_dir1
	    general_commands
	fi
	if [ "$device" == "$device2" ]
	then
	    reader=$reader2
	    writer=$writer2
	    number=$number2
	    driver=$driver2
	    dvd_rw=$dvd_rw2
	    speed=$speed2
	    buffers=$buffers2
	    cd_dir=$cd_dir2
	    dvd_dir=$dvd_dir2
	    general_commands
	fi
	if [ "$device" == "$device3" ]
	then
	    reader=$reader3
	    writer=$writer3
	    number=$number3
	    driver=$driver3
	    dvd_rw=$dvd_rw3
	    speed=$speed3
	    buffers=$buffers3
	    cd_dir=$cd_dir3
	    dvd_dir=$dvd_dir3
	    general_commands
	fi
	if [ "$device" == "$device4" ]
	then
	    reader=$reader4
	    writer=$writer4
	    number=$number4
	    driver=$driver4
	    dvd_rw=$dvd_rw4
	    speed=$speed4
	    buffers=$buffers4
	    cd_dir=$cd_dir4
	    dvd_dir=$dvd_dir4
	    general_commands
	fi
	if [ "$device" == "$device5" ]
	then
	    reader=$reader5
	    writer=$writer5
	    number=$number5
	    driver=$driver5
	    dvd_rw=$dvd_rw5
	    speed=$speed5
	    buffers=$buffers5
	    cd_dir=$cd_dir5
	    dvd_dir=$dvd_dir5
	    general_commands
	fi
}

select_speed() # 1.2.
{
	speed_old="$speed"
	> $tempfile
	echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
	echo --title \"Speed\" \\ >> $tempfile
	echo --radiolist \"Select speed:\" 22 62 15 \\ >> $tempfile
	for value in 1 2 4 6 8 12 16 20 24 32 40 48 56
	do
	    if [ "$speed" == "$value" ]
	    then
		echo $value \" \" on \\ >> $tempfile
	    else
		echo $value \" \" off \\ >> $tempfile
	    fi
	done
	echo 2\> $tempfile >> $tempfile
	chmod u+x $tempfile
	$tempfile
	chmod u-x $tempfile
	retval=$?
	output=`cat $tempfile`
	if [ "$retval" = "0" ]
	then
	    speed="$output"
	fi
	if [ "$speed" == "" ]
	then
	    speed="$speed_old"
	fi
}

scan_scsi_devices() # 1.3.
{
	$reset
	echo "Executing: $cdrecord_scan"
	$cdrecord_scan
	display_message
}

umount_device() # 1.4.
{
	for mounted_device in $cd_dir $dvd_dir
	do
	    if [ "`mount | grep $mounted_device`" != "" ]
	    then
		$umount_call $mounted_device 2> /dev/null
	    fi
	done
}

display_config() # 1.5.
{
	$reset
	echo "$copyright"
	echo
	device_number $1 $2 $3
	config
	display_message
}

audio_cd_media_info() # 2.1.
{
	$reset
	echo "Reading table of contents. Please wait a while..."
	$cdparanoia_test 2> $tempfile
	lacking_drive=`cat $tempfile | grep "Unable to open cdrom drive"`
	lacking_disc=`cat $tempfile | grep "Unable to open disc"`
	if [ "$lacking_drive" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no \"$reader\" device.\
	    " 7 62
	    retval=$?
	elif [ "$lacking_disc" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no audio CD disc in \"$reader\" device.\
	    " 7 62
	    retval=$?
	else
	    $reset
	    echo "Executing: $cdparanoia_test"
	    $cdparanoia_test
	    display_message
	fi
}

read_audio_tracks() # 2.2.
{
	select_ripper
	dialog --backtitle "$backtitle" \
	--title "Audio Tracks" \
	--menu "Read audio tracks:" 22 62 15 \
	1 "Read entire CD" \
	2 "Read selected tracks" \
	2> $tempfile
	retval=$?
	output=`cat $tempfile`
}

select_ripper()
{
	> $tempfile
	echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
	echo --title \"Ripper\" \\ >> $tempfile
	echo --radiolist \"Select ripper:\" 22 62 15 \\ >> $tempfile
	if [ "$default_ripper" == "cdparanoia" ]
	then
	    echo \"cdparanoia\" \" \" on \\ >> $tempfile
	    echo \"cdda2wav\" \" \" off 2\> $tempfile >> $tempfile
	elif [ "$default_ripper" == "cdda2wav" ]
	then
	    echo \"cdparanoia\" \" \" off \\ >> $tempfile
	    echo \"cdda2wav\" \" \" on 2\> $tempfile >> $tempfile
	fi
	chmod u+x $tempfile
	$tempfile
	chmod u-x $tempfile
	retval=$?
	ripper=`cat $tempfile`
	if [ "$ripper" != "" ]
	then
	    default_ripper=$ripper
	fi
}

read_entire_cd() # 2.2.1.
{
	$reset
	echo "Reading table of contents. Please wait a while..."
	$cdparanoia_test 2> $tempfile
	lacking_drive=`cat $tempfile | grep "Unable to open cdrom drive"`
	lacking_disc=`cat $tempfile | grep "Unable to open disc"`
	if [ "$lacking_drive" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no \"$reader\" device.\
	    " 7 62
	    retval=$?
	elif [ "$lacking_disc" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no audio CD in \"$reader\" device.\
	    " 7 62
	    retval=$?
	else
	    tracks=`cat $tempfile | grep -B 1 "TOTAL" | grep "\. " | cut -d "." -f 1`
	    if [ "$tracks" != "" ]
	    then
		declare -i track=1
		while [ "$track" -le "$tracks" ]
		do
		    trackfile=0001.wav
		    for prefix in `seq -w -s " " 1 1 9999`
		    do
			if [ -f "$trackfile" ]
			then
			    trackfile=$prefix.wav
			fi
		    done
		    $reset
		    if [ "$ripper" == "cdparanoia" ]
		    then
			echo "Executing: $nice_read $cdparanoia_read $track $trackfile"
			$nice_read $cdparanoia_read $track $trackfile
			mv track??.$trackfile $trackfile
		    elif [ "$ripper" == "cdda2wav" ]
		    then
			echo "Executing: $nice_read $cdda2wav_read $track $trackfile"
			$nice_read $cdda2wav_read $track $trackfile
		    fi
		    track=$track+1
		done
		if [ "$number" != "" ]
		then
		    $reset
		    echo "Reading indexes. Please wait a while..."
		    $cdda2wav_indexes
		    for file in audio_??.inf
		    do
			tracknumber=`echo $file | sed 's/audio_//;s/.inf//'`
			tracknumber=00$tracknumber
			> $tracknumber.ind
			for index in `cat $file | grep "Index=" | sed 's/Index=//'`
			do
			    if [ $index -gt 0 ]
			    then
				MM=`c int $index/75/60`
				SS=`c int \($index/75 - $MM*60\)`
				FF=`c int \(\($index/75 - $MM*60 - $SS + 0.0065\)*75\)`
				echo "INDEX $MM:$SS:$FF" >> $tracknumber.ind
			    fi
			done
			if [ "$remove_files" == "on" ]
			then
			    if [ ! -s $tracknumber.ind ]
			    then
				rm $tracknumber.ind
			    fi
			fi
		    done
		    if [ "$remove_files" == "on" ]
		    then
			for file in audio.*
			do
			    if [ -e $file ]
			    then
				rm -f $file
			    fi
			done
		    fi
		fi
		if [ -e $toc_file ]
		then
		    rm -f $toc_file
		fi
		$reset
		ls -N *.wav
		ls -N *.inf
		eject_media
		display_message
	    fi
	fi
}

read_selected_tracks() # 2.2.2.
{
	$reset
	echo "Reading table of contents. Please wait a while..."
	$cdparanoia_test 2> $tempfile
	lacking_drive=`cat $tempfile | grep "Unable to open cdrom drive"`
	lacking_disc=`cat $tempfile | grep "Unable to open disc"`
	if [ "$lacking_drive" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no \"$reader\" device.\
	    " 7 62
	    retval=$?
	elif [ "$lacking_disc" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no audio CD disc in \"$reader\" device.\
	    " 7 62
	    retval=$?
	else
	    $reset
	    echo "Reading table of contents. Please wait a while..."
	    $cdparanoia_test 2> $tempfile
	    tracks=`cat $tempfile | grep -B 1 "TOTAL" | grep "\. " | cut -d "." -f 1`
	    if [ "$tracks" != "" ]
	    then
		declare -i n=1
		> $tempfile
		echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
		echo --title \"Track\'s List\" \\ >> $tempfile
		echo --checklist \"Select tracks:\" 22 62 15 \\ >> $tempfile
		while [ $n -lt $tracks ]
		do
		    echo $n \" \" off \\ >> $tempfile
		    n=$n+1
		done
		echo $n \" \" off 2\> $tempfile >> $tempfile
		chmod u+x $tempfile
		$tempfile
		chmod u-x $tempfile
		echo "`cat $tempfile | sed 's/\"//g;s/$/ /'`" > $tempfile
		declare -i n=1
		track=`cat $tempfile | cut -d " " -f $n`
		while [ "$track" != "" ]
		do
		    trackfile=0001.wav
		    for prefix in `seq -w -s " " 1 1 9999`
		    do
			if [ -f "$trackfile" ]
			then
			    trackfile=$prefix.wav
			fi
		    done
		    $reset
		    if [ "$ripper" == "cdparanoia" ]
		    then
			echo "Executing: $nice_read $cdparanoia_read $track $trackfile"
			$nice_read $cdparanoia_read $track $trackfile
			mv track??.$trackfile $trackfile
		    elif [ "$ripper" == "cdda2wav" ]
		    then
			echo "Executing: $nice_read $cdda2wav_read $track $trackfile"
			$nice_read $cdda2wav_read $track $trackfile
		    fi
		    n=$n+1
		    track=`cat $tempfile | cut -d " " -f $n`
		done
		if [ -e $toc_file ]
		then
		    rm -f $toc_file
		fi
		$reset
		ls -N *.wav
		eject_media
		display_message
	    fi
	fi
}

write_audio_tracks() # 2.3.
{
	cddb="no"
	if [ "$number" == "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThe number of device is empty -- probably not plugged.\
	    " 7 62
	    retval=$?
	else
	    $reset
	    echo "Reading disc info. Please wait a while..."
	    $cdrdao_test > $tempfile 2>&1
	    lacking_drive=`cat $tempfile | grep "Cannot open SCSI device"`
	    lacking_disc=`cat $tempfile | grep "Unit not ready, giving up"`
	    empty_disc=`cat $tempfile | grep "CD-R empty           : no"`
	    if [ "$lacking_drive" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no \"$reader\" device.\
		" 7 62
		retval=$?
	    elif [ "$lacking_disc" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no data CD disc in \"$reader\" device.\
		" 7 62
		retval=$?
	    elif [ "$empty_disc" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThe data CD disc in \"$reader\" device is not empty.\
		" 7 62
		retval=$?
	    else
		file_test="no"
		for file in *.wav
		do
		    if [ -f "$file" ]
		    then
			file_test="yes"
		    fi
		done
		if [ "$file_test" = "yes" ]
		then
		    if [ ! -f $toc_file ]
		    then
			dialog --backtitle "$backtitle" \
			--title "CDDB Data" \
			--checklist "Read CDDB data:" 22 62 15 \
			"Update \"$toc_file\" using CDDB data?" " " off 2> $tempfile
			retval=$?
			output=`cat $tempfile`
			if [ "$output" = "\"Update \\\"$toc_file\\\" using CDDB data\\?\"" ]
			then
			    cddb="yes"
			fi
			dialog --backtitle "$backtitle" \
			--title "Pregaps" \
			--checklist "Confirm using pregaps:" 22 62 15 \
			"Use pregaps?" " " off 2> $tempfile
			retval=$?
			output=`cat $tempfile`
			pregaps="no"
			if [ "$output" = "\"Use pregaps\\?\"" ]
			then
			    > $tempfile
			    echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
			    echo --title \"Pregaps List\" \\ >> $tempfile
			    echo --checklist \"Select tracks:\" 22 62 15 \\ >> $tempfile
			    for file in *.wav
			    do
				n=`echo $file | sed 's/\([0-9]*\).*/\1/'`
				echo $n \" \" off \\ >> $tempfile
			    done
			    echo 2\> $tempfile >> $tempfile
			    chmod u+x $tempfile
			    $tempfile
			    chmod u-x $tempfile
			    echo "`cat $tempfile | sed 's/\"//g'`" > $tempfile
			    declare -i n=1
			    pregap_all=`cat $tempfile`
			    pregap_current=`echo $pregap_all | cut -d " " -f $n`
			fi
			toc_file
		    else
			if [ "$display_toc_file" == "on" ]
			then
			    $reset
			    cat $toc_file
			    display_message
			    dialog --title "Query" --clear "$@" \
			    --yesno "\nIs that TOC file correct?" \
			    7 62
			    retval=$?
			    if [ "$retval" == "1" ]
			    then
				dialog --title "Message" --clear "$@" \
				--msgbox "\nPreparing a new \"$toc_file\" TOC file.\
				" 7 62
				retval=$?
				rm -f $toc_file
				write_audio_tracks
			    fi
			fi
		    fi
		    overburn=""
		    dialog --backtitle "$backtitle" \
		    --title "Overburn" \
		    --checklist "Confirm using overburn:" 22 62 15 \
		    "Use overburn? (use it carefully)" " " off 2> $tempfile
		    retval=$?
		    output=`cat $tempfile`
		    if [ "$output" = "\"Use overburn\\? \\(use it carefully\\)\"" ]
		    then
			overburn="--overburn"
		    fi
		    $reset
		    echo "Executing: $nice_write $cdrdao_write $overburn $toc_file"
		    $nice_write $cdrdao_write $overburn $toc_file
		    eject_media
		    display_message
		else
		    dialog --title "Error" --clear "$@" \
		    --msgbox "\nThere are no \"*.wav\" files in the current directory.\
		    " 7 62
		    retval=$?
		fi
	    fi
	fi
}

toc_file()
{
	$reset
	echo "Preparing table of contents. Please wait a while..."
	> $tempfile
	if [ -e audio_01.inf ]
	then
	    for file in audio_*
	    do
		tracknumber=`echo $file | sed 's/audio_//;s/.inf//'`
		tracknumber=00$tracknumber
		album_perf=`cat $file | grep "Albumperformer=" | sed "s/Albumperformer=	//;s/^'//;s/'$//"`
		album_title=`cat $file | grep "Albumtitle=" | sed "s/Albumtitle=	//;s/^'//;s/'$//"`
		album_id=`cat $file | grep "CDINDEX_DISCID=" | sed "s/CDINDEX_DISCID=	//;s/^'//;s/'$//"`
		track_perf=`cat $file | grep "Performer=" | sed "s/Performer=	//;s/^'//;s/'$//"`
		track_title=`cat $file | grep "Tracktitle=" | sed "s/Tracktitle=	//;s/^'//;s/'$//"`
		track_isrc=`cat $file | grep "ISRC=" | sed "s/ISRC=		//;s/ //g"`
		track_isrc_main=`echo $track_isrc | sed "s/-//g"`
		track_length=`cat $file | grep "Tracklength=" | sed "s/Tracklength=	//;s/,.*//"`
		if [ $track_length -gt 0 ]
		then
		    MM=`c int $track_length/75/60`
		    SS=`c int \($track_length/75 - $MM*60\)`
		    FF=`c int \(\($track_length/75 - $MM*60 - $SS + 0.0065\)*75\)`
		    track_length="$MM:$SS:$FF"
		fi
		if [ "$track_length" == "::" ]
		then
		    track_length="0"
		fi
		echo "$track_length" > $tracknumber.lng
		if [ ! -e $tracknumber.txt ]
		then
		    if [ "$track_title" != "" -a "$track_perf" != "" ]
		    then
			if [ "$track_isrc_main" != "" ]
			then
			    echo "ISRC \"$track_isrc_main\"" > $tracknumber.txt
			fi
			echo "CD_TEXT {" >> $tracknumber.txt
			echo "  LANGUAGE 0 {" >> $tracknumber.txt
			echo "    TITLE \"$track_title\"" >> $tracknumber.txt
			echo "    PERFORMER \"$track_perf\"" >> $tracknumber.txt
			if [ "$track_isrc" != "" ]
			then
			    echo "    ISRC \"$track_isrc\"" >> $tracknumber.txt
			fi
			echo "  }" >> $tracknumber.txt
			echo "}" >> $tracknumber.txt
		    fi
		fi
	    done
	fi
	echo "CD_DA" > $tempfile
	if [ -e 0001.txt ]
	then
	    if [ "$album_title" != "" -a "$album_perf" != "" ]
	    then
		echo "CD_TEXT {" >> $tempfile
		echo "  LANGUAGE_MAP {" >> $tempfile
		echo "    0 : EN" >> $tempfile
		echo "  }" >> $tempfile
		echo "  LANGUAGE 0 {" >> $tempfile
		echo "                TITLE \"$album_title\"" >> $tempfile
		echo "                PERFORMER \"$album_perf\"" >> $tempfile
		echo "                DISC_ID \"$album_id\"" >> $tempfile
		echo "                UPC_EAN \"\"" >> $tempfile
		echo "              }" >> $tempfile
		echo "  }" >> $tempfile
	    fi
	fi
	for track in *.wav
	do
	    tracknumber=`echo $track | sed 's/\([0-9]*\).*/\1/'`
	    echo "" >> $tempfile
	    echo "// TRACK $tracknumber" >> $tempfile
	    echo "TRACK AUDIO" >> $tempfile
	    if [ -e $tracknumber.txt ]
	    then
		cat $tracknumber.txt >> $tempfile
	    fi
	    track_length=""
	    if [ -f $tracknumber.lng ]
	    then
		track_length="`cat $tracknumber.lng`"
	    fi
	    if [ "$track_length" = "" ]
	    then
		size=`ls -l "$track" | awk '{print $5}'`
		track_length=`c int \($size / 2352\)`
		MM=`c int $track_length/75/60`
		SS=`c int \($track_length/75 - $MM*60\)`
		FF=`c int \(\($track_length/75 - $MM*60 - $SS + 0.0065\)*75\)`
		track_length="$MM:$SS:$FF"
	    fi
	    track_length="0 $track_length"
	    echo "FILE \"$track\" $track_length" >> $tempfile
	    indexfile=$tracknumber.ind
	    if [ -e $indexfile ]
	    then
		cat $indexfile >> $tempfile
	    fi
	done
	cp -f $tempfile $toc_file
	if [ "$remove_files" == "on" ]
	then
	    for file in audio_*
	    do
		if [ -e $file ]
		then
		    rm -f $file
		fi
	    done
	fi
	if [ "$remove_files" == "on" ]
	then
	    for track in *.wav
	    do
		tracknumber=`echo $track | sed 's/\([0-9]*\).*/\1/'`
		if [ -e $tracknumber.txt ]
		then
		    rm $tracknumber.txt
		fi
		if [ -e $tracknumber.ind ]
		then
		    rm $tracknumber.ind
		fi
		if [ -e $tracknumber.lng ]
		then
		    rm $tracknumber.lng
		fi
		if [ -e $tracknumber.inf ]
		then
		    rm $tracknumber.inf
		fi
	    done
	fi
	if [ "$cddb" = "yes" ]
	then
	    $reset
	    echo "Reading CDDB data. Please wait a while..."
	    cdrdao read-cddb $toc_file
	fi
	if [ ! -s $tempfile ]
	then
	    dialog --title "Critical Error" --clear "$@" \
	    --msgbox "\nEmpty $tempfile.\nProbably there is no Internet connection.\
	    " 8 62
	    retval=$?
	    $reset
	    echo "$copyright"
	    echo
	    exit
	fi
	for track in *.wav
	do
	    tracknumber=`echo $track | sed 's/\([0-9]*\).*/\1/'`
	    if [ "$tracknumber" == "$pregap_current" ]
	    then
		sed -i "s/\(FILE \"$tracknumber.*\)/PREGAP $pregap\n\1/" $toc_file
		n=$n+1
		pregap_current=`echo $pregap_all | cut -d " " -f $n`
	    fi
	done
	if [ "$1" != "silent" ]
	then
	    if [ "$display_toc_file" == "on" ]
	    then
		if [ -f $toc_file ]
		then
		    $reset
		    cat $toc_file
		    display_message
		    dialog --title "Query" --clear "$@" \
		    --yesno "\nIs that TOC file correct?" \
		    7 62
		    retval=$?
		    if [ "$retval" == "1" ]
		    then
			dialog --title "Message" --clear "$@" \
			--msgbox "\nPreparing a new \"$toc_file\" TOC file.\
			" 7 62
			retval=$?
			rm -f $toc_file
			write_audio_tracks
		    fi
		fi
	    fi
	fi
}

rename_wav_files() # 2.4.
{
	file="no"
	for file in *.wav
	do
	    if [ -e $file ]
	    then
		file="yes"
	    fi
	done
	if [ "$file" == "yes" ]
	then
	    cddb="yes"
	    toc_file silent
	    cat $toc_file | grep -E "TITLE|PERFORMER" > $tempfile
	    length=`wc -l $tempfile | cut -d " " -f 1`
	    title=`cat $tempfile | head -n1 | sed 's/TITLE //;s/"//g'`
	    artist=`cat $tempfile | head -n2 | tail -n1 | sed 's/PERFORMER //;s/"//g'`
	    tracks=`expr \( $length - 2 \) / 2`
	    entries=`expr $tracks \* 2`
	    cat $tempfile | tail -n$entries | grep -v "PERFORMER " | sed 's/    TITLE //;s/"//g' > $tempfile.tmp
	    mv $tempfile.tmp $tempfile
	    for file in *.wav
	    do
		tracknumber=`echo $file | sed 's/.wav$//;s/^00//'`
		title=`cat $tempfile | head -n$tracknumber | tail -n1`
		mv $file "$tracknumber $title".wav
	    done
	    $reset
	    ls -N *.wav
	    display_message
	else
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere are no \"*.wav\" files in the current directory.\
	    " 7 62
	    retval=$?
	fi
}

remove_wav_files() # 2.5.
{
	select_directory
	cd "$directory"
	remove="no"
	for file in *.wav *.inf $toc_file
	do
	    if [ -e "$file" ]
	    then
		remove="yes"
	    fi
	done
	if [ "$remove" == "yes" ]
	then
	    dialog --title "Query" --clear "$@" \
	    --yesno "\nAre you sure you want to remove WAV and TOC files?" \
	    7 62
	    retval=$?
	    if [ "$retval" == "0" ]
	    then
		for file in *.wav *.inf $toc_file
		do
		    if [ -e "$file" ]
		    then
			rm -f "$file"
		    fi
		done
	    fi
	else
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere are no \"*.wav\" files in the current directory.\
	    " 7 62
	    retval=$?
	fi
}

copy_audio_cd() # 2.6.
{
	$reset
	echo "Reading table of contents. Please wait a while..."
	$cdparanoia_test 2> $tempfile
	lacking_drive=`cat $tempfile | grep "Unable to open cdrom drive"`
	lacking_disc=`cat $tempfile | grep "Unable to open disc"`
	if [ "$lacking_drive" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no \"$reader\" device.\
	    " 7 62
	    retval=$?
	elif [ "$lacking_disc" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no audio CD disc in \"$reader\" device.\
	    " 7 62
	    retval=$?
	else
	    dialog --backtitle "$backtitle" \
	    --title "BIN File" \
	    --checklist "Keep BIN audio image after writing:" 22 62 15 \
	    "Keep BIN file?" " " off 2> $tempfile
	    retval=$?
	    keep_bin_file=`cat $tempfile`
	    if [ "$keep_bin_file" == "\"Keep BIN file\\?\"" ]
	    then
		keep_bin_file="on"
	    else
		keep_bin_file="off"
	    fi
	    $reset
	    if [ "$keep_bin_file" == "off" ]
	    then
		echo "Executing: $cdrdao_copy"
		$cdrdao_copy
		eject_media
		display_message
	    elif [ "$keep_bin_file" == "on" ]
	    then
		echo "Executing: $cdrdao_copy --keepimage"
		$cdrdao_copy --keepimage
		eject_media
		display_message
	    fi
	fi
}

write_bin_file() # 2.7.
{
	dialog --backtitle "$backtitle" \
	--title "TOC File Selection (use arrows, tabulator, and space keys)" \
	--fselect "./" \
	11 62 2> $tempfile
	retval=$?
	bin_toc_file=`cat $tempfile`
	if [ "`echo $bin_toc_file | grep -E 'cd[0-9]+.toc'`" == "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThe toc file name \"$bin_toc_file\" is not proper.\
	    " 7 62
	    retval=$?
	else
	    if [ "$number" == "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThe number of device is empty -- probably not plugged.\
		" 7 62
		retval=$?
	    else
		$reset
		echo "Executing: $cdrdao_write $bin_toc_file"
		$cdrdao_write $bin_toc_file
		eject_media
		display_message
	    fi
        fi
}

remove_bin_files() # 2.8.
{
	select_directory
	cd "$directory"
	remove="no"
	for file in cd*.bin cd*.toc
	do
	    if [ -e $file ]
	    then
		remove="yes"
	    fi
	done
	if [ "$remove" == "yes" ]
	then
	    dialog --title "Query" --clear "$@" \
	    --yesno "\nAre you sure you want to remove BIN and TOC files?" \
	    7 62
	    retval=$?
	    if [ "$retval" == "0" ]
	    then
		for file in cd*.bin cd*.toc
		do
		    if [ -e $file ]
		    then
			rm -f $file
		    fi
		done
	    fi
	else
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere are no \"*.bin\" nor \"*.toc\" files in the current directory.\
	    " 7 62
	    retval=$?
	fi
}

encode_audio_tracks() # 2.9.
{
	select_encoder
	if [ "$encoder" == "flac" ]
	then
	    comment="comment=freecode.com/projects/cdwrite"
	    flac_compression
	elif [ "$encoder" == "lame" ]
	then
	    comment="freecode.com/projects/cdwrite"
	    variable_bitrates_mp3
	    constant_bitrates_mp3
	    high_quality_mp3
	    if [ "$mp3_quality" == "" -a "$mp3_bitrate" == "" ]
	    then
		dialog --title "Critical Error" --clear "$@" \
		--msgbox "\nNone MP3 quality nor bitrate is selected.\
		" 7 62
		retval=$?
		$reset
		echo "$copyright"
		echo
		exit
	    fi
	elif [ "$encoder" == "oggenc" ]
	then
	    comment="comment=freecode.com/projects/cdwrite"
	    quality_settings_ogg
	    constant_bitrates_ogg
	    if [ "$ogg_quality" == "" -a "$ogg_bitrate" == "" ]
	    then
		dialog --title "Critical Error" --clear "$@" \
		--msgbox "\nNone OGG quality nor bitrate is selected.\
		" 7 62
		retval=$?
		$reset
		echo "$copyright"
		echo
		exit
	    fi
	fi
	encode_tracks
}

select_encoder()
{
	> $tempfile
	echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
	echo --title \"Encoder\" \\ >> $tempfile
	echo --radiolist \"Select encoder:\" 22 62 15 \\ >> $tempfile
	if [ "$encoder" == "" ]
	then
	    if [ "$default_encoder" == "flac" ]
	    then
		echo \"flac \(FLAC files\)\" \" \" on \\ >> $tempfile
		echo \"lame \(MP3 files\)\" \" \" off \\ >> $tempfile
		echo \"oggenc \(OGG files\)\" \" \" off \\ >> $tempfile
	    elif [ "$default_encoder" == "lame" ]
	    then
		echo \"flac \(FLAC files\)\" \" \" off \\ >> $tempfile
		echo \"lame \(MP3 files\)\" \" \" on \\ >> $tempfile
		echo \"oggenc \(OGG files\)\" \" \" off \\ >> $tempfile
	    elif [ "$default_encoder" == "oggenc" ]
	    then
		echo \"flac \(FLAC files\)\" \" \" off \\ >> $tempfile
		echo \"lame \(MP3 files\)\" \" \" off \\ >> $tempfile
		echo \"oggenc \(OGG files\)\" \" \" on \\ >> $tempfile
	    else
		dialog --title "Critical Error" --clear "$@" \
		--msgbox "\nThere is no valid default_encoder setting in config file.\
		" 7 62
		retval=$?
		$reset
		echo "$copyright"
		echo
		exit
	    fi
	elif [ "$encoder" == "flac" ]
	then
	    echo \"flac \(FLAC files\)\" \" \" on \\ >> $tempfile
	    echo \"lame \(MP3 files\)\" \" \" off \\ >> $tempfile
	    echo \"oggenc \(OGG files\)\" \" \" off \\ >> $tempfile
	elif [ "$encoder" == "lame" ]
	then
	    echo \"flac \(FLAC files\)\" \" \" off \\ >> $tempfile
	    echo \"lame \(MP3 files\)\" \" \" on \\ >> $tempfile
	    echo \"oggenc \(OGG files\)\" \" \" off \\ >> $tempfile
	elif [ "$encoder" == "oggenc" ]
	then
	    echo \"flac \(FLAC files\)\" \" \" off \\ >> $tempfile
	    echo \"lame \(MP3 files\)\" \" \" off \\ >> $tempfile
	    echo \"oggenc \(OGG files\)\" \" \" on \\ >> $tempfile
	fi
	echo 2\> $tempfile \\ >> $tempfile
	chmod u+x $tempfile
	$tempfile
	chmod u-x $tempfile
	retval=$?
	encoder=`cat $tempfile`
	if [ "$encoder" == "flac (FLAC files)" ]
	then
	    encoder="flac"
	elif [ "$encoder" == "lame (MP3 files)" ]
	then
	    encoder="lame"
	elif [ "$encoder" == "oggenc (OGG files)" ]
	then
	    encoder="oggenc"
	fi
	if [ "$encoder" != "" ]
	then
	    default_encoder="$encoder"
	fi
}

flac_compression()
{
	flac_compression_old="$flac_compression"
	> $tempfile
	echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
	echo --title \"Speed\" \\ >> $tempfile
	echo --radiolist \"Select speed:\" 22 62 15 \\ >> $tempfile
	for value in 0 1 2 3 4 5 6 7 8
	do
	    if [ "$flac_compression" == "$value" ]
	    then
		echo $value \" \" on \\ >> $tempfile
	    else
		echo $value \" \" off \\ >> $tempfile
	    fi
	done
	echo 2\> $tempfile >> $tempfile
	chmod u+x $tempfile
	$tempfile
	chmod u-x $tempfile
	retval=$?
	output=`cat $tempfile`
	if [ "$retval" = "0" ]
	then
	    flac_compression="-$output"
	fi
	if [ "$flac_compression" == "" ]
	then
	    flac_compression="$flac_compression_old"
	fi
}

variable_bitrates_mp3()
{
	if [ "$mp3_bitrate" != "" ]
	then
	    mp3_quality=""
	fi
	> $tempfile
	echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
	echo --title \"Variable Bitrates\" \\ >> $tempfile
	echo --radiolist \"Select preset:\" 22 62 15 \\ >> $tempfile
	if [ "$mp3_quality" == "--r3mix" ]
	then
	    echo \" --r3mix\" \" \" on \\ >> $tempfile
	else
	    echo \" --r3mix\" \" \" off \\ >> $tempfile
	fi
	if [ "$mp3_quality" == "--preset medium" ]
	then
	    echo \" --preset medium\" \" \" on \\ >> $tempfile
	else
	    echo \" --preset medium\" \" \" off \\ >> $tempfile
	fi
	if [ "$mp3_quality" == "--preset fast standard" ]
	then
	    echo \" --preset fast standard\" \" \" on \\ >> $tempfile
	else
	    echo \" --preset fast standard\" \" \" off \\ >> $tempfile
	fi
	if [ "$mp3_quality" == "--preset standard" ]
	then
	    echo \" --preset standard\" \" \" on \\ >> $tempfile
	else
	    echo \" --preset standard\" \" \" off \\ >> $tempfile
	fi
	if [ "$mp3_quality" == "--preset fast extreme" ]
	then
	    echo \" --preset fast extreme\" \" \" on \\ >> $tempfile
	else
	    echo \" --preset fast extreme\" \" \" off \\ >> $tempfile
	fi
	if [ "$mp3_quality" == "--preset extreme" ]
	then
	    echo \" --preset extreme\" \" \" on \\ >> $tempfile
	else
	    echo \" --preset extreme\" \" \" off \\ >> $tempfile
	fi
	if [ "$mp3_quality" == "--preset insane" ]
	then
	    echo \" --preset insane\" \" \" on \\ >> $tempfile
	else
	    echo \" --preset insane\" \" \" off \\ >> $tempfile
	fi
	echo 2\> $tempfile >> $tempfile
	chmod u+x $tempfile
	$tempfile
	chmod u-x $tempfile
	retval=$?
	output=`cat $tempfile`
	if [ "$output" == " --r3mix" ]
	then
	    mp3_quality="--r3mix"
	elif [ "$output" == " --preset medium" ]
	then
	    mp3_quality="--preset medium"
	elif [ "$output" == " --preset fast standard" ]
	then
	    mp3_quality="--preset fast standard"
	elif [ "$output" == " --preset standard" ]
	then
	    mp3_quality="--preset standard"
	elif [ "$output" == " --preset fast extreme" ]
	then
	    mp3_quality="--preset fast extreme"
	elif [ "$output" == " --preset extreme" ]
	then
	    mp3_quality="--preset extreme"
	elif [ "$output" == " --preset insane" ]
	then
	    mp3_quality="--preset insane"
	fi
	if [ "$mp3_quality" != "" ]
	then
	    mp3_bitrate=""
	    high_quality="yes"
	fi
}

constant_bitrates_mp3()
{
	if [ "$mp3_quality" != "" ]
	then
	    mp3_bitrate=""
	fi
	> $tempfile
	echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
	echo --title \"Constant Bitrates\" \\ >> $tempfile
	echo --radiolist \"Select bitrate:\" 22 62 15 \\ >> $tempfile
	for value in 32 40 48 56 64 80 96 112 128 160 192 224 256 320
	do
	    if [ "$mp3_bitrate" == "$value" ]
	    then
		echo $value \" \" on \\ >> $tempfile
	    else
		echo $value \" \" off \\ >> $tempfile
	    fi
	done
	echo 2\> $tempfile >> $tempfile
	chmod u+x $tempfile
	$tempfile
	chmod u-x $tempfile
	retval=$?
	output=`cat $tempfile`
	if [ "$output" != "" ]
	then
	    mp3_bitrate="$output"
	    mp3_quality=""
	fi
	if [ "$mp3_bitrate" != "" ] && [ $mp3_bitrate -ge 192 ]
	then
	    high_quality="yes"
	elif [ "$mp3_bitrate" != "" ]
	then
	    high_quality="no"
	fi
}

high_quality_mp3()
{
	> $tempfile
	echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
	echo --title \"High Quality MP3\" \\ >> $tempfile
	echo --radiolist \"Select quality:\" 22 62 15 \\ >> $tempfile
	if [ "$high_quality" == "yes" ]
	then
	    echo \"stereo mode\" \" \" on \\ >> $tempfile
	    echo \"joint-stereo mode\" \" \" off \\ >> $tempfile
	else
	    echo \"stereo mode\" \" \" off \\ >> $tempfile
	    echo \"joint-stereo mode\" \" \" on \\ >> $tempfile
	fi
	echo 2\> $tempfile >> $tempfile
	chmod u+x $tempfile
	$tempfile
	chmod u-x $tempfile
	retval=$?
	output=`cat $tempfile`
	if [ "$output" = "stereo mode" ]
	then
	    stereo_mode="-m s"
	elif [ "$output" = "joint-stereo mode" ]
	then
	    stereo_mode=""
	fi
}

quality_settings_ogg()
{
	if [ "$ogg_bitrate" != "" ]
	then
	    ogg_quality=""
	fi
	> $tempfile
	previous="-1"
	echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
	echo --title \"Quality Settings\" \\ >> $tempfile
	echo --radiolist \"Select quality:\" 22 62 15 \\ >> $tempfile
	for value in -1 0 1 2 3 4 5 6 7 8 9
	do
	    if [ "`c \( $ogg_quality == -1 \)`" -a "`c \( $value == -1 \)`" == "1" ]
	    then
		echo $ogg_quality \" \" on \\ >> $tempfile
	    elif [ "`c \( $ogg_quality \> -1 \)`" -a "`c \( $ogg_quality \< 0 \)`" == "1" -a "`c \( $value == 0 \)`" == "1" ]
	    then
		echo $ogg_quality \" \" on \\ >> $tempfile
		echo $value \" \" off \\ >> $tempfile
	    elif [ "`c \( $ogg_quality \> $previous \)`" == "1" -a "`c \( $ogg_quality \< $value \)`" == "1" ]
	    then
		echo $ogg_quality \" \" on \\ >> $tempfile
		echo $value \" \" off \\ >> $tempfile
	    elif [ "`c \( $ogg_quality == $value \)`" == "1" ]
	    then
		echo $value \" \" on \\ >> $tempfile
	    elif [ "`c \( $ogg_quality \> 9 \)`" == "1" -a "`c \( $ogg_quality \< 10 \)`" -a "`c \( $value == 9 \)`" == "1" ]
	    then
		echo $value \" \" off \\ >> $tempfile
		echo $ogg_quality \" \" on \\ >> $tempfile
	    else
		echo $value \" \" off \\ >> $tempfile
	    fi
	    previous="$value"
	done
	value=10
	if [ "$ogg_quality" == "$value" ]
	then
	    echo $value \" \" on 2\> $tempfile >> $tempfile
	else
	    echo $value \" \" off 2\> $tempfile >> $tempfile
	fi
	chmod u+x $tempfile
	$tempfile
	chmod u-x $tempfile
	retval=$?
	output=`cat $tempfile`
	if [ "$output" != "" ]
	then
	    ogg_quality="$output"
	    ogg_bitrate=""
	fi
}

constant_bitrates_ogg()
{
	if [ "$ogg_quality" != "" ]
	then
	    ogg_bitrate=""
	fi
	> $tempfile
	echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
	echo --title \"Variable Bitrates\" \\ >> $tempfile
	echo --radiolist \"Select bitrate:\" 22 62 15 \\ >> $tempfile
	for value in 32 40 48 56 64 80 96 112 128 160 192 224 256 320
	do
	    if [ "$ogg_bitrate" == "$value" ]
	    then
		echo $value \" \" on \\ >> $tempfile
	    else
		echo $value \" \" off \\ >> $tempfile
	    fi
	done
	echo 2\> $tempfile \\ >> $tempfile
	chmod u+x $tempfile
	$tempfile
	chmod u-x $tempfile
	retval=$?
	output=`cat $tempfile`
	if [ "$output" != "" ]
	then
	    ogg_bitrate="$output"
	    ogg_quality=""
	fi
}

encode_tracks()
{
	on_the_fly="no"
	dialog --backtitle "$backtitle" \
	--title "On-The-Fly Encoding" \
	--checklist "Confirm the method:" 22 62 15 \
	"Use on-the-fly encoding?" " " off 2> $tempfile
	retval=$?
	on_the_fly=`cat $tempfile`
	if [ "$on_the_fly" == "\"Use on-the-fly encoding\\?\"" ]
	then
	    on_the_fly="yes"
	fi
	if [ "$on_the_fly" == "yes" ]
	then
	    on_the_fly_encoding
	else
	    select_directory
	    cd "$directory"
	    file_test="no"
	    for file in *.wav
	    do
		if [ -f "$file" ]
		then
		    file_test="yes"
		fi
	    done
	    if [ "$file_test" = "yes" ]
	    then
		wav_files_handling
	    else
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere are no \"*.wav\" files in the current directory.\
		" 7 62
		retval=$?
	    fi
	cd - 1> /dev/null
	fi
}

on_the_fly_encoding()
{
	cddb="no"
	dialog --backtitle "$backtitle" \
	--title "CDDB Data (requires cddb.pl script)" \
	--checklist "Confirm service usage:" 22 62 15 \
	"Use CDDB data for artists and titles?" " " off 2> $tempfile
	retval=$?
	cddb=`cat $tempfile`
	if [ "$cddb" == "\"Use CDDB data for artists and titles\\?\"" ]
	then
	    cddb="yes"
	fi
	select_ripper
	if [ "$cddb" == "yes" ]
	then
	    use_cddb
	    read_tracks_online
	    if [ "$default_encoder" == "lame" ]
	    then
		if [ -e `which mid3v2 2> /dev/null` ] || [ -e `which id3v2 2> /dev/null` ]
		then
		    set_id3_tags
		else
		    dialog --title "Critical Error" --clear "$@" \
		    --msgbox "\nThere is no \"mid3v2\" nor \"id3v2\" program.\nInstall it and run \"settags\" script in the album directory.\
		    " 8 62
		    retval=$?
		    $reset
		    echo "$copyright"
		    echo
		    exit
		fi
	    fi
	else
	    read_tracks_offline
	fi
}

use_cddb()
{
	# To get CDDB access install CDDB_get Perl module package
	# (see: http://search.cpan.org/).
	if [ "`which cddb.pl 2> /dev/null`" == "" ]
	then
	    dialog --title "Critical Error" --clear "$@" \
	    --msgbox "\nThere is no \"cddb.pl\" script.\nInstall CDDB_get Perl module.\
	    " 8 62
	    retval=$?
	    $reset
	    echo "$copyright"
	    echo
	    exit
	elif [ "`man CDDB_get 1>/dev/null`" != "" ]
	then
	    dialog --title "Critical Error" --clear "$@" \
	    --msgbox "\nThere is no \"CDDB_get\" package.\nInstall CDDB_get Perl module.\
	    " 8 62
	    retval=$?
	    $reset
	    echo "$copyright"
	    echo
	    exit
	else
	    $reset
	    echo "Preparing $tempfile. Please wait a while..."
	    cddb.pl -I > $tempfile
	fi
	if [ "$filename_convention" == "Linux" ]
	then
	    sed -i 's/\// | /g' $tempfile
	elif [ "$filename_convention" == "Windows" ]
	then
	    sed -i "s/\"/'/g;s/:/;/g;s/\// - /g;s/\\\/-/g;s/|/-/g;s/*//g;s/?//g;s/<//g;s/>//g;" $tempfile
	fi
	if [ -s $tempfile ]
	then
	    $reset
	    cat $tempfile
	    echo
	    echo "Update $tempfile if necessary using another terminal."
	    display_message
	else
	    dialog --title "Critical Error" --clear "$@" \
	    --msgbox "\nEmpty $tempfile.\nProbably there is no Internet connection.\
	    " 8 62
	    retval=$?
	    $reset
	    echo "$copyright"
	    echo
	    exit
	fi
	if [ "$filename_convention" == "Linux" ]
	then
	    artist="`cat $tempfile | grep '^artist: ' | sed 's/artist: //'`"
	    title="`cat $tempfile | grep '^title: ' | sed 's/title: //'`"
	    genre="`cat $tempfile | grep '^genre: ' | sed 's/genre: //'`"
	    year="`cat $tempfile | grep '^year: ' | sed 's/year: //'`"
	    cddbid="`cat $tempfile | grep '^cddbid: ' | sed 's/cddbid: //'`"
	    trackno="`cat $tempfile | grep '^trackno: ' | sed 's/trackno: //'`"
	elif [ "$filename_convention" == "Windows" ]
	then
	    artist="`cat $tempfile | grep '^artist; ' | sed 's/artist; //'`"
	    title="`cat $tempfile | grep '^title; ' | sed 's/title; //'`"
	    genre="`cat $tempfile | grep '^genre; ' | sed 's/genre; //'`"
	    year="`cat $tempfile | grep '^year; ' | sed 's/year; //'`"
	    cddbid="`cat $tempfile | grep '^cddbid; ' | sed 's/cddbid; //'`"
	    trackno="`cat $tempfile | grep '^trackno; ' | sed 's/trackno; //'`"
	fi
	directory="$artist - $title [$genre, $year]"
	mkdir "$directory"
}

set_id3_tags()
{
	# To get mass ID3 tagging tool install mutagen package
	# (see: https://bitbucket.org/lazka/mutagen/)
	# or id3lib (libid3) and id3v2 packages
	# (see: http://id3lib.sourceforge.net/ and http://id3v2.sourceforge.net/).
	if [ -x `which mid3v2 2> /dev/null` ] || [ -x `which id3v2 2> /dev/null` ]
	then
	    if [ -x `which settags 2> /dev/null` ]
	    then
		jump="off"
		$reset
		if [ -e "$directory" ]
		then
		    jump="on"
		fi
		if [ "$jump" == "on" ]
		then
		    cd "$directory"
		fi
		settags
		echo
		if [ "$jump" == "on" ]
		then
		    cd - 1> /dev/null
		fi
		display_message
	    else
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no \"settags\" script.\nInstall it for ID3 mass tagging.\
		" 8 62
		retval=$?
	    fi
	else
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no \"mid3v2\" nor \"id3v2\" program.\nInstall it for ID3 mass tagging.\
	    " 8 62
	    retval=$?
	fi
}

read_tracks_online()
{
	for track in `seq 1 $trackno`
	do
	    if [ "$filename_convention" == "Linux" ]
	    then
		tracktitle=`cat $tempfile | grep "track $track: " | sed "s/track $track: //"`
	    elif [ "$filename_convention" == "Windows" ]
	    then
		tracktitle=`cat $tempfile | grep "track $track; " | sed "s/track $track; //"`
	    fi
	    tracknumber=$track
	    if [ "$tracknumber" -lt "10" ]
	    then
		tracknumber=0$tracknumber
	    fi
	    $reset
	    if [ "$encoder" == "flac" ]
	    then
		    selected_ripper
		    echo "Executing: $encoding | flac $flac_compression -T \"$comment\" -T \"artist=$artist\" -T \"album=$title\" -T \"genre=$genre\" -T \"date=$year\" -T \"tracknumber=$tracknumber\" -T \"title=$tracktitle\" - -o \"$directory/$tracknumber $tracktitle\".flac"
		    $encoding | flac $flac_compression -T "$comment" -T "artist=$artist" -T "album=$title" -T "genre=$genre" -T "date=$year" -T "tracknumber=$tracknumber" -T "title=$tracktitle" - -o "$directory/$tracknumber $tracktitle".flac
	    elif [ "$encoder" == "lame" ]
	    then
		if [ "$mp3_quality" == "" ]
		then
		    selected_ripper
		    echo "Executing: $encoding | lame -b $mp3_bitrate $stereo_mode - \"$directory/$tracknumber $tracktitle\".mp3"
		    $encoding | lame -b $mp3_bitrate $stereo_mode - "$directory/$tracknumber $tracktitle".mp3
		else
		    selected_ripper
		    echo "Executing: $encoding | lame $mp3_quality $stereo_mode - \"$directory/$tracknumber $tracktitle\".mp3"
		    $encoding | lame $mp3_quality $stereo_mode - "$directory/$tracknumber $tracktitle".mp3
		fi
	    elif [ "$encoder" == "oggenc" ]
	    then
		if [ "$ogg_bitrate" == "" ]
		then
		    selected_ripper
		    echo "Executing: $encoding | oggenc -q $ogg_quality -o \"$directory/$tracknumber $tracktitle\".ogg -c \"$comment\" -a \"$artist\" -l \"$title\" -G \"$genre\" -d \"$year\" -N \"$tracknumber\" -t \"$tracktitle\" -"
		    $encoding | oggenc -q $ogg_quality -o "$directory/$tracknumber $tracktitle".ogg -c "$comment" -a "$artist" -l "$title" -G "$genre" -d "$year" -N "$tracknumber" -t "$tracktitle" -
		else
		    selected_ripper
		    echo "Executing: $encoding | oggenc -b $ogg_bitrate -o \"$directory/$tracknumber $tracktitle\".ogg -c \"$comment\" -a \"$artist\" -l \"$title\" -G \"$genre\" -d \"$year\" -N \"$tracknumber\" -t \"$tracktitle\" -"
		    $encoding | oggenc -b $ogg_bitrate -o "$directory/$tracknumber $tracktitle".ogg -c "$comment" -a "$artist" -l "$title" -G "$genre" -d "$year" -N "$tracknumber" -t "$tracktitle" -
		fi
	    fi
	done
	$reset
	if [ "$encoder" == "flac" ]
	then
	    echo "$directory"
	    echo
	    cd "$directory"
	    ls -N *.flac
	    cd - 1> /dev/null
	    display_message
	elif [ "$encoder" == "lame" ]
	then
	    if [ ! -x `which id3v2 2> /dev/null` ]
	    then
		echo "$directory"
		echo
		cd "$directory"
		ls -N *.mp3
		cd - 1> /dev/null
		display_message
	    fi
	elif [ "$encoder" == "oggenc" ]
	then
	    echo "$directory"
	    echo
	    cd "$directory"
	    ls -N *.ogg
	    cd - 1> /dev/null
	    display_message
	fi
	eject_media
}

read_tracks_offline()
{
	cdparanoia -d $reader -Q 2> $tempfile
	tracks=`cat $tempfile | grep -B1 TOTAL | head -n1 | sed -r 's/.(..).*/\1/'`
	for track in `seq -w 1 $tracks`
	do
	    if [ "$encoder" == "flac" ]
	    then
		for prefix in `seq -w -s " " 1 1 9999`
		do
		    if [ -e "$prefix.flac" ]
		    then
			continue
		    else
			break
		    fi
		done
		$reset
		selected_ripper
		echo "Executing: $encoding | flac $flac_compression -T \"$comment\" - -o $prefix.flac"
		$encoding | flac $flac_compression -T "$comment" - -o $prefix.flac
	    elif [ "$encoder" == "lame" ]
	    then
		for prefix in `seq -w -s " " 1 1 9999`
		do
		    if [ -e "$prefix.mp3" ]
		    then
			continue
		    else
			break
		    fi
		done
		$reset
		if [ "$mp3_quality" == "" ]
		then
		    selected_ripper
		    echo "Executing: $encoding | lame -b $mp3_bitrate $stereo_mode --id3v2-only --tc \"$comment\" - $prefix.mp3"
		    $encoding | lame -b $mp3_bitrate $stereo_mode --id3v2-only --tc "$comment" - $prefix.mp3
		else
		    selected_ripper
		    echo "Executing: $encoding | lame $mp3_quality $stereo_mode --id3v2-only --tc \"$comment\" - $prefix.mp3"
		    $encoding | lame $mp3_quality $stereo_mode --id3v2-only --tc "$comment" - $prefix.mp3
		fi
	    elif [ "$encoder" == "oggenc" ]
	    then
		for prefix in `seq -w -s " " 1 1 9999`
		do
		    if [ -e "$prefix.ogg" ]
		    then
			continue
		    else
			break
		    fi
		done
		$reset
		if [ "$ogg_bitrate" == "" ]
		then
		    selected_ripper
		    echo "Executing: $encoding | oggenc -q $ogg_quality -c \"$comment\" -o $prefix.ogg -"
		    $encoding | oggenc -q $ogg_quality -c "$comment" -o $prefix.ogg -
		else
		    selected_ripper
		    echo "Executing: $encoding | oggenc -b $ogg_bitrate -c \"$comment\" -o $prefix.ogg -"
		    $encoding | oggenc -b $ogg_bitrate -c "$comment" -o $prefix.ogg -
		fi
	    fi
	done
	$reset
	if [ "$encoder" == "flac" ]
	then
	    ls -N *.flac
	elif [ "$encoder" == "lame" ]
	then
	    ls -N *.mp3
	elif [ "$encoder" == "oggenc" ]
	then
	    ls -N *.ogg
	fi
	eject_media
	display_message
}

selected_ripper()
{
	if [ "$ripper" == "cdparanoia" ]
	then
	    ripping_commands
	    encoding="$cdparanoia_encoding"
	elif [ "$ripper" == "cdda2wav" ]
	then
	    ripping_commands
	    encoding="$cdda2wav_encoding"
	fi
}

select_directory()
{
	dialog --backtitle "$backtitle" \
	--title "Directory Selection (use arrows, tabulator, and space keys)" \
	--dselect "`pwd`/" \
	11 62 2> $tempfile
	retval=$?
	directory=`cat $tempfile`
	if [ "$directory" = "" ]
	then
	    directory="."
	fi
}

wav_files_handling()
{
	dialog --backtitle "$backtitle" \
	--title "Files Handling" \
	--checklist "Confirm action:" 22 62 15 \
	"Remove WAV files?" " " off 2> $tempfile
	retval=$?
	remove=`cat $tempfile`
	if [ "$remove" == "\"Remove WAV files\\?\"" ]
	then
	    remove="on"
	fi
	if [ "$retval" = "0" ]
	then
	    if [ "$file_test" = "yes" ]
	    then
		for file in *.wav
		do
		    if [ "$encoder" == "flac" ]
		    then
			encoded_file=`echo $file | sed 's/\.wav/\.flac/'`
			$reset
			if [ "`echo $file | grep '[0-9][0-9] .*'`" != "" ]
			then
			    track=`echo $file | sed -r 's/^(..) .*/\1/'`
			    song=`echo $file | sed 's/...//;s/.wav//'`
			    echo "Executing: flac $flac_compression -T \"$comment\" -T \"tracknumber=$track\" -T \"title=$song\" \"$file\" -o \"$encoded_file\""
			    flac $flac_compression -T "$comment" -T "tracknumber=$track" -T "title=$song" "$file" -o "$encoded_file"
			else
			    echo "Executing: flac $flac_compression -T \"$comment\" \"$file\" -o \"$encoded_file\""
			    flac $flac_compression -T "$comment" "$file" -o "$encoded_file"
			fi
		    elif [ "$encoder" == "lame" ]
		    then
			encoded_file=`echo $file | sed 's/\.wav/\.mp3/'`
			$reset
			if [ "$mp3_bitrate" != "" ]
			then
			    if [ "`echo $file | grep '[0-9][0-9] .*'`" != "" ]
			    then
			        track=`echo $file | sed -r 's/^(..) .*/\1/'`
			        song=`echo $file | sed 's/...//;s/.wav//'`
				echo "Executing: lame -b $mp3_bitrate $stereo_mode --id3v2-only --tc \"$comment\" --tn \"$track\" --tt \"$song\" \"$file\" \"$encoded_file\""
				lame -b $mp3_bitrate $stereo_mode --id3v2-only --tc "$comment" --tn "$track" --tt "$song" "$file" "$encoded_file"
			    else
				echo "Executing: lame -b $mp3_bitrate $stereo_mode --id3v2-only --tc \"$comment\" \"$file\" \"$encoded_file\""
				lame -b $mp3_bitrate $stereo_mode --id3v2-only --tc "$comment" "$file" "$encoded_file"
			    fi
			elif [ "$mp3_quality" != "" ]
			then
			    if [ "`echo $file | grep '[0-9][0-9] .*'`" != "" ]
			    then
			        track=`echo $file | sed -r 's/^(..) .*/\1/'`
			        song=`echo $file | sed 's/...//;s/.wav//'`
				echo "Executing: lame $mp3_quality $stereo_mode --id3v2-only --tn \"$track\" --tt \"$song\" --tc \"$comment\" \"$file\" \"$encoded_file\""
				lame $mp3_quality $stereo_mode --id3v2-only --tn "$track" --tt "$song" --tc "$comment" "$file" "$encoded_file"
			    else
				echo "Executing: lame $mp3_quality $stereo_mode --id3v2-only --tc \"$comment\" \"$file\" \"$encoded_file\""
				lame $mp3_quality $stereo_mode --id3v2-only --tc "$comment" "$file" "$encoded_file"
			    fi
			fi
		    elif [ "$encoder" == "oggenc" ]
		    then
			encoded_file=`echo $file | sed 's/\.wav/\.ogg/'`
			$reset
			if [ "$ogg_bitrate" != "" ]
			then
			    directory=`pwd | sed 's/.*\///'`
			    if [ "`echo $directory | grep -E '.+ - .+ [.+, [0-9]+]'`" != "" ]
			    then
			        artist=`pwd | sed 's/.*\///;s/ - .*//'`
			        album=`pwd | sed -r "s/.*? - //;s/ \[.*//"`
			        genre="`pwd | sed -r "s/.*\[//;s/, //;s/[0-9]+\]//"`"
			        year=`pwd | sed -r "s/.*\[//;s/.*, //;s/\]//"`
			        track=`echo $file | sed -r 's/^(..) .*/\1/'`
			        song=`echo $file | sed 's/...//;s/.wav//'`
			        echo "Executing: oggenc -b $ogg_bitrate -o \"$track $song\".ogg -a \"$artist\" -l \"$album\" -G \"$genre\" -d \"$year\" -N \"$track\" -t \"$song\" -c \"$comment\" -o \"$track $song\".ogg \"$file\""
			        oggenc -b $ogg_bitrate -o "$track $song".ogg -a "$artist" -l "$album" -G "$genre" -d "$year" -N "$track" -t "$song" -c "$comment" -o "$track $song".ogg "$file"
			    elif [ "`echo $file | grep '[0-9][0-9] .*'`" != "" ]
			    then
			        track=`echo $file | sed -r 's/^(..) .*/\1/'`
			        song=`echo $file | sed 's/...//;s/.wav//'`
			        echo "Executing: oggenc -b $ogg_bitrate -o \"$track $song\".ogg -N \"$track\" -t \"$song\" -c \"$comment\" -o \"$track $song\".ogg \"$file\""
			        oggenc -b $ogg_bitrate -o "$track $song".ogg -N "$track" -t "$song" -c "$comment" -o "$track $song".ogg "$file"
			    else
			        echo "Executing: oggenc -b $ogg_bitrate -c \"$comment\" \"$file\""
			        oggenc -b $ogg_bitrate -c "$comment" "$file"
			    fi
			elif [ "$ogg_quality" != "" ]
			then
			    directory=`pwd | sed 's/.*\///'`
			    if [ "`echo $directory | grep -E '.+ - .+ [.+, [0-9]+]'`" != "" ]
			    then
				artist=`pwd | sed 's/.*\///;s/ - .*//'`
			        album=`pwd | sed -r "s/.*? - //;s/ \[.*//"`
			        genre="`pwd | sed -r "s/.*\[//;s/, //;s/[0-9]+\]//"`"
			        year=`pwd | sed -r "s/.*\[//;s/.*, //;s/\]//"`
			        track=`echo $file | sed -r 's/^(..) .*/\1/'`
			        song=`echo $file | sed 's/...//;s/.wav//'`
			        echo "Executing: oggenc -q $ogg_quality -o \"$track $song\".ogg -a \"$artist\" -l \"$album\" -G \"$genre\" -d \"$year\" -N \"$track\" -t \"$song\" -c \"$comment\" \"$file\""
			        oggenc -q $ogg_quality -o "$track $song".ogg -a "$artist" -l "$album" -G "$genre" -d "$year" -N "$track" -t "$song" -c "$comment" "$file"
			    elif [ "`echo $file | grep '[0-9][0-9] .*'`" != "" ]
			    then
			        track=`echo $file | sed -r 's/^(..) .*/\1/'`
			        song=`echo $file | sed 's/...//;s/.wav//'`
			        echo "Executing: oggenc -q $ogg_quality -o \"$track $song\".ogg -N \"$track\" -t \"$song\" -c \"$comment\" \"$file\""
			        oggenc -q $ogg_quality -o "$track $song".ogg -N "$track" -t "$song" -c "$comment" "$file"
			    else
			        echo "Executing: oggenc -q $ogg_quality -c \"$comment\" \"$file\""
			        oggenc -q $ogg_quality -c "$comment" "$file"
			    fi
			fi
		    fi
		    if [ "$remove" == "on" ]
		    then
			rm "$file"
		    fi
		done
		directory=`pwd | sed 's/.*\///'`
		if [ "$encoder" == "lame" -a "`echo $directory | grep -E '.+ - .+ [.+, [0-9]+]'`" != "" ]
		then
		    set_id3_tags
		else
		    $reset
		    if [ "$encoder" == "flac" ]
		    then
			ls -N *.flac
		    elif [ "$encoder" == "lame" ]
		    then
			if [ -x `which id3info 2> /dev/null` -a "`echo 01*.mp3`" != "" ]
			then
			    id3info "`echo 01*.mp3`" | grep -E "COMM|TPE1|TALB|TYER" | sed 's/.*: //'
			    id3info *.mp3 | grep -E "TRCK|TIT2" | sed 's/.*: //'
			else
			    ls -N *.mp3
			fi
		    elif [ "$encoder" == "oggenc" ]
		    then
			ogginfo 01*.ogg | grep -E "comment=|artist=|album=|genre=|date=" | sed 's/.*=//'
			ogginfo *.ogg | grep -E "tracknumber=|title=" | sed 's/.*=//'
		    fi
		    display_message
		fi
	    fi
	fi
}

normalize_audio_files() # 2.10.
{
	select_directory
	cd "$directory"
	normalization_test="no"
	for file in *.flac *.mp3 *.ogg *.wav
	do
	    if [ -e "$file" ]
	    then
		normalization_test="yes"
	    fi
	done
	if [ "$normalization_test" == "yes" ]
	then
	    flac_test="no"
	    mp3_test="no"
	    ogg_test="no"
	    wav_test="no"
	    for file in *.flac
	    do
		if [ -e "$file" ]
		then
		    flac_test="yes"
		fi
	    done
	    for file in *.mp3
	    do
		if [ -e "$file" ]
		then
		    mp3_test="yes"
		fi
	    done
	    for file in *.ogg
	    do
		if [ -e "$file" ]
		then
		    ogg_test="yes"
		fi
	    done
	    for file in *.wav
	    do
		if [ -e "$file" ]
		then
		    wav_test="yes"
		fi
	    done
	    if [ "`which normalize 2> /dev/null`" != "" ]
	    then
		normalize=normalize
	    elif [ "`which normalize-audio 2> /dev/null`" != "" ]
	    then
		normalize=normalize-audio
	    fi
	    normalize_formats
	    if [ "$mp3_test" == "yes" -o "$ogg_test" == "yes" ]
	    then
		if [ "`echo $normalize_formats | grep 'MP3'`" != "" -a "$mp3_normalization" != "mp3gain" -o "`echo $normalize_formats | grep 'OGG'`" != "" ]
		then
		    normalize_bitrates
		fi
	    fi
	    $reset
	    > $tempfile
	    echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
	    echo --title \"Normalization method\" \\ >> $tempfile
	    echo --radiolist \"Select the method:\" 22 62 15 \\ >> $tempfile
	    if [ "$normalize_method" == "Normalize separate files" ]
	    then
		echo \"Normalize the whole album\" \" \" off \\ >> $tempfile
		echo \"Normalize separate files\" \" \" on \\ >> $tempfile
	    elif [ "$normalize_method" == "Normalize the whole album" ]
	    then
		echo \"Normalize the whole album\" \" \" on \\ >> $tempfile
		echo \"Normalize separate files\" \" \" off \\ >> $tempfile
	    else
		echo \"Normalize the whole album\" \" \" on \\ >> $tempfile
		echo \"Normalize separate files\" \" \" off \\ >> $tempfile
	    fi
	    echo 2\> $tempfile >> $tempfile
	    chmod u+x $tempfile
	    $tempfile
	    chmod u-x $tempfile
	    retval=$?
	    normalize_method=`cat $tempfile`
	    $reset
	    normalization_test="no"
	    if [ "`echo $normalize_formats | grep 'FLAC'`" != "" ]
	    then
		if [ "$normalize_method" == "Normalize separate files" ]
		then
		    for file in *.flac
		    do
			echo "Executing: normalize-mp3 --flac \"$file\""
			normalize-mp3 --flac "$file"
			normalization_test="yes"
		    done
		elif [ "$normalize_method" == "Normalize the whole album" ]
		then
		    echo "Executing: normalize-mp3 -b --flac *.flac"
		    normalize-mp3 -b --flac *.flac
		    normalization_test="yes"
		fi
	    fi
	    if [ "`echo $normalize_formats | grep 'MP3'`" != "" ]
	    then
		if [ "$mp3_normalization" == "normalize-mp3" ]
		then
		    for file in *.mp3
		    do
			if [ -e "$file" ]
			then
			    echo "Executing: normalize-mp3 --bitrate $normalize_bitrate \"$file\""
			    normalize-mp3 --bitrate $normalize_bitrate "$file"
			    normalization_test="yes"
			fi
		    done
		elif [ "$mp3_normalization" == "mp3gain" ]
		then
			if [ "$normalize_method" == "Normalize separate files" ]
			then
			    echo "Executing: mp3gain -r *.mp3"
			    mp3gain -r *.mp3
			    normalization_test="yes"
			elif [ "$normalize_method" == "Normalize the whole album" ]
			then
			    echo "Executing: mp3gain -a -k *.mp3"
			    mp3gain -a -k *.mp3
			    normalization_test="yes"
			fi
		fi
	    fi
	    if [ "`echo $normalize_formats | grep 'OGG'`" != "" ]
	    then
		if [ "$normalize_method" == "Normalize separate files" ]
		then
		    for file in *.ogg
		    do
			echo "Executing: normalize-ogg \"$file\""
			normalize-ogg "$file"
			normalization_test="yes"
		    done
		elif [ "$normalize_method" == "Normalize the whole album" ]
		then
		    echo "Executing: normalize-ogg -b *.ogg"
		    normalize-ogg -b *.ogg
		    normalization_test="yes"
		fi
	    fi
	    if [ "`echo $normalize_formats | grep 'WAV'`" != "" ]
	    then
		if [ "$normalize_method" == "Normalize separate files" ]
		then
		    for file in *.wav
		    do
			echo "Executing: $normalize \"$file\""
			$normalize "$file"
			normalization_test="yes"
		    done
		elif [ "$normalize_method" == "Normalize the whole album" ]
		then
		    echo "Executing: $normalize -b *.wav"
		    $normalize -b *.wav
		    normalization_test="yes"
		fi
	    fi
	    if [ "$normalization_test" == "yes" ]
	    then
		display_message
	    else
		dialog --title "Error" --clear "$@" \
		--msgbox "\nNone normalization method was selected.\
		" 7 62
		retval=$?
	    fi
	else
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere are no \"*.flac\", \"*.mp3\", \"*.ogg\", or \"*.wav\" files.\
	    " 7 62
	    retval=$?
	fi
}

normalize_formats()
{
	speed_old="$speed"
	> $tempfile
	echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
	echo --title \"Normalize\" \\ >> $tempfile
	echo --checklist \"Select file formats:\" 22 62 15 \\ >> $tempfile
	if [ "$flac_test" == "yes" ]
	then
		echo FLAC \" \" on \\ >> $tempfile
	fi
	if [ "$mp3_test" == "yes" ]
	then
	    if [ "$mp3_normalization" == "normalize-mp3" ]
	    then
		echo MP3 \" \" off \\ >> $tempfile
	    elif [ "$mp3_normalization" == "mp3gain" ]
	    then
		echo MP3 \" \" on \\ >> $tempfile
	    fi
	fi
	if [ "$ogg_test" == "yes" ]
	then
		echo OGG \" \" off \\ >> $tempfile
	fi
	if [ "$wav_test" == "yes" ]
	then
		echo WAV \" \" on \\ >> $tempfile
	fi
	echo 2\> $tempfile >> $tempfile
	chmod u+x $tempfile
	$tempfile
	chmod u-x $tempfile
	retval=$?
	normalize_formats=`cat $tempfile`
}

normalize_bitrates()
{
	if [ "$normalize_bitrate" == "" ]
	then
	    normalize_bitrate="192"
	fi
	> $tempfile
	echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
	echo --title \"Normalize Bitrates\" \\ >> $tempfile
	echo --radiolist \"Select bitrate:\" 22 62 15 \\ >> $tempfile
	for value in 32 40 48 56 64 80 96 112 128 160 192 224 256 320
	do
	    if [ "$normalize_bitrate" == "$value" ]
	    then
		echo $value \" \" on \\ >> $tempfile
	    else
		echo $value \" \" off \\ >> $tempfile
	    fi
	done
	echo 2\> $tempfile >> $tempfile
	chmod u+x $tempfile
	$tempfile
	chmod u-x $tempfile
	retval=$?
	output=`cat $tempfile`
	if [ "$output" != "" ]
	then
	    normalize_bitrate="$output"
	fi
}

change_gain() # 2.11.
{
	> $tempfile
	echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
	echo --title \"Change Gain\" \\ >> $tempfile
	echo --radiolist \"Select the gain:\" 22 62 15 \\ >> $tempfile
	echo 1 \"-5 dB\" off \\ >> $tempfile
	echo 2 \"-4 dB\" off \\ >> $tempfile
	echo 3 \"-3 dB\" off \\ >> $tempfile
	echo 4 \"-2 dB\" off \\ >> $tempfile
	echo 5 \"-1 dB\" on \\ >> $tempfile
	echo 6 \"+1 dB\" off \\ >> $tempfile
	echo 7 \"+2 dB\" off \\ >> $tempfile
	echo 8 \"+3 dB\" off \\ >> $tempfile
	echo 9 \"+4 dB\" off \\ >> $tempfile
	echo 10 \"+5 dB\" off \\ >> $tempfile
	echo 2\> $tempfile >> $tempfile
	chmod u+x $tempfile
	$tempfile
	chmod u-x $tempfile
	retval=$?
	output=`cat $tempfile`
	if [ "$retval" = "0" ]
	then
	    $reset
	    case $output in
		1)
		option="-5"
		;;
		2)
		option="-4"
		;;
		3)
		option="-3"
		;;
		4)
		option="-2"
		;;
		5)
		option="-1"
		;;
		6)
		option="1"
		;;
		7)
		option="2"
		;;
		8)
		option="3"
		;;
		9)
		option="4"
		;;
		10)
		option="5"
		;;
		esac
	fi
	    for file in *.mp3
	    do
		if [ -f "$file" ]
		then
		    file_test="yes"
		fi
	    done
	    if [ "$file_test" = "yes" ]
	    then
		$reset
		echo "Executing: mp3gain -l 0 $option *.mp3"
		mp3gain -l 0 $option *.mp3
		echo
		echo "Executing: mp3gain -l 1 $option *.mp3"
		mp3gain -l 1 $option *.mp3
		display_message
	    else
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere are no \"*.mp3\" files in the current directory.\
		" 7 62
		retval=$?
	    fi
}

convert_audio_files() # 2.12.
{
	select_directory
	cd "$directory"
	file_test="no"
	for file in *.ape *.flac *.m4a *.mp3 *.mpc *.ogg *.wav *.wma
	do
	    if [ -e "$file" ]
	    then
		file_test="yes"
	    fi
	done
	if [ "$file_test" == "yes" ]
	then
	    ape_test="no"
	    flac_test="no"
	    m4a_test="no"
	    mp3_test="no"
	    mpc_test="no"
	    ogg_test="no"
	    wav_test="no"
	    wma_test="no"
	    for file in *.ape
	    do
		if [ -e "$file" ]
		then
		    ape_test="yes"
		fi
	    done
	    for file in *.flac
	    do
		if [ -e "$file" ]
		then
		    flac_test="yes"
		fi
	    done
	    for file in *.m4a
	    do
		if [ -e "$file" ]
		then
		    m4a_test="yes"
		fi
	    done
	    for file in *.mp3
	    do
		if [ -e "$file" ]
		then
		    mp3_test="yes"
		fi
	    done
	    for file in *.mpc
	    do
		if [ -e "$file" ]
		then
		    mpc_test="yes"
		fi
	    done
	    for file in *.ogg
	    do
		if [ -e "$file" ]
		then
		    ogg_test="yes"
		fi
	    done
	    for file in *.wav
	    do
		if [ -e "$file" ]
		then
		    wav_test="yes"
		fi
	    done
	    for file in *.wma
	    do
		if [ -e "$file" ]
		then
		    wma_test="yes"
		fi
	    done
	    > $tempfile
	    if [ "$input_format" == "" ]
	    then
		input_format="mp3"
	    fi
	    echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
	    echo --title \"Input\" \\ >> $tempfile
	    echo --checklist \"Select input formats:\" 22 62 15 \\ >> $tempfile
	    if [ "$ape_test" == "yes" ]
	    then
		if [ "`echo $input_format | grep 'ape'`" != "" ]
		then
		    echo \"APE\" \" \" on \\ >> $tempfile
		else
		    echo \"APE\" \" \" off \\ >> $tempfile
		fi
	    fi
	    if [ "$flac_test" == "yes" ]
	    then
		if [ "`echo $input_format | grep 'flac'`" != "" ]
		then
		    echo \"FLAC\" \" \" on \\ >> $tempfile
		else
		    echo \"FLAC\" \" \" off \\ >> $tempfile
		fi
	    fi
	    if [ "$m4a_test" == "yes" ]
	    then
		if [ "`echo $input_format | grep 'm4a'`" != "" ]
		then
		    echo \"M4A\" \" \" on \\ >> $tempfile
		else
		    echo \"M4A\" \" \" off \\ >> $tempfile
		fi
	    fi
	    if [ "$mp3_test" == "yes" ]
	    then
		if [ "`echo $input_format | grep 'mp3'`" != "" ]
		then
		    echo \"MP3\" \" \" on \\ >> $tempfile
		else
		    echo \"MP3\" \" \" off \\ >> $tempfile
		fi
	    fi
	    if [ "$mpc_test" == "yes" ]
	    then
		if [ "`echo $input_format | grep 'mpc'`" != "" ]
		then
		    echo \"MPC\" \" \" on \\ >> $tempfile
		else
		    echo \"MPC\" \" \" off \\ >> $tempfile
		fi
	    fi
	    if [ "$ogg_test" == "yes" ]
	    then
		if [ "`echo $input_format | grep 'ogg'`" != "" ]
		then
		    echo \"OGG\" \" \" on \\ >> $tempfile
		else
		    echo \"OGG\" \" \" off \\ >> $tempfile
		fi
	    fi
	    if [ "$wav_test" == "yes" ]
	    then
		if [ "`echo $input_format | grep 'wav'`" != "" ]
		then
		    echo \"WAV\" \" \" on \\ >> $tempfile
		else
		    echo \"WAV\" \" \" off \\ >> $tempfile
		fi
	    fi
	    if [ "$wma_test" == "yes" ]
	    then
		if [ "`echo $input_format | grep 'wma'`" != "" ]
		then
		    echo \"WMA\" \" \" on \\ >> $tempfile
		else
		    echo \"WMA\" \" \" off \\ >> $tempfile
		fi
	    fi
	    echo 2\> $tempfile \\ >> $tempfile
	    chmod u+x $tempfile
	    $tempfile
	    chmod u-x $tempfile
	    retval=$?
	    input_format=`cat $tempfile | tr A-Z a-z | sed 's/"//g'`
	    > $tempfile
	    if [ "$output_format" == "" ]
	    then
		output_format="wav"
	    fi
	    echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
	    echo --title \"Output\" \\ >> $tempfile
	    echo --radiolist \"Select output format:\" 22 62 15 \\ >> $tempfile
	    if [ "$output_format" == "flac" ]
	    then
		echo \"FLAC\" \" \" on \\ >> $tempfile
	    else
		echo \"FLAC\" \" \" off \\ >> $tempfile
	    fi
	    if [ "$output_format" == "mp3" ]
	    then
		echo \"MP3\" \" \" on \\ >> $tempfile
	    else
		echo \"MP3\" \" \" off \\ >> $tempfile
	    fi
	    if [ "$output_format" == "ogg" ]
	    then
		echo \"OGG\" \" \" on \\ >> $tempfile
	    else
		echo \"OGG\" \" \" off \\ >> $tempfile
	    fi
	    if [ "$output_format" == "wav" ]
	    then
		echo \"WAV\" \" \" on \\ >> $tempfile
	    else
		echo \"WAV\" \" \" off \\ >> $tempfile
	    fi
	    echo 2\> $tempfile \\ >> $tempfile
	    chmod u+x $tempfile
	    $tempfile
	    chmod u-x $tempfile
	    retval=$?
	    output_format=`cat $tempfile | tr A-Z a-z`
	    if [ "$input_format" == "" -o "$output_format" == "" ]
	    then
		if [ "$input_format" == "" ]
		then
		    dialog --title "Error" --clear "$@" \
		    --msgbox "\nUndefined input format.\
		    " 7 62
		    retval=$?
		fi
		if [ "$output_format" == "" ]
		then
		    dialog --title "Error" --clear "$@" \
		    --msgbox "\nUndefined output format.\
		    " 7 62
		    retval=$?
		fi
	    else
		if [ "$input_format" == "wav" -a "$output_format" == "wav" ]
		then
		    dialog --title "Error" --clear "$@" \
		    --msgbox "\nI can not convert from \"*.wav\" files to \"*.wav\" ones.\
		    " 7 62
		    retval=$?
		else
		    if [ ! -e *.$input_format ]
		    then
			dialog --title "Error" --clear "$@" \
			--msgbox "\nThere is no \"*.$input_format\" files in the current directory.\
			" 7 62
			retval=$?
		    else
			$reset
			for input in $input_format
			do
			    echo "Executing: musica ${input}2${output_format}"
			    musica ${input}2${output_format}
			done
			display_message
		    fi
		fi
	    fi
	else
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere are no files for conversion here.\
	    " 7 62
	    retval=$?
	fi
}

play_audio_files() # 2.13.
{
	audio_file="no"
	select_directory
	cd "$directory"
	for file in *
	do
	    if [ "`echo $file | grep -E '\.ape$|\.flac$|\.m4a$|\.mp3$|\.mpc$|\.ogg$|\.wav$|\.wma$'`" != "" ]
	    then
		audio_file="yes"
	    fi
	done
	if [ "$audio_file" == "yes" ]
	then
	    > $tempfile
	    echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
	    echo --title \"Music player\" \\ >> $tempfile
	    echo --checklist \"Select files to play:\" 22 62 15 \\ >> $tempfile
	    for file in *
	    do
		if [ "`echo $file | grep -E '\.ape$|\.flac$|\.m4a$|\.mp3$|\.mpc$|\.ogg$|\.wav$|\.wma$'`" != "" ]
		then
	    	    echo \"$file\" \" \" on \\ >> $tempfile
		fi
	    done
	    echo 2\> $tempfile >> $tempfile
	    chmod u+x $tempfile
	    $tempfile
	    chmod u-x $tempfile
	    retval=$?
	    if [ -s $tempfile ]
	    then
		dialog --title "Media Player Help" --clear "$@" \
		--msgbox "\n    <  previous track\n    >  next track\n left  10 seconds backward\nright  10 seconds forward\n    p  pause\n    9  decrease volume\n    0  increase volume\n    q  stop playing and quit\
		" 14 62
		retval=$?
		sed -i 's/" "/\n/g;s/"//g;s/\\//g' $tempfile
		cp $tempfile .playlist.tmp
		$reset
		mplayer -playlist .playlist.tmp
		rm .playlist.tmp
	    else
		dialog --title "Error" --clear "$@" \
		--msgbox "\nEmpty playlist.\
		" 7 62
		retval=$?
	    fi
	else
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere are no audio files here.\
	    " 7 62
	    retval=$?
	fi
}

play_audio_cd() # 2.14.
{
	$reset
	echo "Reading table of contents. Please wait a while..."
	$cdparanoia_test 2> $tempfile
	lacking_drive=`cat $tempfile | grep "Unable to open cdrom drive"`
	lacking_disc=`cat $tempfile | grep "Unable to open disc"`
	if [ "$lacking_drive" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no \"$reader\" device.\
	    " 7 62
	    retval=$?
	elif [ "$lacking_disc" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no audio CD in \"$reader\" device.\
	    " 7 62
	    retval=$?
	else
	    tracks=`cat $tempfile | grep -B 1 "TOTAL" | grep "\. " | cut -d "." -f 1`
	    if [ "$tracks" != "" ]
	    then
		if [ "$cd_player" == "mplayer" ]
		then
		    audio_cd
		    dialog --title "Media Player Help" --clear "$@" \
		    --msgbox "\n    <  previous track\n    >  next track\n left  10 seconds backward\nright  10 seconds forward\n    p  pause\n    9  decrease volume\n    0  increase volume\n    q  stop playing and quit\
		    " 14 62
		    retval=$?
		    $reset
		    for track in $tracks
		    do
			echo "cdda://$track" >> $tempfile
		    done
		    mplayer -cdrom-device $reader -playlist $tempfile -cdda speed=$mplayer_speed -cache $mplayer_cache
		elif [ "$cd_player" == "cdda-player" ]
		then
		    audio_cd
		    dialog --title "CDDA Player Help" --clear "$@" \
		    --msgbox "\n left  previous track\nright  next track\n down  10 seconds backward\n   up  10 seconds forward\n    p  pause\n    -  decrease volume\n    +  increase volume\n    q  stop playing and quit\
		    " 14 62
		    retval=$?
		    $reset
		    cdda-player $reader
		else
		    dialog --title "Error" --clear "$@" \
		    --msgbox "\nThe cd_player setting is not correct.\nSet it to \"mplayer\" or \"cdda-player\" value.\
		    " 8 62
		    retval=$?
		fi
	    fi
	fi
}

audio_cd()
{
	> $tempfile
	echo dialog --backtitle \"$backtitle\" \\ >> $tempfile
	echo --title \"Audio CD\" \\ >> $tempfile
	echo --checklist \"Select tracks to play:\" 22 62 15 \\ >> $tempfile
	for track in `seq 1 $tracks`
	do
	    echo \"$track\" \" \" on \\ >> $tempfile
	done
	echo 2\> $tempfile >> $tempfile
	chmod u+x $tempfile
	$tempfile
	chmod u-x $tempfile
	sed -i 's/"//g' $tempfile
	tracks="`cat $tempfile`"
	> $tempfile
	retval=$?
}

data_cd_media_info() # 3.1.
{
	if [ "$number" == "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThe number of device is empty -- probably not plugged.\
	    " 7 62
	    retval=$?
	else
	    $reset
	    echo "Reading disc info. Please wait a while..."
	    $cdrdao_test > $tempfile 2>&1
	    lacking_drive=`cat $tempfile | grep "Cannot open SCSI device"`
	    lacking_disc=`cat $tempfile | grep "Unit not ready, giving up"`
	    if [ "$lacking_drive" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no \"$reader\" device.\
		" 7 62
		retval=$?
	    elif [ "$lacking_disc" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no data CD disc in \"$reader\" device.\
		" 7 62
		retval=$?
	    else
		$reset
		echo "Executing: $cdrdao_test"
		$cdrdao_test
		display_message
	    fi
	fi
}

create_cd_iso_image() # 3.2.
{
	dialog --backtitle "$backtitle" \
	--title "Data Image" \
	--menu "Create data image:" 22 62 15 \
	1 "First session ISO image" \
	2 "Next session ISO image" \
	2> $tempfile
	retval=$?
	output=`cat $tempfile`
}

first_session_iso_image() # 3.2.1.
{
	symbolic_links
	compatibility_mode
	select_directory
	if [ -d "$directory" ]
	then
	    $reset
	    echo "Executing: $nice_read $mkisofs_read $links -o $iso_file \"$directory\""
	    $nice_read $mkisofs_read $links -o $iso_file "$directory"
	    display_message
	else
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no \"$directory\" directory.\
	    " 7 62
	    retval=$?
	fi
}

next_session_iso_image() # 3.2.2.
{
	if [ "$number" == "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThe number of device is empty -- probably not plugged.\
	    " 7 62
	    retval=$?
	else
	    $reset
	    echo "Reading disc info. Please wait a while..."
	    $cdrdao_test > $tempfile 2>&1
	    lacking_drive=`cat $tempfile | grep "Cannot open SCSI device"`
	    lacking_disc=`cat $tempfile | grep "Unit not ready, giving up"`
	    if [ "$lacking_drive" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no \"$reader\" device.\
		" 7 62
		retval=$?
	    elif [ "$lacking_disc" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no data CD disc in \"$reader\" device.\
		" 7 62
		retval=$?
	    else
		symbolic_links
		compatibility_mode
		select_directory
		$reset
		$cdrecord_test_toc 2> $tempfile
		if [ "`cat $tempfile | grep 'No disc'`" == "" ]
		then
		    if [ -d "$directory" ]
		    then
			$reset
			echo "Executing: $mkisofs_read -C `$cdrecord_test_msinfo` -M $number $links -o $iso_file \"$directory\""
			$mkisofs_read -C `$cdrecord_test_msinfo` -M $number $links -o $iso_file "$directory"
			display_message
		    else
			dialog --title "Error" --clear "$@" \
			--msgbox "\nThere is no \"$directory\" directory.\
			" 7 62
			retval=$?
		    fi
		else
		    dialog --title "Error" --clear "$@" \
		    --msgbox "\nThere is no data disc in \"$writer\" device.\
		    " 7 62
		    retval=$?
		fi
	    fi
	fi
}

symbolic_links()
{
	dialog --backtitle "$backtitle" \
	--title "Symbolic Links" \
	--checklist "Accept to follow symbolic links:" 22 62 15 \
	"Follow symlinks?" " " off 2> $tempfile
	retval=$?
	links=`cat $tempfile`
	if [ "$links" == "\"Follow\ symlinks\\?\"" ]
	then
	    links="-f"
	else
	    links=""
	fi
}

compatibility_mode()
{
	dialog --backtitle "$backtitle" \
	--title "Linux And Windows Compatibility Modes" \
	--radiolist "For Linux only use -R switch for Windows use -r -J -D:" 22 62 15 \
	"Options: -R" " " on 2> $tempfile \
	"Options: -r -J -D" " " off 2> $tempfile
	retval=$?
	compat=`cat $tempfile`
	if [ "$compat" == "Options: -R" ]
	then
	    mkisofs_read="$mkisofs_read_linux"
	elif [ "$compat" == "Options: -r -J -D" ]
	then
	    mkisofs_read="$mkisofs_read_windows"
	fi
	if [ "$mkisofs_read" == "" ]
	then
	    dialog --title "Critical Error" --clear "$@" \
	    --msgbox "\nNone option for mkisofs was declared.\
	    " 7 62
	    retval=$?
	    $reset
	    echo "$copyright"
	    echo
	    exit
	fi
}

write_cd_iso_image() # 3.3.
{
	if [ ! -f $iso_file ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no \"$iso_file\" ISO image file.\
	    " 7 62
	    retval=$?
	else
	    dialog --backtitle "$backtitle" \
	    --title "Data Image" \
	    --menu "Write data image:" 22 62 15 \
	    1 "Multi session writing mode" \
	    2 "Closing session writing mode (see: BUGS)" \
	    2> $tempfile
	    retval=$?
	    output=`cat $tempfile`
	fi
	if [ "$output" == "" ]
	then
	    dialog --title "Critical Error" --clear "$@" \
	    --msgbox "\nNone data image mode declared.\
	    " 7 62
	    retval=$?
	    $reset
	    echo "$copyright"
	    echo
	    exit
	fi
}

remove_iso_file()
{
	dialog --backtitle "$backtitle" \
	--title "ISO File" \
	--checklist "Remove ISO data image after writing:" 22 62 15 \
	"Remove ISO file?" " " off 2> $tempfile
	retval=$?
	remove_iso_file=`cat $tempfile`
	if [ "$remove_iso_file" == "\"Remove ISO file\\?\"" ]
	then
	    remove_iso_file="on"
	else
	    remove_iso_file="off"
	fi
}

multi_session_writing_mode() # 3.3.1.
{
	if [ "$number" == "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThe number of device is empty -- probably not plugged.\
	    " 7 62
	    retval=$?
	else
	    $reset
	    echo "Reading disc info. Please wait a while..."
	    $cdrdao_test > $tempfile 2>&1
	    lacking_drive=`cat $tempfile | grep "Cannot open SCSI device"`
	    lacking_disc=`cat $tempfile | grep "Unit not ready, giving up"`
	    if [ "$lacking_drive" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no \"$reader\" device.\
		" 7 62
		retval=$?
	    elif [ "$lacking_disc" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no data CD disc in \"$reader\" device.\
		" 7 62
		retval=$?
	    else
		cdrecord_overburn
		$reset
		echo "Executing: $cdrecord_write -multi speed=$speed $overburn $iso_file"
		$cdrecord_write -multi speed=$speed $overburn $iso_file
		eject_media
		display_message
	    fi
	fi
}

closing_session_writing_mode() # 3.3.2.
{
	if [ "$number" == "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThe number of device is empty -- probably not plugged.\
	    " 7 62
	    retval=$?
	else
	    $reset
	    echo "Reading disc info. Please wait a while..."
	    $cdrdao_test > $tempfile 2>&1
	    lacking_drive=`cat $tempfile | grep "Cannot open SCSI device"`
	    lacking_disc=`cat $tempfile | grep "Unit not ready, giving up"`
	    if [ "$lacking_drive" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no \"$reader\" device.\
		" 7 62
		retval=$?
	    elif [ "$lacking_disc" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no data CD disc in \"$reader\" device.\
		" 7 62
		retval=$?
	    else
		cdrecord_overburn
		$reset
		echo "Executing: $nice_write $cdrecord_write speed=$speed $overburn $iso_file"
		$nice_write $cdrecord_write speed=$speed $overburn $iso_file
		eject_media
		display_message
	    fi
	fi
}

cdrecord_overburn()
{
	overburn=""
	dialog --backtitle "$backtitle" \
	--title "Overburn Mode" \
	--checklist "Confirm using overburn:" 22 62 15 \
	"Overburn (use it carefully)" " " off 2> $tempfile
	retval=$?
	output=`cat $tempfile`
	if [ "$output" = "\"Overburn \\(use it carefully\\)\"" ]
	then
	    overburn="-overburn"
	fi
}

dvd_media_info() # 4.1.
{
	$dvd_rw_info_test 2> $tempfile
	empty_drive=`cat $tempfile | grep "no media mounted"`
	non_dvd_disc=`cat $tempfile | grep "non-DVD media mounted"`
	if [ "$dvd_rw" == "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nEmpty dvd_rw setting in $configuration file.\
	    " 7 62
	    retval=$?
	elif [ ! -e $dvd_rw ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no \"$dvd_rw\" device.\
	    " 7 62
	    retval=$?
	elif [ "$empty_drive" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no disc in \"$dvd_rw\" device.\
	    " 7 62
	    retval=$?
	elif [ "$non_dvd_disc" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no DVD disc in \"$dvd_rw\" device.\
	    " 7 62
	    retval=$?
	else
	    $reset
	    echo "Executing: $dvd_rw_info_test"
	    $dvd_rw_info_test
	    display_message
	fi
}

write_first_session() # 4.2.
{
	$dvd_rw_info_test 2> $tempfile
	empty_drive=`cat $tempfile | grep "no media mounted"`
	non_dvd_disc=`cat $tempfile | grep "non-DVD media mounted"`
	if [ "$dvd_rw" == "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nEmpty dvd_rw setting in $configuration file.\
	    " 7 62
	    retval=$?
	elif [ ! -e $dvd_rw ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no \"$dvd_rw\" device.\
	    " 7 62
	    retval=$?
	elif [ "$empty_drive" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no disc in \"$dvd_rw\" device.\
	    " 7 62
	    retval=$?
	elif [ "$non_dvd_disc" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no DVD disc in \"$dvd_rw\" device.\
	    " 7 62
	    retval=$?
	else
	    select_directory
	    if [ -d "$directory" ]
	    then
		$reset
		echo "Executing: $nice_read $growisofs_zero \"$directory\""
		$nice_read $growisofs_zero "$directory"
		eject_media
		display_message
	    else
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no \"$directory\" directory.\
		" 7 62
		retval=$?
	    fi
	fi
}

write_next_session() # 4.3.
{
	$dvd_rw_info_test 2> $tempfile
	empty_drive=`cat $tempfile | grep "no media mounted"`
	non_dvd_disc=`cat $tempfile | grep "non-DVD media mounted"`
	if [ "$dvd_rw" == "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nEmpty dvd_rw setting in $configuration file.\
	    " 7 62
	    retval=$?
	elif [ ! -e $dvd_rw ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no \"$dvd_rw\" device.\
	    " 7 62
	    retval=$?
	elif [ "$empty_drive" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no disc in \"$dvd_rw\" device.\
	    " 7 62
	    retval=$?
	elif [ "$non_dvd_disc" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no DVD disc in \"$dvd_rw\" device.\
	    " 7 62
	    retval=$?
	else
	    select_directory
	    if [ -d "$directory" ]
	    then
		$reset
		echo "Executing: $nice_read $growisofs_merge \"$directory\""
		$nice_read $growisofs_merge "$directory"
		eject_media
		display_message
	    else
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no \"$directory\" directory.\
		" 7 62
		retval=$?
	    fi
	fi
}

write_dvd_iso_image() # 4.4.
{
	if [ ! -f $iso_file ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no \"$iso_file\" ISO image file.\
	    " 7 62
	    retval=$?
	else
	    $dvd_rw_info_test 2> $tempfile
	    empty_drive=`cat $tempfile | grep "no media mounted"`
	    non_dvd_disc=`cat $tempfile | grep "non-DVD media mounted"`
	    if [ "$dvd_rw" == "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nEmpty dvd_rw setting in $configuration file.\
		" 7 62
		retval=$?
	    elif [ ! -e $dvd_rw ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no \"$dvd_rw\" device.\
		" 7 62
		retval=$?
	    elif [ "$empty_drive" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no disc in \"$dvd_rw\" device.\
		" 7 62
		retval=$?
	    elif [ "$non_dvd_disc" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no DVD disc in \"$dvd_rw\" device.\
		" 7 62
		retval=$?
	    else
		if [ -f "$iso_file" ]
		then
		    $reset
		    echo "Executing: $nice_write $growisofs_iso"
		    $nice_write $growisofs_iso
		    eject_media
		    display_message
		else
		    dialog --title "Error" --clear "$@" \
		    --msgbox "\nThere is no \"$iso_file\" file in the current directory.\
		    " 7 62
		    retval=$?
		fi
	    fi
	fi
}

read_data_media() # 3.4. () # 4.5.
{
	if [ "$number" == "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThe number of device is empty -- probably not plugged.\
	    " 7 62
	    retval=$?
	else
	    $reset
	    echo "Reading disc info. Please wait a while..."
	    $cdrdao_test > $tempfile 2>&1
	    lacking_drive=`cat $tempfile | grep "Cannot open SCSI device"`
	    lacking_disc=`cat $tempfile | grep "Unit not ready, giving up"`
	    if [ "$lacking_drive" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no \"$reader\" device.\
		" 7 62
		retval=$?
	    elif [ "$lacking_disc" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no data CD or DVD disc in \"$reader\" device.\
		" 7 62
		retval=$?
	    else
		$reset
		echo "Executing: dd if=$reader of=$iso_file"
		dd if=$reader of=$iso_file
		eject_media
		display_message
	    fi
	fi
}

link_iso_file() # 3.5. () # 4.6.
{
	dialog --backtitle "$backtitle" \
	--title "ISO File Selection (use arrows, tabulator, and space keys)" \
	--fselect "./" \
	11 62 2> $tempfile
	retval=$?
	orig_iso_file=`cat $tempfile`
	if [ -f "$orig_iso_file" ]
	then
	    isoinfo -l -i "$orig_iso_file" 1> $tempfile
	    if [ "`cat $tempfile | grep 'Directory listing'`" != "" ]
	    then
		if [ ! -e $iso_file ]
		then
		    $reset
		    echo "Executing: ln -s \"$orig_iso_file\" $iso_file"
		    ln -s "$orig_iso_file" $iso_file
		    display_message
		else
		    dialog --title "Error" --clear "$@" \
		    --msgbox "\nISO image \"$iso_file\" already exists.\
		    " 7 62
		    retval=$?
		fi
	    else
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThe \"$orig_iso_file\" is not valid ISO image.\
		" 7 62
		retval=$?
	    fi
	else
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThe \"$orig_iso_file\" is not valid filename.\
	    " 7 62
	    retval=$?
	fi
}

remove_iso_image() # 3.6. () # 4.7.
{
	select_directory
	cd "$directory"
	file="no"
	if [ -e $iso_file ]
	then
	    file="yes"
	fi
	if [ "$file" == "yes" ]
	then
	    dialog --title "Query" --clear "$@" \
	    --yesno "\nAre you sure you want to remove ISO image?" \
	    7 62
	    retval=$?
	    if [ "$retval" == "0" ]
	    then
		rm -f $iso_file
	    fi
	else
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no \"$iso_file\" file in the current directory.\
	    " 7 62
	    retval=$?
	fi
}

blank_cd_rw() # 2.15. () # 3.7.
{
	if [ "$number" == "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThe number of device is empty -- probably not plugged.\
	    " 7 62
	    retval=$?
	else
	    $reset
	    echo "Reading disc info. Please wait a while..."
	    $cdrdao_test > $tempfile 2>&1
	    lacking_drive=`cat $tempfile | grep "Cannot open SCSI device"`
	    lacking_disc=`cat $tempfile | grep "Unit not ready, giving up"`
	    empty_disc=`cat $tempfile | grep "CD-R empty           : yes"`
	    non_erasable_disc=`cat $tempfile | grep "CD-RW                : no"`
	    if [ "$lacking_drive" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no \"$reader\" device.\
		" 7 62
		retval=$?
	    elif [ "$lacking_disc" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThere is no data CD disc in \"$reader\" device.\
		" 7 62
		retval=$?
	    elif [ "$non_erasable_disc" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThe data CD disc in \"$reader\" device is not erasable.\
		" 7 62
		retval=$?
	    elif [ "$empty_disc" != "" ]
	    then
		dialog --title "Error" --clear "$@" \
		--msgbox "\nThe data CD disc in \"$reader\" device is empty.\
		" 7 62
		retval=$?
	    else
		dialog --backtitle "$backtitle" \
		--title "Blank CD-RW" \
		--radiolist "Select the method:" 22 62 15 \
		1 "Blank the disc fast" on \
		2 "Blank the entire disc" off \
		3 "Blank the last track" off \
		4 "Unreserve a reserved track" off \
		5 "Blank the tail of the track" off \
		6 "Unclose the last session" off \
		7 "Blank the last session" off \
		2> $tempfile
		retval=$?
		output=`cat $tempfile`
		if [ "$retval" = "0" ]
		then
		    $reset
		    $cdrecord_test_toc 2> $tempfile
		    if [ "`cat $tempfile | grep 'No disc'`" == "" ]
		    then
			case $output in
			1)
			    option="fast"
			;;
			2)
			    option="all"
			;;
			3)
			    option="track"
			;;
			4)
			    option="unreserve"
			;;
			5)
			    option="trtail"
			;;
			6)
			    option="unclose"
			;;
			7)
			    option="session"
			;;
			esac
			$reset
			echo "Executing: $cdrecord_write speed=$speed blank=$option"
			$cdrecord_write speed=$speed blank=$option
			eject_media
			display_message
		    else
			dialog --title "Error" --clear "$@" \
			--msgbox "\nThere is no data disc in \"$writer\" device.\
			" 7 62
			retval=$?
		    fi
		fi
	    fi
	fi
}

blank_dvd_fast() # 4.8.
{
	$dvd_rw_info_test > $tempfile 2>&1
	empty_drive=`cat $tempfile | grep "no media mounted"`
	non_dvd_disc=`cat $tempfile | grep "non-DVD media mounted"`
	dvd_rw_disc=`cat $tempfile | grep -E "DVD.RW|DVD.RAM"`
	if [ "$dvd_rw" == "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nEmpty dvd_rw setting in $configuration file.\
	    " 7 62
	    retval=$?
	elif [ ! -e $dvd_rw ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no \"$dvd_rw\" device.\
	    " 7 62
	    retval=$?
	elif [ "$empty_drive" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no disc in \"$dvd_rw\" device.\
	    " 7 62
	    retval=$?
	elif [ "$non_dvd_disc" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no DVD disc in \"$dvd_rw\" device.\
	    " 7 62
	    retval=$?
	elif [ "$dvd_rw_disc" == "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nDVD disc in \"$dvd_rw\" device is not erasable.\
	    " 7 62
	    retval=$?
	else
	    $reset
	    echo "Executing: $nice_write $dvd_rw_blank"
	    $nice_write $dvd_rw_blank
	    eject_media
	    display_message
	fi
}

blank_dvd_all() # 4.9.
{
	$dvd_rw_info_test > $tempfile 2>&1
	empty_drive=`cat $tempfile | grep "no media mounted"`
	non_dvd_disc=`cat $tempfile | grep "non-DVD media mounted"`
	dvd_rw_disc=`cat $tempfile | grep -E "DVD.RW|DVD.RAM"`
	if [ "$dvd_rw" == "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nEmpty dvd_rw setting in $configuration file.\
	    " 7 62
	    retval=$?
	elif [ ! -e $dvd_rw ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no \"$dvd_rw\" device.\
	    " 7 62
	    retval=$?
	elif [ "$empty_drive" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no disc in \"$dvd_rw\" device.\
	    " 7 62
	    retval=$?
	elif [ "$non_dvd_disc" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no DVD disc in \"$dvd_rw\" device.\
	    " 7 62
	    retval=$?
	elif [ "$dvd_rw_disc" == "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nDVD disc in \"$dvd_rw\" device is not erasable.\
	    " 7 62
	    retval=$?
	else
	    $reset
	    echo "Executing: $nice_write $dvd_rw_format"
	    $nice_write $dvd_rw_format
	    eject_media
	    display_message
	fi
}

erase_dvd() # 4.10.
{
	$dvd_rw_info_test > $tempfile 2>&1
	empty_drive=`cat $tempfile | grep "no media mounted"`
	non_dvd_disc=`cat $tempfile | grep "non-DVD media mounted"`
	dvd_rw_disc=`cat $tempfile | grep -E "DVD.RW|DVD.RAM"`
	if [ "$dvd_rw" == "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nEmpty dvd_rw setting in $configuration file.\
	    " 7 62
	    retval=$?
	elif [ ! -e $dvd_rw ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no \"$dvd_rw\" device.\
	    " 7 62
	    retval=$?
	elif [ "$empty_drive" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no disc in \"$dvd_rw\" device.\
	    " 7 62
	    retval=$?
	elif [ "$non_dvd_disc" != "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nThere is no DVD disc in \"$dvd_rw\" device.\
	    " 7 62
	    retval=$?
	elif [ "$dvd_rw_disc" == "" ]
	then
	    dialog --title "Error" --clear "$@" \
	    --msgbox "\nDVD disc in \"$dvd_rw\" device is not erasable.\
	    " 7 62
	    retval=$?
	else
	    $reset
	    echo "Executing: $nice_write $growisofs_erase"
	    $nice_write $growisofs_erase
	    eject_media
	    display_message
	fi
}

clean_up()
{
	if [ "$remove_iso_file" == "on" ]
	then
	    rm -f $iso_file
	fi
	rm -f $tempfile
}

# Help -----------------------------------------------------------------------

help()
{
    echo
    echo "cdwrite [ OPTION [<device_number>] ]"
    echo
    echo "OPTIONS"
    echo "  -c, --config"
    echo "  -C, --commands"
    echo "  -a, --abort"
    echo "  -r, --reset"
    echo "  -u, --unlock"
    echo
    echo "<device_number>"
    echo "  number from 1 to 5"
    echo
}

device_number()
{
    if [ "$3" == "1" -o "$3" == "2" -o "$3" == "3" -o "$3" == "4" -o "$3" == "5" ]
    then
	device_number=device$3
	cat $configuration | grep $device_number | cut -d '"' -f 2 > $tempfile
        output=`cat $tempfile`
	device=`echo $output | sed 's/"//g'`
	devices
fi
    if [ "$2" == "1" -o "$2" == "2" -o "$2" == "3" -o "$2" == "4" -o "$2" == "5" ]
    then
	device_number=device$2
	cat $configuration | grep $device_number | cut -d '"' -f 2 > $tempfile
        output=`cat $tempfile`
	device=`echo $output | sed 's/"//g'`
	devices
    fi
}

config()
{
    echo $configuration
    echo
    echo device = $device
    echo reader = $reader
    echo writer = $writer
    echo number = $number
    echo driver = $driver
    echo dvd_rw = $dvd_rw
    echo speed = $speed
    echo buffers = $buffers
    echo cd_dir = $cd_dir
    echo dvd_dir = $dvd_dir
    echo
    echo umount_call = $umount_call
    echo mkisofs_call = $mkisofs_call
    echo cdrecord_call = $cdrecord_call
    echo cdda2wav_call = $cdda2wav_call
    echo cdrdao_call = $cdrdao_call
    echo dvdrw_format_call = $dvdrw_format_call
    echo growisofs_call = $growisofs_call
    echo
    echo tempfile = $tempfile
    echo iso_file = $iso_file
    echo toc_file = $toc_file
    echo pregap = $pregap
    echo display_toc_file = $display_toc_file
    echo default_ripper = $default_ripper
    echo default_encoder = $default_encoder
    echo flac_compression = $flac_compression
    echo mp3_quality = $mp3_quality
    echo mp3_bitrate = $mp3_bitrate
    echo ogg_quality = $ogg_quality
    echo ogg_bitrate = $ogg_bitrate
    echo mp3_normalization = $mp3_normalization
    echo cd_player = $cd_player
    echo mplayer_speed = $mplayer_speed
    echo mplayer_cache = $mplayer_cache
    echo filename_convention = $filename_convention
    echo eject = $eject
    echo eject_command = $eject_command
    echo remove_files = $remove_files
    echo debug = $debug
    echo message = $message
    echo command = $command
    echo
}

commands()
{
    echo cdparanoia_test = $cdparanoia_test
    echo cdparanoia_read = $cdparanoia_read
    echo cdda2wav_indexes = $cdda2wav_indexes
    echo cdda2wav_read = $cdda2wav_read
    echo cdrdao_test = $cdrdao_test
    echo cdrdao_write = $cdrdao_write
    echo cdrdao_copy = $cdrdao_copy
    echo
    echo mkisofs_read_linux = $mkisofs_read_linux
    echo mkisofs_read_windows = $mkisofs_read_windows
    echo cdrecord_test_toc = $cdrecord_test_toc
    echo cdrecord_scan = $cdrecord_scan
    echo cdrecord_test_msinfo = $cdrecord_test_msinfo
    echo cdrecord_write = $cdrecord_write
    echo
    echo growisofs_zero = $growisofs_zero
    echo growisofs_merge = $growisofs_merge
    echo growisofs_iso = $growisofs_iso
    echo growisofs_erase = $growisofs_erase
    echo dvd_rw_info_test = $dvd_rw_info_test
    echo dvd_rw_blank = $dvd_rw_blank
    echo dvd_rw_format = $dvd_rw_format
    echo
#    echo cdrecord_blank_fast = $cdrecord_blank_fast
#    echo cdrecord_blank_all = $cdrecord_blank_all
#    echo
    echo cdparanoia_encoding = cdparanoia -d $reader -B \$track -
    echo cdda2wav_encoding = cdda2wav -D $reader -x -paranoia -t \$track -
    echo
}

if [ "$1" == "-h" -o "$1" == "--help" ]
then
    help
    exit
fi

if [ "$1" == "-c" -o "$1" == "--config" ]
then
    if [ "$2" == "-C" -o "$2" == "--commands" ]
    then
	$reset
	echo "$copyright"
	echo
	device_number $1 $2 $3
	config
	commands
	exit
    else
	$reset
	echo "$copyright"
	echo
	device_number $1 $2 $3
	config
	exit
    fi
fi

if [ "$1" == "-C" -o "$1" == "--commands" ]
then
    if [ "$2" == "-c" -o "$2" == "--config" ]
    then
	$reset
	echo "$copyright"
	echo
	device_number $1 $2 $3
	commands
	config
	exit
    else
	$reset
	echo "$copyright"
	echo
	device_number $1 $2 $3
	commands
	exit
    fi
fi

if [ "$1" == "-a" -o "$1" == "--abort" ]
then
    device_number $1 $2
    echo "Executing: $cdrecord_call -abort dev=$reader"
    $cdrecord_call -abort dev=$reader
    exit
fi

if [ "$1" == "-r" -o "$1" == "--reset" ]
then
    device_number $1 $2
    echo "Executing: $cdrecord_call -reset dev=$reader"
    $cdrecord_call -reset dev=$reader
    exit
fi

if [ "$1" == "-u" -o "$1" == "--unlock" ]
then
    device_number $1 $2
    echo "Executing: $cdrdao_call unlock --device $reader"
    $cdrdao_call unlock --device $reader
    exit
fi

# Main -----------------------------------------------------------------------

while [ "$masterretval" = "0" ]
do
    main_menu
    case $output in
    1)
	preferences_submenu
	case $output in
	1)
	    select_device
	;;
	2)
	    select_speed
	;;
	3)
	    scan_scsi_devices
	;;
	4)
	    umount_device
	;;
	5)
	    display_config
	;;
	esac
    ;;
    2)
	audio_cd_submenu
	case $output in
	1)
	    audio_cd_media_info
	;;
	2)
	    read_audio_tracks
	    case $output in
	    1)
		read_entire_cd
	    ;;
	    2)
		read_selected_tracks
	    ;;
	    esac
	;;
	3)
	    write_audio_tracks
	;;
	4)
	    rename_wav_files
	;;
	5)
	    remove_wav_files
	;;
	6)
	    copy_audio_cd
	;;
	7)
	    write_bin_file
	;;
	8)
	    remove_bin_files
	;;
	9)
	    encode_audio_tracks
	;;
	10)
	    normalize_audio_files
	;;
	11)
	    change_gain
	;;
	12)
	    convert_audio_files
	;;
	13)
	    play_audio_files
	;;
	14)
	    play_audio_cd
	;;
	15)
	    blank_cd_rw
	;;
	esac
    ;;
    3)
	data_cd_submenu
	case $output in
	1)
	    data_cd_media_info
	;;
	2)
	    create_cd_iso_image
	    case $output in
	    1)
		first_session_iso_image
	    ;;
	    2)
		next_session_iso_image
	    ;;
	    esac
	;;
	3)
	    write_cd_iso_image
	    case $output in
	    1)
		remove_iso_file
		multi_session_writing_mode
	    ;;
	    2)
		remove_iso_file
		closing_session_writing_mode
	    ;;
	    esac
	;;
	4)
	    read_data_media
	;;
	5)
	    link_iso_file
	;;
	6)
	    remove_iso_image
	;;
	7)
	    blank_cd_rw
	;;
	esac
    ;;
    4)
	dvdr_dvdrw_disc_submenu
	case $output in
	1)
	    dvd_media_info
	;;
	2)
	    write_first_session
	;;
	3)
	    write_next_session
	;;
	4)
	    write_dvd_iso_image
	;;
	5)
	    read_data_media
	;;
	6)
	    link_iso_file
	;;
	7)
	    remove_iso_image
	;;
	8)
	    blank_dvd_fast
	;;
	9)
	    blank_dvd_all
	;;
	10)
	    erase_dvd
	;;
	esac
    ;;
    5)
	about_cdwrite_submenu
    ;;
    6)
	masterretval=1
    ;;
    esac
done

clean_up

# Copyright ------------------------------------------------------------------

$reset
echo "$copyright"
echo

