Current File : //sbin/tk_scsiformat
#!/bin/sh
# the next lines restarts using wish or abort with a message \
type wish > /dev/null 2>&1 && exec wish -f "$0" "$@" || \
echo "no /usr/bin/wish found: please install the tk package." && exit 1
#!/usr/bin/wish -f
# Based in parts on scsi-config by:
#     Copyright 1993 Yggdrasil Computing, Incorporated
# Copyright 1997 Michael Weller (eowmob@exp-math.uni-essen.de)
# You may copy this file according to the terms and conditions of version 2
# of the GNU General Public License as published by the Free Software
# Foundation.

# This variable is set if WE issued a format command. Only when set, we will
# issue an mkfs command if a format completes.
set format_initiator 0

set textwidth 28
set winwidth 64
set butwidth 18
set butwidth2 25
set sdevice $argv
set par_fov "-o"
set par_inter "0"
set par_query ""
set par_estim 600
set par_initpat ""
set par_initmode "none"
set par_defform "-d"
set par_erase ""
set par_defects ""
set par_partstyle "-fdos"
set par_parttype "-y83"
set par_partmin "99999"
set par_geomdetect ""
set par_heads "64"
set par_sectors "32"
set par_mkfs "mke2fs -m0 -q <DEVICE>1"

set par_mkfsnames [list \
    "None" \
    "Ext2 (standard)" \
    "Ext2 (no reserved blocks)" \
    "DOS-FAT (1K clusters, up to 32MB)" \
    "DOS-FAT (2K clusters, up to 64MB)" \
    "DOS-FAT (4K clusters, up to 128MB)" \
    "DOS-FAT (8K clusters), up to 256MB)" \
    "DOS-FAT (16K clusters, up to 512MB)" \
    ]
# Value for None is not a valid command at all:
set par_mkfscmds [list \
    "<DEVICE>1" \
    "mke2fs -q <DEVICE>1" \
    "mke2fs -m0 -q <DEVICE>1" \
    "mkdosfs -s2 <DEVICE>1" \
    "mkdosfs -s4 <DEVICE>1" \
    "mkdosfs -s8 <DEVICE>1" \
    "mkdosfs -s16 <DEVICE>1" \
    "mkdosfs -s32 <DEVICE>1" \
    ]
set vars2save [list par_fov par_inter par_query par_estim par_initpat par_initmode \
    par_defform par_erase par_partstyle par_parttype par_partmin par_geomdetect par_heads \
    par_sectors par_disprim par_nvram par_discert par_stop par_mkfs\
    ]

set whoami [exec whoami]

proc save_var {fd var} {
   upvar #0 $var myval
   puts $fd "set $var \"$myval\""
}

proc change_fov {} {
   global par_fov

   if { [string compare $par_fov -o] != 0 } then {
       if { [winfo exists .init_win] == 1 } then {
	  destroy .init_win
       }
       .f_opt.col1.init configure -state disabled
       .f_opt.col1.disprim configure -state disabled
       .f_opt.col1.nvram configure -state disabled
       .f_opt.col2.discert configure -state disabled
       .f_opt.col2.stop configure -state disabled
   } else {
       .f_opt.col1.init configure -state normal
       .f_opt.col1.disprim configure -state normal
       .f_opt.col1.nvram configure -state normal
       .f_opt.col2.discert configure -state normal
       .f_opt.col2.stop configure -state normal
   }
}

proc simple {} {
   global par_inter par_fov par_erase par_defform par_defects par_query

   set par_inter "0"
   set par_fov ""
   set par_erase ""
   change_fov
   if { [winfo exists .defs_win] == 1 } {
	destroy .defs_win
   }
   set par_defform "-d"
   set par_defects ""
   set par_query "-b"
   block_change $par_query
}

proc change_partmin {} {
   global par_partmin

   if { [string compare $par_partmin ""] == 0} then {
	set par_partmin 0
   }
   .f_def.part.menu entryconfigure 9 -label "Maximal Partition Size ${par_partmin}MB"
}

proc change_geom {geomdetect heads sectors} {
   if { [string compare $geomdetect "-G"] == 0} then {
	.f_def.part.menu entryconfigure 8 -label \
	     "Disk Geometry is ${heads} Heads and ${sectors} Sectors"
   } else {
	.f_def.part.menu entryconfigure 8 -label "Autodetect Disk Geometry"
   }
}

proc change_mkfs {} {
    global par_mkfscmds par_mkfs

    if { [lsearch -exact $par_mkfscmds "$par_mkfs"] >= 0 } then {
	set label "Other"
    } else {
	set label "Other ($par_mkfs)"
    }
    .f_def.part.menu.mkfs entryconfigure [expr [llength $par_mkfscmds] + 2] -label "$label"
}

proc bind_eedit {w} {
    bind $w <Key-Delete> {
	%W select clear
	%W delete insert insert
    }
    bind $w <Key-BackSpace> {
	%W select clear
	%W delete [expr [%W index insert] - 1] \
		[expr [%W index insert] - 1]
    }
    bind $w <Key-Left> {
	%W select clear
	%W icursor [expr [%W index insert] - 1]
    }
    bind $w <Key-Right> {
	%W select clear
	%W icursor [expr [%W index insert] + 1]
    }
    bind $w <Button-2> {
	catch {
		%W insert insert [selection get]
	}
    }
}

proc bind_entry {w} {
    bind_eedit $w
    bind $w <Key> {
	if { [ string length [%W get] ] < 5 } {
	    if { [ string first %A "0123456789" ] >= 0 } then {
	        %W insert insert %A
	    }
	}
    }
    bind $w <Control-Key> {set x 1}
    bind $w <Meta-Key> {set x 1}
    bind $w <Alt-Key> {set x 1}
    bind $w <Control-Meta-Key> {set x 1}
    bind $w <Control-Alt-Key> {set x 1}
    bind $w <Meta-Alt-Control-Key> {set x 1}
    bind $w <Button-2> {
	catch {
		set sel [selection get]
		regsub -all "\[^0-9\]" $sel "" sel2
		%W insert insert $sel2
		%W delete 5 end
	}
    }
}

