#!/bin/sh
# \
exec oagwish "$0" "$@"

set auto_path [linsert $auto_path 0  /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0  /usr/local/oag/lib_patch/$env(HOST_ARCH)]

APSStandardSetup

set env(EPICS_CA_AUTO_ADDR_LIST) YES
set env(EPICS_CA_ADDR_LIST) ""

set status ""
proc SetStatus {text} {
    global status
    set status $text
    update
}

set execList ""
proc  KillExecWindows {args} {
    global execList displayList
    foreach win $execList {
	if [winfo exist $win] {
	    destroy $win
	}
    }
    set execList ""
    foreach win $displayList {
	if [winfo exist $win] {
	    destroy $win
	}
    }
    set displayList ""
}

proc ArmMPS {} {
    global sectorList
    SetStatus "Enabling simulator..."
    if [catch {exec cavput -list=APSU:MMPS2:BM:Thold-SP=25 -pend=10
		exec cavput -list=APSU:MMPS2:ID:Thold-SP=5 -pend=10
		exec cavput -list=APSU:MMPS2:Sim:HWmaxBeam-SP=50 -pend=10
		exec cavput -list=APSU:MMPS2:MaxSoftBeam-SP=20 -pend=10
		exec cavput -list=APSU:MMPS2:ArmingBeam-SP=0.7 -pend=10
		exec cavput -list=APSU:MMPS2:Sim:BeamCurrent-SP=25 -pend=10 } result] {
	return -code error "Error sestting MPS current thresholds: $result"
    }
    if [catch {exec cavput -list=APSU:MMPS2:BeamSim-SP=1 -pend=10} result] {
	return -code error "Error enabling beam simulator: $result"
    }
    SetStatus "Check MPS arming status..."
    
    foreach sector $sectorList {
	global S$sector
	if [set S$sector] {
	    if [catch {exec cavget -list=S${sector}-LMPS:SysStatus_B0-Sts.BC -printErrors -pend=10 } armed] {
		return -code error "Error readding $sector MPS arming state: $armed"
	    }
	    if !$armed {
		SetStatus "Error S$sector MPS is not armed, please check."
	    } else {
		SetStatus "S$sector MPS is armed, ready for test."
	    }
	}
    }
}

proc Setup {args} {
    global sectorList
    SetStatus "Disarming..."
    if [catch {APScavput -list=APSU:MMPS2:CtrlReg0-Cmd.B2=0,APSU:MMPS2:BeamSim-SP=0 -pend=10 } result] {
	return -code error "Setup0: $result"
    }
    #reset the timestamp
    SetStatus "set ID and BPM offsets to 0..."
    if [catch {APScavput -list=S -list=[join  $sectorList ,] \
		   -list=-LMPS:BPLD:ID -list=1,2 -list=:AIOR- \
		   -list=H,V -list=1,2 -list=-SP=0 -pend=10} result] {
	return -code error "Error set ID bpm offsts to zero: $result"
    }
    if [catch {APScavput -list=S -list=[join $sectorList ,] \
		   -list=-LMPS:BM_ -list=X,Y -range=begin=1,end=28 -list=_Offset-SP=0 -pend=10} result] {
	return -code error "Error set BM bpm offsets to zero: $result"
    }
    #clear External inputs faults
    if [catch {APScavput -list=S -list=[join $sectorList ,] -list=-LMPS:CtrlReg0-Cmd=0 -pend=10 } result] {
	return -code error "Error clear ext inputs: $result"
    }
    if {0} {
	SetStatus "set ext input fast and slow masks..."
	if [catch {APScavput -list=S -list=[join $sectorList ,] \
		       -list=-LMPS:ExtInFastMask1-SP=1048575,-LMPS:ExtInSlowMask1-SP=0 -pend=10 } result] {
	    return -code error "Error setting masks: $result"
	}
    }
    SetStatus "Reset LMPS and  time stamps..."
    if [catch {APScavput -list=S -list=[join $sectorList ,] -list=-LMPS:UsrReset-,-LMPS:TSReset-,-LMPS:PM:Reset- \
		   -list=SP=1 -pend=10} result] {
	return -code error "Error reset timing stamps: $result"
    }
    SetStatus "Reset Main1 and Main2..."
    if [catch {APScavput -list=APSU:MMPS -list=1,2 -list=:UsrReset-SP=1 -pend=10 } result] {
	return -code error "Error reset main 1 and 2: $result"
    }
    SetStatus "Reset main2 timestamp..."
    if [catch {APScavput -list=APSU:MMPS2:TSReset-SP=1 -pend=10 } result] {
	return -code error "Error reset main2: $result"
    }
   
    
    SetStatus "Wait 5 seconds..."
    after 5000
    SetStatus "Check fault..."
    if [catch {APScavget -list=APSU:MMPS2:SMA:Data10-I,APSU:MMPS2:SMA:Data11-I -pend=10} result] {
	return -code error "Error reading main2 faults: $result"
    }
    set fast [lindex $result 0]
    set slow [lindex $result 1]
    if {$fast || $slow} {
	return -code error "Error: there is fast or slow fault in Main2, system not ready!"
    }
    SetStatus "System is ready for test."
}

