#!/usr/local/bin/expectk -f

global lise lisr
list lisr
list lise

set timeout -1
#problem dealing with double button when setting data field
wm title . "XMILL for 1-D"
set ltype "1-D"
# Default to regrouped data
global expect_out(buffer)
set ress expect_out(buffer)
# should use info to do following
puts "\nInitial check for environment variables \n 
SAS_DIR SAS_INSTRUMENT SAS_DATA_PATH  \n
set with command setenv \n\n"
set sasdir $env(SAS_DIR)
set a $env(SAS_INSTRUMENT)
set a $env(SAS_DATA_PATH)


proc ipect {} {
expect "*E,H> $" {puts "AOK"}
}


# main frame

frame .f
pack .f -expand 1 -fill both



frame .fm 
pack .fm -in .f -expand 1 -fill both

proc checkv { field  defblank defval button bstat} {
# input should clear buton status
$button deselect
set bstat 0
checkvs $field $defblank $defval
}


proc checkvs { field  defblank defval } {
mess  ""
set aaa  [ $field get ]
# if blank input then input not in use
if  {$aaa == ""} {
$field delete 0 end
$field  insert 0 $defblank
} else {
# if bum real input the next line objects
if { $aaa != $defblank } {
if { [catch {expr $aaa }]==0} {
set bbb   [expr $aaa ]
mess  "" 
} else {
# error message for bum input
$field delete  0 end
$field  insert 0 $defval
mess  "Last entry needs numerical value" 
}
}
}
}


proc mess { txt } {
.mes config -text $txt
}


proc invbut { invnam vzB1  rebox eebox  } {
#   checks input run presence
#   deals with sending back/div runs commands to mill 
#   pick up values from global fields
  mess ""
  set rzB1 [ $rebox  get ]
  set ezB1 [ $eebox  get ]
  puts "input status is $vzB1"
  if { $vzB1  == 0 } {
# already selected - deselect
    $rebox delete 0 end
    $rebox insert 0 "unused"
    $eebox delete 0 end
    $eebox insert 0 "0"
    return 0
    }


# try to select  run info....
# get entry box value for run
  if { $rzB1 == "unused" } {
    return 0
#   deselect - no valid info yet...
    }


# send command and see if run data is actually found...
  exp_send "x  $rzB1 $ezB1  \r"

  expect {
    "? input ignored !"        { 
       mess "data not found"
#messsage from RGIO
       ipect
       set vzB1 0
       $invnam deselect
       return  0
                                }
 ipect
 return  1
 }
}


# end procedure invbut



proc dobut {invnam rebox eebox mebox cmd} {
  set rzB1 [ $rebox  get ]
  set ezB1 [ $eebox  get ]
  set mzB1 [ $mebox  get ]
  set cc $cmd
  exp_send "$cc $rzB1 $ezB1 $mzB1 \r"
  ipect
}
#end dobut



proc defset { fld def } {
$fld delete 0 end
$fld insert 0 $def
}

proc razz {} {
global lise lisr
.frx delete 0 end
set lise ""
set lisr ""
defset .ferzB1 unused
defset .feezB1 0
defset .femzB1 1.0
defset .ferzB2 unused
defset .feezB2 0
defset .femzB2 1.0
defset .ferzB2 unused
defset .feezB3 0
defset .femzB3 1.0
defset .ferzB4 unused
defset .feezB4 0
defset .femzB4 1.0
defset .ferzD1 unused
defset .feezD1 0
defset .femzD1 1.0
defset .ferzS1 ""
defset .felzS1 ""
defset .feezS1 0
defset .femzS1 1.0
defset .feszS1 1
.ftzB1 deselect
.ftzB2 deselect
.ftzB3 deselect
.ftzB4 deselect
.ftzD1 deselect
exp_send "z \r"
ipect
}



# picture of equation 

image create photo equat -file "$sasdir/equat.gif"
label .leq -image equat -relief groove \
-borderwidth 1 -bg red

pack .leq -in .fm -side top  -expand 1 \
-fill both



#top labels and buttons



# Choice of 1-D or 2-D data

