#!../../bin/linuxRT-x86_64/evrClientLab

## You may have to change evrLab to something else
## everywhere it appears in this file

< envPaths

# ====================================================================
# Setup some additional environment variables
# ====================================================================
# Setup environment variables 

# tag log messages with IOC name
# How to escape the "vioc-b84-ev21" as the PERL program
# will try to repplace it.
# So, uncomment the following and remove the backslash
epicsEnvSet("EPICS_IOC_LOG_CLIENT_INET","${IOC}")

epicsEnvSet(TOP,"${IOC_APP}")

# ============================================
# Set MACROS for EVRs
# ============================================
# FAC = SYS0 ==> LCLS1
# FAC = SYS1 ==> FACET

# System Location:
epicsEnvSet("LOCA","B84")

epicsEnvSet(IOC_NAME,"EVR:B84:EV21")
epicsEnvSet(UNIT,"EV21")
epicsEnvSet(FAC,"SYS0")

# ========================================================
# Support Large Arrays/Waveforms; Number in Bytes
# Please calculate the size of the largest waveform
# that you support in your IOC.  Do not just copy numbers
# from other apps.  This will only lead to an exhaustion
# of resources and problems with your IOC.
# The default maximum size for a channel access array is
# 16K bytes.
# ========================================================
epicsEnvSet("EPICS_CA_MAX_ARRAY_BYTES", "800000")

# END: Additional environment variables
# ====================================================================

cd ${TOP}
# ====================================================
## Register all support components
dbLoadDatabase("dbd/evrClientLab.dbd",0,0)
evrClientLab_registerRecordDeviceDriver(pdbbase)
# ====================================================

###########################################################
# Initialize all hardware first                           #
###########################################################
# ======================================================
# Init PMC EVR: To support Timing System Event Receiver
# ======================================================
# In this case this EVR is running in a PC under linux

#system("/bin/su root -c $IOC_BOOT/suSetupVevrs.cmd")

### wait the system kernel comes up
#epicsThreadSleep(2)

# =====================================================================
# Setup for EVR:
# =====================================================================
# Add eevrmaConfigure for each VEVR before iocInit.
#
#    eevrmaConfigure <instance> <vevrDevName>
#
#    where instance = EVR instance, starting from 0, incrementing by 1
#                     for each subsequent card.  Only 1 EVR instance
#                    is allowed for Embedded EVRs.
#
# ======================================================================
# Debug interest level for EVR Driver
# ErDebug=100

# Here we have an VEVR
eevrmaConfigure(0, "/dev/vevr21")

# Add evrInitialize (after ErConfigure) if a fiducial routine will be
# registered before iocInit driver initialization:
#evrInitialize()
# ======= EVR Setup Complete ============================================



########################################################################
# BEGIN: Load the record databases
#######################################################################
# =====================================================================
# Load EVENT databases to support use of the timing system
# =====================================================================

dbLoadRecords("db/Pattern.db","IOC=${IOC_NAME},SYS="${FAC})

# PMC EVR230
dbLoadRecords("db/EvrPmc.db","EVR=${IOC_NAME},CRD=0,SYS=${FAC}")
dbLoadRecords("db/PMC-trig.db","IOC=${IOC_NAME},LOCA=${LOCA},UNIT=${UNIT},SYS=${FAC}")

# BSA Data Source Simulator Databases
#dbLoadRecords("db/bsaSimulator.db","DEVICE=LEPTON")

# BSA Database for each data source from above
#1GdbLoadRecords("db/Bsa.db","DEVICE=LEPTON, ATRB=COUNTER")
#dbLoadRecords("db/Bsa.db","DEVICE=LEPTON, ATRB=SINE")

# ==============================================================================

# =====================================================================
# Load iocAdmin databases to support IOC Health and monitoring
# =====================================================================
dbLoadRecords("db/iocAdminSoft.db","IOC=${IOC_NAME}")
dbLoadRecords("db/iocAdminScanMon.db","IOC=${IOC_NAME}")

# The following database is a result of a python parser
# which looks at RELEASE_SITE and RELEASE to discover
# versions of software your IOC is referencing
# The python parser is part of iocAdmin
dbLoadRecords("db/iocRelease.db","IOC=${IOC_NAME}")

# =====================================================================
# Load database for autosave status
# =====================================================================
dbLoadRecords("db/save_restoreStatus.db", "P=${IOC_NAME}:")

# =====================================================================
#Load Additional databases:
# =====================================================================
## Load record instances
#dbLoadRecords("db/dbExample.db","user=ernesto,no=1,scan=1 second")

# END: Loading the record databases
########################################################################

# =====================================================================
## Begin: Setup autosave/restore
# ====================================================================                                        
# ============================================================
# If all PVs don't connect continue anyway
# ============================================================
save_restoreSet_IncompleteSetsOk(1)

# ============================================================
# created save/restore backup files with date string
# useful for recovery.
# ============================================================
#save_restoreSet_DatedBackupFiles(1)
save_restoreSet_DatedBackupFiles(0)

# ============================================================
# Where to find the list of PVs to save
# ============================================================
set_requestfile_path("/data/${IOC}/autosave-req")

# ============================================================
# Where to write the save files that will be used to restore
# ============================================================
set_savefile_path("/data/${IOC}/autosave")

# ============================================================
# Prefix that is use to update save/restore status database
# records
# ============================================================
save_restoreSet_UseStatusPVs(1)
save_restoreSet_status_prefix("${IOC_NAME}:")

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

# =====================================================================
# End: Setup autosave/restore
# =====================================================================

# =====================================================================
# Channel Access Security:
# This is required if you use caPutLog.
# Set access security filea
# Load common LCLS Access Configuration File
< ${ACF_INIT}



# =============================================================
# Start EPICS IOC Process (i.e. all threads will start running)
# =============================================================

## Run this to trace the stages of iocInit
#traceIocInit()

iocInit()

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

## Start any sequence programs
#seq sncExample,"user=gwbrownHost"

## ===========================================================
## Start autosave routines to save our data
## ===========================================================
# optional, needed if the IOC takes a very long time to boot.
#epicsThreadSleep( 1.0)

cd("/data/${IOC}/autosave-req")
iocshCmd("makeAutosaveFiles")

# Start the save_restore task
# save changes on change, but no faster
# than every 5 seconds.
# Note: the last arg cannot be set to 0
create_monitor_set("info_positions.req", 5 )
create_monitor_set("info_settings.req" , 30 )

#cd ${TOP}/iocBoot/${IOC}
cd /afs/slac/g/lcls/epics/iocTop/evrClientLab/MAIN_TRUNK/iocBoot/vioc-b84-ev21

#cd ${EPICS_IOC_APP}
# Setup Real-time priorities after iocInit for 
# driver threads
system("/bin/su root -c `pwd`/rtPrioritySetup.cmd")

# An example of using the CEXP Shell:
#cexpsh("-c",'printf("hello\n")')
