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

# output files:
# PSS.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

exec sddsprocess /home/helios/oagData/pvdata/iocRecNamesOAG.sdds -pipe=out \
  "-match=column,rec_name=S????-PSS:FES:*M,rec_name=S????-PSS:Sta?:*M,|,rec_name=S????-PSS:*ExistsM,|,rec_name=S????-PSS:*StatusM,|,rec_name=S????-PSS:Flow??:*M,|,rec_name=S????-PSS:*blinkerM,|,rec_name=S????-PSS:SBS:*M,|,rec_name=S????-PSS:SCS:*M,|,rec_name=S????-PSS:SDS:*M,|,rec_name=S????-PSS:FlightPath:*M,|,rec_name=S????-PSS:Mirror:*M,|,rec_name=*PSS:Faults*,!,&,rec_name=*PSS:IOFaults*,!,&,rec_name=*PSS:Rack*,!,&,rec_name=*PSS:Warn*,!,&" -delete=column,ioc_name | \
  sddsconvert -pipe \
  -rename=column,rec_name=ControlName | \
  sddssort -pipe=in ../DataLogging/PSS.loc.new \
  -col=ControlName -num -uniq


sdds load ../DataLogging/PSS.loc.new data
unset data(Description.Text)
unset data(Description.Contents)
sdds save ../DataLogging/PSS.loc.new data

exec sddscainfo ../DataLogging/PSS.loc.new ../DataLogging/PSS.loc.new.info -pend=10
file delete ../DataLogging/PSS.loc.new

exec sddsprocess ../DataLogging/PSS.loc.new.info -pipe=out -match=column,Active=y -nowarn \
  "-print=parameter,InstallLocation,/home/helios/oagData/logging/B-BEFI/PSS.loc" | \
  sddsprocess -pipe -delete=column,Active,IOCHost,ReadAccess,WriteAccess,StructureID | \
  sddsconvert -pipe=in ../DataLogging/PSS.loc.new -ascii -rename=column,ElementCount=ExpectElements,FieldType=ExpectFieldType,Units=ReadbackUnits
file delete ../DataLogging/PSS.loc.new.info

sdds load ../DataLogging/PSS.loc.new data

set data(ColumnNames) "ControlName Provider ExpectNumeric ExpectFieldType ExpectElements"
set data(ColumnInfo.ExpectElements) "type SDDS_LONG"
set data(ColumnInfo.ExpectNumeric) "type SDDS_CHARACTER"
set en ""
foreach ndt [lindex $data(Column.NativeDataType) 0] \
  eft [lindex $data(Column.ExpectFieldType) 0] \
  cn [lindex $data(Column.ControlName) 0] \
  ee [lindex $data(Column.ExpectElements) 0] {
      if {($ndt == "double") || ($ndt == "int") || ($eft == "ENUM structure")} {
          lappend en y
      } elseif {$ndt == "string"} {
          puts "makeB-BEFILoggerInput: Cant log string PVs with the data logger: $cn"
#          lappend en n
          exit
      } else {
          puts "makeB-BEFILoggerInput: Unexepected native data type: $ndt"
          exit
      }
      if {$ee == 1} {
          lappend eftList scalar
      } else {
          puts "makeB-BEFILoggerInput: Unexepected element count: $ee $cn"
          exit
      }
  }
set data(Column.ExpectNumeric) [list $en]
set data(Column.ExpectFieldType) [list $eftList]


sdds save ../DataLogging/PSS.loc.gen4 data
file delete ../DataLogging/PSS.loc.new

exec sddsprocess ../DataLogging/PSS.loc.gen4  -nowarn \
  "-print=column,Description,"



