#!/bin/bash -norc
#
#==============================================================
#
#  Abs:  Starts a given XAL application for LCLS
#
#  Name: xalapplauncher
#
#  Usage: xalapplauncher <applicationname> [{arguments}+0]
#
#    applicationname is the name of an XAL application. There must be
#    a jar file of that name in $XAL_HOME/build/jar/apps/. This is the
#    application whose jar file will be started.
#
#    [{arguments}+0] means 0 or more arguments. Arguments with whitespace
#    must be quoted. These arguments will be passed on to the application
#    given in the first arg.
#
#  Limitations: No way to pass on VM args to the app.
#
#  Facility:  SLAC
#
#  Auth: 08-Apr-2008, Greg White (greg@slac.stanford.edu), 
#                     Mike Zelazny (zelazny@slac.stanford.edu)
#
#==============================================================
#
export app=$1
shift

# Additions and changes to standard environment
#
source $PHYSICS_TOP/setup/javaSetup.bash

#
# Make a log file assuming physics user
#
if [ -z $PHYSICS_USER ]
then
  user=`whoami`
else
  if [ none = $PHYSICS_USER ]
  then
    user=`whoami`
  else
    user=$PHYSICS_USER
  fi
fi

# At least for now, softegr overwrites the physics user
#
if [ -z $SOFTEGR_USER ]
then
  user=`whoami`
else
  if [ none = $SOFTEGR_USER ]
  then
    user=`whoami`
  else
    user=$SOFTEGR_USER
  fi
fi

# Construct log filename from date/time and user
#
starttime=$(date +%F-%T)
if [ -e $PHYSICS_DATA/log ]; then
  log_file=$PHYSICS_DATA/log/$app-$starttime-$user.log
else
  log_file=$1GUI$user$year$month$day$time.log
fi

#
# Actually run the application
#
echo APP=$XAL_HOME/build/jar/apps/${app}.jar 
echo ARGS=$@
echo LOG=$log_file

# Launch it; redirect stderr to the log file, leave stdout to console.
#
java -DuseDefaultAccelerator="true" -jar $XAL_HOME/build/jar/apps/${app}.jar "$@" 2> $log_file
