#!/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 same file name as User Code) "
echo "------------------------------------------"

read d4file
if test -f $d4file.data
  then
    cp $d4file.data   egs5job.inp
    echo ""
    echo "  --> d4file used, $d4file.data copied to egs5job.inp"
elif test -f $ucode.data
  then
    cp $ucode.data    egs5job.inp
    echo ""
    echo "  --> $ucode.data copied to egs5job.inp"
else
    touch egs5job.inp  #Create dummy data to avoid error message
    echo ""
    echo "  --> Empty file created as egs5job.inp"
fi

#-------------------------------------------------
# Link the UNIT=25 data as a .inp file (for PEGS5)
#-------------------------------------------------
echo " "
echo "------------------------------------------"
echo "  Enter name of UNIT(25) (pegs input file)"
echo "      (file extension must be '.inp')     "
echo "   (<CR> for same file name as data file  "
echo "      or same file name as User Code)     "
echo "------------------------------------------"

read d25file
if test -f $d25file.inp
  then
    ln -s $d25file.inp   pgs5job.pegs5inp
    echo ""
    echo "  --> d25file used, $d25file.inp linked to pgs5job.pegs5inp"
elif test -f $d4file.inp
  then
    ln -s $d4file.inp   pgs5job.pegs5inp
    echo ""
    echo "  --> d4file used, $d4file.inp linked to pgs5job.pegs5inp"
elif test -f $ucode.inp
  then
    ln -s $ucode.inp   pgs5job.pegs5inp
    echo ""
    echo "  --> $ucode.inp linked to pgs5job.pegs5inp"
else
    echo ""
    echo " Script stopped - $d25file.inp file (required by PEGS) not found"
    exit 1
fi

#------------------------------------------------------------
# Compile FORTRAN 
#-----------------------------------------------------------
if test "$1" = "db"
  then  
    echo ""      
    echo "---------------------"
    echo "Compiling (with debug)"
    echo "---------------------"
    $COMPILER $CFLAGS $DEBUG -o egs5job.exe egs5job.f
else
    echo ""
    echo "------------------------------------"
    echo "Compiling (with $CFLAGS and $OPT_LEVEL)"
    echo "------------------------------------"
    $COMPILER $CFLAGS $OPTIMIZED -o egs5job.exe egs5job.f  
fi

#--------------------------------
# Make sure executable is present
#--------------------------------
if test ! -x egs5job.exe
  then
    echo ""
    echo "Script stopped -- egs5job.f did not compile and/or link"
    exit 1
fi

#------------------------------------
# Link data directory
#------------------------------------
ln -s $BASKET/data               data

#------------------
# Run the job
#------------------
if test "$1" = "db"
  then
    echo " "
    echo "      ***********************************************************"
    echo "      * User code $ucode.f has been compiled with debug enabled *"
    echo "      ***********************************************************"
elif test "$1" = "comp" 
  then 
    echo " " 
    echo "      ***********************************************************" 
    echo "      * User code $ucode.f has been compiled but is not running *" 
    echo "      ***********************************************************"
else

    #---------------------------
    # Query about terminal input
    #---------------------------
    echo " "
    echo "-----------------------------------------------"
    echo "  Does this user code read from the terminal?  "
    echo "  (Enter 1 for yes, anything else for no)      "
    echo "-----------------------------------------------"
    read interactive

    if test "$1" = "pegs"
      then
        echo " "
        echo "      ********************************************************"
        echo "      * PEGS-only user code has been compiled and is starting *"
        echo "      ********************************************************" 
    else
        echo " "
        echo "      *******************************************************"
        echo "      * User code $ucode.f has been compiled and is starting *"
        echo "      *******************************************************"
    fi

    if test "$interactive" = "1" 
      then
        echo " "
        echo " Ready for user input:"
        echo " "
        time $PWD/egs5job.exe
    else
        echo " "
        echo " Running egs5job.exe in background"
        time $PWD/egs5job.exe 2>&1 > $PWD/egs5job.log &
    fi
fi

echo " "
echo "========================"
echo "egs5run script has ended"
echo "========================"
