#!/bin/bash
#==============================================================
#  Name: rfViewPlotsRecent.bash
#  Abs:  Plot recent data files, currently tailored for 
#        LCLS-II LLRF conventions
#
#  Facility: LCLS-II
#
#  Auth: Sonya Hoobler, 27 June 2019
#  Rev:  dd-mmm-yyyy, First Lastname (USERNAME)
#      
#
#--------------------------------------------------------------
#  Mod:
#   
#--------------------------------------------------------------
#
#  Usage:
#         rfViewPlotsRecent.bash <path> <filter> <filename>
#
#         Calls python script to plot most recent data files.
#		  Assumes that the directory structure is 
#		  <path>/*<filter>*/<filename>. ":" are replaced by "_"
#
#============================================================

function usage {
echo "
   Purpose: Plot data from LLRF script

   Usage:
	  rfViewPlotsRecent.bash <path>
"
}

# Check for input arguments
NARGS=$#
if [ $NARGS -ne 1 ]
then
        echo "rfViewPlotsRecent.bash: 1 argument expected, but received ${NARGS}."
	usage
        exit 0
fi

PATHARG=`echo $1 | tr : _`

DIR=`ls -td ${PATHARG}/* | head -1`

RVAL=${PIPESTATUS[0]}
if [ $RVAL -ne 0 ]
then
    echo "rfViewPlotsRecent.bash: ls of log file failed:"
    echo "echo ls -td ${PATHARG}/* | head -1"
    exit 0
fi

export PACKAGE_TOP=/usr/local/lcls/package
export PATH=$PATH:$PACKAGE_TOP/lcls2_llrf/software/prc

SCRIPT=`which rf_plot_auto_data.py`
if [ ! -x "${SCRIPT}" ] ; then
    echo "ERROR: cannot find ${SCRIPT}"
    exit 1
fi

python3 $SCRIPT -p $DIR