proc bind_text {w} {
   bind $w <Control-Key> {set x 1}
   bind $w <Meta-Key> {set x 1}
   bind $w <Alt-Key> {set x 1}
   bind $w <Control-Meta-Key> {set x 1}
   bind $w <Control-Alt-Key> {set x 1}
   bind $w <Meta-Alt-Control-Key> {set x 1}
   bind $w <Key-Delete> {
	%W delete insert {insert + 1 chars}
	%W yview -pickplace insert
   }
   bind $w <Key-BackSpace> {
	%W delete {insert - 1 chars} insert
	%W yview -pickplace insert
   }
   bind $w <Key-Left> {
	%W mark set insert {insert - 1 chars}
	%W yview -pickplace insert
   }
   bind $w <Key-Right> {
	%W mark set insert {insert + 1 chars}
	%W yview -pickplace insert
   }
   bind $w <Key-Up> {
	%W mark set insert {insert - 1 lines}
	%W yview -pickplace insert
   }
   bind $w <Key-Down> {
	%W mark set insert {insert + 1 lines}
	%W yview -pickplace insert
   }
}

proc block_change { block } {
    if { [ string compare $block "-b" ] == 0 } then {
	.f_block.f_estim configure -foreground black
	.f_block.f_estim.t configure -state normal
	.f_block.f_estim.t configure -background white
    } else {
	.f_block.f_estim configure -foreground gray70
	.f_block.f_estim.t configure -state disabled
	.f_block.f_estim.t configure -background bisque
    }
}

proc copyrights {} {
  global butwidth butwidth2
  if { [winfo exists .version] == 1 } {
	raise .version
	return;
  }
  toplevel .version
  wm title .version "TkScsiformat Version Information"
  wm group .
  frame .version.header
  label .version.header.label -bitmap info -bg pink
  message .version.header.message -width 400 -bg pink -text \
	"Version Information." 

  pack .version.header.label .version.header.message -side left -padx 15
  message .version.copy0 -width 400 -background yellow -justify center -text \
     { This is version 1.0, 20th Aug 1997 of tk_scsiformat.}
  pack .version.header .version.copy0\
         -side top -pady 10 -padx 15

  message .version.copy1 -width 400 -background green -justify left -text\
     {Copyright 1993 Yggdrasil Computing, Incorporated.
Copyright 1997 Michael Weller (eowmob@exp-math.uni-essen.de).}

  message .version.copy2 -width 400 -justify center -text {This configuration\
     script may be copied and modified according to the terms and conditions\
     of version 2 of the GNU General Public License as published by the\
     Free Software Foundation (Cambridge, Massachusetts),\
     provided that this notice is not removed.

     This script was originally the control panel from the Yggdrasil\
     distribution.  Eric Youngdale thought it looked pretty keen, and it was easy\
     to adapt, so he used it as the basis for the SCSI configuration manager.\

     The configuration manager was extended by Michael Weller, and finally \
     this utility was written and based on it.

     Well, honestly, not more than ten or twenty lines are copied from scsi-config. \
     Thus repeating the copyrights above is probably not required. This is released \
     under GPL nevertheless, though.
}


   button .version.dismiss -text "Dismiss" -activebackground green \
	-command {destroy .version} -width $butwidth2
   pack .version.copy1 .version.copy2 .version.dismiss\
         -side top -pady 10 -padx 15

   tkwait window .version
}

