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

if {![info exists env(OAG_TOP_DIR)] || [string length $env(OAG_TOP_DIR)]==0} {
    set env(OAG_TOP_DIR) /usr/local
}
set auto_path [linsert $auto_path 0  $env(OAG_TOP_DIR)/oag/apps/lib/$env(HOST_ARCH)]

APSStandardSetup

# Per emails from J. Xu, 3/31/2023
set excludeList [list ID01us:USTLinearEncoder.VAL \
    ID11ds:DSTLinearEncoder.VAL \
    ID15ds:USRotaryEncoder.VAL \
    ID19ds:USRotaryEncoder.VAL ID19ds:DSRotaryEncoder.VAL \
    ID34ds:USBLinearEncoder.VAL ID34ds:USTLinearEncoder.VAL \
    ID34ds:DSBLinearEncoder.VAL ID34ds:DSTLinearEncoder.VAL ]

for {set sector 1} {$sector<=35} {incr sector} {
    foreach region {us ds} {
        # This loop covers four-motor devices and two-motor devices. We'll use CA to determine which
        # actually exist.
        foreach axis {DSB USB DST UST US DS} {
            foreach encoder {Linear Rotary} {
                set mainPV [format ID%02d${region}:${axis}${encoder}Encoder.VAL $sector]
                if {[lsearch -exact $excludeList $mainPV] == -1} {
                    lappend mainList $mainPV
                    lappend refList  [format ID%02d${region}:${axis}Motor.RBV $sector]
                    lappend maskList [format ID%02d${region}:Busy $sector]
                }
            }
        }
    }
}

for {set sector 1} {$sector<=35} {incr sector} {
    foreach type {Gap TaperGap} {
        # Not all these PVs will exist. We'll use CA to handle that below.
        lappend mainList [format ID%02dus:%sSet.VAL $sector $type]
        lappend mainList [format ID%02dds:%sSet.VAL $sector $type]

        lappend refList [format ID%02dus:%s.VAL $sector $type]
        lappend refList [format ID%02dds:%s.VAL $sector $type]

        lappend maskList [format ID%02dus:Busy $sector]
        lappend maskList [format ID%02dds:Busy $sector]
    }
}


exec sddsmakedataset -pipe=out \
  -column=ControlName,type=string -data=[join $mainList ,] \
  -column=ReferenceControlName,type=string -data=[join $refList ,] \
  -column=MaskControlName,type=string -data=[join $maskList ,] \
  | sddsprocess -pipe \
  -print=parameter,AlarmStartCmd, -print=parameter,AlarmStopCmd, \
  "-print=parameter,LabelForProcess,ID Gap Delta Monitor" \
  -define=parameter,UpdateInterval,2,type=long -define=parameter,AppendErrorPVs,0,type=long \
  -define=col,MinLimit,-1.0 -define=col,MaxLimit,1.0 -define=col,Tolerance,0 -define=col,MaxReading,1e10 \
  -define=col,CountRequired,10,type=long -define=col,CountTimeFrame,300,type=long,units=s \
  -define=col,MaskIfNotEqualTo,0,type=long \
  | sddscasr -pipe -save -pendIoTime=10 \
  | sddsprocess -pipe -match=col,CAError=n \
  -print=parameter,InstallLocation,/home/helios/oagData/PVMonitorFiles/sr/IDWatchdog.config \
  | sddsconvert -pipe=in IDWatchdog.config -retain=col,Sector,*ControlName,*Limit,Tol*,MaxR*,Count*,Mask*,Region \
  -retain=param,AlarmStartCmd,AlarmStopCmd,LabelForProcess,UpdateInterval,AppendErrorPVs,InstallLocation

puts stdout "Use cppatch to install IDWatchdog.config after checking."