set patternList \
  [list \
     P?:??BM:IACIS_GLBONLN \
     P?:??ID:IACIS_GLBONLN \
     P?:??BM:I?_ACIS_GLBONLN \
     P?:??ID:I?_ACIS_GLBONLN \
     P?:??BM:ACIS_GLOBAL_ONLINE \
     P?:??ID:ACIS_GLOBAL_ONLINE \
     P?:??BM:AGlobalOnline \
     P?:??ID:AGlobalOnline \
     PSS:??BM:FES_GLOBAL_ONLINE \
     PSS:??ID:FES_GLOBAL_ONLINE \
\
     P?:??BM:ACIS_FES_PERMIT \
     P?:??ID:ACIS_FES_PERMIT \
     PA:??BM:IACIS_FEPERM \
     PA:??ID:IACIS_FEPERM \
     P?:??BM:I?_ACIS_FE_PERMIT \
     P?:??ID:I?_ACIS_FE_PERMIT \
     P?:??BM:ACIS_FE_PERMIT \
     P?:??ID:ACIS_FE_PERMIT \
     P?:??BM:AAcisFePerm \
     P?:??ID:AAcisFePerm \
     PSS:??BM:FES_ACIS_PERM \
     PSS:??ID:FES_ACIS_PERM \
\
     P?:??BM:FEEPS_FES_PERMIT \
     P?:??ID:FEEPS_FES_PERMIT \
     PA:??BM:IFEEPS_FEPERM \
     PA:??ID:IFEEPS_FEPERM \
     P?:??BM:I?_FEEPS_FE_PERMIT \
     P?:??ID:I?_FEEPS_FE_PERMIT \
     P?:??BM:FEEPS_PERMIT \
     P?:??ID:FEEPS_PERMIT \
     P?:??BM:AFeEpsPerm \
     P?:??ID:AFeEpsPerm \
     PSS:??BM:FES_FEEPS_PERM \
     PSS:??ID:FES_FEEPS_PERM \
\
     PA:??BM:?_APS_KEY \
     PA:??ID:?_APS_KEY \
     PA:??BM:?_APS_STATUS \
     PA:??ID:?_APS_STATUS \
     P?:??BM:STA_?_APS_ENABLED \
     P?:??ID:STA_?_APS_ENABLED \
     P?:??BM:STA_?_APS_ENBLE_PL \
     P?:??ID:STA_?_APS_ENBLE_PL \
     P?:??BM:?ApsStatus \
     P?:??ID:?ApsStatus \
     PSS:??BM:?_APS_KS \
     PSS:??ID:?_APS_KS \
\
     P?:??BM:STA_?_APSDISBLE_PL \
     P?:??ID:STA_?_APSDISBLE_PL \
\
     P?:??BM:STA_?_USER_KEY_SW \
     P?:??ID:STA_?_USER_KEY_SW \
     PA:??BM:?_USER_KEY \
     PA:??ID:?_USER_KEY \
     P?:??BM:I?_STA_?_USER_KEY \
     P?:??ID:I?_STA_?_USER_KEY \
     P?:??BM:STA_?_USER_KEY \
     P?:??ID:STA_?_USER_KEY \
     P?:??BM:?UserKey \
     P?:??ID:?UserKey \
     PSS:??BM:?_USER_KS \
     PSS:??ID:?_USER_KS \
\
     P?:??BM:?_SEARCHED \
     P?:??ID:?_SEARCHED \
     P?:??BM:STA_?_SEARCHED \
     P?:??ID:STA_?_SEARCHED \
     P?:??BM:STA_?_SEARCHED_PL \
     P?:??ID:STA_?_SEARCHED_PL \
     P?:??BM:?SandSecure \
     P?:??ID:?SandSecure \
     PSS:??BM:?_SEARCHED_Z1 \
     PSS:??ID:?_SEARCHED_Z1 \
\
     P?:??BM:FES_PERMIT \
     P?:??ID:FES_PERMIT \
     P?:??BM:S?_PERMIT \
     P?:??ID:S?S_PERMIT \
     P?:??BM:?_BEAM_READY \
     P?:??ID:?_BEAM_READY \
     P?:??BM:STA_?_BEAM_READY \
     P?:??ID:STA_?_BEAM_READY \
     P?:??BM:STA_?_BEAMREADY_PL \
     P?:??ID:STA_?_BEAMREADY_PL \
     P?:??BM:?BeamReady \
     P?:??ID:?BeamReady \
     PSS:??BM:FES_PERMIT \
     PSS:??ID:FES_PERMIT \
\
     P?:??BM:?_BEAM_ACTIVE \
     P?:??ID:?_BEAM_ACTIVE \
     P?:??BM:STA_?_BEAM_ACTIVE \
     P?:??ID:STA_?_BEAM_ACTIVE \
     P?:??BM:?BeamActive \
     P?:??ID:?BeamActive \
     P?:??BM:STA_?_NO_ACCESS \
     P?:??ID:STA_?_NO_ACCESS \
\
     P?:??BM:S?S_BLOCKING_BEAM \
     P?:??ID:S?S_BLOCKING_BEAM \
     PSS:??BM:S?S_BLOCKING_BEAM \
     PSS:??ID:S?S_BLOCKING_BEAM \
\
     P?:??BM:?_BEAM_PRESENT \
     P?:??ID:?_BEAM_PRESENT \
     PSS:??BM:?_BEAM_PRESENT \
     PSS:??ID:?_BEAM_PRESENT \
\
     P?:??BM:?_SHTRS_CLOSED \
     P?:??ID:?_SHTRS_CLOSED \
     P?:??BM:I?_FES_PS2_CLOS_LS \
     P?:??ID:I?_FES_PS2_CLOS_LS \
     P?:??BM:STA_?_SHUTTER_CLOSED \
     P?:??ID:STA_?_SHUTTER_CLOSED \
     P?:??BM:STA_?_S?S_CLSD_PL \
     P?:??ID:STA_?_S?S_CLSD_PL \
     P?:??BM:STA_?_FES_CLSD_PL \
     P?:??ID:STA_?_FES_CLSD_PL \
     P?:??BM:I?_A_SH1_MS1_CLOS_LS \
     P?:??ID:I?_A_SH1_MS1_CLOS_LS \
     P?:??BM:?BlEpsStatus \
     P?:??ID:?BlEpsStatus \
     P?:??BM:AFEStatus \
     P?:??ID:AFEStatus \
\
     P?:??BM:IBLEPS_PERM? \
     P?:??ID:IBLEPS_PERM? \
     PSS:??BM:S?S_BLEPS_PERM \
     PSS:??ID:S?S_BLEPS_PERM \
\
     P?:??BM:OBLEPS_STAT? \
     P?:??ID:OBLEPS_STAT? \
\
     P?:??BM:NumModeShtrs \
     P?:??ID:NumModeShtrs \
\
     P?:??BM:OPSS_MODE1 \
     P?:??ID:OPSS_MODE1 \
     P?:??BM:Rem_?shtrOpen \
     P?:??ID:Rem_?shtrOpen \
     P?:??BM:Rem_?shtrClose \
     P?:??ID:Rem_?shtrClose \
     PA:??BM:FLT:1 \
     PA:??ID:FLT:1 \
     PC:??ID:pfbAMin016 \
     PC:??ID:pfbASer007 \
     PC:??ID:pfbAMaj005 \
     PA:??BM:MINOR_FAULT_01 \
     PA:??BM:MAJOR_FAULT_01 \
     PB:??BM:MINOR_FAULT_01 \
     PB:??BM:MAJOR_FAULT_01 \
     PC:??BM:WARN_FAULT_01 \
     PC:??BM:MINOR_FAULT_01 \
     PA:??ID:MINOR_FAULT_01 \
     PA:??ID:MAJOR_FAULT_01 \
     PB:??ID:MINOR_FAULT_01 \
     PB:??ID:MAJOR_FAULT_01 \
     PC:??ID:WARN_FAULT_01 \
     PC:??ID:MINOR_FAULT_01 \
     S:ActualMode \
     S:ActualModeCalc \
     S:DesiredMode \
     PA:??BM:STA_A_FES_OPEN_PL \
     PA:??ID:STA_A_FES_OPEN_PL \
     PB:??BM:STA_A_FES_CLSD_PL \
     PB:??ID:STA_A_FES_CLSD_PL \




    ]

