#!/bin/sh
#
# set -x          # Remove comment to echo script progress, line by line
#
# **********************************************************************
# *******************                                        KEK VERSION
# *    egs5run      *
# *******************                                        23 AUG 2006
# **********************************************************************
# USAGE:  
#     egs5run       (compile user code and execute)
#     egs5run comp  (compile user code but do not execute)
#     egs5run pegs  (compile pegs-only user code and execute)
#     egs5run db    (compile user code for debug; do not execute)
#     egs5run cl    (clear out files (and links) and exit script)
# **********************************************************************
#
# The user must also set the following script variables appropriately: 
#
#   BASKET       the directory containing the main EGS5 subdirectories:
#                egs, pegs, include, pegscommons, auxcode, auxcommons, 
#                and data
#
#   MY_MACHINE   name of the OS on the cpu being used -- this can be any
#                name the user wishes, as long it can be found in the 
#                section below in which the compiler is determined.  If 
#                the user creates a name not listed below, the user must
#                add a compiler definition for the given name.
#
#   OPT_LEVEL    the optimization level for this compilation.  Note that
#                this should be the required compiler flag and not just 
#                an integer ("O2" instead of just "2", for example).
#
# Examples:
#
#   BASKET=/home/wrn/egs5
#   MY_MACHINE=Linux
#   OPT_LEVEL=O
#
#   BASKET=/home/Ralph/egs5
#   MY_MACHINE=Cygwin-Linux
#   OPT_LEVEL=O2
#
#   BASKET=/afs/slac.stanford.edu/g/rp/egs5
#   MY_MACHINE=sparc
#   OPT_LEVEL=
#
# **********************************************************************

BASKET=
MY_MACHINE=
OPT_LEVEL=

echo " "
echo "============================"
echo "egs5run script has started "
echo "============================"

echo " "
echo "working directory is $PWD"

#------------------------------------
# Make sure this is a valid directory
#------------------------------------
if test "$PWD/" = "$BASKET"
  then
    echo " "
    echo "ERROR:  egs5run can not be run from BASKET -- aborting."
    echo " "
    exit 1
elif test "$PWD" = "$BASKET"
  then
    echo " "
    echo "ERROR:  egs5run can not be run from BASKET -- aborting."
    echo " "
    exit 1
elif test "$PWD" = "$BASKET/egs"
  then
    echo " "
    echo "ERROR:  egs5run can not be run from BASKET/egs -- aborting."
    echo " "
    exit 1
elif test "$PWD" = "$BASKET/pegs"
  then
    echo " "
    echo "ERROR:  egs5run can not be run from BASKET/pegs -- aborting."
    echo " "
    exit 1
elif test "$PWD" = "$BASKET/include"
  then
    echo " "
    echo "ERROR:  egs5run can not be run from BASKET/include -- aborting."
    echo " "
    exit 1
elif test "$PWD" = "$BASKET/pegscommons"
  then
    echo " "
    echo "ERROR:  egs5run can not be run from BASKET/pegscommons -- aborting."
    echo " "
    exit 1
elif test "$PWD" = "$BASKET/auxcode"
  then
    echo " "
    echo "ERROR:  egs5run can not be run from BASKET/auxcode -- aborting."
    echo " "
    exit 1
elif test "$PWD" = "$BASKET/auxcommons"
  then
    echo " "
    echo "ERROR:  egs5run can not be run from BASKET/auxcommons -- aborting."
    echo " "
    exit 1
elif test "$PWD" = "$BASKET/data"
  then
    echo " "
    echo "ERROR:  egs5run can not be run from BASKET/data -- aborting."
    echo " "
    exit 1
elif test "$PWD" = "$BASKET/docs"
  then
    echo " "
    echo "ERROR:  egs5run can not be run from BASKET/docs -- aborting."
    echo " "
    exit 1
fi

#-------------------------------------------------------------------
# Erase old files and symbolic links to include and data directories
#-------------------------------------------------------------------
echo " "
echo "------------------------------------------------------------"
echo "Erasing files (and links) from previous runs (if they exist)"
echo "------------------------------------------------------------"
rm -f egs5job.*         2> /dev/null
rm -f fort.*            2> /dev/null
rm -f pgs5job.*         2> /dev/null
rm -f -r include           2> /dev/null
rm -f -r pegscommons       2> /dev/null
rm -f -r auxcommons        2> /dev/null
rm -f -r data              2> /dev/null

