#!/usr/local/bin/bash
#
####################################################################
#
#   IBM Software
#   Licensed Material - Property of IBM
#   (C) Copyright International Business Machines Corp. 2006, 2017
#   All Rights Reserved.
#
#   U.S. Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with
#   IBM Corp.
#
####################################################################
# This is a front-end script for invoking the IBM System Simulator.
#
# 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

SIMNAME=
SIMARGS=
RLWRAP=

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

    -rl )
            # rlwrap provides a libreadline wrapper
            RLWRAP=$(which rlwrap 2>/dev/null)
            if [ -z "$RLWRAP" ]
            then
                echo "Unable to find rlwrap, continuing without it."
            fi
            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
                SIMARGS="${SIMARGS} $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 RUN_DIR=${SYSTEMSIM_TOP}/run
export IMAGES_DIR=${SYSTEMSIM_TOP}/images



####################################################################
# 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 simulator executable
exec ${RLWRAP} ${EXENAME} ${SIMARGS}
