#! /bin/sh
#set -x
#=========================================================================
# Copyright (C) GemTalk Systems 1986-2024.  All Rights Reserved.
#
# Name - runcachepgsvrmain.sh
#
# Purpose - script to set environment and start a remote cache page server
#
#=========================================================================

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

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

Syntax="Usage: ${comid} <monName> <numPages> <numProcs> <memLock> <spinLockCount>
              <targetFreeFrames> <numFreeFrameServers> <numSharedCounters>
              <shrPcMonPort> <spcPerms> <spcMemPolicy> <spcLargePageSizeMb>
              <stoneHost> <stonePort>

	monName		    - name of shared page cache monitor
	numPages	    - number of memory pages for monitor
	numProcs	    - number of process slots for monitor
	memLock		    - true if monitor's memory should be locked in place
	spinLockCount	    - spin count for shared locks
        targetFreeFrames    - target free frame limit for the cache
        numFreeFrameServers - number of free frame page servers to start
        numSharedCounters   - number of shared counters to create
        shrPcMonPort        - port number to be used by the SPC monitor process
        spcPerms            - permissions for the shared memory segment
        spcMemPolicy        - policy for large memory pages
        spcLargePageSizeMb  - large memory page size for the shared cache
        stoneHost           - host name where the stone is running
        stonePort           - port number the stone is using
"

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

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

# pgsvrmain is required
if [ ! -x "$GEMSTONE/sys/pgsvrmain" ]; then
  echo "${comid}[Error]:   cannot find file $GEMSTONE/sys/pgsvrmain"
  exit 1 
fi

# show what we have
echo "${comid}r[Info]:     Description of arguments:"
# 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`

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

# The default behaviour is to keep log file for this process when it exits.
# If you would like to delete the log file when the process exits normally, 
# then comment out the following line:
GEMSTONE_KEEP_LOG=1 ; export GEMSTONE_KEEP_LOG

echo "   the hostname is:     $hName"
echo "   GEMSTONE is:         $GEMSTONE"
echo "   pgsvr arguments are: $*"

# Uncomment this code to force the page server to keep the log file
# on exit and print out each command as its executed.  
#
# PgsvrTraceMode=1
# export PgsvrTraceMode

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

# start a server; ignore timeout (last arg)
exec "$GEMSTONE/sys/pgsvrmain" "$@"
# with the above exec nothing after this is ever executed