radiobutton .lma -text "Regrouped Data '1-D'" -variable dim\
-value 1 -command {
#send a change value to mill (and reset fields as well...)
set spawn_id $sp_id
exp_send "c 1 \r"
set ltype "1-D"
ipect
mess "1-D: now set backgrounds/divider then Sample sets "
set forapp "1"
razz
wm title . "XMILL for 1-D"
# should do generic resets on paras and run numbers and logicals
}
.lma select
# default is regrouped data
radiobutton .lmb -text "Ungrouped data '2-D'" -variable dim\
-value 2 -command {
#send a change value to fortran
set spawn_id $sp_id
exp_send "c 2 \r"
ipect
razz 

set ltype "2-D"
wm title . "XMILL for 2-D"
# should do generic resets on paras and run numbers and logicals
.leq config -bg purple
mess "2-D: now set backgrounds/divider then Sample sets "
set forapp "2"
}
pack .lma .lmb -in .fm -side left -expand 1 -fill both

#  messsage bar

label .mes -fg red
.mes config -text "Select 1-D or 2-D data treatment"
pack .mes -in .f -side top   -expand 1 \
-fill both


# MAIN INPUT WINDOWS AND RESULT WINDOW



# double frame for run nos and results
frame .f2
pack .f2 -in .f -side top -expand 1 -fill both

#  background runs

frame .fr -relief groove -borderwidth 3
frame .fi -relief groove -borderwidth 3

pack .fi .fr -in .f2 -side left -fill both

frame .fb -relief groove -borderwidth 3
frame .fs -relief groove -borderwidth 3
frame .fx -relief groove -borderwidth 3
pack .fb .fs .fx -in .fi -side top -expand 1 -fill both

frame .fbb 
pack .fbb  -in .fb -side top





# set of background runs
label .fb00 -text "\n Background Runs\n"
frame .fzB0 
frame .fzB1
frame .fzB2
frame .fzB3
frame .fzB4

label .fd00 -text "\n Divider Run \n"
frame .fzD0
frame .fzD1
pack .fb00 .fzB1 .fzB2 .fzB3 .fzB4  .fd00 .fzD1 -in .fbb -side top -expand 1 -fill x

# Sample sets
frame .fss 
pack .fss -in .fs -side top -expand 1 -fill both

frame .fzS0
frame .fzS1
frame .fzS2
frame .fzS3
pack .fzS0 .fzS1 .fzS2 .fzS3 -in .fss -side top -expand 1 -fill x

label .fs00 -text "\n Sequence of Sample Runs  S \n"
pack .fs00 -in .fzS0 -expand 1 -fill x




# first background run  (generic data input run/ext/mult)



set rzB1 "unused"
set ezB1 "0"
set mzB1 "1.0"
set vzB1 0
set szB1 0
entry .ferzB1 -width 6 -textvariable rzB1
entry .feezB1 -width 6 -textvariable ezB1
entry .femzB1 -width 8 -textvariable mzB1
label .ftrzB1 -text "B1 Run No."
label .ftezB1 -text "Ext"
label .ftmzB1 -text "Mul b1"
# activate....
checkbutton .ftzB1 -variable vzB1 -command {
set szB1  [ invbut .ftzB1 $vzB1   .ferzB1 .feezB1  ]
}
pack .ftzB1 .ftrzB1 .ferzB1 .ftezB1 .feezB1 .ftmzB1 .femzB1 -in .fzB1\
-side left -expand 1 -fill both 
 
# functions....
bind .ferzB1 <Key-Return> {checkv .ferzB1 "unused" "invalid" .ftzB1 vzB1}
bind .ferzB1 <Key-Tab> {checkv .ferzB1 "unused" "invalid" .ftzB1 vzB1}
bind .feezB1 <Key-Return> {checkv .feezB1 0 0 .ftzB1 vzB1}
bind .feezB1 <Key-Tab> {checkv .feezB1 0 0 .ftzB1 vzB1}
bind .femzB1 <Key-Return> {checkv .feezB1 1.0 1.0 .ftzB1 vzB1}
bind .femzB1 <Key-Tab> {checkv .feezB1 1.0 1.0 .ftzB1 vzB1}





# second background run  (generic data input run/ext/mult)




