#!../../bin/rhel6-x86_64/OperationsSupport

# Source envPaths
< envPaths
< ${EPICS_IOCS}/facility/st.cmd.soft.photon

# set appropriate EPICS_CA_MAX_ARRAY_BYTES
epicsEnvSet("EPICS_CA_MAX_ARRAY_BYTES","300000")

# Change directory to TOP of application
cd(${TOP})

## Register all support components
dbLoadDatabase("dbd/OperationsSupport.dbd")
OperationsSupport_registerRecordDeviceDriver(pdbbase)

# Make loadEnergyJitterDBs.cmd
cd(${TOP}/iocBoot/${IOC})
system("./makeEnergyJitterDBs.sh")
cd(${TOP})

## Load sioc-sys0-op01 database
dbLoadRecords("db/PhysicsAppSupport.db")

## Load specific databases
dbLoadRecords("db/ModeLogicPPS.db")
dbLoadRecords("db/KlysSupportLCLS.db","IOC=SIOC:SYS0:OP00")
dbLoadRecords("db/KlysInstance.db")
dbLoadRecords("db/UndulatorCUD.vdb")
dbLoadRecords("db/ScoreSupport.db","SYS=SYS0")
dbLoadRecords("db/cmLogSupport.vdb","IOC=SIOC:SYS0:OP00")
dbLoadRecords("db/AnomalyDetection.vdb")
dbLoadRecords("db/CameraWatcher.vdb","dev=OTRS:DMP1:695")
< iocBoot/${IOC}/loadEnergyJitterDBs.cmd
dbLoadRecords("db/Zernike.vdb")
dbLoadRecords("db/SpatialModulation.vdb")
dbLoadRecords("db/CookieBox.vdb")
dbLoadRecords("db/XrayPulseIntensity.db")
dbLoadRecords("db/Und1AlineJitter.db")
dbLoadRecords("db/und1PointServer.db")
#dbLoadRecords("db/ModelSupport.db")
# Uncomment for testing on DEV
#dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=1")
#dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=2")
#dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=3")

dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=10")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=11")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=12")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=13")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=14")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=15")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=16")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=17")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=20")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=21")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=22")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=23")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=24")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=30")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=31")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=32")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=33")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=34")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=40")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=41")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=42")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=43")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=44")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=45")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=46")
dbLoadRecords("db/MatlabPrelaunchSupport.db","AREA=ACR0,OPI_NUM=47")

## Load common databases

## Setup autosave/restore
set_requestfile_path("${IOC_DATA}/${IOC}/autosave-req")
set_savefile_path("${IOC_DATA}/${IOC}/autosave")
save_restoreSet_status_prefix("SIOC:SYS0:OP00:")
dbLoadRecords("db/save_restoreStatus.db", "P=SIOC:SYS0:OP00:")

## iocAdmin
epicsEnvSet("LOCATION","lcls-daemon1")
epicsEnvSet("STARTUP","/usr/local/lcls/epics/iocCommon/${IOC}")
epicsEnvSet("ST_CMD","startup.cmd")
dbLoadRecords("db/iocAdminSoft.db","IOC=SIOC:SYS0:OP00")
dbLoadRecords("db/iocRelease.db"  ,"IOC=SIOC:SYS0:OP00")

## Restore datasets
set_pass0_restoreFile("info_positions.sav")
set_pass1_restoreFile("info_settings.sav")

## Note that autosave/restore spews a lot of messages and message
## throttling throttles all of the seq messages below.

iocInit()

## Start logging messages before starting sequences.
iocLogInit()


## Start any sequence programs
#seq CameraWatcher,"sys=SYS0,dev=OTRS:DMP1:695,script=rebootOTRDMP.sh,param=,name=OTRS:DMP1:695:CameraWatcher"
seq CookieBox,"name=CookieBox"

## Uncomment for testing on DEV
#seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=1,IOC=SIOC:SYS0:OP00"
#seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=2,IOC=SIOC:SYS0:OP00"
#seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=3,IOC=SIOC:SYS0:OP00"

seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=10,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=11,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=12,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=13,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=14,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=15,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=16,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=17,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=20,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=21,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=22,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=23,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=24,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=30,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=31,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=32,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=33,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=34,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=40,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=41,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=42,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=43,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=44,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=45,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=46,IOC=SIOC:SYS0:OP00"
seq MatlabPrelaunchReserve,"AREA=ACR0,OPI_NUM=47,IOC=SIOC:SYS0:OP00"

## Start saving datasets
cd("${IOC_DATA}/${IOC}/autosave-req")
makeAutosaveFiles()
create_monitor_set("info_positions.req",60,"")
create_monitor_set("info_settings.req",60,"")

