#!/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

$GEMSTONE/bin/topaz -l -T50000 << EOF

output pushnew $GEMSTONE_LOGDIR/Hyper_server-${1}.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/Hyper_server-${1}.pid'.
file nextPutAll: (System gemVersionReport at: 'processId') printString.
file cr.
file close.
(ObjectLogEntry 
  info: 'Hyper: startup' 
  object: 
    'pid: ', (System gemVersionReport at: 'processId') printString, ' ',
    'port: ', $1 printString) addToLog.
System commitTransaction.
%

run
| server site |
true "enable for remote breakpoints and profiling"
  ifTrue: [
    GemToGemAnnouncement installStaticHandler.
    Exception
      installStaticException:
        [:ex :cat :num :args | 
          BreakpointNotification signal.
          "needed to avoid infinite loop when resuming from a breakpoint"
          ex _incrementBreakpointsToIgnore. ]
      category: GemStoneError
      number: 6005
      subtype: nil.
   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."
      [
        SeasidePlatformSupport transactionMutex 
          critical: [ 
            SeasidePlatformSupport 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.


server := SwazooServer singleton.
server initialize.

site := (SeasideSite new)
  name: 'Seaside';
  initialize;
  host: GsSocket getLocalHostName
    ip: (GsSocket getHostAddressByName: GsSocket getLocalHostName)
    port: $1;
  initializeResourceFromUriPattern: '/', 'seaside'.

server addSite: site.

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

SwazooServer startSite: 'Seaside'.
"does not return, unless there's an error"
%
run
GemToGemAnnouncement uninstallStaticHandler.
System beginTransaction.
(ObjectLogEntry
  fatal: 'Hyper: topaz exit'
  object:
    'port: ', $1 printString, ' ',
    'pid: ', (System gemVersionReport at: 'processId') printString) addToLog.
System commitTransaction.
%
EOF


