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

# Revision 1.2  2007/03/12 16:17:52  emery
# Missing $ for variable in rtfb section.
#
# Revision 1.1  2007/01/26 20:28:51  emery
# Shang's first version.
#

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 OCDir /home/helios/oagData/sr/orbitControllaw/lattices

proc GetLattices {args} {
    set type OC
    APSParseArguments {type}

    global ${type}Dir

    set files [glob [set ${type}Dir]/*]
    set latticeList ""
    foreach file $files {
        set item [file tail $file]
        if {![string match "*default*" $item] && \
                ![string match "*public*" $item]!=0 && \
                [file isdirectory $file]} {
            set files1 [glob -nocomplain $file/h.*] 
            if [llength $files1] {
                lappend latticeList $item
            }
        }
    }
    return [lsort $latticeList]
}

proc GetConfigList {args} {
    set plane h
    set lattice ""
    set type ""
    APSParseArguments {plane lattice type} 

    global ${type}Dir
    set mainDir [set ${type}Dir]
   # set oldDir [pwd]
   # cd $mainDir/$lattice   
    set configList [exec sdds2stream -col=Filename $mainDir/$lattice/${plane}configDesc.sdds]
    return $configList
    
    set files [glob -nocomplain ${plane}.*]
    
    set configList ""
    set configList1 ""
    foreach file $files {
        if [file isdirectory $file] {
            set year [scan $file ${plane}.%ld]
            if {[string length $year]!=4} {
                lappend configList $file
            } else {
                lappend configList1 $file
            }
        }
    }
    set configList [lsort $configList]
    set configList1 [lsort -decreasing $configList1]
    cd $oldDir
    return [concat $configList $configList1]
}

proc UpdateConfigList {args} {
    set lattice ""
    set type ""
    APSParseArguments {lattice type}
    
    global ${type}Lattice1 ${type}Lattice2 planeIndex h${type}Config1 v${type}Config1
    global h${type}ConfigW1 v${type}ConfigW1 h${type}Config2 v${type}Config2
    global h${type}ConfigW2 v${type}ConfigW2 status
    
    foreach plane {h v} {
        set configList [GetConfigList -type $type -lattice [set ${type}Lattice$lattice] -plane $plane]
        if ![llength $configList] {
            set status "No $plane plane general configs found for $type [set ${type}Lattice$lattice]"
            update
            continue
        }
        set ${plane}${type}Config$lattice [lindex $configList 0]
        set widget [set ${plane}${type}ConfigW$lattice]
        if [winfo exist $widget.config$lattice] {
            destroy $widget.config$lattice
        }
        APSComboboxFrame .config$lattice -parent $widget -label "Configure$lattice:" \
            -width 30 -textVariable  ${plane}${type}Config$lattice \
            -itemList "$configList"
    }
}

proc CompareConfig {args} {
    set type ""
    set plane ""
    APSParseArguments {type plane}
    
    global ${type}Lattice1 ${type}Lattice2 ${plane}${type}Config1 ${plane}${type}Config2
    global ${type}Dir status
    
    set lattice1 [set ${type}Lattice1]
    set lattice2 [set ${type}Lattice2]
    set config1 [set ${plane}${type}Config1]
    set config2 [set ${plane}${type}Config2]
    set mainDir [set ${type}Dir]
    
    set status "Compare $type $lattice1/$config1 with $lattice2/$config2"
    if ![file exist $mainDir/$lattice1/$config1] {
        set status "$config1 does not exist for $type lattice $lattice1"
        return
    }
    if ![file exist $mainDir/$lattice1/$config1] {
        set status "$config1 does not exist for $type lattice $lattice1"
        return
    }
    if ![file exist $mainDir/$lattice2/$config2] {
        set status "$config2 does not exist for $type lattice $lattice2"
        return
    }
    if ![file exist $mainDir/$lattice1/$config1/config] {
        set status "$type $lattice1 ${config1}/config does not exist!"
        return
    }
    if ![file exist $mainDir/$lattice2/$config2/config] {
        set status "$type $lattice2 ${config2}/config does not exist!"
        return
    }
    set tmpRoot /tmp/[APSTmpString]
    global config1Status config2Status
    if [catch {exec sddsselect $mainDir/$lattice1/$config1/config \
                   $mainDir/$lattice2/$config2/config \
                   -match=Name -invert $tmpRoot.1 } result] {
        return -code eror $result
    }
    if [catch {exec sddsselect $mainDir/$lattice2/$config2/config \
                   $mainDir/$lattice1/$config1/config \
                   -match=Name -invert  $tmpRoot.2} result] {
        return -code eror $result
    }
    if [catch {exec sddscombine $tmpRoot.1 -pipe=out -merge | sdds2stream -pipe -rows=bar} rows1] {
        return -code error $rows1
    }
    if [catch {exec sddscombine $tmpRoot.2 -pipe=out -merge | sdds2stream -pipe -rows=bar} rows2] {
        return -code error $rows2
    }
    
    if {$rows1==0 && $rows2==0} {
        set status "$type $lattice1/$config1 and $lattice2/$config2 are the same."
        update
        return
    }
   
    if {$rows1>0} {
        set config1Status "Following devices were added to $type $lattice1/$config1 relative to $lattice2/$config2"
        catch {exec sddsprocess $tmpRoot.1 -pipe=out \
                   -define=par,rows,n_rows -nowarnings \
                   | sddsprocess -pipe -filter=par,rows,0,0,! -nowarnings \
                   | sddsprintout -par=NameType -notitle -col=Name -pipe=in} result
        set config1Status "$result\n"
        update
    } else {
        set config1Status "No devices were added to $type $lattice1/$config1 relative to $lattice2/$config2\n"
        update
    }
    if {$rows2>0} {
        set config2Status "Following devices were added to $type $lattice2/$config2 relative to $lattice1/$config1"
        catch {exec sddsprocess $tmpRoot.2 -pipe=out \
                   -define=par,rows,n_rows -nowarnings \
                   | sddsprocess -pipe -filter=par,rows,0,0,! -nowarnings \
                   | sddsprintout -par=NameType,endsline -notitle -col=Name -pipe=in} result
        set config2Status "$result\n"
        update
    } else {
        set config2Status "No devices were added to $type $lattice2/$config2 relative to $lattice1/$config1\n"
        update
    }
    return
}
proc CreateCompareFrame {widget args} {
    set parent ""
    set type ""
    APSParseArguments {parent type}
    
    global ${type}Lattice1 ${type}Lattice2 planeIndex h${type}Config1 v${type}Config1
    global h${type}ConfigW1 v${type}ConfigW1 h${type}Config2 v${type}Config2
    global  h${type}ConfigW2 v${type}ConfigW2
    
    set latticeList [GetLattices -type $type]
    set ${type}Lattice1 release-2024-03-29 
    set ${type}Lattice2 release-2024-01-09
    
    APSFrame $widget -parent $parent -label "Select Configuration for Comparison" 
    
    APSFrameGrid .grid1 -parent $parent$widget.frame -xList {x1 x2} -label "Lattice Selection" 
    set w1 $parent$widget.frame.grid1.x1
    set w2 $parent$widget.frame.grid1.x2
    
    APSComboboxFrame .lattice1 -parent $w1 -label "Lattice1:" \
        -width 40 -textVariable  ${type}Lattice1 -itemList $latticeList \
        -callback "UpdateConfigList -lattice 1 -type $type"
    APSComboboxFrame .lattice2 -parent $w2 -label "Lattice2:" \
        -width 40 -textVariable  ${type}Lattice2 -itemList $latticeList \
        -callback "UpdateConfigList -lattice 2 -type $type"
    
    set widgetList [APSTabFrame .tab -parent $parent$widget.frame -label "" -width 1000 -height 80 \
                        -labelList {horizontal vertical} -packOption "-expand yes -fill both" \
                        -frameIndexVariable planeIndex]
    set hwidget [lindex $widgetList 0]
    set vwidget [lindex $widgetList 1] 
    
    foreach plane {h v} {
        set configList1 [GetConfigList -type $type -lattice [set ${type}Lattice1] -plane $plane]
        set configList2 [GetConfigList -type $type -lattice [set ${type}Lattice2] -plane $plane]
        set ${plane}${type}Config1 [lindex $configList1 0]
        set ${plane}${type}Config2 [lindex $configList2 0]
        APSFrameGrid .grid2 -parent [set ${plane}widget] -xList {x1 x2 x3} -packOption "-expand yes -fill both"
        set w1 [set ${plane}widget].grid2.x1
        set w2 [set ${plane}widget].grid2.x2 
        set w3 [set ${plane}widget].grid2.x3
        set ${plane}${type}ConfigW1 $w1
        set ${plane}${type}ConfigW2 $w2
        APSComboboxFrame .config1 -parent $w1 -label "Configure1:" \
            -width 30 -textVariable  ${plane}${type}Config1 \
            -itemList "$configList1"
        APSComboboxFrame .config2 -parent $w2 -label "Configure2:" \
            -width 30 -textVariable  ${plane}${type}Config2 \
            -itemList "$configList2"
        APSButton .compare -parent $w3 -text "Compare" -size small -command \
            "CompareConfig -type $type -plane $plane" -packOption "-side top"
        APSFrameGrid .grid3 -parent [set ${plane}widget] -xList {x1 x2} 
        set w1 [set ${plane}widget].grid3.x1
        set w2 [set ${plane}widget].grid3.x2 
       # global ${plane}${type}BpmResults  ${plane}${type}CorrResults
       # set ${plane}${type}BpmResults ""
       # set ${plane}${type}CorrResults ""
       # APSScrolledStatus .corr  -parent $w1  -textVariable ${plane}${type}BpmResults -width 52 \
       #     -height 15 -withButtons 1 -packOption "-expand yes -fill both" \
       #     -label "BPMs comparison"
       # APSScrolledStatus .corr  -parent $w2  -textVariable ${plane}${type}CorrResults -width 52 \
       #     -height 15 -withButtons 1 -packOption "-expand yes -fill both" \
       #     -label "Correctors comparison"
    }
}

set status ""
APSApplication . -name SRConfigCompare  \
    -overview {This utility compares the two configurations.}
APSScrolledStatus .status  -parent .userFrame  -textVariable status -width 100 \
  -height 4 -withButtons 1 -packOption "-fill x"

set topIndex -1
 
APSFrame .sel -parent .userFrame -relief flat
set widgetList [APSTabFrame .tab -parent .userFrame.sel.frame -label "" \
                    -labelList "OrbitCorrection" \
                    -width 1000 -height 170   \
                    -frameIndexVariable topIndex]

CreateCompareFrame .oc -parent [lindex $widgetList 0] -type OC

set config1Status ""
set config2Status ""
APSFrameGrid .grid -parent .userFrame -packOption "-expand yes -fill both" -xList {x1 x2}
APSScrolledStatus .bpm  -parent .userFrame.grid.x1  -textVariable config1Status -width 50 \
    -height 15 -withButtons 1 -packOption "-expand yes -fill both" \
    -label "PVs that are added to config1"
APSScrolledStatus .corr  -parent .userFrame.grid.x2  -textVariable config2Status -width 50 \
    -height 15 -withButtons 1 -packOption "-expand yes -fill both" \
    -label "PVs that are added to config2"
