#!/bin/sh
#==============================================================
# st.acctestioc
#
# ARGUMENTS: 
#	$1 = IOC name in $IOC/iocBoot/screeniocs
# OPTION:     
#
# Facility:  SLAC/LCLS/FACET
#
# 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.facetioc 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
#        10-Dec-2010, Jingchen Zhou
#        Adapted to support FACET
#        22-Nov-2011, Jingchen Zhou
#        Cloned for Acctest
#        28-Mar-2012, Judy Rock
#        Added copy of screenlog.0 to backup
#        02-Jun-2014, Murali Shankar
#        Changes for the python version of iocConsole
#        09-Aug-2018, An Le
#        Removed unnecessary iocConsole cleanup command
#--------------------------------------------------------------
IOCNAME=$1

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

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

if [ -f /afs/slac/g/acctest/epics/setup/epicsReset_acctest.bash ]; then
    . /afs/slac/g/acctest/epics/setup/epicsReset_acctest.bash
else
    echo "ERROR: this machine is not supported for FACET 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 --detach --stayup ${IOCNAME}

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

exit 0

# End of script
