#!/bin/bash
#==============================================================
# luSectorToEpics
#
# Facility:  SLAC/LCLS
#
# Performs most of the functions associated with switching
# a Linac sector's CAMAC from SCP/Micro control to EPICS/IOC control
#
# Usage: luSectorToEpics <location>
#
# Arguments: 
#            location: ioc location name, for example li28
# 
#----------------------------------------------------------------------------
# Mod:
#        
#==============================================================

loca=`echo $1 | tr '[:upper:]' '[:lower:]'`

LOCA=`echo $1 | tr '[:lower:]' '[:upper:]'`

# Start message

caput IOC:${LOCA}:CV01:QC_MSG "Begin ${LOCA} switch to EPICS control..."

echo  Setting ${LOCA} to EPICS control...
echo

# Set PSCD/MBCD PV

caput IOC:${LOCA}:CV01:QC_MSG "Set CAMAC control PV to EPICS mode..."

echo  Setting PSCD Mode...

upgradeSetCamacControl ${LOCA} 0

# Update PV gateway cofiguration and restart gateway

caput IOC:${LOCA}:CV01:QC_MSG "Restarting PV gateway..."

caput SIOC:${LOCA}:1:CAMAC_CTRL 1

ssh laci@mcclogin ~mccuser/runluBuildGWConfig

$EPICS_IOC_TOP/QCT/scripts/gateway/restartGW.py

# Rebooting CAMAC IOC with standard Channel Access configuration...

caput IOC:${LOCA}:CV01:QC_MSG "Rebooting IOC-${LOCA}-CV01..."

luCamacIocStandardPorts ${LOCA}

# Restarting Klystron Support soft ioc with EPICS databases...

caput IOC:${LOCA}:CV01:QC_MSG "Rebooting SIOC-${LOCA}-KY00..."

luKlystronSupportEpicsMode ${LOCA}

kdata=`caget -t IOC:${LOCA}:CV01:KLYDATA`
tdata=`caget -t IOC:${LOCA}:CV01:TIMDATA`

if [ ${kdata} = "SLC" ]; then 

    caput IOC:${LOCA}:CV01:QC_MSG "Pause 30 seconds (SIOC rebooting)..."

    echo Pausing for 30 seconds...

    sleep 30
    
    # Set soft ioc Klystron PV Initial values...

    caput IOC:${LOCA}:CV01:QC_MSG "Copying SLC klystron data to SIOC..."

    luKlystronSupportSetInitialConditions ${LOCA}

    caput IOC:${LOCA}:CV01:QC_MSG "Pause 20 seconds (IOC rebooting)..."

    echo Pausing for 20 seconds...

    sleep 20

    # Set RTEMS ioc Klystron PV Initial values...

    caput IOC:${LOCA}:CV01:QC_MSG "Copying SLC klystron data to IOC..."

    luKlystronSupportSetInitialConditionsIOC ${LOCA}

fi

# Set AIDA Klystron service to EPICS

caput IOC:${LOCA}:CV01:QC_MSG "Set ${LOCA} AIDA Klys Service to EPICS..."

echo  Switching ${LOCA} AIDA KLystron Service to EPICS...

aidaqc_slctoepics -a ${LOCA}

# Reconfigure and restart CAMAC CW process

caput IOC:${LOCA}:CV01:QC_MSG "Restarting CAMAC CW process..."

echo  Reconfiguring CAMAC Channel Watcher process...

luCWRestart ${LOCA} 1

if [ ${tdata} = "SLC" ]; then 

    if [ ${kdata} = "EPICS" ]; then
	caput IOC:${LOCA}:CV01:QC_MSG "Pause 90 seconds (IOC rebooting)..."
	sleep 90
    fi

    if [ "${LOCA}" = "LI20" ]; then 
	configfile='tim02-24.cnf'
    else
	configfile='tim21-30.cnf'
    fi	

    echo ${configfile}

    caput IOC:${LOCA}:CV01:QC_MSG "Copy SLC timing data (takes 3 min)..."

    luViewTimingData ${LOCA} ${configfile}

else
    caput IOC:${LOCA}:CV01:QC_MSG "Pause 110 seconds (IOC rebooting)..."
    sleep 110
fi


# Restarting matlab klystron CUD process

caput IOC:${LOCA}:CV01:QC_MSG "Restarting Matlab Klys CUD process..."

ssh physics@lcls-srv03 Matlab_stop klystronCud

sleep 10

ssh physics@lcls-srv03 Matlab_startup klystronCud &

sleep 5

# Restart MDL loop

caput IOC:${LOCA}:CV01:QC_MSG "Restarting MDL loop (takes 5 min)..."

echo  Restarting MDL loop, takes 5 minutes...

caput FBCK:MDL:1:STATUSDESC "Restarting MDL loop"

caput FBCK:MDL0:1:STATE 0

# Leave message up for 30 seconds so people see it but don't wait for restart to complete 
# before returning to switchover script. This is because people don't have to wait for the
# MDL loop restart to use the sector.

sleep 30 

luMDLRestart &

# Enable IOC alarms

echo Enabling IOC alarms...

caput IOC:${LOCA}:CV01:TODSUMFP 0
caput IOC:${LOCA}:CV01:HEARTBEATSUMFP 0

# End message

caput IOC:${LOCA}:CV01:QC_MSG "${LOCA} switch to EPICS mode complete"

echo
echo

echo Switchover script complete

exit 0
