#!/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
#
# 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 [ "$USER" == physics ] || [ "$USER" == laci ]
  then

     # Make sure physics/laci is running the script on lcls-srv03
     #
     if    [ `uname -n` != "lcls-srv03" ]
       then
           echo ""
	   echo "This script must be run from the physics or laci account"
	   echo "on lcls-srv03 - 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, or ASTA.\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\n\n"
	   exit
fi

#####################################
# Log file on nfs -rename old one
#####################################
if [ -e $PHYSICS_DATA/log/python ]
  then
    log_file=$PHYSICS_DATA/log/python/$1$2.log
  else
    log_file=$1$2.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 [ "$1" == "" ]
  then
    printf "\n\t-No python script provided\n\n"
    sleep 2
    exit
  else
    printf "\n\tStarting $1 $2 ($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 $CMD $2 | tee -a $log_file 2>&1 &

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