proc StartTest {args} {
    global LMPS
    global varList sectorList localSector outDir rootname execList
    eval global $varList
    set outDir $LMPS(outDir)
    #APSDisableButton .userFrame.start.button
    if [catch {APScavget -list=APSU:MMPS2:BeamCurrent-I -pend=10 -printErrors} current] {
	return -code error "Error reading simulation current: $current"
    }
    if {$current>0.5} {
	SetStatus "The current is greater than the MPS arming current threshold (0.5mA), can not run MPS validation."
	#SetStatus "Validation can not be run when the system is armed! Disarm the MPS and run it again"
	return
    }
    ChangeButtonState -enable 0
    switch $localSector {
	odd {
	    set odd 1
	    set even 0
	} 
	even {
	    set even 1
	    set odd 0
	}
    }
    set doList ""
    set rootnameList ""
    foreach sector $sectorList {
	global S${sector}Done
	set S${sector}Done 0
	if [set S$sector] {
	    if [catch {exec cavget -list=S${sector}-LMPS:SysStatus_B0-Sts.BC -pend=10 -num -printErrors} armed] {
		APSEnableButton .userFrame.start.button
		return -code error "Error reading arm state of S$sector: $armed"
	    }
	    if $armed {
		SetStatus "S$sector is being armed, can not run MPS validation!"
		continue
	    }
	    lappend doList $sector
	    SetStatus "Testing S$sector ..."
	    set rootname S${sector}-LMPS-[clock format [clock seconds] -format %Y-%m%d]
	    lappend rootnameList $rootname
	    set command "SRLMPSTest -dir $outDir -sector $sector -odd $odd -even $even -rootname "
	    APSExecLog .s$sector  -width 80 -lineLimit 1048 -name "Test S$sector" \
		-unixCommand "$command" \
		-callback "TestCallback -sector $sector -rootname $rootname -odd $odd" \
		-abortCallback "set S${sector}Done aborted" \
		-cancelCallback "set S${sector}Done canceled"
	    lappend execList .s$sector
	}
    }
    if ![llength $doList] {
	SetStatus "None validate sectors chosen."
	return
    }
    SetStatus "Testing sector $doList ..."
    foreach sector $doList {
	tkwait variable S${sector}Done
	SetStatus "S$sector test [set S${sector}Done]."
    }
    #APSEnableButton .userFrame.start.button
    ChangeButtonState -enable 1
}

proc ChangeButtonState {args} {
    set enable 0
    APSParseArguments {enable}
    if $enable {
	APSEnableButton .userFrame.start.button
	APSEnableButton .userFrame.setup.button
    }  else {
	APSDisableButton .userFrame.start.button
	APSDisableButton .userFrame.setup.button
    }
}

set displayList ""
proc Display0 {args} {
    global displayList
    set filename ""
    APSParseArguments {filename}
    set widget [APSUniqueName .sdispaly] 
    APSFileDisplayWindow $widget  -fileName $filename  -width 140  -height 30 -printCommand "enscript -r"
    lappend displayList $widget
    
}

