#! /bin/sh
#set -x
#=========================================================================
# Copyright (C) GemTalk Systems 1986-2024.  All Rights Reserved.
#
# Name - runsymbolgem.sh
#
# Purpose - Script to run the symbol gem.
#
#=========================================================================

if [ "a$GEMSTONE" = "a" ]; then
  echo "ERROR: GemStone scripts require a GEMSTONE environment variable."
  echo "       Please set it to the directory where GemStone resides."
  exit 1
fi

# echo $LD_LIBRARY_PATH
# LD_LIBRARY_PATH="$GEMSTONE/lib"
# export LD_LIBRARY_PATH

if [ "x$PATH" = "x" ]; then PATH=:/bin:/usr/bin:/usr/ucb; export PATH; fi 

# error control - do not allow hup
trap '' 1

comid="runsymbolgem"			# this script's name

Syntax="Usage: $comid gemCommand gemStoneName args "
Hp="       gemStoneName    - the name of the stone"

Descript="
This Bourne shell script is invoked by a netldi or stone to start a GemStone
Symbol Creation Gem process.
By default, the file $GEMSTONE/bin/services.dat translates the GemStone service
name $comid to a command that executes this script.  If no such translation 
is found, the command \$HOME/$comid is executed instead.
"

# give a little help
if [ "x$1" = "xhelp" ] || [ "x$1" = "x-h" ] || [ "x$1" = "x-H" ]; then
  echo "$Syntax"; echo "$Hp"; echo "$Descript"; exit
fi

###########################################################################
################# User-definable symbols 
###########################################################################
gemname="gem"                   # name of the gem to execute
gemdir=""                       # directory where the gem lives
systemConfig=""                 # GEMSTONE_SYS_CONF
exeConfig=""                    # GEMSTONE_EXE_CONF
# GEM_TEMPOBJ_CACHE_SIZE now comes from -T command line arg,
#   generated from STN_SYMBOL_GEM_TEMPOBJ_CACHE_SIZE entry in stone config file.
###########################################################################
#netldiSpec not set
#runpgsvrSpec not set
appName="SymbolGem"  		# application name, for configuration

###########################################################################
################# User-Changeable Option 
###########################################################################

# Misc support...
. "$GEMSTONE/bin/misc.sh"

# set configuration args
. "$GEMSTONE/bin/setconfig.sh"

# determine gemname's location
if [ "$gemdir" = "" ]; then
  gemdir="$GEMSTONE/sys"
fi
if [ ! -f "$gemdir/$gemname" ]; then
  echo "${comid}[Error]:   cannot execute file $gemdir/$gemname"
  exit 1
fi

gemStoneName=$2
# we must have a gemStoneName
if [ "x$gemStoneName" = "x" ]; then 
   if ( test -t ); then
      echo "enter _gemStoneName: " stopstone.getgsname
      read gemStoneName
   fi
   if [ "x$gemStoneName" = "x" ]; then 
      # no gemStoneName given
      echo "stopstone[Error]: you must specify the gemStoneName."
      exit 1
   fi
fi

# bug 40199 - determine correct location for uname
if [ -f "/bin/uname" ]; then
  UNAME="/bin/uname"
elif [ -f "/usr/bin/uname" ]; then
  UNAME="/usr/bin/uname"
else
# Weird case.  Let's just hope it's in the path...
  UNAME="uname"
fi
hName=`$UNAME -n`

# show what we have
echo "${comId}[Info]:    the hostname is:  $hName"
echo "   GEMSTONE is:      \"${GEMSTONE}\""
echo "   ${gemname}'s location is: $gemdir"
echo "   system config file is:     $GEMSTONE_SYS_CONF"
echo "   executable config file is: $GEMSTONE_EXE_CONF"
echo "   ${gemname}'s arguments are: $* "

# unset this, in case it was set in the parent process and we inherited it.
unset GEMSTONE_KEEP_LOG

# The default behaviour is to delete this process's log file if it exits
# normally. If you want to keep this process's log file even on normal exit
# then uncomment the following
# GEMSTONE_KEEP_LOG=1 ; export GEMSTONE_KEEP_LOG

# By default a gem will raise its number of descriptors to whatever the
# hard limit is. By setting GEMSTONE_MAX_FD to a positive number the
# gem will only raise its descriptors to that number.
# If GEMSTONE_MAX_FD is set to 0 then the gem will not modify its descriptor
# limit
# GEMSTONE_MAX_FD=? ; export GEMSTONE_MAX_FD

# start up gemname   
#  	sleep 3600 # enable for DEBUGGING
#       exit 0 # enable for DEBUGGING
echo "cmd= exec $gemdir/$gemname $* "
exec "$gemdir/$gemname" $* 
# with the above exec nothing after this is ever executed
