#!/bin/sh
# Copyright (c) 1997-2003 by ilink Kommunikationssysteme GmbH.  All Rights Reserved.
#
# Start/stop TeamCall Server
#
# Calling user has to be root!
# Modify according to your needs.

# set your PBX-Type 
# for valid types use ./CSTAServer --help
# example PBX=a4400
PBX=%%PBX%%

# set the nodename for teamcall server instance
# it is usefull for a multi teamcall server installation
# example NODENAME=berlin (do not use spaces or special caracters for Nodename)
NODENAME=%%NODE%%

# set the main teamcall install path
# example: TCINSTALL=/usr/local/ilink/teamcall
TCINSTALL=%%TCINSTALL%%


### DO NOT CHANGE FOLLOWS###

NODEDIR="$TCINSTALL/node_$NODENAME"
SERVER="CSTAServer_$NODENAME"
SERVERNAME="TeamCall Server $NODENAME"
PIDFILE="TeamCall.pid"
pid=0

case $1 in
start)
	if [ -f "$NODEDIR/run/$PIDFILE" ]; then
	   pid=`cat "$NODEDIR/run/$PIDFILE"`
           checkpid=`ps -f -p $pid | grep $pid | grep node_$NODENAME/`
           # if not then delete this pid-File and start TeamCall Prozess
           # if yes then exit
           if [ ! "$checkpid" ]; then
              echo "remove old pid-file"
              rm "$NODEDIR/run/$PIDFILE"
           else
              echo "$SERVER is already running";
              exit 0;
           fi
	fi
	if [ -f "$NODEDIR/Default.conf" ];then
	   cd $NODEDIR
	   "./$SERVER" -c "$NODEDIR/Default.conf" -pbx $PBX 2> /dev/null 1>&2 &
	   echo $! > "$NODEDIR/run/$PIDFILE"
	   echo "$SERVERNAME started."
	else
	   echo "$SERVERNAME NOT started."
	fi
;;

stop)
	if [ ! -f "$NODEDIR/run/$PIDFILE" ]; then
	   echo "$SERVER is not running"
	   exit 0; 
	fi   
	pid=`cat "$NODEDIR/run/$PIDFILE"`
	if [ x != "x$pid" ]; then
           kill -15 $pid
	   rm "$NODEDIR/run/$PIDFILE"
	   echo "$SERVER stopped."
	fi
;;

*)
    echo "Invalid argument: " $1
    echo "Use [start or stop]"
;;

esac

exit