set rzB2 "unused"
set ezB2 "0"
set mzB2 "1.0"
set vzB2 0
set szB2 0
entry .ferzB2 -width 6 -textvariable rzB2
entry .feezB2 -width 6 -textvariable ezB2
entry .femzB2 -width 8 -textvariable mzB2
label .ftrzB2 -text "B2 Run No."
label .ftezB2 -text "Ext"
label .ftmzB2 -text "Mul b2"
# activate....
checkbutton .ftzB2 -variable vzB2 -command {
set szB2 [invbut .ftzB2 $vzB2    .ferzB2 .feezB2 ]
# .......end activation
}
 
pack .ftzB2 .ftrzB2 .ferzB2 .ftezB2 .feezB2 .ftmzB2 .femzB2 -in .fzB2\
-side left -expand 1 -fill both 
 
# functions....
bind .ferzB2 <Key-Return> {checkv .ferzB2 "unused" "invalid" .ftzB2 vzB2}
bind .feezB2 <Key-Return> {checkv .feezB2 0 0 .ftzB2 vzB2}
bind .femzB2 <Key-Return> {checkv .feezB2 1.0 1.0 .ftzB2 vzB2}
bind .ferzB2 <Key-Tab> {checkv .ferzB2 "unused" "invalid" .ftzB2 vzB2}
bind .feezB2 <Key-Tab> {checkv .feezB2 0 0 .ftzB2 vzB2}
bind .femzB2 <Key-Tab> {checkv .feezB2 1.0 1.0 .ftzB2 vzB2}




# Third background run  (generic data input run/ext/mult)




set rzB3 "unused"
set ezB3 "0"
set mzB3 "1.0"
set vzB3 0
set szB3 0
entry .ferzB3 -width 6 -textvariable rzB3
entry .feezB3 -width 6 -textvariable ezB3
entry .femzB3 -width 8 -textvariable mzB3
label .ftrzB3 -text "B3 Run No."
label .ftezB3 -text "Ext"
label .ftmzB3 -text "Mul b3"
# activate....
checkbutton .ftzB3 -variable vzB3 -command {
set szB3 [invbut .ftzB3 $vzB3   .ferzB3 .feezB3 ]
# .......end activation
}
 
pack .ftzB3 .ftrzB3 .ferzB3 .ftezB3 .feezB3 .ftmzB3 .femzB3 -in .fzB3\
-side left -expand 1 -fill both 
 
# functions....
bind .ferzB3 <Key-Return> {checkv .ferzB3 "unused" "invalid" .ftzB3 vzB3}
bind .feezB3 <Key-Return> {checkv .feezB3 0 0 .ftzB3 vzB3}
bind .femzB3 <Key-Return> {checkv .feezB3 1.0 1.0 .ftzB3 vzB3}
bind .ferzB3 <Key-Tab> {checkv .ferzB3 "unused" "invalid" .ftzB3 vzB3}
bind .feezB3 <Key-Tab> {checkv .feezB3 0 0 .ftzB3 vzB3}
bind .femzB3 <Key-Tab> {checkv .feezB3 1.0 1.0 .ftzB3 vzB3}



# Fourth background

set rzB4 "unused"
set ezB4 "0"
set mzB4 "1.0"
set vzB4 0
set szB4 0
entry .ferzB4 -width 6 -textvariable rzB4
entry .feezB4 -width 6 -textvariable ezB4
entry .femzB4 -width 8 -textvariable mzB4
label .ftrzB4 -text "B4 Run No."
label .ftezB4 -text "Ext"
label .ftmzB4 -text "Mul b4"
# activate....
checkbutton .ftzB4 -variable vzB4 -command {
set szB4 [invbut .ftzB4 $vzB4    .ferzB4 .feezB4 ]
# .......end activation
}
 
pack .ftzB4 .ftrzB4 .ferzB4 .ftezB4 .feezB4 .ftmzB4 .femzB4 -in .fzB4\
-side left -expand 1 -fill both 
 
