#!/bin/sh
#######################################################################
#
# This script ensures that Oops will be restarted in case of
# termination. Designed for SUN Solaris.
#
# Contributed by Andy Igoshin <ai@vsu.ru>
#
#######################################################################
#
# Place this script in Oops home directory.
#
# SIGS:  1   2   3    4   5    6    7   16   17
trap "" HUP INT QUIT ILL TRAP ABRT EMT USR1 USR2
trap "i=1" TERM

OOPS_HOME=/opt/local/oops       # Oops home directory
OOPS_NAME=oops			# Oops executable file name
OOPS_USER=oops                  # Owner of Oops process
OOPS_WAIT=10                    # Time between attempts to restart Oops
OOPS_LOG=$OOPS_HOME/oops.out	# Oops log file
OOPS_DB=$OOPS_HOME/DB		# Oops DB path

LD_LIBRARY_PATH=""
export LD_LIBRARY_PATH

NAME=`/bin/basename $0`
PG=`/bin/pgrep -x $NAME`
if [ `echo $PG | /bin/wc -w` -gt 1 ] || /bin/pgrep -x -U $OOPS_USER $OOPS_NAME > /dev/null; then
    echo "$NAME: Oops is already running"
    exit 1
fi

ulimit -HS -n unlimited
ulimit -HS -d unlimited
ulimit -HS -s unlimited
ulimit -HS -c unlimited

echo $$ > $OOPS_HOME/logs/$NAME.pid
echo `date`: Starting $NAME > $OOPS_LOG

cd $OOPS_HOME

i=0
while [ $i -eq 0 ]; do
	[ -f $OOPS_HOME/core ] && mv $OOPS_HOME/core $OOPS_HOME/core.`date +"%Y.%m.%d-%H:%M:%S"`
	echo `date`: ulimit -a - started >> $OOPS_LOG
	ulimit -a >> $OOPS_LOG
	echo `date`": ulimit -a - ended\n" >> $OOPS_LOG
	echo "LD_LIBRARY_PATH = $LD_LIBRARY_PATH\n" >> $OOPS_LOG
	for mod in ./oops ./oopsctl ./modules/*.so ; do
	    echo "ldd $mod" >> $OOPS_LOG
	    /bin/ldd "$mod" >> $OOPS_LOG ; echo "" >> $OOPS_LOG
	done
	echo `date`": Starting Oops\n" >> $OOPS_LOG
	$OOPS_HOME/$OOPS_NAME -c $OOPS_HOME/oops.cfg >> $OOPS_LOG 2>&1
	[ $i -eq 0 ] && sleep $OOPS_WAIT
done

echo `date`: Stopping $NAME >> $OOPS_LOG
/bin/rm -f $OOPS_HOME/logs/$NAME.pid

exit 0
