#!/bin/bash -x
#norc
#==============================================================
#
#  Abs:  Starts  Matlab scripts
#
#  Name: Mpsgui_server_stop_facet
#
#
# ------------------------------------------------------------
# This script gets called from facethome which performs an
# SSH call to facet-srv02 and sets it's display to the
# Virtual frame buffer (XVFB) before launching this script.
# ------------------------------------------------------------
#
#  Facility:  SLAC
#
#  Auth: 18-NOV-2015, Bianchini
#         
# Mod 
#
# 12-Sep-2018 Thomas Kurty
#  Copying script from LCLS for FACET-II
# 18-Nov-2015 Carolina Bianchini
#  Set MPSGUI_SERVER_STOP_SCRIPT & MPSGUI_SERVER_LOG_FILE_NAME.
# 07-Nov-2016 Carolina Bianchini
#  Adjust the command to kill all the mpsgui running.
#
#==============================================================
#
# Make sure physics is running the script on lcls-srv04
#

if  ! [ "$USER" = "fphysics" ] && [ `uname -n` = "facet-srv02" ]
  then
     echo ""
     echo "This script must be run from the fphysics account"
     echo "on facet-srv02 - Preferably from facethome"
     echo ""
     exit
fi

# Set the display
#
OLD_DISP=`echo $DISPLAY`
export DISPLAY=:1.0

# 
# Setup LCLS Environment
#
#  if [ -e ${LCLS_ROOT}/tools/matlab/setup/matlabSetup.bash ]; then
#     . ${LCLS_ROOT}/tools/matlab/setup/matlabSetup.bash


#
# Stop the MPSGUI_SERVER script
#
PID=`ps -ef | grep "MPS:SYS1:1:GUI_HRTBT" | grep -v grep | awk '{print $2}'`
kill -kill $PID 
echo "****************************************" >> $PHYSICS_DATA/log/mpsgui_server/mpsgui_server.log
echo "`date` : $PID stopped" >> $PHYSICS_DATA/log/mpsgui_server/mpsgui_server.log 