proc enter_init {} {
   global butwidth2 par_initpat par_initmode textwidth winwidth
   global my_initmode

   set my_initmode ${par_initmode}

   if { [winfo exists .init_win] == 1 } {
	raise .init_win
	return;
   }
   toplevel .init_win
   wm title .init_win "Initialization of Logical Blocks"
   wm group .

   frame .init_win.header
   label .init_win.header.label -bitmap questhead -bg pink
   message .init_win.header.message -width 400 -bg pink -text \
	"Initialization Pattern for the Logical Blocks." 

   pack .init_win.header.label .init_win.header.message \
	 -side left -padx 15
   pack .init_win.header -side top -pady 10 -padx 15

   frame .init_win.row1
   frame .init_win.row2
   radiobutton .init_win.row1.log -text "of each logical block" \
	-variable my_initmode -value "-L" -width $textwidth
   radiobutton .init_win.row1.phy -text "of each physical block" \
	-variable my_initmode -value "-P" -width $textwidth
   radiobutton .init_win.row2.def -text "Don't write them anywhere" \
	-variable my_initmode -value "none" -width $textwidth

   pack .init_win.row1.log .init_win.row1.phy \
	 -side left -pady 5 -padx 15 -ipadx 2 -pady 2
   pack .init_win.row2.def \
	 -side left -pady 5 -padx 15 -ipadx 2 -pady 2

   label .init_win.mess1 -width $winwidth -text \
	"Write the corresponding logical block number in the first 4 bytes:"
   pack .init_win.mess1 -side top -pady 5 -padx 15
   pack .init_win.row1 -side top -pady 5 -padx 15
   pack .init_win.row2 -side top -pady 5 -padx 15

   label .init_win.mess2 -width $winwidth -text \
	"Enter an (optional) sequence of hexnumbers to fill the blocks with:"
   pack .init_win.mess2 -side top -pady 5 -padx 15

   frame .init_win.frame
   scrollbar .init_win.frame.yscroll -relief sunken \
        -command ".init_win.frame.page yview"
   text .init_win.frame.page -width $winwidth -background white -relief sunken \
	-borderwidth 2 -height 8 -wrap char -exportselection 1 \
	-yscrollcommand ".init_win.frame.yscroll set"
   .init_win.frame.page insert 1.0 $par_initpat
   pack .init_win.frame.yscroll -side right -fill y
   pack .init_win.frame.page -side top -expand 1 -fill both

   bind .init_win.frame.page <Key> {
	if { [ string first %A " 0123456789abcdefgABCDEF" ] >= 0 } then {
	    .init_win.frame.page insert insert [string toupper %A]
	    .init_win.frame.page yview -pickplace insert
	}
	if { [ string first %A "
" ] >= 0 } then {
	    .init_win.frame.page insert insert "\x0a"
	    .init_win.frame.page yview -pickplace insert
	}
   }
   bind .init_win.frame.page <Button-2> {
	catch {
		set sel [string toupper [selection get]]
		regsub -all "\[^ 0123456789ABCDEF\x0a\]" $sel "" sel2
		%W insert insert $sel2
	}
   }
   bind_text .init_win.frame.page
   pack .init_win.frame -side top -pady 5 -padx 15
   frame .init_win.f_bot
   button .init_win.f_bot.ok -text "Ok" \
        -activebackground green -command {
		set par_initpat [ .init_win.frame.page get 1.0 end ]
		set par_initmode $my_initmode
		destroy .init_win
        } -width $butwidth2
   button .init_win.f_bot.dismiss -text "Abort" \
        -activebackground red -activeforeground white \
	-command {destroy .init_win} -width $butwidth2
   pack .init_win.f_bot.ok .init_win.f_bot.dismiss \
	 -side left -pady 15 -padx 15 -ipadx 2 -pady 2

   pack .init_win.f_bot -side top -pady 10 -padx 15

   tkwait window .init_win
}

proc set_msg { form } {
    switch -- $form {
	-D { .defs_win.mess3 configure -text \
	    {Enter defects in the format <integer>:<integer>:<integer> with three numbers \
             for cylinder, head, physical sector. Specifying sector -1 marks the whole \
             track as defect. You may preceed numbers with 0x or 0 for hexadecimal or \
	     octal representation }
	}
	-B { .defs_win.mess3 configure -text \
	    {Enter defects in the format <integer>:<integer>:<integer> with three numbers \
             for cylinder, head, bytes from index. This marks that number of bytes from \
	     the index mark as bad. Specifying -1 marks the whole \
             track as defect. You may preceed numbers with 0x or 0 for hexadecimal or \
	     octal representation }
	}
	-d { .defs_win.mess3 configure -text \
	    {Enter defects in the format <integer> where <integer> is the number of the \
	     defect logical block. You may preceed numbers with 0x or 0 for hexadecimal or \
	     octal representation }
	}
    }
}

proc enter_defs {} {
   global butwidth2 par_defform par_defects textwidth winwidth
   global my_defform

   set my_defform $par_defform
   if { [winfo exists .defs_win] == 1 } {
	raise .defs_win
	return;
   }
   toplevel .defs_win
   wm title .defs_win "Manual Defects Addition"
   wm group .
   frame .defs_win.header
   label .defs_win.header.label -bitmap questhead -bg pink
   message .defs_win.header.message -width 400 -bg pink -text \
	"Defects to be added manually." 

   pack .defs_win.header.label .defs_win.header.message \
	 -side left -padx 15
   pack .defs_win.header -side top -pady 10 -padx 15

   frame .defs_win.row1
   frame .defs_win.row2
   radiobutton .defs_win.row1.phy -text "By Physical Sectors" \
	-variable my_defform -value "-D" -width $textwidth -command { set_msg $my_defform }
   radiobutton .defs_win.row1.byt -text "By Bytes from Index" \
	-variable my_defform -value "-B" -width $textwidth -command { set_msg $my_defform }
   radiobutton .defs_win.row2.def -text "By Logical Block Numbers" \
	-variable my_defform -value "-d" -width $textwidth -command { set_msg $my_defform }

   pack .defs_win.row1.phy .defs_win.row1.byt \
	 -side left -pady 5 -padx 15 -ipadx 2 -pady 2
   pack .defs_win.row2.def \
	 -side left -pady 5 -padx 15 -ipadx 2 -pady 2

   label .defs_win.mess1 -width $winwidth -text \
	"Select the format in which you want to enter the defects:"
   pack .defs_win.mess1 -side top -pady 5 -padx 15
   pack .defs_win.row1 -side top -pady 5 -padx 15
   pack .defs_win.row2 -side top -pady 5 -padx 15

   label .defs_win.mess2 -width $winwidth -text \
	"Enter an (optional) sequence of of defects:"
   pack .defs_win.mess2 -side top -pady 5 -padx 15

   frame .defs_win.frame
   scrollbar .defs_win.frame.yscroll -relief sunken \
        -command ".defs_win.frame.page yview"
   text .defs_win.frame.page -width $winwidth -background white -relief sunken \
	-borderwidth 2 -height 8 -wrap char -exportselection 1 \
	-yscrollcommand ".defs_win.frame.yscroll set"
   .defs_win.frame.page insert 1.0 $par_defects
   pack .defs_win.frame.yscroll -side right -fill y
   pack .defs_win.frame.page -side top -expand 1 -fill both

   bind .defs_win.frame.page <Key> {
	if { [ string first %A " 0123456789abcdefgABCDEF:-" ] >= 0 } then {
	    .defs_win.frame.page insert insert [string toupper %A]
	}
	if { [ string compare %A "
" ] == 0 } then {
	    .defs_win.frame.page insert insert "\x0a"
	}
	if { [ string first %A "xX" ] >= 0 } then {
	    .defs_win.frame.page insert insert "x"
	}
	.defs_win.frame.page yview -pickplace insert
   }
   bind .defs_win.frame.page <Button-2> {
	catch {
		set sel [string toupper [selection get]]
		regsub -all "\[^ 0123456789ABCDEFX:\x0a-\]" $sel "" sel2
		regsub -all "X" $sel2 "x" sel
		%W insert insert $sel
	}
   }
   bind_text .defs_win.frame.page
   pack .defs_win.frame -side top -pady 5 -padx 15

   message .defs_win.mess3 -justify center -text "X" -background green \
	-aspect 800

   set_msg $my_defform

   pack .defs_win.mess3 -side top -pady 5 -padx 15

   frame .defs_win.f_bot
   button .defs_win.f_bot.ok -text "Ok" \
        -activebackground green -command {
		set par_defects [ .defs_win.frame.page get 1.0 end ]
		set par_defform $my_defform
		destroy .defs_win
        } -width $butwidth2
   button .defs_win.f_bot.dismiss -text "Abort" \
        -activebackground red -activeforeground white \
	-command {destroy .defs_win} -width $butwidth2
   pack .defs_win.f_bot.ok .defs_win.f_bot.dismiss \
	 -side left -pady 15 -padx 15 -ipadx 2 -pady 2

   pack .defs_win.f_bot -side top -pady 10 -padx 15

   tkwait window .defs_win
}

proc handle_trans {w ontop} {
   wm transient $w $ontop
   wm withdraw $w
   update idletasks
   set x [expr [winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
            - [winfo vrootx [winfo parent $w]]]
   set y [expr [winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
            - [winfo vrooty [winfo parent $w]]]
   wm geom $w +$x+$y
   wm deiconify $w

   set oldFocus [focus]
   grab $w
   focus $w

   tkwait window $w
   if { [string compare $ontop "."] != 0 } then {
	grab $ontop
   }
   focus $oldFocus
}

proc post_partsize {} {
   global butwidth2 par_partmin

   toplevel .partsize
   frame .partsize.f_bot
   wm title .partsize "Maximal partition size"
   wm group .
   frame .partsize.header
   label .partsize.header.label -bitmap questhead -bg pink
   message .partsize.header.message -width 400 -bg pink -text \
	"Maximal partition size." 

   pack .partsize.header.label .partsize.header.message \
	 -side left -padx 15
   pack .partsize.header -side top -pady 10 -padx 15

   message .partsize.expl -bg green -width 400 -text \
	"Enter the maximal partition size in Megabyte. If it is zero, \
	no partition is created and the partition table is left empty. \
	Otherwise a partition of this size is created as primary \
	partition 1. If the size does not fit the partition is made as \
	large as possible."

   pack .partsize.expl -side top -pady 10 -padx 15

   label .partsize.f_inter -text "Maximal partition size in MB:             "
   entry .partsize.f_inter.t -width 5 -background white -relief sunken -borderwidth 2 \
	-exportselection 1
   .partsize.f_inter.t insert 0 $par_partmin
   bind_entry .partsize.f_inter.t
   place .partsize.f_inter.t -in .partsize.f_inter -anchor e -relx 1.0 -rely 0.5

   pack .partsize.f_inter -side top -pady 20 -padx 15

   button .partsize.f_bot.ok -text "Ok" \
        -activebackground green -command {
	   set par_partmin [ .partsize.f_inter.t get ]
	   change_partmin
	   destroy .partsize
        } -width $butwidth2
   button .partsize.f_bot.dismiss -text "Abort" \
        -activebackground red -activeforeground white \
	-command {destroy .partsize} -width $butwidth2
   pack .partsize.f_bot.ok .partsize.f_bot.dismiss \
	 -side left -pady 15 -padx 15 -ipadx 2 -pady 2

   pack .partsize.f_bot -side top -pady 10 -padx 15

   handle_trans .partsize .
}

proc change_geodet { geodet } {
    if { [ string compare $geodet "-G" ] == 0 } then {
	.geom.hese.heads configure -foreground black
	.geom.hese.heads.t configure -state normal
	.geom.hese.heads.t configure -background white
	.geom.hese.sectors configure -foreground black
	.geom.hese.sectors.t configure -state normal
	.geom.hese.sectors.t configure -background white
    } else {
	.geom.hese.heads configure -foreground gray70
	.geom.hese.heads.t configure -state disabled
	.geom.hese.heads.t configure -background bisque
	.geom.hese.sectors configure -foreground gray70
	.geom.hese.sectors.t configure -state disabled
	.geom.hese.sectors.t configure -background bisque
    }
}

proc post_geom {} {
   global winwidth butwidth2 par_geomdetect par_heads par_sectors textwidth
   global my_geomdetect

   set my_geomdetect $par_geomdetect

   toplevel .geom
   frame .geom.f_bot
   wm title .geom "Disk Drive Geometry"
   wm group .
   frame .geom.header
   label .geom.header.label -bitmap questhead -bg pink
   message .geom.header.message -width 400 -bg pink -text \
	"Disk Drive Geometry." 

   pack .geom.header.label .geom.header.message \
	 -side left -padx 15
   pack .geom.header -side top -pady 10 -padx 15

   message .geom.expl -bg green -width 400 -text \
	"Enter the geometry of the disk. Not the real one, but the one \
	DOS will see, s.t. the partition table can be build in a way that \
	DOS and other OS's will be able to handle it. Generally, linux \
	device drivers report the setting back which the corresponding adapters \
	use under DOS. If this fails, 64 heads and 32 are usually used. "

   pack .geom.expl -side top -pady 10 -padx 15

   checkbutton .geom.auto -variable my_geomdetect -onvalue "" -offvalue "-G" \
	-text " Query Geometry from Scsidriver " -command {change_geodet $my_geomdetect}
   
   pack .geom.auto -side top -pady 20 -padx 15

   frame .geom.hese

   label .geom.hese.heads -text   "Heads:             "
   entry .geom.hese.heads.t -width 5 -background white -relief sunken -borderwidth 2 \
	-exportselection 1
   .geom.hese.heads.t insert 0 $par_heads
   bind_entry .geom.hese.heads.t
   place .geom.hese.heads.t -in .geom.hese.heads -anchor e -relx 1.0 -rely 0.5

   label .geom.hese.sectors -text "Sectors:           "
   entry .geom.hese.sectors.t -width 5 -background white -relief sunken -borderwidth 2 \
	-exportselection 1
   .geom.hese.sectors.t insert 0 $par_sectors
   bind_entry .geom.hese.sectors.t
   place .geom.hese.sectors.t -in .geom.hese.sectors -anchor e -relx 1.0 -rely 0.5

   pack .geom.hese.heads  .geom.hese.sectors  -side left -pady 15 -padx 15
   pack .geom.hese -side top
   change_geodet $my_geomdetect

   button .geom.f_bot.ok -text "Ok" \
        -activebackground green -command {
	   set par_heads [ .geom.hese.heads.t get ]
	   set par_sectors [ .geom.hese.sectors.t get ]
	   set par_geomdetect $my_geomdetect
	   change_geom $par_geomdetect $par_heads $par_sectors
	   destroy .geom
        } -width $butwidth2
   button .geom.f_bot.dismiss -text "Abort" \
        -activebackground red -activeforeground white \
	-command {destroy .geom} -width $butwidth2
   pack .geom.f_bot.ok .geom.f_bot.dismiss \
	 -side left -pady 15 -padx 15 -ipadx 2 -pady 2

   pack .geom.f_bot -side top -pady 10 -padx 15

   handle_trans .geom .
}

proc post_mkfs {} {
   global butwidth2 par_mkfs

   toplevel .mkfs
   frame .mkfs.f_bot
   wm title .mkfs "Make FileSystem Command"
   wm group .
   frame .mkfs.header
   label .mkfs.header.label -bitmap questhead -bg pink
   message .mkfs.header.message -width 400 -bg pink -text \
	"Make FileSystem Command."

   pack .mkfs.header.label .mkfs.header.message \
	 -side left -padx 15
   pack .mkfs.header -side top -pady 10 -padx 15

   message .mkfs.expl -bg green -width 400 -text \
	"Enter a command to execute to make a filesystem on the freshly created partition.\
         The string <DEVICE> in the string will be replaced by the name of disk device\
	 being formatted. Generally you'll have to use <DEVICE>1 to refer to the first\
	 partition."

   pack .mkfs.expl -side top -pady 10 -padx 15

   frame .mkfs.f_inter
   scrollbar .mkfs.f_inter.s -relief sunken -orient horiz -command \
            ".mkfs.f_inter.t xview"
   entry .mkfs.f_inter.t -background white -relief sunken -borderwidth 2 \
	-exportselection 1 -xscrollcommand ".mkfs.f_inter.s set"
   .mkfs.f_inter.t insert 0 $par_mkfs

   bind_eedit .mkfs.f_inter.t
   bind .mkfs.f_inter.t <Key-KP_Enter> { .mkfs.f_bot.ok invoke }
   bind .mkfs.f_inter.t <Key-Return> { .mkfs.f_bot.ok invoke }

   pack .mkfs.f_inter.t .mkfs.f_inter.s -side top -fill both -expand yes
   pack .mkfs.f_inter -side top -pady 20 -padx 15 -fill both -expand yes

   button .mkfs.f_bot.ok -text "Ok" \
        -activebackground green -command {
	   set par_mkfs [ .mkfs.f_inter.t get ]
	   change_mkfs
	   destroy .mkfs
        } -width $butwidth2
   button .mkfs.f_bot.dismiss -text "Abort" \
        -activebackground red -activeforeground white \
	-command {destroy .mkfs} -width $butwidth2
   pack .mkfs.f_bot.ok .mkfs.f_bot.dismiss \
	 -side left -pady 15 -padx 15 -ipadx 2 -pady 2

   pack .mkfs.f_bot -side top -pady 10 -padx 15

   handle_trans .mkfs .
}

proc set_rands {} {
   set tworands [exec /usr/lib/scsi/tworands]
   .conf.equa.sol delete 0 end
   .conf.equa.a configure -state normal
   .conf.equa.b configure -state normal
   .conf.equa.a delete 0 end
   .conf.equa.b delete 0 end
   .conf.equa.a insert 0 [lindex $tworands 0]
   .conf.equa.b insert 0 [lindex $tworands 1]
   .conf.equa.a configure -state disabled
   .conf.equa.b configure -state disabled
}

proc query_format {} {
   global winwidth butwidth2 textwidth format_cmd sdevice par_fov par_inter par_query \
	  par_estim par_initpat par_initmode par_defform par_defects par_erase par_partstyle \
	  par_parttype par_partmin par_geomdetect par_heads par_sectors par_disprim par_nvram \
	  par_discert par_stop


   toplevel .conf
   frame .conf.f_bot
   wm title .conf "Format Confirmation Request"
   wm group .
   frame .conf.header
   label .conf.header.label -bitmap warning -bg pink
   message .conf.header.message -width 400 -bg pink -text \
	"Format Confirmation Request."

   pack .conf.header.label .conf.header.message \
	 -side left -padx 15
   pack .conf.header -side top -pady 10 -padx 15

   message .conf.expl -bg red -fg white -width 400 -justify center -text \
"Formatting will erase all data on $sdevice, regardless on which \
partition, and there will be NO way to recover your data.

Therefore, please check now if you really want to format this device. \
To ensure that your are really with me and know what you do, please \
answer the simple question below."

   pack .conf.expl -side top -pady 10 -padx 15

   frame .conf.equa

   entry .conf.equa.a -width 3 -relief sunken -borderwidth 2 -state disabled
   label .conf.equa.times -text "*"
   entry .conf.equa.b -width 3 -relief sunken -borderwidth 2 -state disabled
   label .conf.equa.equa -text "="
   entry .conf.equa.sol -width 5 -background white -relief sunken -borderwidth 2 \
	-exportselection 1
   bind_entry .conf.equa.sol
   set_rands

   pack .conf.equa.a .conf.equa.times .conf.equa.b .conf.equa.equa .conf.equa.sol \
	-side left -pady 10 -padx 3
   pack .conf.equa -side top

   message .conf.result -bg red -fg white -aspect 900 -text "Wrong! Please retry or bail out!"

   frame .conf.space
   pack .conf.space -side top -pady 8

   button .conf.f_bot.ok -text "Confirm" \
        -activebackground yellow -command {
	  if { [expr [.conf.equa.a get] * [.conf.equa.b get]] == [.conf.equa.sol get] } then {
	    destroy .conf
	    do_format $format_cmd
	  } else {
	    set_rands
	    pack .conf.result -after .conf.equa -side top -pady 2 -padx 15
          }
        } -width $butwidth2
   button .conf.f_bot.show -text "Show ScsiFormat Command" \
        -activebackground green -command {
	     show_format "$format_cmd $sdevice"
        } -width $butwidth2

   pack .conf.f_bot.ok .conf.f_bot.show -side left -pady 15 -padx 15 -ipadx 2 -pady 2

   button .conf.dismiss -text "Bail Out" \
        -activebackground red -activeforeground white \
	-command {destroy .conf} -width $butwidth2

   pack .conf.f_bot -side top -padx 15
   pack .conf.dismiss -side top -pady 10

   set format_cmd "/usr/sbin/scsiformat $par_erase -i$par_inter"

   regsub -all "\x0a" [string trim $par_defects] " " tmpstr
   regsub -all " +" $tmpstr "," tmpstr
   if { [string length $tmpstr] > 0 } then {
	append format_cmd " $par_defform$tmpstr"
   }
   if { [string compare $par_partstyle "none"] != 0 } then {
	append format_cmd " $par_partstyle $par_parttype -M$par_partmin"
   }
   if { [string compare $par_geomdetect "-G"] == 0 } then {
	append format_cmd " $par_geomdetect${par_heads}x$par_sectors"
   }
   if { [string compare $par_fov "-o"] == 0} then {
	append format_cmd " $par_fov"
	if { [string compare $par_initmode "none"] != 0 } then {
	    append format_cmd " $par_initmode"
	}
	regsub -all "\[ \x0a\]" [string trim $par_initpat] "" tmpstr
	if { [string length $tmpstr] > 0 } then {
	    append format_cmd " -I$tmpstr"
	}
	append format_cmd " $par_discert $par_disprim $par_stop $par_nvram"
   }
   if { [string compare $par_query "-b"] == 0 } then {
	append format_cmd " -b$par_estim"
   }
   #trim unneccessary spaces:
   regsub -all " +" [string trim $format_cmd] " " format_cmd
   handle_trans .conf .
}

proc show_format {cmd} {
   global winwidth butwidth2 textwidth

   toplevel .showcmd
   frame .showcmd.f_bot
   wm title .showcmd "Show ScsiFormat Command"
   wm group .
   frame .showcmd.header
   label .showcmd.header.label -bitmap info -bg pink
   message .showcmd.header.message -width 400 -bg pink -text \
	"Show ScsiFormat Command."

   pack .showcmd.header.label .showcmd.header.message \
	 -side left -padx 15
   pack .showcmd.header -side top -pady 10 -padx 15

   message .showcmd.expl -width 400 -text \
	"This is the command which will perform the scsi low level format you requested:"
   pack .showcmd.expl -side top -pady 10 -padx 15

   message .showcmd.cmd -width 400 -text $cmd -relief sunken
   pack .showcmd.cmd -side top -pady 10 -padx 15

   button .showcmd.dismiss -text "Dismiss" \
        -activebackground red -activeforeground white \
	-command {destroy .showcmd} -width $butwidth2

   pack .showcmd.dismiss -side top -pady 10

   handle_trans .showcmd .conf
}

proc do_format {fcmd} {
   global par_query par_estim sdevice
   global winwidth butwidth2 textwidth
   global check_cmd format_initiator

   set security "-F'Ene Mene Meck, und Du bist weg!'"
   append fcmd " $security -t0 -X $sdevice"
   
   if { [catch {exec sh -c "$fcmd" >& /var/run/scsiformat.[pid] } err] != 0 } then {
        set fd [open /var/run/scsiformat.[pid] r]
	set err "$err\x0a[read $fd]"
	close $fd
        exec rm /var/run/scsiformat.[pid]
	show_err scsiformat $err
	return;
   }
   exec rm /var/run/scsiformat.[pid]
   set format_initiator 1
   show_busy
}

proc show_err {cmd msg} {
   global winwidth butwidth2 textwidth

   if { [winfo exists .error] == 1 } {
	.error.msg configure -text $msg
	raise .error
        tkwait window .error
	return;
   }
   toplevel .error
   frame .error.f_bot
   wm title .error "ScsiFormat Error"
   wm group .
   frame .error.header
   label .error.header.label -bitmap error -bg pink
   message .error.header.message -width 400 -bg pink -text \
	"Show $cmd Error Message."

   pack .error.header.label .error.header.message \
	 -side left -padx 15
   pack .error.header -side top -pady 10 -padx 15

   message .error.expl -width 600 -text \
	"$cmd returned the following error message:"
   pack .error.expl -side top -pady 10 -padx 15

   message .error.msg -width 600 -text $msg -relief sunken
   pack .error.msg -side top -pady 10 -padx 15

   button .error.dismiss -text "Dismiss" \
        -activebackground red -activeforeground white \
	-command {destroy .error} -width $butwidth2

   pack .error.dismiss -side top -pady 10

   tkwait window .error
}

proc query_prog {} {
   global sdevice errorCode wasbusy format_initiator par_mkfs

   set check_cmd "/usr/sbin/scsiformat -T -t1 -X $sdevice"

   if { [winfo exists .showprog] != 1 } {
	return;
   }
   set errorCode [list CHILDSTATUS 0 0]
   catch {exec sh -c "$check_cmd"} err
   if { ([string compare [lindex $errorCode 0] CHILDSTATUS] != 0) 
				|| ([lindex $errorCode 2] > 1) } then {
	show_err scsiformat $err
   } elseif { [lindex $errorCode 2] == 1 } then {
	if {[catch {
	    .showprog.row2.start configure -text [lindex $err 0]
	    .showprog.row2.end configure -text [lindex $err 3]
	    set percent [lindex $err 1]
	    if { [string compare $percent "-"] != 0} then {
		.showprog.expl configure -text [format "%.10g%% in %s" [expr 100 * $percent] \
		    [lindex $err 2] ]
		if { $percent >= 1.0 } then { set percent 1.0 }
	    } else {
		.showprog.expl configure -text [lindex $err 2]
		set percent 0
	    }
	    place .showprog.slider.inner -in .showprog.slider -relwidth $percent -relheight 1.0 \
		-anchor nw -x 0 -y 0
	    set wasbusy 1
	    after 5000 query_prog}] != 0} then {
#Something weird went on. Like illegal device name, read-only device, etc..
		destroy .showprog
	}
   } else {
	destroy .showprog
	if { $format_initiator == 1} then {
	    set format_initiator 0
	    if { [string compare $par_mkfs "<DEVICE>1"] != 0} then {
		regsub "<DEVICE>" $par_mkfs "${sdevice}" cmd
#
#it seems that we have to wait even more...
#
#a mayor reason is that the format might have completed, but scsiformat is
#still creating the partion which will also include some sleep()'s
#
		exec sync
		exec sh -c "sleep 10"
		set errorCode [list NONE 0 0]
		catch {exec sh -c "$cmd"} err
		if { ([string compare [lindex $errorCode 0] CHILDSTATUS] == 0) } then {
		    show_err "Filesystem maker"\
		         "'$cmd' exited with errorcode - [lindex $errorCode 2]\x0a$err"
		}
	    }
	}
	if { $wasbusy == 1 } then {
	    puts -nonewline stderr "\x0a"
	    flush stderr
	}
   }
}

proc show_busy {} {
   global textwidth wasbusy

   toplevel .showprog
   frame .showprog.f_bot
   wm title .showprog "Show ScsiFormat Progress"
   wm group .
   frame .showprog.header
   label .showprog.header.label -bitmap hourglass -bg pink
   message .showprog.header.message -width 400 -bg pink -text \
	"Formatting in Progress."

   pack .showprog.header.label .showprog.header.message \
	 -side left -padx 15
   pack .showprog.header -side top -pady 10 -padx 15
   frame .showprog.space
   pack .showprog.space -side top -pady 5
   frame .showprog.row1
   frame .showprog.row2
   pack .showprog.row1 .showprog.row2 -side top -expand yes -fill both -padx 4

   label .showprog.row1.start -text "Started" -borderwidth -1
   pack .showprog.row1.start -side left
   frame .showprog.row1.space
   pack .showprog.row1.space -after .showprog.row1.start -side left -expand yes -fill both
   label .showprog.row1.end -text "Estimated End" -borderwidth -1
   pack .showprog.row1.end -side right

   label .showprog.row2.start -borderwidth -1
   pack .showprog.row2.start -side left
   frame .showprog.row2.space
   pack .showprog.row2.space -after .showprog.row2.start -side left -expand yes -fill both
   label .showprog.row2.end -borderwidth -1
   pack .showprog.row2.end -side right

   frame .showprog.slider -borderwidth 2 -height 20 -relief sunken
   pack .showprog.slider -side top -expand yes -fill both -pady 4 -padx 4
   frame .showprog.slider.inner -borderwidth 2 -relief raised -bg SlateGray1

   message .showprog.expl -width 400 -text "X" -borderwidth -1
   pack .showprog.expl -side top
   frame .showprog.space2
   pack .showprog.space2 -side top -pady 2

   set wasbusy 0
   query_prog
   if { [winfo exists .showprog] != 1 } {
	return;
   }
   handle_trans .showprog .
}

wm title . "TkScsiformat Version 1.0"

if { [string compare $whoami root] != 0} then {
    frame .warn
    frame .warn.h
    label .warn.h.l -bitmap info -background pink
    message .warn.h.m -aspect 200 -background pink -justify center -text \
	{You need to be root in order to format a SCSI device!}
    pack .warn.h.l .warn.h.m -side left -padx 10
    button .warn.quit -text "Quit" -command exit \
	-activeforeground white -activebackground red
    pack .warn.h .warn.quit -side top -pady 10
    pack .warn
    tkwait window .warn
}

if { [llength $argv] > 1 } then {
    puts stderr "Usage: tk_scsiformat \[device\]"
    exit
}

while { [string compare [lindex $sdevice 0] ""] == 0 } {
    frame .select
    frame .select.h
    label .select.h.l -bitmap questhead -background pink
    message .select.h.m -justify center -aspect 500 -background pink -text \
	{Select a SCSI device to low level format.}
    frame .select.f
    button .select.f.quit -text "    Quit    " -command exit \
	-activeforeground white -activebackground red
    button .select.f.continue -text " Continue " -activebackground green \
	-command {destroy .select}
    pack .select.f.continue .select.f.quit -side left -padx 10
    pack .select.h.l .select.h.m -side left -padx 10
    pack .select.h -pady 10
    pack .select
    set line [exec /sbin/scsiinfo -l]
    set n 0
    foreach x $line { 
        if {[string first "/dev/sd" "$x"] != 0} continue;
        radiobutton .select.$n -text "$x" -width 10 \
           -variable sdevice -value $x -anchor w
	pack .select.$n  -padx 3
        set n [expr $n+1]
    }
    if { $n == 0 } then {
	.select.f.continue configure -state disabled
    } else {
	.select.0 select
    }
    pack .select.f -pady 10
    tkwait window .select
}

set save_file "/usr/lib/scsi/scsiformat.defs.[file tail $sdevice]"
catch {source $save_file}

frame .header
label .header.label -bitmap question -bg pink
message .header.message -width 400 -bg pink -text \
	"Scsi Low Level Format - $sdevice." 

pack .header.label .header.message -side left -padx 15

frame .f_bot1
button .f_bot1.copyrights -activebackground green -text "Version Info"    \
	-command { copyrights } -width $butwidth
button .f_bot1.filesave -activebackground green -text "Save Defaults"    \
	-width $butwidth -command {
          global vars2save save_file
          set fd [open $save_file w]
	  foreach var $vars2save { save_var $fd $var }
	  close $fd
        }
button .f_bot1.simple -activebackground green -text "Simple Format Cmd"    \
	-command "simple" -width $butwidth
pack .f_bot1.copyrights .f_bot1.filesave .f_bot1.simple \
        -side left -padx 12 -ipadx 2 -ipady 2 -pady 5

frame .f_bot2
button .f_bot2.format -text "Format" -command { query_format } \
	-activebackground yellow -activeforeground black -width $butwidth2
button .f_bot2.quit -text "Quit" -command { exit } \
	-activebackground red -activeforeground white -width $butwidth2
pack .f_bot2.format .f_bot2.quit -side left -padx 12 -ipadx 2 -ipady 2 -pady 5

frame .f_mand -width $winwidth
checkbutton .f_mand.erase -offvalue "" -onvalue "-e" -variable par_erase -text \
	"Erase Grown Defect List" -width [expr $textwidth - 1]

label .f_mand.f_inter -text "Interleave (0 for default):            " -width $textwidth
entry .f_mand.f_inter.t -width 5 -background white -relief sunken -borderwidth 2 \
	-exportselection 1 -textvariable par_inter
bind_entry .f_mand.f_inter.t
place .f_mand.f_inter.t -in .f_mand.f_inter -anchor e -relx 1.0 -rely 0.5

pack .f_mand.erase .f_mand.f_inter -side left -padx 12 -ipadx 2 -ipady 2

frame .f_def
button .f_def.defects -text "Add Defects Manually" -activebackground yellow -command enter_defs \
	-width [expr $textwidth]
menubutton .f_def.part -text "Simple Partioning" \
	-width [expr $textwidth] -relief raised -menu .f_def.part.menu \
	-activebackground SlateGray1
menu .f_def.part.menu -background SlateGray1 -activebackground SlateGray2
.f_def.part.menu add radiobutton -label "Don't touch Partion Sector" \
	-variable par_partstyle -value "none"
.f_def.part.menu add radiobutton -label "DOS Style Partion" \
	-variable par_partstyle -value "-fdos"
.f_def.part.menu add radiobutton -label "Tight Style Partion (not on Cylinder Boundary)" \
	-variable par_partstyle -value "-ftight"
.f_def.part.menu add separator
.f_def.part.menu add cascade -menu .f_def.part.menu.types -label "Partition type"
.f_def.part.menu add cascade -menu .f_def.part.menu.mkfs -label "Filesystem type"
.f_def.part.menu add separator
.f_def.part.menu add command -command post_geom
.f_def.part.menu add command -command post_partsize

menu .f_def.part.menu.types -background SlateGray1 -activebackground SlateGray2
.f_def.part.menu.types add radiobutton -variable par_parttype \
	-label "Linux/Minix" -value "-y81"
.f_def.part.menu.types add radiobutton -variable par_parttype \
	-label "Linux swap" -value "-y82"
.f_def.part.menu.types add radiobutton -variable par_parttype \
	-label "Linux native" -value "-y83"
.f_def.part.menu.types add radiobutton -variable par_parttype \
	-label "DOS 12-bit FAT" -value "-y01"
.f_def.part.menu.types add radiobutton -variable par_parttype \
	-label "DOS 16-bit FAT <32M" -value "-y52"
.f_def.part.menu.types add radiobutton -variable par_parttype \
	-label "DOS 16-bit FAT >=32M" -value "-y64"

menu .f_def.part.menu.mkfs -background SlateGray1 -activebackground SlateGray2
for {set i 0} {$i < [llength $par_mkfsnames]} {incr i} {
    .f_def.part.menu.mkfs add radiobutton -variable par_mkfs -command change_mkfs \
	-label "[lindex $par_mkfsnames $i]" -value "[lindex $par_mkfscmds $i]"
}
.f_def.part.menu.mkfs add separator
.f_def.part.menu.mkfs add command -command post_mkfs
change_mkfs

change_partmin
change_geom $par_geomdetect $par_heads $par_sectors

pack .f_def.defects .f_def.part -side left -padx 12 -ipadx 2 -ipady 2

frame .f_opt -width $winwidth -relief raised -borderwidth 2
checkbutton .f_opt.fov -offvalue "" -onvalue "-o" -variable par_fov -text \
	"    Use Custom Format Parameters" -command change_fov -activebackground yellow \
	-width [expr $winwidth - 2]
pack .f_opt.fov -side top -padx 12 -ipadx 2 -ipady 2 -pady 5
frame .f_opt.col1
frame .f_opt.col2

button .f_opt.col1.init -text "Enter initialization pattern" -activebackground yellow \
         -command enter_init -width [expr $textwidth + 2]
checkbutton .f_opt.col1.disprim -offvalue "" -onvalue "-p" -variable par_disprim -text \
	"Ignore Primary Defects" -width $textwidth
checkbutton .f_opt.col1.nvram -offvalue "" -onvalue "-S" -variable par_nvram -text \
	"Erase MODE SELECT Nvram" -width $textwidth
pack .f_opt.col1.init .f_opt.col1.disprim .f_opt.col1.nvram \
	-side top -padx 12 -ipadx 2 -ipady 2 -pady 5
checkbutton .f_opt.col2.discert -offvalue "" -onvalue "-c" -variable par_discert -text \
	"Disable Media Certification" -width $textwidth
checkbutton .f_opt.col2.stop -offvalue "" -onvalue "-s" -variable par_stop -text \
	"Stop On Missing Defectlist" -width $textwidth
label .f_opt.col2.dummy 
pack .f_opt.col2.discert .f_opt.col2.stop .f_opt.col2.dummy \
	-side top -padx 12 -ipadx 2 -ipady 2 -pady 5
pack .f_opt.col1 .f_opt.col2 -side left

frame .f_block
checkbutton .f_block.par_block -offvalue "-b" -onvalue "" -variable par_query -text \
	"Query progress from target" -width $textwidth -command { block_change $par_query }

label .f_block.f_estim -text "Estimated run time in sec:             " -width $textwidth
entry .f_block.f_estim.t -width 5 -background white -relief sunken -borderwidth 2 \
	-exportselection 1 -textvariable par_estim
bind_entry .f_block.f_estim.t

place .f_block.f_estim.t -in .f_block.f_estim -anchor e -relx 1.0 -rely 0.5
pack  .f_block.par_block .f_block.f_estim -side left -padx 12 -ipadx 2 -ipady 2
block_change $par_query

change_fov

pack .header -padx 10 -pady 10
pack .f_mand .f_def -pady 4
pack .f_opt -padx 10 -pady 10
pack .f_block -padx 10 -pady 4
pack .f_bot1 .f_bot2 -padx 10 -pady 4

show_busy