#!/bin/sh
# ers_ctl

if [ ! "$ERS_QUIET" = "y" ]; then
	echo
	echo "PostgreSQL Enterprise Replication Server 1.20";
	echo "   Copyright (C) 2001-2002 PostgreSQL, Inc";
	echo "*********************************************";
	echo
fi

FLAGS=" "
MAX_JVM=512
MODE=$1

if [ "$REPLIC_HOME" = "" ]; then
	. @prefix@/bin/ers_setvars
fi

PID_FILE=$REPLIC_HOME/erserver.pid


if [ "$MODE" = "start" ]; then
    if [ -f "$PID_FILE" ]; then
        pid=`cat $PID_FILE`;
        echo "File $PID_FILE already exists - check process $pid!";
	echo
        exit 1;
    fi
fi

if [ "$MODE" = "stop" -o "$MODE" = "" ]; then
     if [ -f "$PID_FILE" ]; then
         pid=`cat $PID_FILE`;
         echo "kill $pid";
         kill "$pid";
         if [ "$?" -eq 0 ]; then
             echo "Waiting until $pid dies ...";
             sleep 10;
         fi
         ps -ef | grep "$pid"  | grep -v grep
         if [ "$?" -eq 0 ]; then
             echo "Cannot kill $pid";
             exit 1;
         fi
         rm "$PID_FILE";
         if [ "$MODE" = "stop" ]; then
             exit 0;
         fi
     else
         if [ "$MODE" = "stop" ]; then
             echo "File $PID_FILE does not exists!";
             exit 1;
         fi
     fi
fi


if [ "$REPLIC_JAVA" = "" ]; then
     echo "REPLIC_JAVA is not set!"
     exit 1;
fi

if  [ "$REPLIC_THREADS" -ne 0 ]; then
     FLAGS="$FLAGS -threads ";
fi

if  [ "$REPLIC_RECOVER" -ne 0 ]; then
     FLAGS="$FLAGS -recover ";
fi

if  [ "$REPLIC_REPLIC" -ne 0 ]; then
     FLAGS="$FLAGS -replic ";
fi

if  [ "$REPLIC_CLEAN" -ne 0 ]; then
     FLAGS="$FLAGS -clean ";
fi

if  [ "$REPLIC_DEBUG" -eq 1 ]; then
     FLAGS="$FLAGS -debug ";
fi


JVM_FLAGS="-Xms64m -Xmx"$MAX_JVM"m"

$REPLIC_JAVA $JVM_FLAGS -Dreplic.home=${REPLIC_HOME} com.postgres.replic.server.ReplicationServer $FLAGS $@ >> $REPLIC_HOME/log/stderr 2>&1 &

proc_pid=$!
if [ ! "$ERS_QUIET" = "y" ]; then
	echo
	echo "Starting eRServer..."
	echo
fi
echo $proc_pid > $PID_FILE

