#!/bin/bash
#==============================================================
#  Name: siocRestart
#  Abs:  Script to restart sioc
#
#        Assume EPICS setup has been run and $IOC is defined
#        Determine which host sioc runs on
#        Define user name to use
#        Log into host and restart sioc
#
#  Facility: LCLS/FACET/DEV/TESTFAC
#
#  Auth: Sonya Hoobler, 22 August 2012
#  Rev:  dd-mmm-yyyy, First Lastname (USERNAME)
#
#
#--------------------------------------------------------------
#  Mod:
#    01-Jul-2020, K. Luchini (luchini):
#      replace tabs in screeniocs with spaces
#      in a temporary file and then search
#      the temporary file for the ioc specified
#      in the input argument.
#    16-Oct-2019, S. Hoobler (sonya)
#      Added cryo-daemon1
#    12-Aug-2019, A. Le (anle)
#      Add ability to restart vioc
#    17-May-2017, K. Luchini (luchini)
#      chg path=$IOC/All/Dev to path=$IOC/facility
#      chg path=$IOC/All/prod to have an if statement
#    04-Dec-2014, S. Hoobler (sonya)
#      Added facet-b244-cs04 server
#    30-Apr-2014, L. Piccoli (lpiccoli)
#      Added facet-li20-pm03 server
#    19-Feb-2014, L. Piccoli (lpiccoli)
#      Added facet-li20-pm02 server
#    17-Dec-2013, Sonya Hoobler (sonya)
#      Improve parsing to ignore commented lines, match
#      any case (upper/lower/mixed), and to catch node
#      that is not sioc
#    16-Dec-2013, Sonya Hoobler (sonya)
#      Change screeniocs search to not rely on bin
#    13-Dec-2013, Sonya Hoobler (sonya)
#      Add lcls-dmp1-cs01
#    25-Nov-2013, Judy Rock (jrock)
#      Add FACET facet-li20-pm01 with user flaci
#    22-Nov-2013, Sonya Hoobler (sonya)
#      Add ASTA testfac-asta-cs01 with user acctf
#    19-Sept-2013, Judy Rock (jrock)
#      1. modify search for sioc host to accommodate Murali-release-scheme bin directory
#         new style: sioc-sys1-ml01 /usr/local/facet/epics/iocCommon/sioc-sys1-ml01/iocSpecificRelease/bin/linux-x86/matlabSupport sioc facet-daemon1
#         old style: sioc-sys1-al00 /usr/local/facet/epics/iocCommon/sioc-sys1-al00/bin/alhPV         sioc  facet-daemon1
#      2. sleep 10 seconds instead of 5 between stop and start
#
#
#--------------------------------------------------------------
#
#  Usage:
#         siocRestart <SIOC>
#
#         SIOC: Name of soft ioc.
#               Can be in PV format, i.e. SIOC:SYS0:BP01
#               or
#               node name format, i.e. sioc-sys0-bp01
#
#==============================================================

# Check for input argument
if [ $# != 1 ]
then
	echo "Missing SIOC name input argument. Quitting"
	exit 1
fi

# Convert ioc name to lower case.
node=`echo $1 | tr '[:upper:]' '[:lower:]'`
node=`echo ${node//\:/\-}`

# Define path to screeniocs file
if [ -d $IOC/facility ]; then
	path=$IOC/facility
        if [ ! -f $path/screeniocs ]; then
	  echo "Can't find $path/screniocs."
          exit 1
        fi
else
	echo "Can't find $IOC/facility"
	exit 1
fi

# Search for ${node} at beginning of line to avoid inadvertently matching part of the node name
# and to exclude any commented lines. If it's a linux ioc then "sioc" will in the 3rd column.
# If it's a linuxRT ioc then the 3rd column is the account. If it's a VME ioc the it's a number
# that represents the terminal server port.
sed -e 's/\t/    /g' ${path}/screeniocs > /tmp/screeniocs-notabs
siocline=`grep -i "^${node} " /tmp/screeniocs-notabs`
rm -f /tmp/screeniocs-notabs
siochost=`echo ${siocline} | awk ' { FS=" " ; print $4 } '`
devtype=`echo ${siocline} | awk ' { FS=" " ; print $3 } '`

# Debugging
#echo  "IOC="$IOC
#echo  "path="$path
#echo "siocline="$siocline
#echo "devtype="${devtype}

# Is this a linux soft ioc?
if [ "${devtype}" = "sioc" ]
then
    if [ "${siochost}" = "" ]
    then
    	echo "Failed to find sioc host for ${node} in ${path}/screeniocs. Quitting"
    	exit 1
    else
        echo "Found sioc on host ${siochost} for node ${node}"
    fi
# Is this a linuxRT ioc?
elif [ -n "${devtype}" ] && [[ ! "${devtype}" =~ ^[0-9]+$ ]]
then
    # We ssh into the cpu and call iocConsole.sh -r <vioc> to restart the ioc
    cpu=`echo ${siocline} | awk ' { FS=" " ; print $2 } '`
    user=${devtype}
    echo "Found cpu ${cpu} for node ${node}"
    echo "Restarting ${node} on ${cpu}"
    ssh ${cpu} -l ${user} "sh -c \"(source /etc/profile; iocConsole.sh -r ${node}; touch nohup-exit) > /dev/null 2>&1\""
    exit 0
# This ioc type is not supported.
else
    echo "Device does not appear to be sioc (3rd column in screeniocs must be sioc). Quitting"
    exit 1
fi

# Determine user name for logging into sioc server
case ${siochost} in

"lcls-daemon0" )
	user=laci
	;;

"lcls-daemon1" )
	user=laci
	;;

"lcls-dmp1-cs01" )
	user=laci
	;;

"lcls-dev1" )
	user=laci
	;;

"lcls-dev2" )
	user=laci
	;;

"lcls-dev3" )
	user=laci
	;;

"facet-daemon1" )
	user=flaci
	;;

"facet-li20-pm01" )
	user=flaci
	;;

"facet-li20-pm02" )
	user=flaci
	;;

"facet-li20-pm03" )
	user=flaci
	;;

"facet-li20-pm04" )
	user=flaci
	;;

"testfac-daemon1" )
	user=acctf
	;;

"testfac-camsrv01" )
	user=acctf
	;;

"testfac-camsrv02" )
	user=acctf
	;;

"testfac-asta-cs01" )
	user=acctf
	;;

"lcls-devcam01" )
	user=laci
	;;

"lcls-devcam02" )
	user=laci
	;;

"facet-b244-cs01" )
	user=flaci
	;;

"facet-b244-cs02" )
	user=flaci
	;;

"facet-b244-cs03" )
	user=flaci
	;;

"facet-b244-cs04" )
	user=flaci
	;;

"facet-b244-cs05" )
	user=flaci
	;;

"icd-pc85638" )
	user=laci
	;;

"esd-pc80849" )
	user=laci
	;;

"cryo-daemon1" )
	user=laci
	;;

*)
	echo "Unknown soft ioc server ${siochost}. Ask S. Hoobler to add yours to the list."
	exit 1
esac

echo "Restarting ${node}"

ssh ${siochost} -l ${user} /etc/init.d/st.${node} stop
sleep 10 # restart sometimes fails, so do stop, sleep, start for now
ssh ${siochost} -l ${user} /etc/init.d/st.${node} start

exit 0
