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

# output files:
# EPS.loc

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 patternList \
  [list \
     S????-FEEPS:FES_PermitM \
     S????-FEEPS:MPS:SlowPermitM \
     S????-FEEPS:FEV:VacuumPermitM \
     S????-FEEPS:FEV:PositionM \
     S????-FEEPS:BIV:PositionM \
     S????-FEEPS:PS1:PositionM \
     S????-FEEPS:VacuumPermitWarnM \
     S????-FEEPS:ShutterPermitWarnM \
     S????-FEEPS:BLEPS_ShutterPermitM \
     S????-FEEPS:SRV:OpenedM \
     PA:????:STA_A_APS_KEY_SW \
     PA:????:STA_B_APS_KEY_SW \
     PA:????:STA_C_APS_KEY_SW \
     PA:????:STA_D_APS_KEY_SW \
     PA:????:STA_E_APS_KEY_SW \
     PA:????:STA_F_APS_KEY_SW \
     PA:????:STA_G_APS_KEY_SW \
     PA:????:A_APS_KEY \
     PA:????:B_APS_KEY \
     PA:????:C_APS_KEY \
     PA:????:D_APS_KEY \
     PA:????:E_APS_KEY \
     PA:????:F_APS_KEY \
     PA:????:G_APS_KEY \
     PA:????:FLT:1 \
     EPS:??:??:VAC:CC2 \
     S:ActualMode \
     S:ActualModeCalc \
     S:DesiredMode \
    ]

set descriptionList \
  [list \
     "Front-end Perm to PSS" \
     "Slow MPS RF Permit" \
     "Front-end vacuum status from FEEPS" \
     "Front-end valve status from FEEPS" \
     "Beamline isolation valve status from FEEPS" \
     "PS1 position from FEEPS" \
     "Beamline EPS vacuum healthy signal to FEEPS" \
     "Beamline EPS shutter permit signal to FEEPS" \
     "BLEPS Shutter Permit" \
     "SR valve opened" \
     "Beamline 'APS Enable' key for station A from PSS" \
     "Beamline 'APS Enable' key for station B from PSS" \
     "Beamline 'APS Enable' key for station C from PSS" \
     "Beamline 'APS Enable' key for station D from PSS" \
     "Beamline 'APS Enable' key for station E from PSS" \
     "Beamline 'APS Enable' key for station F from PSS" \
     "Beamline 'APS Enable' key for station G from PSS" \
     "Beamline 'APS Enable' key for station A from PSS" \
     "Beamline 'APS Enable' key for station B from PSS" \
     "Beamline 'APS Enable' key for station C from PSS" \
     "Beamline 'APS Enable' key for station D from PSS" \
     "Beamline 'APS Enable' key for station E from PSS" \
     "Beamline 'APS Enable' key for station F from PSS" \
     "Beamline 'APS Enable' key for station G from PSS" \
     "PSS fault" \
     "Front-end EPS vacuum cold-cathode gauge #2" \
     "Actual Beam Mode" \
     "Actual Beam Mode" \
     "Desired Beam Mode" \
    ]

set matchOpt ""
foreach pattern $patternList {
    if ![string length $matchOpt] {
        set matchOpt "-match=column,ControlName=$pattern"
    } else {
        append matchOpt ,ControlName=$pattern,|
    }
}

set tmpRoot /tmp/[APSTmpString]
APSAddToTempFileList $tmpRoot

exec sddsconvert /home/helios/oagData/pvdata/iocRecNamesOAG.sdds -pipe=out \
    -retain=column,rec_name -rename=column,rec_name=ControlName \
    | sddsprocess -pipe=in $tmpRoot.all $matchOpt
  

set list1 ""
set index 0
foreach pattern $patternList description $descriptionList {
    APSAddToTempFileList $tmpRoot.$index
    if [catch {exec sddsprocess $tmpRoot.all $tmpRoot.$index \
                 -match=column,ControlName=$pattern \
                 "-print=column,Description,$description"} result] {
        puts "$argv0: problem for pattern $pattern"
        continue
    }
    lappend list1 $tmpRoot.$index
    incr index
}


eval exec sddscombine $list1 -merge -pipe=out | sddssort -pipe=in ./../DataLogging/EPS.loc -col=ControlName -numeric -unique
eval file delete $list1 $tmpRoot.all

if {[catch {exec sddsprocess ./../DataLogging/EPS.loc -print=column,Provider,ca -print=column,ExpectNumeric,y,type=character -print=column,ExpectFieldType,scalar -define=column,ExpectElements,1,type=long -nowarn} results]} {
    puts "ERROR: $results"
    exit
}


sdds load ./../DataLogging/EPS.loc data
set data(ParameterNames) "InstallLocation"
set data(Parameter.InstallLocation) [list /home/helios/oagData/logonchange/EPS/EPS.loc]
set data(Layout.DataMode.Mode) "ascii"
sdds save ./../DataLogging/EPS.loc data

sdds load /home/helios/oagData/flcoord/stations.sdds data
foreach sector [lindex $data(Column.Sector) 0] \
  beamline [lindex $data(Column.Beamline) 0] \
  stationA [lindex $data(Column.StationA) 0] \
  stationB [lindex $data(Column.StationB) 0] \
  stationC [lindex $data(Column.StationC) 0] \
  stationD [lindex $data(Column.StationD) 0] \
  stationE [lindex $data(Column.StationE) 0] \
  stationF [lindex $data(Column.StationF) 0] \
  stationI [lindex $data(Column.StationI) 0] \
  stationX [lindex $data(Column.StationX) 0] {
      set numStations($sector$beamline) 0
      if {$stationA == "y"} {
          incr numStations($sector$beamline)
      }
      if {$stationB == "y"} {
          incr numStations($sector$beamline)
      }
      if {$stationC == "y"} {
          incr numStations($sector$beamline)
      }
      if {$stationD == "y"} {
          incr numStations($sector$beamline)
      }
      if {$stationE == "y"} {
          incr numStations($sector$beamline)
      }
      if {$stationF == "y"} {
          incr numStations($sector$beamline)
      }
      if {$stationI == "y"} {
          incr numStations($sector$beamline)
      }
      if {$stationX == "y"} {
          incr numStations($sector$beamline)
      }
}

set missingSectors ""
for {set i 1} {$i <= 40} {incr i} {
    foreach beamline {BM ID} {
        set rows [exec sddsprocess ./../DataLogging/EPS.loc -pipe=out "-match=column,ControlName=EPS:[format %02d $i]:${beamline}:*,ControlName=P?:[format %02d $i]${beamline}:*,|" -nowarn | sdds2stream -pipe=in -rows=bare]
        if {(![info exists numStations($i$beamline)]) && ($rows > 0)} {
            puts "There may be a problem with the /home/helios/oagData/flcoord/stations.sdds configuration file. PVs were found for sector [format %02d $i]${beamline}"
        } elseif {(![info exists numStations($i$beamline)])} {
            append missingSectors [format %02d $i]${beamline}
            
        } else {
            if {$rows < 9} {
                puts "There was a problem getting PVs from sector [format %02d $i]${beamline}"
            }
        }
    }
}

if {[llength $missingSectors]} {
    set result [exec cavget -list=PA: -list=[join ${missingSectors} ,] -list=:FE:PERM]
    foreach r $result s $missingSectors {
        if {$r != "?"} {
            puts "There was a problem getting PVs from sector $s"
        }
    }    
}

puts "use cppatch to install EPS.loc"



