#!/bin/bash -norc
#==============================================================
#
#  Abs:  Starts  Python scripts
#
#  Name: python_startup
#   $1 :  python script to start
#  [$2]:  Possible parameter passed to python script
#
# ------------------------------------------------------------
# This script gets called from lclshome which performs an
# SSH call to lcls-srv03 and launches this script
# ------------------------------------------------------------
#
#  Facility:  SLAC
#
#  Auth:  30-Sep-2013, Brobeck
#
# Mod
#
# 12-Aug-2019, S. Alverson (alverson)
#  Added ability to pass full paths to script and better error checking
#
# 7-Mar-2019, Brobeck
#  Added the mcc-simul code to run specific environment
#
# 8 July 2014 Garth Brown
#  Added ASTA/testfac environment
#
#  14-Oct-2013, Brobeck
#    Added code to make this work for either LCLS or Facet
#
#==================================================================
#
##########################################################
# -Check to see what user is running script and setup
#  appropriate environment
##########################################################

if [ -f "$1" ]
  then
     FULL_PATH="$1"
     FILE=$(basename "$1")
else
     FILE=$1
     FULL_PATH=$(which $FILE 2> /dev/null)
     if [ ! -f "$FULL_PATH" ]
       then  
	  printf "\n\t-No python script provided\n\n"
	  sleep 2
	  exit
     fi
fi

if [ "$#" -ge 2 ]
  then
     FAC="$2"
else
     FAC=""
fi

#echo $FULL_PATH
#echo $FILE
#echo $FAC

if [ "$USER" == physics ] || [ "$USER" == laci ]
  then
     # Make sure physics/laci is running the script on lcls-srv03 or mcc-simul 
     #
     if    [ `uname -n` != "lcls-srv03" ] && [ `uname -n` != mcc-simul ]
       then
           echo ""
	   echo "This script must be run from the physics or laci account"
	   echo "on lcls-srv03 or mcc-simul - Preferably from lclshome"
	   echo ""
	   exit
     fi
  elif [ "$USER" == fphysics ] || [ "$USER" == flaci ]
    then
           if    [ `uname -n` != "facet-srv02" ]
	     then
	         echo ""
		 echo "This script must be run from the fphysics or flaci account"
		 echo "on facet-srv02 - Preferably from facethome"
		 echo ""
		 exit
	   fi
  elif [ "$USER" == acctf ]
    then
           if    [ `uname -n` != "testfac-srv02" ]
	     then
	         echo ""
		 echo "This script must be run from the acctf account"
		 echo "on testfac-srv02 - Preferably from astahome"
		 echo ""
		 exit
	   fi
   else
           printf "\n\n\tThis script starts up the Python scripts for\n"
	   printf "\tLCLS, FACET, ASTA, or mcc-simul.\n\n"
	   printf "\tLCLS:\t physics and laci accounts on lcls-srv03\n"
	   printf "\tFacet:\tfphysics and flaci accounts on facet-srv02\n"
	   printf "\tASTA:\tacctf account on testfac-srv02n\n"
	   printf "\tmcc-simul server: \tlaci account\n\n"
	   exit
fi

if [ "$USER" == laci ] && [ `uname -n` == "mcc-simul" ]
 then
    # Setup environment for mcc-simul server
    if [ -e "/afs/slac/g/ad/simul/ENV" ]
      then
	source /afs/slac/g/ad/simul/ENV
      else
	printf "\n\tFile not Found: /afs/slac/g/ad/simul/ENV\n\n"
        exit
    fi
fi

#####################################
# Log file on nfs -rename old one
#####################################
if [ -e $PHYSICS_DATA/log/python ]
  then
    log_file=$PHYSICS_DATA/log/python/$FILE$FAC.log
  else
    log_file=$FILE$FAC.log
fi

MDATE=`date '+%d_%b_%Y_%H_%M'`
#printf "\n---$MDATE---\\n\n"

if [ -e $log_file ]
  then
    /bin/mv -f ${log_file} ${log_file}-$MDATE
fi
#
# For accounting purposes
#
#    export PYTHON_STARTUP_SCRIPT=$1
#    export PYTHON_LOG_FILE_NAME=$log_file

###########################
# Start the Python script
###########################
if [ "$FILE" == "" ]
  then
    printf "\n\t-No python script provided\n\n"
    sleep 2
    exit
  else
    printf "\n\tStarting $FILE $FAC ($MDATE)\n\n" > $log_file
#
# - python is used until this is added to the top of the
#   python script:
#        #!/usr/bin/env python
#
#    CMD=`which $1`

    python $FULL_PATH $FAC >> $log_file 2>&1 &

fi
#####################################
# log file cleanup -keep 5 log files
#####################################
LOGCNT=`ls -t $PHYSICS_DATA/log/python/$FILE$FAC.log* | wc -l`
if [ "$LOGCNT" -gt 5 ]
  then
	y=`expr $LOGCNT - 5`
	for x in `ls -t  $PHYSICS_DATA/log/python/$FILE$FAC.log* | tail -n$y`
       	do
	  /bin/rm -f $x
	done
fi