# functions....
bind .ferzB4 <Key-Return> {checkv .ferzB4 "unused" "invalid" .ftzB4 vzB4}
bind .feezB4 <Key-Return> {checkv .feezB4 0 0 .ftzB4 vzB4}
bind .femzB4 <Key-Return> {checkv .feezB4 1.0 1.0 .ftzB4 vzB4}
bind .ferzB4 <Key-Tab> {checkv .ferzB4 "unused" "invalid" .ftzB4 vzB4}
bind .feezB4 <Key-Tab> {checkv .feezB4 0 0 .ftzB4 vzB4}
bind .femzB4 <Key-Tab> {checkv .feezB4 1.0 1.0 .ftzB4 vzB4}




# Divider





set rzD1 "unused"
set ezD1 "0"
set mzD1 "1.0"
set vzD1 0
set szD1 0
entry .ferzD1 -width 6 -textvariable rzD1
entry .feezD1 -width 6 -textvariable ezD1
entry .femzD1 -width 8 -textvariable mzD1
label .ftrzD1 -text "D Run No."
label .ftezD1 -text "Ext"
label .ftmzD1 -text "Mul d"
# activate....
checkbutton .ftzD1 -variable vzD1 -command {
set szD1 [invbut .ftzD1 $vzD1   .ferzD1 .feezD1 ]
# .......end activation
}
 
pack .ftzD1 .ftrzD1 .ferzD1 .ftezD1 .feezD1 .ftmzD1 .femzD1 -in .fzD1\
-side left -expand 1 -fill both 
 
# functions....
bind .ferzD1 <Key-Return> {checkv .ferzD1 "unused" "invalid" .ftzD1 vzD1}
bind .feezD1 <Key-Return> {checkv .feezD1 0 0 .ftzD1 vzD1}
bind .femzD1 <Key-Return> {checkv .feezD1 1.0 1.0 .ftzD1 vzD1}
bind .ferzD1 <Key-Tab> {checkv .ferzD1 "unused" "invalid" .ftzD1 vzD1}
bind .feezD1 <Key-Tab> {checkv .feezD1 0 0 .ftzD1 vzD1}
bind .femzD1 <Key-Tab> {checkv .feezD1 1.0 1.0 .ftzD1 vzD1}







# First Sample Line - run ext into .fzS1

set rzS1 ""
set ezS1 "0"
set mzS1 "1.0"
set lzS1 ""
set szS1 1
entry .ferzS1 -width 6 -textvariable rzS1
entry .feezS1 -width 6 -textvariable ezS1
entry .femzS1 -width 8 -textvariable mzS1
entry .felzS1 -width 6 -textvariable lzS1
entry .feszS1 -width 3 -textvariable szS1
label .ftrzS1 -text "First Run No."
label .ftezS1 -text "Ext"
label .ftmzS1 -text "Mul m "
label .ftlzS1 -text "Last Run No."
label .ftszS1 -text "Step"




button .ftzS1 -text "Calculate results R" -command {
mess ""
if { $lzS1 == "" } { set lzS1 $rzS1 }
if { $lzS1 == 0  } { set lzS1 $rzS1 }
set last $lzS1
set step $szS1
if { $rzS1 == "" } {
mess "Please set first Sample Run"
} else {

# set up back and divider runs:

set dum "dum"
set cmd "b 1"
set cmd1 "b 2"
if { $vzB1 == 1 } { dobut dum .ferzB1 .feezB1 .femzB1 $cmd } 
if { $vzB2 == 1 } { dobut dum .ferzB2 .feezB2 .femzB2 "B 2" } 
if { $vzB3 == 1 } { dobut dum .ferzB3 .feezB3 .femzB3 "B 3" } 
if { $vzB4 == 1 } { dobut dum .ferzB4 .feezB4 .femzB4 "B 4" } 
if { $vzD1 == 1 } { dobut dum .ferzD1 .feezD1 .femzD1 "D " } 

#now do manual loop over input set to find errors!

set irst $rzS1
expect *
while { $irst <= $last } {



exp_send "s $irst $ezS1 $irst 1 $mzS1 \r"
expect {
"*Input data absent*Option" { mess "input data $irst / $ezS1 missing"}
"*Result is in" {
expect *
global lisr lise
set fff [ lindex $expect_out(buffer) 0 ]
set ggg [ lindex $expect_out(buffer) 2 ]
mess "Last result is in $fff / $ggg "
.frx insert end "$fff /$ggg" 
.frx see end
# add in to ends of lisr and lise
lappend lisr $fff
lappend lise $ggg
}
}
incr irst $step
#clear out final prompt
expect *
}



}
}


