#!/usr/bin/wish -f
# Copyright 1993 Yggdrasil Computing, Incorporated
# 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.

set postgres_file /etc/rc.postgres

set whoami [exec whoami]
if { [string compare $whoami root] != 0 && [string compare [lindex $argv 0] -demo] != 0} then {
    frame .warn
    label .warn.l -bitmap info
    message .warn.m -aspect 200 -background pink -text \
	{You need to be root in order to change scsi configuration\
	 options for a scsi device}
    frame .warn.f
    button .warn.f.quit -text "Quit" -command exit \
	-activeforeground white -activebackground red
    button .warn.f.continue -text "Continue anyway" -activebackground green \
	-command {destroy .warn}
    pack .warn.f.continue .warn.f.quit -side left -padx 10
    pack .warn.l .warn.m .warn.f -pady 10
    pack .warn
    tkwait window .warn
}

set sdevice $argv
while { [string compare [lindex $sdevice 0] ""] == 0 } {
    frame .select
    label .select.l -bitmap info
    message .select.m -aspect 200 -background pink -text \
	{Select a scsi device}
    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.l .select.m .select.f -pady 10
    pack .select
    exec /usr/bin/scsiinfo -l > /tmp/devices
    if {[catch {set file [open /tmp/devices r]}] == 1} return;
    gets $file line
    close $file
    exec rm /tmp/devices
    set n 0
    foreach x $line { 
       radiobutton .select.$n -text "$x" -width 10   -variable sdevice -value $x -anchor w
	pack .select.$n  -padx 3m
        set n [expr $n+1]
    }
    tkwait window .select
}

frame .header
label .header.label 
message .header.message -width 400 -bg pink -text \
	"Scsi Mode Page configuration - $sdevice." 

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

frame .f_top

label .f_top.l -bitmap info -bg pink

pack .f_top.l -side right


button .f_top.info -text "Device Info" -command "exec /usr/lib/scsi/inquiry $sdevice" \
	-activebackground red -activeforeground white

button .f_top.save -text "Save Changes"    \
    -command "exec /usr/lib/scsi/save-changes $sdevice"

button .f_top.quit -text "Quit" -command { exit } \
	-activebackground red -activeforeground white

pack  .f_top.info .f_top.save .f_top.quit -side left -padx 25 -ipadx 10 -ipady 2

pack .header .f_top -padx 10 -pady 10

message .copyright -width 400 -bg green -text \
     {Copyright 1993 Yggdrasil Computing, Incorporated.  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.  I thought it looked pretty keen, and it was easy to
# adapt, so I used it as the basis for the scsi configuration manager
message .auinfo -width 400  -bg yellow -text\
     {Modified by Eric Youngdale to form an X interface to the scsiinfo\
      program.  Note: Any changes you make will be lost when you power down \
	the scsi device unless you save them. }

pack .copyright -side top -pady 10 -padx 15
pack .auinfo -side top -pady 10 -padx 15

frame .f1
frame .f2

button .f1.cache_button -text "Cache Control Page" \
    -command "exec /usr/lib/scsi/cache $sdevice"
button .f1.control_button -text "Control Mode Page" \
    -command "exec /usr/lib/scsi/control $sdevice"
button .f1.disconnect_button -text "Disconnect/Reconnect Page" \
    -command "exec /usr/lib/scsi/disconnect $sdevice"
button .f1.notch_button -text "Notch Page" \
    -command "exec /usr/lib/scsi/notch $sdevice"
button .f2.format_button -text "Format Device Page" \
    -command "exec /usr/lib/scsi/format $sdevice"
button .f2.error_button -text "Read-Write Error Recovery Page" \
    -command "exec /usr/lib/scsi/error $sdevice"
button .f2.rigid_button -text "Rigid Disk Geometry Page" \
    -command "exec /usr/lib/scsi/rigid $sdevice"

label .f2.dummy -text " "

pack .f1.cache_button .f1.control_button .f1.disconnect_button .f1.notch_button  \
    -anchor w -pady 1

pack .f2.format_button .f2.error_button .f2.rigid_button .f2.dummy -pady 1 -anchor w 

pack .f1 .f2 -side left -padx 15 -pady 10

set line {}
exec /usr/bin/scsiinfo -X -i $sdevice > /tmp/cachepage
if {[catch {set file [open /tmp/cachepage r]}] == 1} return;
gets $file line
set devtype [lindex $line 7]
close $file
exec rm /tmp/cachepage

#Now that we know the device type, we might want to disable a few buttons
echo $devtype
if {$devtype != 0} then {
.f2.rigid_button configure -state disabled
.f2.format_button configure -state disabled
.f1.notch_button configure -state disabled
}
