#!../../bin/rhel7-x86_64/tpgSupport

# =====================================================
# Source envPaths
# =====================================================
< envPaths

# =====================================================
# Source Channel Access Security
# =====================================================
< ${ACF_INIT}


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

# =====================================================
## Register all support components
# =====================================================
dbLoadDatabase("dbd/tpgSupport.dbd")
tpgSupport_registerRecordDeviceDriver(pdbbase)

# =====================================================
## Load pvs for bsaReady/bsaControl
# =====================================================
dbLoadRecords("db/bsaReady.db","SYS=SYS0,UNIT=1")

# =====================================================
## Load seqStats
# =====================================================
#dbLoadRecords("db/devSeqCar.db","SIOC=SIOC:SYS0:TS06")

# =====================================================
# =====================================================
## Setup autosave/restore
# =====================================================
set_requestfile_path("${IOC_DATA}/${IOC}/autosave-req")
set_savefile_path("${IOC_DATA}/${IOC}/autosave")

save_restoreSet_status_prefix("SIOC:SYS0:TS06:")
dbLoadRecords("db/save_restoreStatus.db", "P=SIOC:SYS0:TS06:")

# =====================================================
# =====================================================
## 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:TS06")
dbLoadRecords("db/iocRelease.db"  , "IOC=SIOC:SYS0:TS06")

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

#////////////////////////////////////////#
#Run script to generate archiver files   #
#////////////////////////////////////////#
cd(${TOP}/tpgSupportApp/scripts/)
system("./makeArchive.sh ${IOC} &")
cd(${TOP})

# =====================================================
## Initialize the IOC
# =====================================================
iocInit()

# =====================================================
## Start logging messages before starting sequences.
# =====================================================
iocLogInit()
iocLogPrefix("fac=TPG proc=${IOC} ")

# =====================================================
# Turn on caPutLogging:
# Log values only on change to the iocLogServer:
# =====================================================
caPutLogInit("${EPICS_CA_PUT_LOG_ADDR}")
caPutLogShow(2)

# =====================================================
## Start any sequence programs
# =====================================================
< iocBoot/${IOC}/bsaControl.cmd

# =====================================================
## Start saving datasets
# =====================================================
cd("${IOC_DATA}/${IOC}/autosave-req")
makeAutosaveFiles()
create_monitor_set("info_positions.req",60,"")
create_monitor_set("info_settings.req",60,"")
system("cp ${TOP}/archive/${IOC}.archive ${IOC_DATA}/${IOC}/archive")
dbpf TPG:SYS0:1:CONTINUOUS_LOAD.ASG TIMING
dbpf TPG:SYS0:1:BURST_LOAD.ASG TIMING
dbpf TPG:SYS0:1:APPLY.ASG TIMING