pack .ftrzS1 .ferzS1 .ftezS1 .feezS1 .ftmzS1 .femzS1 -in .fzS1\
-side left -expand 1 
pack .ftlzS1 .felzS1 .ftszS1 .feszS1  -in .fzS2 -side left  -expand 1 
pack .ftzS1 -in .fzS3 -expand 1 
bind .ferzS1 <Key-Return> {mess "";checkvs .ferzS1 "" "invalid" }
bind .ferzS1 <FocusOut> {mess "";checkvs .ferzS1 "" "invalid" }
bind .feezS1 <Key-Return> {mess "";checkvs .feezS1 "" "invalid" }
bind .feezS1 <FocusOut> {mess "";checkvs .feezS1 0 0 }
bind .femzS1 <Key-Return> {mess "";checkvs .femzS1 1 1 }
bind .femzS1 <FocusOut> {mess "";checkvs .femzS1 1 1 }
bind .felzS1 <Key-Return> {mess "";checkvs .felzS1 0 0 }
bind .felzS1 <FocusOut> {mess "";checkvs .felzS1 0 0 }
bind .feszS1 <Key-Return> {mess "";checkvs .feszS1 1 1 }
bind .feszS1 <FocusOut> {mess "";checkvs .feszS1 1 1 }




# results scroll

frame .frr
pack .frr -in .fr -side top -expand 1
label .frl -text "Results \n\n Double click\nto view"
listbox .frx -yscrollcommand ".frs set" -selectmode single  -width 12
scrollbar .frs -command ".frx yview"
bind .frx <Double-Button-1> {
global lisr lise ltype
set ly [.frx curselection ] 

#list $lisr
#list $lise
set rrr [lindex $lisr $ly]
set eee [lindex $lise $ly]
if { $ltype == "1-D" } {
set comish "$env(SAS_DIR)/pdqpg $rrr $eee "
eval exec $comish
} else {
set comish "$env(SAS_DIR)/pdqpt $rrr $eee"
eval exec $comish
}
}

pack .frl -in .frr -fill x
pack .frs -in .frr -side right -expand 1 -fill y
pack .frx -in .frr -side left -expand 1 -fill both


# preview data

frame .fxx
pack .fxx -in .fx
#preview
button .ftzX1 -text "Preview" -command {
mess ""
set rrrx $rzX1
set eeex $ezX1
if { $ltype == "1-D" } {
set comish "$env(SAS_DIR)/pdqpg $rrrx $eeex "
eval exec $comish
} else {
set comish "$env(SAS_DIR)/pdqpt $rrrx $eeex"
eval exec $comish
}
}
set rzX1 ""
set ezX1 "0"
entry .ferzX1 -width 6 -textvariable rzX1
entry .feezX1 -width 6 -textvariable ezX1
label .ftrzX1 -text " Run No."
label .ftezX1 -text "Ext"

pack .ftzX1 .ftrzX1 .ferzX1 .ftezX1 .feezX1 -in .fxx -side left -expand 1\
-fill both
bind .ferzX1 <Key-Return> {mess "";checkvs .ferzX1 "" "invalid" }
bind .ferzX1 <FocusOut> {mess "";checkvs .ferzX1 "" "invalid" }
bind .feezX1 <Key-Return> {mess "";checkvs .feezX1 "" "invalid" }
bind .feezX1 <FocusOut> {mess "";checkvs .feezX1 0 0 }

# EXIT
button .ftzE1 -text "EXIT" -command {
exp_send "e \r"
expect "% end !" {exit}

}
pack .ftzE1 -in .fx
set version 1.0
exec $env(SAS_DIR)/monbug XMLL $version

# start Fortran Task
set rin 0
spawn "$sasdir/mill"
set sp_id $spawn_id
exp_send "c 1 \r"
ipect
# by default set to regrouped data
