#!/bin/sh

# Copyright  2005 Sun Microsystems, Inc.  All rights reserved.
#
# Sun Microsystems, Inc. has intellectual property rights relating to
# technology embodied in the product that is described in this document.
# In particular, and without limitation, these intellectual property rights
# may include one or more of the U.S. patents listed at
# http://www.sun.com/patents and one or more additional patents or pending
# patent applications in the U.S. and in other countries.
#
# U.S. Government Rights - Commercial software.  Government users are subject
# to the Sun Microsystems, Inc. standard license agreement and applicable
# provisions of the FAR and its supplements.
#
# Use is subject to license terms.
#
# This distribution may include materials developed by third parties.Sun,
# Sun Microsystems and  the Sun logo are trademarks or registered trademarks
# of Sun Microsystems, Inc. in the U.S. and other countries.  
#
# Copyright  2005 Sun Microsystems, Inc. Tous droits rservs.
# Sun Microsystems, Inc. dtient les droits de proprit intellectuels relatifs
#  la technologie incorpore dans le produit qui est dcrit dans ce document.
# En particulier, et ce sans limitation, ces droits de proprit
# intellectuelle peuvent inclure un ou plus des brevets amricains lists
#  l'adresse http://www.sun.com/patents et un ou les brevets supplmentaires
# ou les applications de brevet en attente aux Etats - Unis et dans les
# autres pays.
#
# L'utilisation est soumise aux termes du contrat de licence.
#
# Cette distribution peut comprendre des composants dvelopps par des
# tierces parties.
#
# Sun,  Sun Microsystems et  le logo Sun sont des marques de fabrique ou des
# marques dposes de Sun Microsystems, Inc. aux Etats-Unis et dans
# d'autres pays.

JAVA_HOME=/usr/jdk/entsys-j2se

OS_ARCH=`/bin/uname -s`

IMQ_JAR_PATH=/usr/share/lib
JMS_JAR_PATH=/usr/share/lib
BDB_JAR_PATH=/usr/share
BDB_SO_PATH=/usr/lib
AM_HOME=/opt/SUNWam

if [ "$OS_ARCH" = "Linux" ]; then
	IMQ_JAR_PATH=/opt/sun/mq/share/lib
	JMS_JAR_PATH=/opt/sun/mq/share/lib
	BDB_JAR_PATH=/usr/share/bdb
	BDB_SO_PATH=/opt/sun/private/lib
	AM_HOME=/opt/sun/identity
fi



CLASSPATH=${IMQ_JAR_PATH}/imq.jar:${JMS_JAR_PATH}/jms.jar:${BDB_JAR_PATH}/db.jar:${AM_HOME}/locale:${AM_HOME}/lib/am_sessiondb.jar:.

OS_ARCH=`/bin/uname -s`

if [ -z "$LD_LIBRARY_PATH" ] ; then
	LD_LIBRARY_PATH=${BDB_SO_PATH}
else
	LD_LIBRARY_PATH=${BDB_SO_PATH}:$LD_LIBRARY_PATH
fi

if [ "$OS_ARCH" = "Linux" ]; then
	LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${JAVA_HOME}/jre/lib/i386/native_threads
fi

export LD_LIBRARY_PATH

JAVA_OPTS="-Xms128m -Xmx512m -Dsleepycat.db.libname=db_java"
RESTART=true

shutdown_amsessiondb() {
    RESTART=false
    if [ -n "$_javapid" ]; then
	kill -TERM $_javapid
    fi
    echo "done. \n"
}

# If the script process is killed make sure we shutdown the Broker's Java process
trap "shutdown_amsessiondb" TERM INT QUIT

RESTART=true
while [ "$RESTART" = "true" ]; do
     echo "Starting...  $RESTART" >> /tmp/test
    _javapid=""
     echo "$JAVA_HOME/bin/java $JAVA_OPTS -classpath $CLASSPATH com.iplanet.dpro.session.jmqdb.client.AMSessionDB"
     $JAVA_HOME/bin/java $JAVA_OPTS -classpath "$CLASSPATH" com.iplanet.dpro.session.jmqdb.client.AMSessionDB "$@" &
    _javapid=$!
    wait $_javapid
    _status=$?

    if [ $_status -eq 0 -o $_status -eq 1 -o $_status -eq 129 -o $_status -eq 130 -o $_status -eq 143 ]; then
        RESTART=false 
    elif [ "$AM_SFO_RESTART" = "true" ]; then
        RESTART=true
    else
        RESTART=false
    fi
   
done

