#!/bin/bash

if [ -s $GEMSTONE/seaside/etc/gemstone.secret ]; then
    . $GEMSTONE/seaside/etc/gemstone.secret
else
    echo 'Missing password file $GEMSTONE/seaside/etc/gemstone.secret'
    exit 1
fi

# Requires a port number as a parameter
if [ "a$1" = "a" ]; then
        echo 'Missing argument <server port number>'
        exit 1
fi

cat << EOF | nohup $GEMSTONE/bin/topaz -l -T50000 2>&1 >> $GEMSTONE_LOGDIR/${1}_server-${2}.log &

set user DataCurator pass $GEMSTONE_CURATOR_PASS gems $GEMSTONE_NAME

display oops
iferror where

login

run
"record gems pid in the pid file"
| file |
(GsFile isServerDirectory: '$GEMSTONE_DATADIR') ifFalse: [ ^nil ].
file := GsFile openWriteOnServer: '$GEMSTONE_DATADIR/${1}_server-${2}.pid'.
file nextPutAll: (System gemVersionReport at: 'processId') printString.
file cr.
file close.
%

run
System transactionMode: #manualBegin.

Exception 
  installStaticException: 
    [:ex :cat :num :args |
      "Run the abort in a lowPriority process, since we must acquire the
       transactionMutex."
      [
        GRPlatform current transactionMutex 
          critical: [ 
            GRPlatform current doAbortTransaction ].
        System enableSignaledAbortError.
      ] forkAt: Processor lowestPriority.
    ]
  category: GemStoneError
  number: 6009
  subtype: nil.
System enableSignaledAbortError.
"This thread is needed to handle the SigAbort exception, when the primary
 thread is blocked on an accept. Assuming default 60 second 
 STN_GEM_ABORT_TIMEOUT, wake up at 30 second intervals."
[ 
  [ true ] whileTrue: [ (Delay forSeconds: 30) wait ].
] forkAt: Processor lowestPriority.


GsFile gciLogServer: '$1 Server started on port ', $2 printString.

WAGemStoneRunSeasideGems startGemServerOn: $2.
"does not return"
%
run
GemToGemAnnouncement uninstallStaticHandler.
System beginTransaction.
(ObjectLogEntry
  fatal: '$2: topaz exit'
  object:
    'port: ', $1 printString, ' ',
    'pid: ', (System gemVersionReport at: 'processId') printString) addToLog.
System commitTransaction.
%
EOF