proc Display {args} {
    global LMPS varList sectorList selectFile displayList
    eval global $varList
    set outDir $LMPS(outDir)
    cd $outDir
    set files ""
    foreach sector $sectorList {
	if [set S$sector] {
	    foreach type {Odd Even} {
		set files0 [glob -nocomplain S${sector}-LMPS-*${type}.txt]
		set files [concat $files $files0]
	    }
	}
    }
    
    if ![llength $files] {
	SetStatus "No files found."
	continue
    }
    set selectFile ""
    
    APSScrolledListWindow .display -height 10 -name "Select File" -itemList $files  -selectionVar selectFile
    tkwait variable selectFile
    destroy .display
    if ![string length $selectFile] {
	SetStatus "No file is chosen."
	continue
    }
    if [file exist $selectFile] {
	Display0 -filename $selectFile
    }
}

proc TestCallback {args} {
    set sector ""
    set rootname ""
    set odd ""
    APSParseArguments {sector rootname odd}
    
    global outDir S${sector}Done
    if $odd {
	set filename $outDir/${rootname}-Odd.txt
    } else {
	set filename $outDir/${rootname}-Even.txt
    }
    set  S${sector}Done "done"
    if [file exist $filename] {
	Display0 -filename $filename
    }
}

set args $argv
set lab 0
APSStrictParseArguments {lab}

APSApplication . -name SRLocalMPSExtInputValidation \
  -overview "SRLocalMPSValidation."

APSScrolledStatus .status -parent .userFrame -width 80 \
        -textVariable status

set sectorList ""
#set labelList ""
for {set sector 1} {$sector<40} {incr sector 2} {
    
    lappend sectorList [format %02d $sector]
    lappend sectorLabelList [format %02d $sector]([format %02d [expr $sector+1]])
    set S[format %02d $sector] 0
    lappend varList S[format %02d $sector]
   # lappend labelList [format %02d $sector](format %02d [expr $sector+1]])
}


set S01 1

if $lab {
    set archiveDir /home/oxygen/DIAG/oagData/sr/BPLD/Validation/LMPS
} else {
    set archiveDir /home/helios/oagData/sr/BPLD/Validation/LMPS
}
set LMPS(archiveDir) $archiveDir
set LMPS(outDir) $archiveDir

set outDir [APSGoToDailyDirectory -subdirectory LMPSVal]
APSLabeledEntry .dir -parent .userFrame -textVariable LMPS(outDir) -width 70 -label "Output directory:"
APSButton .daily -parent .userFrame.dir -packOption "-side right" -text "daily" -size small \
    -command "set LMPS(outDir) [APSGoToDailyDirectory -subdirectory LMPSVal]"
APSButton .archive -parent .userFrame.dir -packOption "-side right" -text "archive" -size small \
    -command "set LMPS(outDir) $archiveDir"

set localSector odd
APSRadioButtonFrame .local -parent .userFrame -buttonList {odd even} -valueList {odd even} \
    -variable localSector -orientation horizontal -label "Local sector (odd/even)" \
    -contextHelp "select odd/even sector for each chosen double sector, odd and even sector in each double sector can only be validated one by one."


#APSFrameGrid .test -parent .userFrame -yList {x1 x2}

#for {set i 1} {$i<40} {incr i 2} {
#    set index [expr int($i/20)+1]
#    APSButton .test$i -parent .userFrame.test.x$index  -text "Test S[format %02d $i]" \
#	-command "StartTest -sector $i"
#}

APSCheckButtonFrame .sector -parent .userFrame -buttonList $sectorLabelList -variableList $varList -limitPerRow 5 \
    -label "Double sector (ioc):" -orientation horizontal -allNone 1

APSButton .setup -parent .userFrame -text "Setup" -command "Setup"
APSButton .start -parent .userFrame -text "Start" -command StartTest
APSButton .display -parent .userFrame -text "Display" -command Display
APSButton .enable -parent .userFrame -text "Enable Buttons" -command "ChangeButtonState -enable 1"
#APSButton .arm -parent .userFrame -text "Arm MPS" -command ArmMPS
APSButton .kill -parent .userFrame -text "Kill Exec/Display Windows" -command "KillExecWindows"