set descriptionList \
  [list \
     "ACIS Global OnLine Permit" \
     "ACIS Global OnLine Permit" \
     "ACIS Global OnLine Permit" \
     "ACIS Global OnLine Permit" \
     "ACIS Global OnLine Permit" \
     "ACIS Global OnLine Permit" \
     "ACIS Global OnLine Permit" \
     "ACIS Global OnLine Permit" \
     "ACIS Global OnLine Permit" \
     "ACIS Global OnLine Permit" \
     "ACIS FE Shutter Permit" \
     "ACIS FE Shutter Permit" \
     "ACIS FE Shutter Permit" \
     "ACIS FE Shutter Permit" \
     "ACIS FE Shutter Permit" \
     "ACIS FE Shutter Permit" \
     "ACIS FE Shutter Permit" \
     "ACIS FE Shutter Permit" \
     "ACIS FE Shutter Permit" \
     "ACIS FE Shutter Permit" \
     "ACIS FE Shutter Permit" \
     "ACIS FE Shutter Permit" \
     "FE-EPS FE Shutter Permit" \
     "FE-EPS FE Shutter Permit" \
     "FE-EPS FE Shutter Permit" \
     "FE-EPS FE Shutter Permit" \
     "FE-EPS FE Shutter Permit" \
     "FE-EPS FE Shutter Permit" \
     "FE-EPS FE Shutter Permit" \
     "FE-EPS FE Shutter Permit" \
     "FE-EPS FE Shutter Permit" \
     "FE-EPS FE Shutter Permit" \
     "FE-EPS FE Shutter Permit" \
     "FE-EPS FE Shutter Permit" \
     "APS Enabled" \
     "APS Enabled" \
     "APS Enabled" \
     "APS Enabled" \
     "APS Enabled" \
     "APS Enabled" \
     "APS Enabled" \
     "APS Enabled" \
     "APS Enabled" \
     "APS Enabled" \
     "APS Enabled" \
     "APS Enabled" \
     "APS Disabled" \
     "APS Disabled" \
     "User Key" \
     "User Key" \
     "User Key" \
     "User Key" \
     "User Key" \
     "User Key" \
     "User Key" \
     "User Key" \
     "User Key" \
     "User Key" \
     "User Key" \
     "User Key" \
     "Searched Status" \
     "Searched Status" \
     "Searched Status" \
     "Searched Status" \
     "Searched Status" \
     "Searched Status" \
     "Searched Status" \
     "Searched Status" \
     "Searched Status" \
     "Searched Status" \
     "Beam Ready" \
     "Beam Ready" \
     "Beam Ready" \
     "Beam Ready" \
     "Beam Ready" \
     "Beam Ready" \
     "Beam Ready" \
     "Beam Ready" \
     "Beam Ready" \
     "Beam Ready" \
     "Beam Ready" \
     "Beam Ready" \
     "Beam Ready" \
     "Beam Ready" \
     "Beam Active" \
     "Beam Active" \
     "Beam Active" \
     "Beam Active" \
     "Beam Active" \
     "Beam Active" \
     "Beam Active" \
     "Beam Active" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Shutters Closed" \
     "Beamline EPS Permit" \
     "Beamline EPS Permit" \
     "Beamline EPS Permit" \
     "Beamline EPS Permit" \
     "Beamline EPS Status" \
     "Beamline EPS Status" \
     "Mode Shutter Present" \
     "Mode Shutter Present" \
     "Current Mode" \
     "Current Mode" \
     "Shutter Open" \
     "Shutter Open" \
     "Shutter Close" \
     "Shutter Close" \
     "Fault" \
     "Fault" \
     "Minor Fault" \
     "Serious Fault" \
     "Major Fault" \
     "Minor Fault" \
     "Major Fault" \
     "Minor Fault" \
     "Major Fault" \
     "Warn Fault" \
     "Minor Fault" \
     "Minor Fault" \
     "Major Fault" \
     "Minor Fault" \
     "Major Fault" \
     "Warn Fault" \
     "Minor Fault" \
     "Actual Beam Mode" \
     "Actual Beam Mode" \
     "Desired Beam Mode" \
     "Sta A FES/A Opened Green LED" \
     "Sta A FES/A Opened Green LED" \
     "Sta A FES/A Closed Red LED" \
     "Sta A FES/A Closed Red LED" \
    ]

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" -nowarn} result] {
        return -code error "$argv0: problem for pattern $pattern : $result"
    }
    lappend list1 $tmpRoot.$index
    incr index
}


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

