#!/bin/sh
#  
#  File name:  S75otp.system
#  Purpose:    Automatically starts Erlang and applications when the 
#              system starts
#  Author:     
#  Resides in: /etc/rc3.d/init.d
#

MYNAME=`basename $0`
ALLARGS=$*

ROOTDIR=/usr2/omni_esme
NODE_NAME=omni_esme
MNESIA_DIR=$ROOTDIR/db
PIPEDIR=$ROOTDIR/pipe/
LOGDIR=$ROOTDIR/logs
RELEASEDIR=$ROOTDIR/system
COOKIE=wavenetlk
ERL=$RELEASEDIR/bin/erl
ERL_TOP=$RELEASEDIR
RUN_USER=root
NUM_THREADS=4

RUN_ERL_LOG_GENERATIONS=5
RUN_ERL_LOG_MAXSIZE=10000000
HEART_COMMAND="/etc/rc.d/init.d/$MYNAME $ALLARGS"
HEART_BEAT_TIMEOUT=30
HEART_BEAT_BOOT_DELAY=10
TERM=sun
export RUN_ERL_LOG_GENERATIONS RUN_ERL_LOG_MAXSIZE HEART_COMMAND HEART_BEAT_TIMEOUT HEART_BEAT_BOOT_DELAY TERM ERL_TOP

if [ ! -d /usr/bin ]
then                    # /usr not mounted
        exit
fi

if test ! -d $RELEASEDIR
then
    echo "Release directory does not exist ($RELEASEDIR)"
    exit
fi

if test ! -d $PIPEDIR
then
    echo "Pipe directory does not exist."
    mkdir $PIPEDIR
else
    echo "Pipe directory already exists."
fi

if test ! -d $LOGDIR
then
    echo "Log directory does not exist. Create a new one."
    mkdir $LOGDIR
else
    echo "Log directory already exists."
fi

if [ -z "$RELDIR" ]
then
   RELDIR=$RELEASEDIR/releases
fi

START_ERL_DATA=$RELDIR/start_erl.data

killproc() {            # kill the named process(es)
	echo "Killing $1."
  pid=`pidof $1`
  [ "$pid" != "" ] && kill $pid
}

# Start/stop processes required for Erlang

case $1 in

  start)
		echo  "Starting ISUP STP"
		echo $RELEASEDIR/bin/run_erl -daemon $PIPEDIR $LOGDIR "$RELEASEDIR/bin/start_erl $RELEASEDIR $RELDIR $START_ERL_DATA \
				-heart -sname $NODE_NAME -mnesia dir '\"$MNESIA_DIR\"' +A$NUM_THREADS -setcookie $COOKIE".
		$RELEASEDIR/bin/run_erl -daemon $PIPEDIR $LOGDIR "$RELEASEDIR/bin/start_erl $RELEASEDIR $RELDIR $START_ERL_DATA \
				-heart -sname $NODE_NAME -mnesia dir '\"$MNESIA_DIR\"' +A$NUM_THREADS -setcookie $COOKIE" 
    ;;
 
  debug)
		echo  "Starting ISUP STP in debug mode"
    $RELEASEDIR/bin/start_erl $RELEASEDIR $RELDIR $START_ERL_DATA \
				-sname $NODE_NAME -mnesia dir '"$MNESIA_DIR"' +A$NUM_THREADS -setcookie $COOKIE
    ;;
 
  stop)
    echo "Stopping ISUP STP"
    #$ERL -noshell -sname isupstp_stopper -pa $PA \
    #       -s web_server stop webserver001@$HOSTNAME
    killproc heart
    ;;
 
  *)
    echo "Usage: $0 {start|stop|debug}"
    exit 1
esac
 
exit 0


