#!/bin/bash
#==============================================================
#  Name:  matlab_prelaunch.sh
#
#  Rem:   This script starts a few MATLAB processes in the background.
#
#  Usage: This script is called from lclshome
#
#  Auth:  15-Feb-2017, Thomas Kurty (tkurty)
#  Rev:   dd-mmm-yyyy, Whoever (user):
#         17-Feb-2017, Thomas Kurty:
#           Added more info to display to user
#         12-May-2017, Janos Vamosi
#           Font and window size increased
#         31-May-2017, Janos Vamosi
#           Initial PV cleanup added
#--------------------------------------------------------------

# Starts a few MATLAB processes in the background

# Get machine hostname
CHECK_HOST=`hostname`

# Uncomment for testing on lcls-dev2, pretend we are on "opi2"
#if [ "$CHECK_HOST" = "lcls-dev2" ] ; then
#    CHECK_HOST="opi2"
#fi

# Uncomment for testing on lcls-dev3, pretend we are on "opi3"
#if [ "$CHECK_HOST" = "lcls-dev3" ] ; then
#    CHECK_HOST="opi3"
#fi


for i in `seq 1 50` ; do
    if [ "$CHECK_HOST" = "opi$i" ] ; then # check if machine is an OPI, and get OPI number
        export OPI_NUM=$i
        export AREA=ACR0
        break;
    fi
done

# Prelaunching Matlab
# only on OPI
if [ -n "$OPI_NUM" ] ; then
    NUM_MATLAB_PRELAUNCHES=`caget -t OPI:$AREA:$OPI_NUM:NUM_MATLAB_PRELAUNCHES`
    if [ -z "$NUM_MATLAB_PRELAUNCHES" ] ; then # check if IOC is active
        echo "Could not find OPI:$AREA:$OPI_NUM:MATLAB PVs, no Matlab prelaunching"
    else
        # Check if there are running MATLAB sessions
        MATLABS_RUNNING=`caget -t OPI:$AREA:$OPI_NUM:MATLABS_RUNNING`

        if [ -z "$MATLABS_RUNNING" ] ; then
            MATLABS_RUNNING=0
        fi

        # Check the number of matlab processes
        MATLAB_PROC_NUM=$(ps -Fu `whoami` | grep -E '[M]ATLAB.*startMATPRE' | wc -l)
        if [ -z "$MATLAB_PROC_NUM" ] ; then
            MATLAB_PROC_NUM=0
        fi

        if [ "$MATLABS_RUNNING" -gt "$MATLAB_PROC_NUM" ] ; then
            # PV indicates more processes than actually running so cleanup required
            echo "Found fewer Matlab sessions than expected. Restarting Matlab Prelaunch sessions."
            clearMatlabPV.sh $AREA $OPI_NUM

            # Wait for Matlab sessions to complete
            sleep 1

            # Reset counters
            caput OPI:$AREA:$OPI_NUM:MATLABS_RUNNING 0
            caput OPI:$AREA:$OPI_NUM:MATLABS_WAITING 0
            caput OPI:$AREA:$OPI_NUM:MATLABS_BUSY 0
        fi

        NUM_MATLABS_WAITING=`caget -t OPI:$AREA:$OPI_NUM:MATLABS_WAITING`
        count=$((NUM_MATLAB_PRELAUNCHES-NUM_MATLABS_WAITING))
        if [ "$count" -le "0" ] ; then
         echo "Already at maximum number of Matlab sessions, no additional prelaunching"
        else
            echo "Launching $count Matlab sessions in the background"
        fi
        for i in `seq $count` ; do
            xterm -iconic -fn 10x20 -geometry 100x40 -T "MatlabGUI.prelaunch - DO NOT PRESS [X]" -e MatlabGUI.prelaunch startMATPRE &
        done
    fi
fi

