#!/bin/csh -f
#==============================================================
#
#  Abs:  Exectuable C shell script to run an executable created
#        by compiling a Matlab script
#
#  Name: matExeRun
#
#  Args:
#    $1 = name of program to run
#         This program must be in PATH and must already be extracted 
#         using the matlab extractCTF command.
#    $2 to $10 = optional arguments to the program
#
#--------------------------------------------------------------
#
if ($#argv < 1) then
  echo "Program name not provided"
  exit 1
endif
source /afs/slac/g/lcls/epics/config/epicsReset.csh
#
# See http://www.mathworks.com/support/solutions/data/1-2R5223.html?solution=1-2R5223
# for references on LD_LIBRARY_PATH.
#
# Required for libmwmclmcrrt.so.7.5:
if ( `echo $LD_LIBRARY_PATH | grep ${MATLAB_ROOT}/sys/os/${MATLAB_HOST_ARCH}` == "" ) then
  setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:${MATLAB_ROOT}/sys/os/${MATLAB_HOST_ARCH}
endif
if ( `echo $LD_LIBRARY_PATH | grep ${MATLAB_ROOT}/bin/${MATLAB_HOST_ARCH}` == "" ) then
  setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:${MATLAB_ROOT}/bin/${MATLAB_HOST_ARCH}
endif
# Required for java:
if ( `echo $LD_LIBRARY_PATH | grep ${MATLAB_ROOT}/sys/java/jre/${MATLAB_HOST_ARCH}/jre1.5.0/lib/${MATLAB_PROC_ARCH}` == "" ) then
  setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:${MATLAB_ROOT}/sys/java/jre/${MATLAB_HOST_ARCH}/jre1.5.0/lib/${MATLAB_PROC_ARCH}/native_threads
  setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:${MATLAB_ROOT}/sys/java/jre/${MATLAB_HOST_ARCH}/jre1.5.0/lib/${MATLAB_PROC_ARCH}/client
  setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:${MATLAB_ROOT}/sys/java/jre/${MATLAB_HOST_ARCH}/jre1.5.0/lib/${MATLAB_PROC_ARCH}
endif
# For running matlab guis:
# http://www.mathworks.com/support/solutions/data/1-YIHQ4.html?solution=1-YIHQ4
# http://www.faqs.org/faqs/Xt-FAQ/section-21.html
setenv XAPPLRESDIR ${MATLAB_ROOT}/X11/app-defaults
#
# http://www.mathworks.com/support/solutions/data/1-2M4LWD.html?solution=1-2M4LWD
#
# Even though the above reference says we shouldn't need to do 
# this for 2006b, setting this flag gets rid of this annoying 
# message when running a compiled matlab script:
#
# Unable to obtain exclusive lock on CTF directory. If you are 
# running multiple simutaneous instances of this application, 
# initialization errors may occur.
setenv MCR_INHIBIT_CTF_LOCK 1
#
set matExe_command = $1
#echo "invoking $matExe_command $2 $3 $4 $5 $6 $7 $8 $9 $10"
$matExe_command $2 $3 $4 $5 $6 $7 $8 $9 $10
#end of script
