#!/bin/sh
#------------------------------------------------------------------------
#
#  IBM Software
#  Licensed Material - Property of IBM
#  (C) Copyright International Business Machines Corp. 2013, 2017
#  All Rights Reserved.
#
#  U.S. Government Users Restricted Rights - Use, duplication or
#  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
#------------------------------------------------------------------------

# This script can be called from anywhere, provided the user has
# systemsim in their PATH. If they do not, it should still run
# the old way, but only from a subdirectory under 'run'.

CALLEDAS=$(basename $0)

TRYPATH=$(/usr/bin/which systemsim 2>/dev/null)

# OS/X returns "no systemsim ..." as stdout, not stderr.  Get rid of it
TRYPATH=$(echo $TRYPATH | sed "s/^no systemsim.*$//")

if [ "$TRYPATH" ]; then
   TRYPATH=$(dirname $TRYPATH)/..
else
   TRYPATH=$(dirname $0)/../..
fi

if [ "$CALLEDAS" = "power9" ]; then
   OPTPATH=$(find /opt -name "systemsim*" -maxdepth 3 -type d 2>/dev/null)
fi

SYSTEMSIM_TOP=
for TRYTOP in 	$TRYPATH $OPTPATH;
do
   TRYTOP=$(cd $TRYTOP; pwd)
   if [ -x $TRYTOP/bin/systemsim ]; then
      SYSTEMSIM_TOP=$TRYTOP
      break
   fi
done

if [ "$SYSTEMSIM_TOP" ]; then
   export SIMHOST=p9
   export SYSTEMSIM_TOP
   export LD_LIBRARY_PATH=$SYSTEMSIM_TOP/trc:$LD_LIBRARY_PATH
   exec $SYSTEMSIM_TOP/bin/systemsim -p9 $*
else
   echo "Cannot find systemsim in your path"
   echo "Exiting..."
   exit 1
fi
