#! /bin/sh
#set -xv
#=========================================================================
# Copyright (C) GemTalk Systems 1986-2014.  All Rights Reserved..
#
# Name - startshrpcmon.sh
#
# Purpose - start shared page cache monitor
#
# $Id: startshrpcmon.sh 34503 2014-10-15 16:05:56Z otisa $
#
#=========================================================================

echo "startshrpcmon.sh GEMSTONE: $GEMSTONE"

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 
					
. "$GEMSTONE/bin/misc.sh"

comid="startshrpcmon"

# Add shared memory permissions arg for 41949
# Add large page policy arg for 42666

Syntax="Usage: startshrpcmon <monitorname> <numpages> <numprocs> <memlock> <spinlockcount> <targetfflimit> <numFFpgsvrs> <numSharedCtrs> <portNumber> <perms> <largePagePolicy> <stoneConnectTimeout> [-h]

        monitorname     - monitor name (usually stone name & hostId)
        numpages        - number of pages to allocate for the shared cache
        numprocs        - max number of processes that need to attach to it
        spinlockcount   - number trys to get lock before sleeping on semaphore
	memlock         - (boolean) whether to lock the cache in memory
        targetfflimit   - target free frame limit
        numFFpgsvrs     - number of free frame page servers to start
        numSharedCtrs   - number of shared counters for this shared cache
        portNumber      - port number to be used, or 0 to have the system select a port.
        perms           - permissions for the shared memory segment
        largePagePolicy - large memory page policy for the shared cache. Values: 0,1, or 2
        stoneConnectTimeout - in seconds

This Bourne shell script is invoked by a netldi in reponse to a
request of the netldi to provide a GemStone SharedPageCache monitor. By
default, netldi uses the file $GEMSTONE/bin/services.dat to translate the
name startshrpcmon to a command that executes this script.  If no such
translation is found, the command \$HOME/startshrpcmon is executed by
netldi instead.
"

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

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

# shrpcmonitor is required
if [ ! -x "$GEMSTONE/sys/shrpcmonitor" ]; then
  echo "${comid}[Error]:   cannot find file $GEMSTONE/sys/shrpcmonitor"
  exit 1 
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`

# 41949
# unlike bash and ksh, sh does not support ${10}, so we must shift the args
NAME=$1
PAGES=$2
PROCS=$3
LOCKED=$4
SPINS=$5
FF_TARGET=$6
FF_SERVERS=$7
COUNTERS=$8
PORT=$9
shift 9
PERMS=$1
POLICY=$2
TIMEOUT=$3

echo "SharedPageCache Monitor Summary:"
echo "   the hostname is:    $hName"
echo "   GEMSTONE is:        \"${GEMSTONE}\""
echo "   shrpcmon arguments are:" 
echo "      SharedPageCache Monitor name  $NAME."
echo "      Number of pages               $PAGES."
echo "      Number of processes           $PROCS."
echo "      Cache locked in memory        $LOCKED."
echo "      SpinLockCount                 $SPINS."
echo "      TargetFreeFrameCount          $FF_TARGET."
echo "      Number of free frame servers  $FF_SERVERS."
echo "      Number of shared counters     $COUNTERS."
echo "      Port number                   $PORT."
echo "      Permissions                   $PERMS."
echo "      Large Page Policy             $POLICY."
echo "      Stone/cachepgsvr connect timeout $TIMEOUT."

raiseDescriptors   
# start shrpcmon - shrpcmon needs GEMSTONE
exec "$GEMSTONE/sys/shrpcmonitor" "'"$NAME"'" setlocationstr $PAGES setnumpages \
  $PROCS setnumprocs $LOCKED setmemlock $SPINS setspinlockcount $FF_TARGET settargetfflimit \
  $FF_SERVERS setnumffpgsvrs $COUNTERS setnumsharedctrs $PORT setportnumber $PERMS setspcperms \
  $POLICY setlargepagepolicy $TIMEOUT settimeout \
  startup sloop exit
# with the above exec nothing after this is ever executed
