#!/bin/bash
#
# ./startSeaside30_Adaptor runs a Seaside server Gem the class WAGemStoneRunSeasideGems
#   determines whether a FastCGI, Swazoo or Zinc adaptor is used
#     - first argument is a label used to name files
#     - second argument is a port number
#

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 server type label as a parameter
if [ "a$1" = "a" ]; then
        echo 'Missing argument <server type label>'
        exit 1
fi

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

cat << EOF | nohup $GEMSTONE/bin/topaz -u "Seaside-$1-$2" -l -e $GEMSTONE/seaside/etc/seaside30.conf 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
| x |
"set _cacheName: for Gem to get unique id in statmon output. see http://code.google.com/p/glassdb/issues/detail?id=132"
System _cacheName: ((x := 'Seaside-$1-$2-' , (GsSession serialOfSession: System session) printString , '-' , System myUserProfile userId) copyFrom: 1 to: (x size min: 31)).
%

run

GsProcess usingNativeCode not
  ifTrue: [
    "Enable remote Breakpoing handling"
    Breakpoint trappable: true.
    GemToGemAnnouncement installStaticHandler.
    System commitTransaction ifFalse: [ nil error: 'Could not commit for GemToGemSignaling' ]. 
  ].

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: '$1: topaz exit'
  object:
    'port: ', $2 printString, ' ',
    'pid: ', (System gemVersionReport at: 'processId') printString) addToLog.
System commitTransaction.
%
EOF

