#!/bin/sh
#==============================================================
# st.lclsioc
#
# ARGUMENTS: 
#	$1 = IOC name in $IOC/iocBoot/screeniocs
# OPTION:     
#
# Facility:  SLAC/LCLS
#
# Rem:   Type II script to start a soft IOC at bootup:
#        - start up soft ioc executable
#        - create the detached screen session to the soft ioc 
#        - start up siocStart.pl (the soft ioc monitoring) 
#          st.lclsioc will be called from Type I startup at booting time.
#          
# Auth:  13-Feb-2007, Jingchen Zhou   (JINGCHEN)
# Rev:   dd-mmm-yyyy, Reviewer's Name (USERNAME)
#        11-Nov-2007, Jingchen Zhou   (JINGCHEN)
#        Updated to support the standalone production environment 
#        04-Apr-2009, Jingchen Zhou
#        Updated to support AFS based development environment           
#        Changed `hostname -a` to `hostname` to keep dev and prod in sync
#        29-Mar-2011, Jingchen Zhou
#        Commented out hostname check 
#        09-Apr-2012, Judy Rock
#        added screenlog.0 copy 
#--------------------------------------------------------------
IOCNAME=$1

if [ $# -ne 1 ]; then
    echo "Usage: $0 IOCNAME"
    echo "Example: $0 UW00"  
    exit 1
fi

#if [ "`hostname`" != "lcls-dev2" -a "`hostname`" != "lcls-daemon1" ]; then
#    echo "ERROR: Please login lcls-dev2 or lcls-daemon1 to run $0 !"
#    exit 1
#fi

if [ -f /afs/slac/g/lcls/epics/setup/epicsReset.bash ]; then
    . /afs/slac/g/lcls/epics/setup/epicsReset.bash
elif [ -f /usr/local/lcls/epics/setup/epicsReset.bash ]; then
    . /usr/local/lcls/epics/setup/epicsReset.bash
else
    echo "ERROR: this machine is not supported for LCLS dev/prod" 
    exit 1    
fi

export PATH=/afs/slac/g/lcls/package/python/current/bin:${PATH}
cmdpath=`which iocConsole`
if [ ! -x "${cmdpath}" ]; then
        echo "ERROR: cannot find ${cmdpath}"
        exit 1
fi

if [ -f ${IOC_DATA}/${IOCNAME}/screenlog.0 ]; then
    cp ${IOC_DATA}/${IOCNAME}/screenlog.0 ${IOC_DATA}/${IOCNAME}/screenlog.0.AUTOBK
fi

iocConsole --cleanup ${IOCNAME}
iocConsole --detach --stayup ${IOCNAME} 

if [ $? -ne 0 ]; then
    echo ERROR: $0 failed.  
    exit 1
fi

exit 0

# End of script
