#!/bin/sh
#
# CommuniGate  This shell script takes care of starting and stopping
#              the CommuniGate Pro Communication server
#
# Linux version
#
# (c) 1998-2011, Stalker Software, Inc.
#
# chkconfig: 345 80 30
# description: CommuniGate Pro Communication Server
### BEGIN INIT INFO
# Default-Start:  3 4 5
# Default-Stop: 0 1 2 6
# Provides:       CommuniGate
# Required-Start: $network
# Required-Stop: $network
# Description:    Start CommuniGate Pro Communication Server
### END INIT INFO
#
# The default location for the CommuniGate Pro application folder is
#    /opt/
# If you have placed the application folder in a different directory,
#    change the APPLICATION variable
#
# The default location for the CommuniGate Pro "base directory" (a folder
# containing accounts, settings, logs, etc.) is /var/CommuniGate
# If you want to use a different location, change the BASEFOLDER variable
#
#
APPLICATION="/opt"
BASEFOLDER="/var/CommuniGate"
SUPPLPARAMS=

[ -f ${APPLICATION}/CommuniGate/CGServer ] || exit 0

# Some Linux distributions come with the "NPTL" threads library
# that crashes quite often. The following lines are believed to force
# Linux to use the old working threads library.
#
#LD_ASSUME_KERNEL=2.4.1
#export LD_ASSUME_KERNEL

# Source function library.
if [ -f /etc/rc.d/init.d/functions ]; then
  . /etc/rc.d/init.d/functions
elif [ -f /etc/init.d/functions ]; then
  . /etc/init.d/functions
fi

ulimit -u 2000
ulimit -c 2097151
umask 0

# Custom startup parameters
if [ -f ${BASEFOLDER}/Startup.sh ]; then
  . ${BASEFOLDER}/Startup.sh
fi

case "$1" in
  start)
    if [ -d ${BASEFOLDER} ] ; then
      echo 
    else
      echo "Creating the CommuniGate Base Folder..."
      mkdir ${BASEFOLDER}
      chgrp mail ${BASEFOLDER}
      chmod 2770 ${BASEFOLDER}
    fi
    echo -n "Starting CommuniGate Pro"
    ${APPLICATION}/CommuniGate/CGServer \
        --Base ${BASEFOLDER} --Daemon ${SUPPLPARAMS} \
#	--ClusterBackend
#	--ClusterFrontend

    touch /var/lock/subsys/CommuniGate
    ;;
  controller)
    echo "Starting CommuniGate Pro Cluster Controller"
    ${APPLICATION}/CommuniGate/CGServer \
        --Base ${BASEFOLDER} --Daemon ${SUPPLPARAMS} \
	--ClusterController
    touch /var/lock/subsys/CommuniGate
    ;;
  stop)
     if [ -f ${BASEFOLDER}/ProcessID ]; then
       echo "Shutting down the CommuniGate Pro Server"
       kill `cat ${BASEFOLDER}/ProcessID`
       sleep 5
     else
       echo "It looks like the CommuniGate Pro Server is not running"
     fi
     rm -f /var/lock/subsys/CommuniGate
     ;;
  *)
     echo "Usage: $0 [ start | stop ]"
     exit 1
esac

exit 0