#--------------------------------------------
# Jump out of script if "cl" option is chosen
#--------------------------------------------
if test "$1" = "cl"
  then
    exit 1
fi

#-------------------------------------------
# Get information related to current machine
#-------------------------------------------
echo ""
echo "                  OS_TYPE  = $MY_MACHINE"

#---------------------------------------------------------------------
# Assign variables for compiler command (depending on current machine)
# (User may have to add their machine or alter compiler options)
#---------------------------------------------------------------------
if test "$MY_MACHINE" = "sparc"
  then 
    COMPILER="f77"
    DEBUG="-C -g"
    CFLAGS=
    if test "$OPT_LEVEL" = ""
      then
        OPTIMIZED=""
    else
        OPTIMIZED="-$OPT_LEVEL"
    fi
elif test "$MY_MACHINE" = "Linux"
  then 
    COMPILER="g77"
    DEBUG="-g -ffortran-bounds-check"
    CFLAGS="-fno-automatic -finit-local-zero"
    if test "$OPT_LEVEL" = ""
      then
        OPTIMIZED=""
    else
        OPTIMIZED="-$OPT_LEVEL"
    fi
elif test "$MY_MACHINE" = "Cygwin-Linux"
  then 
    COMPILER="g77"
    DEBUG="-g -ffortran-bounds-check"
    CFLAGS="-fno-automatic -finit-local-zero"
    if test "$OPT_LEVEL" = ""
      then
        OPTIMIZED=""
    else
        OPTIMIZED="-$OPT_LEVEL"
    fi
elif test "$MY_MACHINE" = "Digital-Unix"
  then 
    COMPILER="f77"
    DEBUG="-C -g"
    CFLAGS=
    if test "$OPT_LEVEL" = ""
      then
        OPTIMIZED=""
    else
        OPTIMIZED="-$OPT_LEVEL -ffast-math"
    fi
elif test "$MY_MACHINE" = "user_defined_machine"
  then
    COMPILER="user_defined_compiler"
    DEBUG="user_defined_debug_flags"
    CFLAGS="user_defined_compilation_flags"
    if test "$OPT_LEVEL" = ""
      then
        OPTIMIZED=""
    else
        OPTIMIZED="-$OPT_LEVEL user_defined_optimizations"
    fi
else
    echo "Exiting script because machine is not in the list"
    exit 1
fi
echo ""
echo "Your Compiler is $COMPILER"

#-------------------------
# Build the egs5job.f file 
#-------------------------
# Start with User Code
#-------------------------
echo " "
echo "---------------------------------------"
echo "        Enter name of User Code        "
echo "      (file extension must be '.f')    "
echo "---------------------------------------"
read ucode
if test -f $ucode.f
  then
    cat $ucode.f >> egs5job.f
else
    echo ""
    echo "Script stopped - $ucode.f file (the User Code) not found"
    exit 1
fi

#-----------------------------------------------------------------
# Add auxiliary code (both user and system) plus PEGS and EGS code
#-----------------------------------------------------------------
if test -d user_auxcode
  then
    echo ""
    echo "Using user auxiliary code found in $PWD/user_auxcode"
    cat $PWD/user_auxcode/*.f     >> egs5job.f
fi
cat $BASKET/egs/COPYRIGHT       >> egs5job.f
if test "$1" = "pegs" 
  then 
    cat $BASKET/egs/egs5_block*.f   >> egs5job.f
else
    cat $BASKET/egs/*.f             >> egs5job.f
    cat $BASKET/auxcode/*.f         >> egs5job.f
fi
cat $BASKET/pegs/*.f              >> egs5job.f

#------------------------------------------------
# Set up symbolic links for various include files
#------------------------------------------------
ln -s $BASKET/include             include
ln -s $BASKET/pegscommons         pegscommons
ln -s $BASKET/auxcommons          auxcommons

#--------------------------------------------
# Copy the UNIT=4 data as an egs5job.inp file
#--------------------------------------------
echo " "
echo "------------------------------------------"
echo "      Enter name of READ(4) data file     "
echo "      (file extension must be '.data')    "
echo "   (<CR> for s