#=======================================================
#
# Name:     clearMatlabPV.sh
#
# Desc:     clear a PV slot used by Matlab Prelaunch
#           system
#
# Usage:    clearMatlabPV.sh <AREA> <OPI_NUM> [N]
#           ex: clearMatlabPV.sh ACR0 21 5 6
#           used by matlab_prelaunch.edl "reset" button
#
# Authored: 05-Dec-2016, Thomas Kurty (tkurty)
#
# Revised:  dd-mmm-yyyy, Author (user)
#
#=======================================================

# Usage info, needs at least 2 arguments
if [ $# -lt 2 ] ; then
    echo "Usage: $0 <AREA> <OPI_NUM> [N]"
    echo "  to clear PVs OPI:\$(AREA):\$(OPI_NUM):MATLAB\$(N)_STATUS"
    echo "  and OPI:\$(AREA):\$(OPI_NUM):MATLAB\$(N)_SCRIPT."
    echo "  Leave N blank to clear all."
    exit 1
fi

# Parse AREA and OPI_NUM arguments
AREA=$1
shift
OPI_NUM=$1
shift

# If no PV is specified, clear all
if [ $# = 0 ] ; then
    for i in `seq 1 50` ; do
        caput OPI:$AREA:$OPI_NUM:MATLAB${i}_STATUS ''
        caput OPI:$AREA:$OPI_NUM:MATLAB${i}_SCRIPT 'exit'
    done

# Otherwise, clear specified PVs
else
    for index in $@ ; do
        caput OPI:$AREA:$OPI_NUM:MATLAB${index}_STATUS ''
        caput OPI:$AREA:$OPI_NUM:MATLAB${index}_SCRIPT 'exit'
    done
fi