if {[catch {exec sddsprocess ./../DataLogging/PSS.loc.gen1 -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
}

exec sddscombine ./../DataLogging/PSS.loc.gen1 ./../DataLogging/PSS.loc.gen4 ./../DataLogging/PSS.loc -merge -overwrite

file delete ./../DataLogging/PSS.loc.gen1 ./../DataLogging/PSS.loc.gen4

sdds load ./../DataLogging/PSS.loc data
set data(ParameterNames) "InstallLocation"
set data(Parameter.InstallLocation) [list /home/helios/oagData/logonchange/PSS/PSS.loc]
set data(Layout.DataMode.Mode) "ascii"
sdds save ./../DataLogging/PSS.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/PSS.loc -pipe=out "-match=column,ControlName=P?:[format %02d $i]${beamline}:*" -nowarn | sdds2stream -pipe=in -rows=bare]
        if {(![info exists numStations($i$beamline)]) && ($rows > 0)} {
            puts "There is a problem with the /home/helios/oagData/flcoord/stations.sdds configuration file. There are stations for sector [format %02d $i]${beamline}"
        } elseif {(![info exists numStations($i$beamline)])} {
            append missingSectors [format %02d $i]${beamline}
            
        } else {
            #puts "[format %02d $i]${beamline} $rows"
        }
    }
}

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


puts "use cppatch to install PSS.loc"


