#!/bin/bash

####################################################################
# This is a front-end script for invoking the IBM Full System
# Simulator for the Cell Broadband Engine Architecture (CBEA).  A
# key function of this script is to locate the install directory
# of simulator and save this location in the SYSTEMSIM_TOP
# environment variable.  Then the script processes the command
# line arguments to determine how the simulator should be
# invoked.
#
# Command line arguments can either be "script arguments", which
# are handled by this script, or "program arguments", which are
# just passed through to the simulator.  The general syntax for
# invoking this script is:
#
#     systemsim [script-arguments] [program-arguments]
#
# The script processes arguments until it finds one that it does
# not understand.  All the remaining arguments are then
# considered to be program arguments and are passed through to
# the simulator.

SIMBASE=systemsim


####################################################################
# Attempt to locate the systemsim install directory

if [ -z "$SYSTEMSIM_TOP" ]
then

    X=$(/usr/bin/which ${SIMBASE})
    if [ ! -z "$X" ]
    then
        # systemsim is in the path; the install directory should be one level up
        TRYTOP=$(/usr/bin/dirname $X)/..
    else
        # Otherwise try the great-grandparent directory.
        TRYTOP=../../..
    fi

    if [ -x "$TRYTOP/bin/${SIMBASE}" ]
    then
        SYSTEMSIM_TOP=$TRYTOP
    else
        echo "Cannot locate the install directory of the systemsim release."
        echo "Please set SYSTEMSIM_TOP to the top directory of the systemsim release"
        echo "or put the systemsim bin directory into your path."
        exit 1
    fi
elif [ ! -x "$SYSTEMSIM_TOP/bin/systemsim" ]
then
        echo "The SYSTEMSIM_TOP environment variable does not appear to be set properly"
        echo "Please set SYSTEMSIM_TOP to the top directory of the systemsim release"
        echo "or put the systemsim bin directory into your path."
        exit 1
fi


####################################################################
# Now parse the command line arguments

DEBUGGER=
SIMNAME=
ARGS=

while [ ! -z "$1" ]
do
    case $1 in

    -gdb | -insight )
            DEBUGGER=${1#-}
            shift
            echo set args $*  > gdbcmds.tmp
            ;;

    -ddd )
            DEBUGGER=${1#-}
            shift
            echo set args $*  > gdbcmds.tmp
            ;;

    -d )
            SUFFIX=-d
            shift
            echo set args $*  > gdbcmds.tmp
            ;;

    -tv | -totalview )
            DEBUGGER="totalview"
            shift ;;

    -valgrind)
            DEBUGGER="valgrind -q --tool=memcheck --leak-check=yes --error-limit=no"
            shift ;;



    *)      # Check if this argument specifies which simulator to run
            if [ -x "$SYSTEMSIM_TOP/bin/${SIMBASE}$1" -o -x "$SYSTEMSIM_TOP/bin/${SIMBASE}$1-d" ]
            then
                SIMNAME=${SIMBASE}$1
                shift
            else
                # Nope.  We assume this is a command for the program
                ARGS="${ARGS} $1"
				shift
            fi
            ;;
    esac
done

####################################################################
# if there are simulator script arguments, we reset DEFAULT
# to empty, and then define it according to the argmuments.

if [ -z "$SIMNAME" ]
then
    TRYSIM=$(cd $SYSTEMSIM_TOP/bin; /bin/ls ${SIMBASE}-* 2>/dev/null)
    if [ -z "$TRYSIM" ]
    then
        echo "No simulator found in $SYSTEMSIM_TOP/bin"
        exit 1
    elif [ $(echo $TRYSIM | wc -w) != 1 ]
    then
        echo "Please specify the version of the simulator you wish to run.  Choices are"
        echo ${TRYSIM//systemsim/}
        exit 1
    fi
    SIMNAME=$TRYSIM
fi

####################################################################
# Now set things up and invoke the simulator

export EXEC_DIR=${SYSTEMSIM_TOP}/bin
export LIB_DIR=${SYSTEMSIM_TOP}/lib
export IMAGES_DIR=${SYSTEMSIM_TOP}/images

# This is used by the BLT widget library
export BLT_LIBRARY=${LIB_DIR}/blt2.4z

# This is used by the emitter profiler tools
export PROFILER_DIR=${LIB_DIR}/ppc/profiler



####################################################################
# define the name of the executable file
# check that it exists and is executable
EXENAME=${EXEC_DIR}/${SIMNAME}$DEFAULT${SUFFIX}
if [ ! -e "${EXENAME}" ]
then
    echo "Cannot find file ${EXENAME}"
    exit 1
fi
if [ ! -x "${EXENAME}" ]
then
    echo "${EXENAME} exists, but is not executable"
    exit 1
fi



####################################################################
# run that sucker -- either the program or a debugger

if [ "${DEBUGGER}" = "" ]
then
    # run simulator executable
    exec ${EXENAME} ${ARGS}
else
    # debug simulator executable.
    if [ -e gdbcmds.tmp ]; 
    then
        eval ${DEBUGGER} ${EXENAME} -x gdbcmds.tmp
    elif [ "${DEBUGGER}" = "ddd" ]
    then
        ${DEBUGGER} --args ${EXENAME} ${ARGS}
    elif [ "${DEBUGGER}" = "totalview" ]
    then
        ${DEBUGGER} ${EXENAME} -a ${ARGS}
    else
        ${DEBUGGER} ${EXENAME} ${ARGS}
    fi
fi
