#!/bin/sh
# the next line restarts using wish (Emacs clue: -*- tcl -*- ) \
    exec wish "$0" "$@"

proc interface {} {

wm title . "CDR Toaster - [version]"

label .title -text {CDR Toaster} -font -*-times-bold-r-*-*-30-*-*-*-*-*-*-*

frame .b -border 3 -relief raised
button .b.burn -text {Have a Cook-Off}

set m .b.trick.m
menubutton .b.trick -text {Do Tricks} -menu $m -relief raised -border 2
menu $m -tearoff 0
$m add command -label "Read CDROM to create ISO image" -command read_iso
$m add command -label "Read audio tracks from CDROM" -command read_audio
$m add command -label "Blank a CDRW Media" -command blank_cdrw

button .b.quit -text {Never Mind}

set m .b.help.m
menubutton .b.help -text {Get Help!} -menu $m -relief raised -border 2
menu $m -tearoff 0
$m add command -label "About" -command help
$m add command -label "Clarifications" -command help_clarify

frame .audio
label .audio.la -text {Audio Track List} 
frame .audio.f -border 3 -relief sunken 
listbox .audio.f.lb -height 16 -width 30 -selectmode browse  -yscrollcommand {.audio.f.sb set}
scrollbar .audio.f.sb -orient v -command {.audio.f.lb yview}
frame .audio.m
button .audio.m.up -text {Move up} -command {
 set num [.audio.f.lb curselection]
 if {""==$num} return
 if {$num==0} return
 flip $num
 incr num -1
 .audio.f.lb selection set $num
 .audio.f.lb see $num
}
button .audio.m.dn -text {Move Down} -command {
 set num [.audio.f.lb curselection]
 if {""==$num} return
 incr num 1
 if {$num==[.audio.f.lb index end]} return
 flip $num
 .audio.f.lb selection set $num
 .audio.f.lb see $num
}
frame .audio.ar -relief flat 
button .audio.ar.add -text {Add} -command {
audio_add
}
button .audio.ar.rem -text {Remove} -command {
audio_remove
}
button .audio.remall -text {Remove All} -command audio_removeall

label .lds -text "Data Track Source"

frame .ds -border 3 -relief sunken 
radiobutton .ds.none -text {Make audio-only disk} -variable {ds} -value none
radiobutton .ds.mk -text {Make data track on the fly} -variable {ds} -value mkisofs
radiobutton .ds.pre -text {Use pre-made data track} -variable {ds} -value premade
entry .ds.tname -width 20 -font 7x14
.ds.tname insert 0 "image.iso"

## Andrew found the missing code, so now TK8 users (of which I have become)
## can reap the benefits of a standard dialog.

# Lets make the selection button if you have tk8.0 +
if {[info tclversion] >= 8.0} {
  button .ds.select -text {Select} -command {my_image}
} else {
  button .ds.select -text {Select} -command {
    perusefile
    .ds.tname delete 0 end
    .ds.tname insert 0 $perusedfile
  }
}


label .lm -text {Data Track Creation Options}
frame .m -border 3 -relief sunken

frame .m.scan
checkbutton .m.scan.all -text {Keep all files (backups etc.)} -variable {all}
checkbutton .m.scan.links -text {Follow SymLinks} -variable {follow}

frame .m.file
checkbutton .m.file.joliet -text {Joliet support} -variable {joliet}
checkbutton .m.file.trans -text {Translation Tables (old)} -variable {trans}

frame .m.rock -border 0 -relief flat 
label .m.rock.l -text {Rock Ridge:}
radiobutton .m.rock.none -text {None} -variable {rock} -value none
radiobutton .m.rock.rational -text {Rationalized} -variable {rock} -value rat
radiobutton .m.rock.literal -text {Literal} -variable {rock} -value lit

frame .m.volid -border 0
label .m.volid.l -text {Volume ID (name)}
entry .m.volid.e -width 32 -font 7x14
frame .m.root -border 0 -relief flat
label .m.root.l -text {Root of Tree}
entry .m.root.e -width 24 -font 7x14
button .m.root.p -text "Peruse" -command {
 peruse
 .m.root.e delete 0 end
 .m.root.e insert 0 $perused
}

frame .m.custom -border 0
checkbutton .m.custom.use -text "Use Custom Layout" -variable custom(use)
button .m.custom.b -text {Set Custom Layout} -command custom_layout

frame .m.boot
checkbutton .m.boot.check -text "El Torito Bootable CD" -variable boot
button .m.boot.opts -text "Boot Setup" -command boot_setup

label .lc -text {cdrecord options} 
frame .c -border 3 -relief sunken
frame .c.flag
checkbutton .c.flag.dummy -text {Dummy Burn} -variable {dummy}
checkbutton .c.flag.eject -text {Eject when done} -variable {eject}
button .c.flag.pad -text "Padding..." -command padbox
frame .c.dev -border 0 -relief flat 
label .c.dev.l -text {Device} 
entry .c.dev.e -width 20 -font 7x14 -textvariable device
frame .c.speed
label .c.speed.l -text "Speed:"
radiobutton .c.speed.1 -text "1x" -variable speed -value 1
radiobutton .c.speed.2 -text "2x" -variable speed -value 2
radiobutton .c.speed.4 -text "4x" -variable speed -value 4
radiobutton .c.speed.6 -text "6x" -variable speed -value 6
radiobutton .c.speed.8 -text "8x" -variable speed -value 8
radiobutton .c.speed.10 -text "10x" -variable speed -value 10  
radiobutton .c.speed.12 -text "12x" -variable speed -value 12  


pack .title -side top
pack .b -side top -ipadx 30 -ipady 10
pack .b.burn -side left -expand 1
pack .b.trick -side left -expand 1 -ipady 1 -ipadx 1
pack .b.quit -side left -expand 1
pack .b.help -side left -expand 1
pack .audio -side right -anchor n
pack .audio.la -side top
pack .audio.f -side top
pack .audio.f.lb -side left
pack .audio.f.sb -side left -fill y
pack .audio.m -side top
pack .audio.m.up -side left
pack .audio.m.dn -side left
pack .audio.ar -side top
pack .audio.ar.add -side left
pack .audio.ar.rem -side left
pack .audio.remall -side top
pack .lds -side top
pack .ds -side top -padx 5
pack .ds.none -side bottom -anchor w
pack .ds.mk -side bottom -anchor w
pack .ds.pre -side left -anchor w
pack .ds.tname -side left -anchor w
pack .ds.select -side right -anchor n
pack .lm -side top
pack .m -side top
pack .m.scan -side top -fill x
pack .m.scan.all -side left
pack .m.scan.links -side right
pack .m.file -side top -fill x
pack .m.file.joliet -side left
pack .m.file.trans -side right
pack .m.rock -side top -anchor w
pack .m.rock.l -side left
pack .m.rock.none -side left
pack .m.rock.rational -side left
pack .m.rock.literal -side left
pack .m.volid -side top -anchor w
pack .m.volid.l -side left
pack .m.volid.e -side left
pack .m.root -side top -anchor w -fill x
pack .m.root.l -side left
pack .m.root.e .m.root.p -side left
pack .m.custom -side top -fill x
pack .m.custom.use -side left
pack .m.custom.b -side right
pack .m.boot -side top -anchor w -fill x
pack .m.boot.check -side left
pack .m.boot.opts -side left -expand 1
pack .lc -side top
pack .c -side top
pack .c.flag -side top
pack .c.flag.dummy -side left
pack .c.flag.eject -side left
pack .c.flag.pad -side left
pack .c.dev -side top
pack .c.dev.l -side left
pack .c.dev.e -side left
pack .c.speed -side top
pack .c.speed.l .c.speed.1 .c.speed.2 .c.speed.4 .c.speed.6 .c.speed.8 .c.speed.10 .c.speed.12 -side left

frame .spacer -height 10
pack .spacer -side top


}
proc toast {} {
global apad dpad ds

if {$ds == "none"} {
set source ""
}

if {$ds == "premade"} {
 set source [.ds.tname get]
 if {![file isfile $source]} {
 alert "Premade data track must be a regular file."
 return
 }
}

if {$ds == "mkisofs"} {
 set source -
 set mk [make_mkisofs_cmd]
 if {$mk == ""} {
  return
 }
}

set cd [base_cdrec]

set nuke ""
set convlist ""

if {$source != ""} {
 lappend cd -data
 if $dpad {lappend cd "-pad"}
 lappend cd $source
}
set tmp [.audio.f.lb get 0 end]
if {$tmp == ""} {
 if {$ds == "none"} {
  alert "No audio tracks were specified for an audio-only disk. This makes no sense."
  return
 }
} else {
 lappend cd -audio
 if $apad {lappend cd "-pad"} else {
  if $dpad {lappend cd "-nopad"}
 }
 set warned 0
 foreach i $tmp {
  ## Have to validate audio track type
  switch [file extension $i] {
   .mp2 -
   .mpg -
   .mp3 {
    if {$warned == 0} {
     # Give use opportunity to opt out. Also collect info for conversion.
     set tempdir [get_mp3_burn_info]
     if {$tempdir==""} {
      alert "An understandable choice."
      return
     }
     set warned 1
    }
    # Queue for conversion burning, and nuking.
    # Yes, there are race conditions. No, I don't care too much. If
    # you have a fix for the race conditions which is not too complicated,
    # tell me and I'll probably add your fix. Thanks.
    set tail [file tail $i]
    set tempfile [file join $tempdir "###${tail}.wav"]
    if [file exists $tempfile] {
     alert "$tempfile already exists. I will cancel the burn and let you decide what to do."
     return
    }
    lappend convlist $i
    lappend nuke $tempfile
    lappend cd $tempfile
   }
   
   .wav -
   .raw -
   .pcm {
    # queue for burning
    lappend cd $i
   }
   
   default {
    # Note the offending file for the user.
    alert "I don't recognize the file extention on '$i'. For safety reasons, I won't burn that file as an audio track.\n\nI'd suggest removing it and others like it from the audio track list."
    return
   }
  }
 }
}

if {$ds == "mkisofs"} { set cd [concat $mk $cd] }

set shellCommand $cd
regsub -all {///PIPE///} $shellCommand "|" shellCommand

if {[sure "About to run command:\n\n$shellCommand\n\nAre you sure?"]} {
 set err 0
 set err [catch {
  # Run necessary conversions
  if [llength $convlist] {
    
    toplevel .conv
    wm title .conv "Converting MP3s"
    text .conv.t -width 80 -height [llength $convlist]
    pack .conv.t
    
    foreach orig $convlist tempfile $nuke {
      .conv.t insert end "$orig --> $tempfile ..."
      update
      exec mpg123 -s $orig | sox -t raw -w -s -c 2 -r 44100 - -t wav $tempfile >&/dev/null
      .conv.t insert end " Done.\n"
    }
    
    destroy .conv
  }
 }]
 
 set cmd_list [list $cd]
 
 if $err {
  alert "Some error occured during the MPEG conversion process. You probably ran out of space or don't have the mpg123 and sox tools installed."
 } else {
  # Remove temp files when done...
  foreach tempfile $nuke {
    lappend cmd_list [list rm $tempfile]
  }
  
  dispatch $cmd_list {CD Burn Progress}
 }

}

}
proc audio_add {} {
catch {destroy .add}
toplevel .add
wm title .add {Add Audio Tracks}


label .add.l -text {Track Name (Globals OK)} 
entry .add.e -width 32 -font 7x14 
button .add.ok -text {OK} -command {
set tmp [glob -nocomplain [.add.e get]]
foreach i $tmp {.audio.f.lb insert end $i}
destroy .add
}
button .add.cancel -text {Cancel} -command {
destroy .add
}

pack .add.l -side top
pack .add.e -side top
pack .add.ok -side left
pack .add.cancel -side right


}
proc audio_remove {} {
set victim [.audio.f.lb curselection]
catch {.audio.f.lb delete $victim}
}
proc audio_removeall {} {
.audio.f.lb delete 0 end
}
proc flip n {
.audio.f.lb selection clear 0 end
set above [expr $n - 1]
set tmp [.audio.f.lb get $n]
.audio.f.lb insert $above $tmp
.audio.f.lb delete [expr $n + 1]
}
proc alert msg {
tk_dialog .alert Alert $msg info 0 "So it goes."
}
proc help {} {
catch {destroy .help}
toplevel .help
wm title .help {Help!}

button .help.ok -text {This Rules!} -command {destroy .help}
text .help.t -width 60 -height 18 -yscrollcommand {.help.sb set} -wrap word
scrollbar .help.sb -orient v -command {.help.t yview}

pack .help.ok -side bottom
pack .help.t -side left
pack .help.sb -side left -fill y

.help.t insert 1.0 "About CDR-Toaster version [version]"
.help.t insert end {

This is a graphical front-end for the excellent programs 'mkisofs', 'cdrecord', and 'cdparanoia'.

It's good for:
 o Avoiding coasters due to command-line mistakes
 o Eliminating the tedium of consulting documentation
 o Feeling more secure in a correct set of options
 o Being lazy

As you can guess, I wrote it for what it's good for.

CDR-Toaster is copyright 1999 Ian Kjos <brooke@jump.net>

CDR-Toaster contains some code contributed by:
  Daniel Caujolle-Bert <lobadia@club-internet.fr>
  Andrew Williams <woodchuk@bellatlantic.net>
  Claus Brunzema <chb@ossi.fho-emden.de>
  Aaron Sherman <ajs@ajs.com>
  Fred D. Feirtag <fdfeirt@valinor.sandia.gov>
  And a cast of thousands!
  
  If you don't see your name here and you should,
  please send me some e-mail about it. Preferably
  with a diff.

CDR-Toaster is availible under the GNU General
Public License version 2.0 or later (at your option).
This license can be found at 
http://www.gnu.org/copyleft/gpl.html

}

.help.t config -state disabled

}
proc sure msg {
return [expr ![tk_dialog .alert Confirmation $msg questhead -1 yes no]]
}
proc read_iso {} {
catch {destroy .top}
toplevel .top
wm title .top "Read ISO image"

frame .top.dev -border 0 -relief flat 
frame .top.file -border 0 -relief flat 
frame .top.b -border 0 -relief flat 
label .top.dev.l -text {device to read:} 
label .top.file.l -text {file to write:} 
entry .top.dev.e -width 20 -font 7x14 
entry .top.file.e -width 20 -font 7x14 
button .top.b.ok -text {Do it} -command {
if {![file readable [.top.dev.e get]]} {
 alert "You do not have read permissions on '[.top.dev.e get]'. You should probably be running this as root."
 return
}
set cmd_echo [list echo "This takes a good while."]
set cmd_dd [list dd if=[.top.dev.e get] of=[.top.file.e get] bs=1024k]
set cmd_eject [list eject [.top.dev.e get]]
dispatch [list $cmd_echo $cmd_dd $cmd_eject] "Reading ISO Image"
destroy .top
}
button .top.b.dis -text {Forget it} -command {
destroy .top
}

pack .top.dev -side top
pack .top.file -side top
pack .top.b -side top -fill x
pack .top.dev.l -side left
pack .top.file.l -side left
pack .top.dev.e -side left
pack .top.file.e -side left
pack .top.b.ok -side left -expand 1
pack .top.b.dis -side left -expand 1

.top.dev.e insert 0 [detect_cdrom_device]
.top.file.e insert 0 "image.iso"

}
proc read_audio {} {
catch {destroy .top}
toplevel .top
wm title .top "Read audio tracks"

frame .top.path1
label .top.path1.l -text "(optional) Rip Source: "
entry .top.path1.source -width 15
button .top.path1.peruse -text "Peruse" -command {
peruse_block
.top.path1.source delete 0 end
.top.path1.source insert 0 $perusedfile
}

frame .top.path2
label .top.path2.l -text "Rip Destination: "
entry .top.path2.dest
button .top.path2.peruse -text "Peruse" -command {
peruse
.top.path2.dest delete 0 end
.top.path2.dest insert 0 $perused
}

frame .top.1 -border 0 -relief flat 
frame .top.2 -border 0 -relief flat 
frame .top.3 -border 0 -relief flat 
frame .top.4 -border 0 -relief flat 
radiobutton .top.1.entire -text {Rip Entire Disk} -variable {rip} -value {all}
radiobutton .top.2.track -text {Tracks:} -variable {rip} -value {track}
radiobutton .top.3.custom -text {Custom Options} -variable {rip} -value {custom}
button .top.4.ok -text {Do it} -command {
 set tmp "Ripping Audio"
 switch $rip {
  all {
    if {[.top.path1.source get] != ""} {
      set dp [list "cdparanoia -d [.top.path1.source get] -w -B 1-"]
    } else {
      set dp [list "cdparanoia -s -w -B 1-"]
    }
  }
  track {
   if {[.top.path1.source get] != ""} {
    set dp [list "cdparanoia -d [.top.path1.source get] -w -B [.top.2.from get]-[.top.2.to get]"]
   } else {
    set dp [list "cdparanoia -s -w -B [.top.2.from get]-[.top.2.to get]"]
   }
  }
  custom {
   set dp [list "cdparanoia [.top.3.opt get]"]
  }
 }
 if {[.top.path2.dest get] != ""} {
  set dp [concat [list "cd [.top.path2.dest get]"] $dp]
 }
 dispatch $dp $tmp
 destroy .top
}
button .top.4.dis -text {Not now.} -command {
 destroy .top
}
label .top.2.lfr -text {From:} 
entry .top.2.from -width 4 -font 7x14 
label .top.2.lto -text {To:} 
entry .top.2.to -width 4 -font 7x14 
entry .top.3.opt -width 20 -font 7x14 

pack .top.path1 -side top -fill x
pack .top.path1.l .top.path1.source .top.path1.peruse -side left
pack .top.path2 -side top -fill x
pack .top.path2.l .top.path2.dest .top.path2.peruse -side left
pack .top.1 -side top -fill x
pack .top.2 -side top -fill x
pack .top.3 -side top -fill x
pack .top.4 -side top -fill x
pack .top.1.entire -side left -anchor w
pack .top.2.track -side left -anchor w
pack .top.3.custom -side left -anchor w
pack .top.4.ok -side left -expand 1
pack .top.4.dis -side left -expand 1
pack .top.2.lfr -side left -anchor e
pack .top.2.from -side left -anchor e
pack .top.2.lto -side left -anchor e
pack .top.2.to -side left -anchor e
pack .top.3.opt -side left -anchor e

global rip
set rip all


}
proc blank_cdrw {} {
global blank

catch {destroy .top}
toplevel .top
wm title .top "Blank CDRW Media"

frame .top.type -border 0 -relief flat 
frame .top.b -border 0 -relief flat 
label .top.type.l -text {Blank Type} 
frame .top.type.r
radiobutton .top.type.all -text {Entire Disk} -variable {blank} -value {all}
radiobutton .top.type.session -text {Session} -variable {blank} -value {session}
radiobutton .top.type.track -text {Track} -variable {blank} -value {track}
radiobutton .top.type.fast -text {Fast} -variable {blank} -value {fast}
radiobutton .top.type.r.unreserve -text {Unreserve} -variable {blank} -value {unreserve}
radiobutton .top.type.r.trtail -text {Track Tail} -variable {blank} -value {trtail}
radiobutton .top.type.r.unclose -text {Unclose} -variable {blank} -value {unclose}
button .top.b.ok -text {Do it} -command {
 set cmd [base_cdrec]
 lappend cmd "blank=${blank}"
 dispatch [list $cmd] "Blanking CDRW: ${blank}"
 destroy .top
}
button .top.b.dis -text {Let's not.} -command {
 destroy .top
}

pack .top.type -side top
pack .top.b -side top -fill x
pack .top.type.l -side left
pack .top.type.r -side right -fill y
pack .top.type.all -side top -anchor w
pack .top.type.session -side top -anchor w
pack .top.type.track -side top -anchor w
pack .top.type.fast -side top -anchor w
pack .top.type.r.unreserve -side top -anchor w
pack .top.type.r.trtail -side top -anchor w
pack .top.type.r.unclose -side top -anchor w
pack .top.b.ok -side left -expand 1
pack .top.b.dis -side left -expand 1

set blank session

}

proc dispatch {cmdList title} {
 
 set shellCommand ""
 set systype [exec /bin/uname -s]
 
 switch $systype {
 	"SunOS" { set xdir "/usr/openwin/bin" }
 	default { set xdir "/usr/X11R6/bin" }
 }
 
 foreach cmd $cmdList {
   set tmp [join $cmd "' '"]
   append shellCommand "'$tmp'; "
 }
 
 regsub -all {\'///PIPE///\'} $shellCommand "|" shellCommand

 append shellCommand "echo Done. Press Enter; read"
 
 puts $shellCommand
 
 exec $xdir/xterm -geometry 80x6 -title $title -e sh -c $shellCommand &
 
}

proc base_cdrec {} {
global dummy eject speed
set cd [list cdrecord -v]
if {$dummy} {lappend cd -dummy}
if {$eject} {lappend cd -eject}
set dev [.c.dev.e get]
if {![check_device $dev]} {
 alert "You don't have write permission to the device you selected. You should probably be running this as root. If that is not an option, get the system administrator to give you write privileges for the cd-writer."
 return -code return
}
lappend cd "dev=$dev" "speed=$speed"
return $cd
}
proc help_clarify {} {
catch {destroy .help}
toplevel .help
wm title .help {Help!}

set helpstuff {
 {All Files}
{   Ordinarily files with names containing '#' or '~'
   are backup files on Linux. CDR-Toaster will instruct
   mkisofs to skip these files in the creation of a
   CD. If you DON'T want to skip them, click this box.
}
 {Joliet support}
{   This stores long filenames on a CD so Windows 9x/NT
   machines can read them. Unlike the Joliet CDs produced
   on MS-Windows, you can still see the 8.3 filenames on
   MS-Dos and other systems that don't support Joliet.
   Linux kernels 2.2 and up can also read Joliet names.
}
 {Rock Ridge}
{   This is another way to store long filenames on a CD.
   It works with all Un*x-like systems that also support
   CDs. It allows file permissions, ownership info, and
   symbolic links to be stored and appear as such on the
   CD. If you choose "Literal", the final CD will look in
   all respects identical to the directory hierarchy it
   was created from, except for some reason you can't
   write to it. This could be good for system backups.
   If you choose "Rationalized", you get a more generally
   useful CD. It sets the user and group of all files to
   root, removes SUID and SGID bits, makes all files and
   folders readable globally, sets all execute bits for
   anything that has any execute bits, and clears all
   write bits.
}
 {Follow SymLinks}
{   Normally symbolic links are ignored, unless you use
   Rock Ridge, when they are copied literally. If you
   want the actual files and folders pointed to by the
   symbolic links to show up on the CD, check this box.
}
 {Translation Tables}
{   Puts a file called "TRANS.TBL" in every directory on
   the CD which maps 8.3 files to their long equivalents.
   Very simple, very old.
}
 {MPEG Compressed Audio}
{   CDR-Toaster will extract MPEG Compressed audio (mp3)
   if you have mpg123 and sox installed. This way you
   can burn an audio CD from mp3 files. The sound will
   not be as good quality as the original uncompressed
   audio, but that's the trade off that a lossy compression
   like MPEG makes to attain the high ratios that it does.
   For more information, point your web browser at these:
   http://www.mpeg.org/MPEG/
   http://drogo.cselt.stet.it/mpeg/
}
 {PCM vs. WAV}
{   WAV is a format composed of 44 bytes of header data,
   then raw PCM data. PCM means only pulse code modulation,
   which is to say a digitial audio format made of numbers
   to signify each sample. There is no attempt made to
   compress the signals beyond the decisions of sample rate
   and dynamic range.

   For CDR-Toaster to burn audio, you can feed it either a
   raw PCM file or a WAV file. WAV files are better because
   they contain enough information in the header to ensure
   that the audio format is correct. To get an audio track
   to burn properly, the PCM data needs to be composed of
   exactly the same structure as on a CD - that is 41.1 khz
   16 bit stereo. Anything else and you get an unusable
   audio track.

   When using WAV files ripped from a CD, (as CDPARANOIA
   does) you will get files that are already correct. If
   you want to use a WAV file you recorded yourself, it is
   wise to ensure you recorded cd-quality (16 bit stereo
   44.1khz) uncompressed audio. If you did otherwise, there
   are many format converters available which can convert to
   this format without any signal loss. The other issue with
   audio files you record yourself is that they must be a
   precise multiple of 1/75th of a second. This is where
   audio padding comes into play. If you do not have the
   right number of samples, the Pad Audio option can round
   up using absolute silence.
}
 {Burning Bootable CDs}
{   When your computer boots from a CD, it treats part of
   the CD as a floppy drive. The emulation is at the BIOS
   level, so programs like the Linux Kernel operate
   normally but programs like LILO or SYSLINUX are fooled.
   All attempts to access the (primary) floppy drive are
   mapped to the "BOOT" extent on the CD.

   Step by step, the easiest way to make a bootable CD
   is as follows:

   1. Make a real live floppy disk that boots your computer
      ok. There are many rescue disk packages which are
      good for this purpose, or you can use your operating
      system install disk. Make sure the floppy actually
      boots properly.

   2. Copy this floppy disk to an image file. Under Linux
      you can type:
         dd if=/dev/fd0 of=bootimage.bin bs=18k

   3. Put this file in the directory you are going to make
      your CD from:
         mv bootimage.bin /tmp/my_cdrom/

   4. Bring up CDR-Toaster. Put a check in the "El Torito
      Bootable CD" box, and click the "Boot Setup" button.

   5. For "Boot Image Path" type "bootimage.bin"

   6. For "Boot Catalog Path" type "bootcat.bin"

   7. Click OK, and set the "Root of Tree" appropriately.
      From the example above, it would be "/tmp/my_cdrom"

   8. Finish burning your CD as you would normally.

   9. Set your bios to boot from CD, and things should
      be happy.

   The Boot Catalog is a place for information about the
   Boot Image. It is generated automatically. It need not
   exist beforehand. In fact, if it does exist, it will
   be overwritten.

   Both paths are interpretted relative to the root
   of the CD.

   For more information, see "man mkisofs". It should be on
   your system for CDR-Toaster to operate properly.
}
 {Using ATAPI CDR(W) Devices}
{   ATAPI CDR(W)s are special. The Linux kernel needs to be 
   compiled specially for them to work properly.

   Basically, you must TURN OFF IDE CDROM support and 
   instead use both "SCSI emulation" and "SCSI generic 
   support".

   If you have never compiled a kernel before, you are wise 
   to read the Kernel Howto.

   I tend to use "make menuconfig" to configure the kernel, 
   and these are the things I did:

   Under "Block devices", I set "Include IDE/ATAPI CDROM 
   support" to "n" and I set "SCSI emulation support" to 
   "Y".

   Under "SCSI support", I make sure there is "SCSI CD-ROM 
   support" and "SCSI generic support" turned on. Since I 
   have no true SCSI cards, I need nothing from the "SCSI 
   low-level drivers" section.

   I built and installed this kernel. As a result, my 
   CD-ROM detected as a scsi device (/dev/scd0 in my case) 
   during bootup and I have been able to burn to /dev/sg0 
   as is the default in CDR-Toaster.

   For more information on the special needs of your 
   CDR(W), see the documentation for "cdrecord" which is 
   the burn program that CDR-Toaster calls out to.
}
}

frame .help.b

set m .help.b.topic.m
menubutton .help.b.topic -menu $m -text Topic -relief raised
menu $m

button .help.b.ok -text {This Rules!} -command {destroy .help}
text .help.t -width 60 -height 18 -yscrollcommand {.help.sb set} -wrap word
scrollbar .help.sb -orient v -command {.help.t yview}

pack .help.b -side bottom -fill x
pack .help.b.topic -side left
pack .help.b.ok -side bottom
pack .help.t -side left
pack .help.sb -side left -fill y

.help.t insert 1.0 {Clarifications:

}

foreach {topic body} $helpstuff {
 set end [.help.t index end]
 set line [expr $end - 1]
 $m add command -label $topic -command ".help.t yview $line"
 .help.t insert end " o $topic\n"
 .help.t insert end "$body\n"
 .help.t tag add head $line $end
}

.help.t tag configure head -foreground white -background blue -font 8x16

.help.t tag add top 1.0 2.0
.help.t tag config top -foreground white -background blue -font 10x20

.help.t config -state disabled

}
proc padbox {} {

catch {destroy .pad}
toplevel .pad
wm title .pad {Padding Options}

frame .pad.audio
checkbutton .pad.audio.cb -text {Pad Audio Tracks} -variable {apad}
message .pad.audio.amesg -text {This option will pad audio track data to be a multiple of 2352 bytes. The padding is done with all zeroes which means complete silence. See also PCM vs. WAV in help.
}
frame .pad.data
checkbutton .pad.data.cb -text {Pad Data Track} -variable {dpad}
message .pad.data.dmesg -text {This option appends 15 sectors of zeroes to each data track. This works around a bug in very old Linux kernels.}
button .pad.ok -text Dismiss -command {destroy .pad}

pack .pad.audio -fill x
pack .pad.audio.cb -side left -anchor n
pack .pad.audio.amesg
pack .pad.data -fill x
pack .pad.data.cb -side left -anchor n
pack .pad.data.dmesg
pack .pad.ok

}
proc peruse {} {
# Just browse directories

global perused

set perused ""

catch {destroy .peruse}
toplevel .peruse
wm title .peruse {Peruse Folders}

frame .peruse.dir -border 0 -relief flat 
frame .peruse.mod -border 0 -relief flat 
frame .peruse.control -border 0 -relief flat
menubutton .peruse.dir.up -menu .peruse.dir.up.m -relief raised -border 2
button .peruse.dir.ok -text {<-- Accept} -command {}
listbox .peruse.mod.lb -height 10 -width 20 -selectmode browse -yscrollcommand {.peruse.mod.sb set}
scrollbar .peruse.mod.sb -orient v -command {.peruse.mod.lb yview}
button .peruse.control.go -text {Delve} -command {}
button .peruse.control.cancel -text {Cancel} -command {}

pack .peruse.dir -side top -fill x
pack .peruse.mod -side top -fill x
pack .peruse.control -side top -fill x
pack .peruse.dir.up -side left
pack .peruse.dir.ok -side right
pack .peruse.mod.lb -side left -fill y -anchor w
pack .peruse.mod.sb -side left -fill y -anchor w
pack .peruse.control.go -side left -expand 1
pack .peruse.control.cancel -side left -expand 1

peruse_populate [pwd]
tkwait window .peruse

}
proc perusefile {} {
# Just browse files

global perusedfile

set perusedfile ""

catch {destroy .perusefile}
toplevel .perusefile
wm title .perusefile {Peruse Files}

frame .perusefile.dir -border 0 -relief flat 
frame .perusefile.mod -border 0 -relief flat 
frame .perusefile.control -border 0 -relief flat
menubutton .perusefile.dir.up -menu .perusefile.dir.up.m -relief raised -border 2
button .perusefile.dir.ok -text {<-- Accept} -command {
}
listbox .perusefile.mod.lb -height 10 -width 20 -selectmode browse -yscrollcommand {.perusefile.mod.sb set}
scrollbar .perusefile.mod.sb -orient v -command {.perusefile.mod.lb yview}
button .perusefile.control.go -text {Delve} -command {
}
button .perusefile.control.cancel -text {Cancel} -command {
}

pack .perusefile.dir -side top -fill x
pack .perusefile.mod -side top -fill x
pack .perusefile.control -side top -fill x
pack .perusefile.dir.up -side left
pack .perusefile.dir.ok -side right
pack .perusefile.mod.lb -side left -fill y -anchor w
pack .perusefile.mod.sb -side left -fill y -anchor w
pack .perusefile.control.go -side left -expand 1
pack .perusefile.control.cancel -side left -expand 1

perusefile_populate [pwd]
tkwait window .perusefile

}
proc peruse_populate dir {

set tmp [file tail $dir]
if {$tmp == ""} {set tmp /}
.peruse.dir.up configure -text $tmp

set m .peruse.dir.up.m
catch {
destroy $m
}
menu $m -tearoff 0

set up /
foreach dcomp [file split $dir] {
 set up [file join $up $dcomp]
 $m add command -label $dcomp -command "peruse_populate \"$up\""
}

.peruse.mod.lb delete 0 end
set tmp [lsort [glob $dir/.* $dir/*]]
foreach dcan $tmp {
 if [file isdirectory $dcan] {
  set tail [file tail $dcan]
  if {$tail != "." && $tail != ".."} {
   .peruse.mod.lb insert end $tail
  }
 }
}

## Bindings

.peruse.control.cancel config -command {
 set perused ""
 destroy .peruse
}
.peruse.dir.ok config -command [subst {
 set perused "$dir"
 destroy .peruse
}]

set go [subst -nocommands {
 if {[.peruse.mod.lb get active] != ""} {
  peruse_populate [file join "$dir" [.peruse.mod.lb get active]]
 } else bell
}]

bind .peruse.mod.lb <Double-1> $go
.peruse.control.go config -command $go

}
proc perusefile_populate dir {

set tmp [file tail $dir]
if {$tmp == ""} {set tmp /}
.perusefile.dir.up configure -text $tmp

set m .perusefile.dir.up.m
catch {
destroy $m
}
menu $m -tearoff 0

set up /
foreach dcomp [file split $dir] {
 set up [file join $up $dcomp]
 $m add command -label $dcomp -command "perusefile_populate \"$up\""
}

.perusefile.mod.lb delete 0 end
if {[catch {lsort [glob $dir/.* $dir/*]} catchvar]} {
  set tmp ""
} else {
  set tmp $catchvar
}

foreach dcan $tmp {
  set tail [file tail $dcan]
  if {$tail != "." && $tail != ".."} {
   .perusefile.mod.lb insert end $tail
  }
}

## Bindings

.perusefile.control.cancel config -command {
 set perusedfile ""
 destroy .perusefile
}
.perusefile.dir.ok config -command [subst {
 set perusedfile "$dir"
 destroy .perusefile
}]

set go [subst -nocommands {
 if {[.perusefile.mod.lb get active] != ""} {
  perusefile_populate [file join "$dir" [.perusefile.mod.lb get active]]
 } else bell
}]

bind .perusefile.mod.lb <Double-1> $go
.perusefile.control.go config -command $go

}
proc boot_setup {} {
global bootimg bootcat

catch {destroy .boot}
toplevel .boot
wm title .boot {Bootable CD Setup}


frame .boot.img -border 0 -relief flat 
frame .boot.cat -border 0 -relief flat 
frame .boot.cmd -border 0 -relief flat 
label .boot.img.l -text {Boot Image Path} 
label .boot.cat.l -text {Boot Catalog Path} 
entry .boot.img.e -width 20 -font 7x14 
entry .boot.cat.e -width 20 -font 7x14 

# Got TK8? You get a pretty button.
if {[info tclversion] >= 8.0} {
  button .boot.img.b -text {Select} -command open_bootfile
  button .boot.cat.c -text {Select} -command open_catfile
}

button .boot.cmd.ok -text {OK} -command {
 set bootimg [.boot.img.e get]
 set bootcat [.boot.cat.e get]
 destroy .boot
}
button .boot.cmd.cancel -text {Cancel} -command {
 destroy .boot
}
button .boot.cmd.revert -text {Revert} -command {
 .boot.img.e delete 0 end
 .boot.cat.e delete 0 end
 .boot.img.e insert 0 $bootimg
 .boot.cat.e insert 0 $bootcat
}

# Populate Data Fields
.boot.img.e insert 0 $bootimg
.boot.cat.e insert 0 $bootcat

pack .boot.img -side top -fill x
pack .boot.cat -side top -fill x
pack .boot.cmd -side top -fill x
pack .boot.img.l -side left
pack .boot.cat.l -side left
pack .boot.img.e -side right
pack .boot.cat.e -side right
pack .boot.cmd.ok -side left -expand 1
pack .boot.cmd.cancel -side left -expand 1
pack .boot.cmd.revert -side left -expand 1

# Got TK8? You get a pretty button.
if {[info tclversion] >= 8.0} {
  pack .boot.img.b -side right
  pack .boot.cat.c -side right
}

# frame .m.img
# label .m.img.l -text "Boot Image Path"
# entry .m.img.e
# frame .m.cat
# label .m.cat.l -text "Boot Catalog Path"
# entry .m.cat.e
}
proc get_mp3_burn_info {} {
# Returns either the empty list, indicating a cancelation,
# or the temp directory.

global perused

set warn {\
You have chosen to burn an MPEG compressed file as an audio track. This will not be the same quality as the original from which it was made. However, the burn can proceed if given a space to uncompress the audio files.

Do you wish to proceed?}

set go [sure $warn]
if {!$go} {
 return {}
}

set df [exec -keepnewline df]

catch {destroy .df}
toplevel .df
wm title .df {Free Disk Space}

text .df.t -width 80 -height 10 -setgrid 1 -yscrollcommand {.df.sb set}
scrollbar .df.sb -orient v -command {.df.t yview}

.df.t insert end "Please browse to a directory with sufficient free space.\n\n"
.df.t insert end $df
.df.t config -state disabled

pack .df.sb -side right -fill y
pack .df.t -side left

peruse
destroy .df

alert "Be aware that the MP3 conversion process typically takes some time."

return $perused
}
proc custom_layout {} {

global custom

catch {destroy .custom}
toplevel .custom
wm title .custom {Custom CD Layout}


frame .custom.dirs -border 0 -relief flat 
frame .custom.table -border 0 -relief flat 
frame .custom.cmds -border 0 -relief flat 
frame .custom.help -border 0 -relief flat 
label .custom.dirs.a -text {Make contents of:} 
label .custom.dirs.b -text {Show up on CD within:} 

foreach x {0 1 2 3 4 5 6 7 8 9} {
 frame .custom.table.$x -border 0 -relief flat 
 button .custom.table.$x.peruse -text {Peruse} -command "custom_peruse $x"
 entry .custom.table.$x.from -width 25 -font 7x14 
 entry .custom.table.$x.to -width 25 -font 7x14 
 
 button .custom.table.$x.clear -text {Clear} -command [subst {
 .custom.table.$x.from delete 0 end
 .custom.table.$x.to delete 0 end
 }]
 
 pack .custom.table.$x -side top -fill x
 pack .custom.table.$x.peruse -side left -anchor w
 pack .custom.table.$x.from -side left -anchor w
 pack .custom.table.$x.clear -side right -anchor e
 pack .custom.table.$x.to -side right -anchor e
 
}

scrollbar .custom.help.sb -orient v -command {} -command {.custom.help.t yview}
text .custom.help.t -width 70 -height 10 -setgrid 1 -yscrollcommand {.custom.help.sb set}
button .custom.cmds.ok -text {OK} -command {
 set from {}
 set to {}
 foreach s [pack slaves .custom.table] {
  lappend from [$s.from get]
  lappend to [$s.to get]
 }
 set custom(from) $from
 set custom(to) $to
 set custom(use) 1
 destroy .custom
}
button .custom.cmds.revert -text {Revert} -command {
 foreach s [pack slaves .custom.table] from $custom(from) to $custom(to) {
  $s.from delete 0 end
  $s.to delete 0 end
  $s.from insert 0 $from
  $s.to insert 0 $to
 }
}
button .custom.cmds.cancel -text {Cancel} -command {
destroy .custom
}

pack .custom.dirs -side top -fill x
pack .custom.table -side top -fill x
pack .custom.cmds -side top -fill x
pack .custom.help -side top -fill both -expand 1
pack .custom.dirs.a -side left -anchor w
pack .custom.dirs.b -side right -anchor e
pack .custom.help.sb -side right -fill y -anchor e
pack .custom.help.t -side right -fill both -anchor w
pack .custom.cmds.ok -side left -expand 1
pack .custom.cmds.revert -side left -expand 1
pack .custom.cmds.cancel -side left -expand 1

.custom.cmds.revert invoke

.custom.help.t insert 1.0 {              Custom CD Layout Help

This was always the biggest pain for me. I want stuff from the hard
drive from arbitrary places to show up on the final CD in (other)
arbitrary places.

I've tried to make this fairly general. Name what you want on the
left, and where to put it on the right. The one thing to remember
is that if you put a directory on the left, you have to give it
a name on the right unless you want a whole bunch of files sprayed
all over the root of the CD.

The names you give on the right do not have to exist anywhere
except your imagination. If they don't exist within other parts of
what you burn, the directories will be automatically created to
exist on the CD. Directories created in this way will be owned by
root.root and have permissions 555 (read/exec for all).

I'm not yet sure how these custom layouts interact with bootable
CD creation. I think that you specify the boot paths relative to
the final CD image. I know it works that way in the simple case.

}

.custom.help.t tag add cool 1.0 2.0
.custom.help.t tag config cool -font 10x20
.custom.help.t tag config cool -background blue
.custom.help.t tag config cool -foreground white

.custom.help.t config -state disabled
}
proc custom_peruse x {
global perused
peruse
if {$perused != ""} {
  .custom.table.$x.from delete 0 end
  .custom.table.$x.from insert 0 $perused
}}
proc make_mkisofs_cmd {} {
 global all joliet follow trans ds rock
 global boot bootimg bootcat custom
 
 set vol [.m.volid.e get]
 set root [.m.root.e get]

 # Presumably this is not necessary if there is a custom layout.
 
 if {![file isdirectory $root]} {
 alert "For on-the-fly ISO image creation, the root of tree must be a directory."
 return
 }
 
 set mk mkisofs
 if {$all} {lappend mk -all-files}
 if {$joliet} {lappend mk -joliet}
 switch -exact $rock {
  none {}
  rat {lappend mk -r}
  lit {lappend mk -R}
 }
 if {$follow} {lappend mk -follow-links}
 if {$trans} {lappend mk -translation-table}
 if {$boot} {
  if {($bootimg == "") || ($bootcat == "")} {
   alert "You selected 'Bootable CD' but either the boot image or boot catalog entries are blank. This will not work."
   return
  }
  if {![file exists [file join $root $bootimg]]} {
   alert "[file join $root $bootimg] is not there. You can't make a bootable CD without a bootimage."
   return
  }
  if {[file exists [file join $root $bootcat]]} {
   if {![sure "[file join $root $bootcat] will be overwritten if you continue. Is this ok?"]} {return}
  }
  lappend mk -b $bootimg -c $bootcat
 } else {
  if {$bootimg != ""} {
   set ok [sure "You have stuff in the bootimage entry, but have not chosen the 'Bootable CD' option. Do you want a standard non-bootable cd?"]
   if {!$ok} {return}
  }
 }
 if {$vol != ""} {lappend mk -volid $vol}
 
 ## Here we need to (maybe) process the custom layout entries.
 lappend mk $root
 
 if {$custom(use)} {
  foreach dir $custom(from) graftpoint $custom(to) {
   if {$dir == ""} continue
   set isdir [file isdirectory $dir]
   if {$isdir} {set dir "$dir/"}
   lappend mk "$graftpoint/=$dir"
  }
 }
 
 ## Last gasp.
 # use a illegal filename as symbol for the pipe so it can be recognized later
 lappend mk "///PIPE///"
 return $mk
 }
proc open_bootfile {} {
set types {
    {{Disk Images}      {.img}        }
    {{All Files}        *             }
}
set boot_img [tk_getOpenFile -filetypes $types -initialdir ~/ -title {Choose Image}]
.boot.img.e delete 0 250
.boot.img.e insert 0 $boot_img
}
proc open_catfile {} {
set types {
    {{All Files}        *             }
}
set boot_cat [tk_getOpenFile -filetypes $types -initialdir ~/ -title {Choose File}]
.boot.cat.e delete 0 250
.boot.cat.e insert 0 $boot_cat
}
proc check_device dev {

if {[file writable $dev]} {
  return 1
}

# Now gotta test for numeric device specification

set len [string length $dev]
set field {0123456789,}

for {set i 0} {$i < $len} {incr i} {
  set try [string index $dev $i]
  if {![string match *$try* $field]} {return 0}
}

return 1
}
proc version {} {
# Just return the version of this CDR-Toaster copy.
# Keeps everything in one place.

return "1.12"
}
proc detect_cdrom_device {} {
# Try some likely prospects:

set prospects [list "/dev/cdrom" "/dev/cdroms/cdrom0"]
foreach try $prospects {
  if {[file exists $try] && ! [file isdirectory $try]} {
    return $try
  }
}

# Give up and hope for user input.
return "/dev/"
}
proc my_image {} {
## TK8.0+ Dialog for file selection

set types {
    {{ISO9660 Images}   {.iso}        }
    {{XCD-Roast Images} {.raw}        }
    {{All Files}        *             }
}
set image [tk_getOpenFile -filetypes $types -defaultextension *.raw -initialdir /iso -title {Choose Image}]
.ds.tname delete 0 250
.ds.tname insert 0 $image

}
proc peruse_block args {
# This procedure should use the "peruse" strategy to let
# the user select a block-special file.
# Also, I'm going to re-write the layout so it looks better.
#

global perusedfile
set perusedfile ""

set w ".perusefile"

catch {destroy $w} 
toplevel $w
wm title $w {Peruse Block Devices}

frame $w.up
frame $w.dir
frame $w.file
frame $w.ctl

menubutton $w.up.mb -menu $w.up.mb.m -relief raised -border 2

listbox $w.dir.lb  -yscrollcommand [list $w.dir.sb set]
listbox $w.file.lb -yscrollcommand [list $w.file.sb set]

scrollbar $w.dir.sb  -orient v -command [list $w.dir.lb yview]
scrollbar $w.file.sb -orient v -command [list $w.file.lb yview]

button $w.ctl.accept -text accept -command {}
button $w.ctl.cancel -text cancel -command {}

pack $w.up -side top -fill x
pack $w.dir $w.file $w.ctl -side left -fill y
pack $w.dir.sb $w.file.sb -side right -fill y
pack $w.dir.lb $w.file.lb $w.up.mb -side left
pack $w.ctl.accept $w.ctl.cancel -side top


peruse_block_populate "/dev"         
tkwait window $w

return $perusedfile

}
proc peruse_block_populate dir {
# Here we fill in the info for the peruse window and
# configure up the bindings.

set w ".perusefile"

# Configure the menu button
set tmp [file tail $dir]
if {$tmp == ""} {set tmp /}
$w.up.mb configure -text $tmp

# Replace the menu
set m $w.up.mb.m
catch {destroy $m}
menu $m -tearoff 0

set up /
foreach dcomp [file split $dir] {
 set up [file join $up $dcomp]
 $m add command -label $dcomp -command "peruse_block_populate \"$up\""
}


### Populate the list boxes:

# empty them
$w.dir.lb delete 0 end
$w.file.lb delete 0 end

# enumerate the interesting files
set file_list [lsort [glob -nocomplain -- $dir/.* $dir/*]]

# place them in the correct box
foreach file $file_list {
  set tail [file tail $file]
  if {$tail != "." && $tail != ".."} {
    file stat $file stat
    set type $stat(type)
    
    if {$type == "directory"} {
      $w.dir.lb insert end [file tail $file]
    } elseif {$type == "blockSpecial"} {
      $w.file.lb insert end [file tail $file]
    }
  }
}

## Bindings

$w.ctl.cancel config -command {
 set perusedfile ""
 destroy .perusefile
}
$w.ctl.accept config -command [list peruse_block_accept $dir]

set go [subst -nocommands {
 if {[$w.dir.lb get active] != ""} {
  peruse_block_populate [file join "$dir" [$w.dir.lb get active]]
 } else bell
}]

bind $w.dir.lb <Double-1> $go
# .perusefile.control.go config -command $go


}
proc peruse_block_accept dir {

# If appropriate, pluck the currently selected file name from the file
# list and set it into the return global.

global perusedfile

set w ".perusefile"

set chosen [$w.file.lb get active]

if {$chosen != ""} {
  set perusedfile [file join $dir $chosen]
  destroy $w
}

}




# This is CDR-Toaster

set device "/dev/sg0"
set speed 2
set joliet 1
set rock none
set dummy 1
set eject 1
set ds mkisofs
set apad 1
set dpad 0
set bootimg ""
set bootcat ""

set custom(from) {}
set custom(to) {}
set custom(use) 0

interface

.b.quit configure -command {destroy .}
.b.burn configure -command {toast}





