#!/bin/bash

#==============================================================
# This is a bash version of siocStart.pl
#
#  Abs:   Script for starting a soft ioc
#
#  Name:  siocStart                  
#         
#  Usage: siocStart <executable> <script> <iocname> <stayup>
#         where executable = executable name (unique for this ioc)
#         where script     = script name
#         where iocname    = soft ioc name
#         where stayup     = automatic restart flag (0, 1)
#
#  Auth:  24-Feb-2004, S. Allison
#--------------------------------------------------------------
#  Mods: (Latest to oldest)
#
#        07-MAR-2016, M. Shankar (mshankar):
#        Converted to bash
#        01-APR-2006, S. Allison (saa):
#        Modified for linux
#        27-APR-2004, S. Allison (saa):
#        IOC now writes core dump file to NFS data area.
#        20-APR-2004, Debbie Rogind (drogind):
#        added CD_IOC case for $path
#        06-APR-2004, Debbie Rogind (drogind):
#        added /usr/local/bin/ path to perl
#==============================================================

#  Usage: siocStart <executable> <script> <iocname> <stayup>

iocExecutable=$1
iocScript=$2
iocName=$3
stayup=$4


if [[ $stayup == "1" ]]; then
	stayupFlagBool=true
else 
	stayupFlagBool=false
fi

#IOC_INFO or IOC_DATA or CD_DATA_IOC is path of bootparams file.
#It is also used in determining the location of the screenlog file if
#deflog is set to "on" in the screenrc file.  It is also the
#directory that will contain any core file dumped by the IOC.

if [[ -n "$IOC_INFO" ]]; then
    iocinfo="$IOC_INFO"
elif [[ -n "$IOC_DATA" ]]; then
    iocInfo="$IOC_DATA"
elif [[ -n "$CD_DATA_IOC" ]]; then
    iocInfo="$CD_DATA_IOC"
else
    iocInfo="."
fi

iocLogDir="${iocInfo}/${iocName}"

if [[ ! -d $iocLogDir ]]; then
    echo "The folder for the ioc logs $iocLogDir does not exist. Defaulting to $iocInfo"
    iocLogDir=$iocInfo
    
fi

echo "IOC log directory is $iocLogDir"
cd ${iocLogDir}

if [[ -n "$IOC" ]]; then
    iocDir="$IOC" 
    iocPath="${iocDir}/${iocName}"   
elif [[ -n "$CD_IOC" ]]; then
    iocDir="$CD_IOC" 
    iocPath="${iocDir}/${iocName}"   
else
    iocDir = "." 
    iocPath="${iocDir}/${iocName}"   
fi

iocStartupScript="${iocPath}/${iocScript}"
echo "The full path to the ioc startup script is ${iocStartupScript}"

die() {
  echo "$1" 2>&1
  exit 1
}

restarts=0
if $stayupFlagBool; then
  trap 'echo -e "\nThis IOC was started with stayup; Ctrl-Cs are intercepted and ignored. To exit the stayup loop; please use iocConsole --cleanup"' INT
fi 	

while true
do
	pgrep -l -f  "${iocExecutable} ${iocStartupScript}"
	if [[ $? == 0 ]]; then
		die "IOC ${iocName} is already running; exiting this invocation of siocStart"
	fi
	
    echo "Type  ^A d  to detach from iocConsole session."
    echo "Type  ^A ?  for screen help."
    cd ${iocLogDir}; umask 000; ${iocExecutable} ${iocStartupScript}
    if [[ $? == 0 ]]; then
        restarts=0
        echo "IOC ${iocName} terminated with zero return code; in this case we reset restarts back to 0"
    else
        echo "IOC ${iocName} terminated with non-zero return code - Curr restarts: ${restarts}"
        let "restarts += 1"
    fi

    if (( $restarts > 100 )); then
        die "IOC ${iocName} terminated - maximum restart count reached"
    elif $stayupFlagBool; then
        echo "Wait 5 seconds and restart"
        sleep 5
    else
        die "IOC ${iocName} terminated - and stayup was not set. Terminating."
    fi
done
     

