#!../../bin/_ARCH_/_APPNAME_

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

< envPaths

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

epicsEnvSet("ENGINEER","Curly Johnson")
epicsEnvSet("LOCATION","Twilight Zone")

# tag log messages with IOC name
# How to escape the "_IOC_" 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,"../..")

epicsEnvSet(PATH,"/afs/slac/g/lcls/epics/base/base-R3-14-12/bin/linuxRT-x86")

# ========================================================
# 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/_APPNAME_.dbd",0,0)
_APPNAME__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

# =====================================================================
# Setup for EVR:
# =====================================================================
# Add ErConfigure for each EVR before iocInit.
#
#    VME:      ErConfigure(<instance>,<address>,<vector>,<level>,0)
#    PMC:      ErConfigure(<instance>,    0    ,    0   ,   0   ,1)
#    Embedded: ErConfigure(<instance>,    0    ,<vector>,<level>,2)
#
#    where instance = EVR instance, starting from 0, incrementing by 1
#                     for each subsequent card.  Only 1 EVR instance
#                    is allowed for Embedded EVRs.
#    and   address  = VME card address, starting from 0x300000,
#                     incrementing by 0x100000 for each subsequent card
#                     (0 for PMC and Embedded)
#    and   vector   = VME or Embedded interrupt vector.
#                    For VME, start from 0x60 and increment by 0x02 for
#                    each subsequent card.
#                     (0 for PMC)
#    and   level    = VME or Embedded interrupt level.
#                    For VME, set to 4.  Can be the same for all EVRs.
#                     (0 for PMC)
#    and   0        = VME
#       or 1        = PMC
#       or 2       = Embedded
#
# ======================================================================
# Debug interest level for EVR Driver
# ErDebug=100

ErConfigure(0, 0, 0, 0, 1)

# 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 iocAdmin databases to support IOC Health and monitoring
# =====================================================================
dbLoadRecords("db/iocAdminSoft.db","IOC=${IOCNAME}")
dbLoadRecords("db/iocAdminScanMon.db","IOC=${IOCNAME}")

# 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=${IOCNAME}")

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

# ========================================================
# Load EVR Databases for the Timing system
# ========================================================
dbLoadRecords("db/${IOCNAME}-pattern.db")
dbLoadRecords("db/${IOCNAME}-evr.db")
dbLoadRecords("db/${IOCNAME}-trig.db")
dbLoadRecords("db/${IOCNAME}-bsa.db")
# ========================================================

# =====================================================================
#Load Additional databases:
# =====================================================================
## Load record instances
dbLoadRecords("db/dbExample.db","user=_USER_Host,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(0)

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

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

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

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

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

# =============================================================
# Start EPICS IOC Process (i.e. all threads will start running)
# =============================================================
cd ${TOP}/iocBoot/${IOC}
iocInit()

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

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

## Start autosave process:
create_monitor_set("info_settings.req",60,"")

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