#!/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.

### To Debug this script set AMDEBUG to true ####
AMDEBUG=false

CONF_FILE="/opt/SUNWam/lib/amsfo.conf"
OS_ARCH=`/bin/uname -s`

if [ "$OS_ARCH" = "Linux" ]; then
        CONF_FILE="/opt/sun/identity/lib/amsfo.conf"
fi

##### sourceing amsfo config file #### 
. $CONF_FILE 


AM_BIN_DIR=$AM_HOME_DIR/bin
JMQ_BIN_DIR=/usr/bin
JMQ_PID_FILE="$LOG_DIR/jmq.pid"
AM_PID_FILE="$LOG_DIR/amdb.pid"

if [ "$OS_ARCH" = "Linux" ]; then
    JMQ_BIN_DIR=/opt/sun/mq/bin
fi

AMEXECUTABLE=$AM_BIN_DIR/amsessiondb
JMQEXECUTABLE=$JMQ_BIN_DIR/imqbrokerd

BROKER_OPTIONS="-silent"

if [ "$AMDEBUG" = "true" ]; then
    echo "restart $AM_SFO_RESTART"
    echo "cluster list $CLUSTER_LIST"
fi


stop_jmq() {
   if [ "$AMDEBUG" = "true" ] ; then
       echo "stopping JMQ Broker.."
   fi
   if test -f $JMQ_PID_FILE ; then
        kill -TERM `cat $JMQ_PID_FILE` >> /dev/null
        status=$?
        if [ "$AMDEBUG" = "true" ]; then
            if [ $status != 0 ]; then
               echo "Could not stop the JMQ Broker"
            else
               echo "JMQ Broker is shutdown"
            fi
        fi
        rm $JMQ_PID_FILE
   else
        if [ "$AMDEBUG" = "true" ]; then
            echo server not running
        fi 
        exit 1
   fi

}


start_jmq() {
    mkdir -p $LOG_DIR
    if [ "$AMDEBUG" = "true" ]; then
        echo "starting JMQ Broker"
    fi

    if [ $AM_SFO_RESTART = "true" ]; then
        BROKER_OPTIONS="-autorestart $BROKER_OPTIONS"
    fi

    _jmqpid=""

    if test -f $JMQ_PID_FILE ; then
        brok_pid=`cat $JMQ_PID_FILE`
        brok_status=`ps -e | grep $brok_pid`
    else
        brok_status=""
    fi

    if [ "$brok_status" != "" ]; then
        if [ "$AMDEBUG" = "true" ] ; then
             echo "$JMQEXECUTABLE is already running."
        fi
    else
        if [ "$AMDEBUG" = "true" ]; then 
             echo " $JMQEXECUTABLE -bgnd $BROKER_OPTIONS -vmargs $BROKER_VM_ARGS -name $BROKER_INSTANCE_NAME -port $BROKER_PORT -cluster $CLUSTER_LIST"
        fi

        $JMQEXECUTABLE -bgnd $BROKER_OPTIONS -vmargs "$BROKER_VM_ARGS" -name $BROKER_INSTANCE_NAME -port $BROKER_PORT -cluster $CLUSTER_LIST &
        _jmqpid=$!
        echo $_jmqpid > $JMQ_PID_FILE
    fi
}


start_am() {
    mkdir -p $LOG_DIR
    export AM_SFO_RESTART

    if [ "$AMDEBUG" = "true"  ]; then
        echo "starting amsessiondb client"
    fi
    # Check if the server is already running.
    _amqpid=""
     amdb_status=""
     if test -f $AM_PID_FILE ; then
        amdb_pid=`cat $AM_PID_FILE`
        amdb_status=`ps -e | grep $amdb_pid`
     else
        amdb_status=""       
     fi
     echo $amdb_status
         
     if [ "$amdb_status" != "" ]; then
        if [ "$AMDEBUG" = "true" ] ; then
            echo "$AMEXECUTABLE is already running."
        fi
     else
        if [ "$DELETE_DATABASE" = true ] ; then
            rm -rf $DATABASE_DIR
        fi

        if [ "$AMDEBUG" = "true" ] ; then
            echo "$AMEXECUTABLE -a $CLUSTER_LIST -u $USER_NAME -f $PASSWORDFILE -b $DATABASE_DIR $AMSESSIONDB_ARGS"
        fi        
           
        mkdir -p $DATABASE_DIR
        $AMEXECUTABLE -a $CLUSTER_LIST -u $USER_NAME -f $PASSWORDFILE -b $DATABASE_DIR $AMSESSIONDB_ARGS >> $LOG_DIR/amsessiondb.log & 
        _ampid=$!
        echo $_ampid > $AM_PID_FILE
     fi
}

stop_am() {
   if [ "$AMDEBUG" = "true" ]; then
       echo "stopping amsessiondb client."
   fi
   if test -f $AM_PID_FILE ; then
        kill -TERM `cat $AM_PID_FILE`
        status=$?
        if [ "$AMDEBUG" = "true" ]; then
            if [ $status != 0 ]; then
                echo "Could not stop the amsessiondb client"
            else
                echo "amsessiondb is shutdown"
            fi
        fi
        rm $AM_PID_FILE
   else
        if [ "$AMDEBUG" = "true" ]; then
            echo amsessiondb not running
        fi
   fi

}

case "$1" in
'start')
       if [ "$START_BROKER" = "true" ]; then
           start_jmq
           ## Wait for 5 sec for the broker to start ##
           sleep 5
       fi
       start_am
       sleep 5
       ;;

'stop')
       stop_am
       ## Wait untill the AMDB shuts down properly ## 
       if [ "$START_BROKER" = "true" ]; then
           sleep 5
           stop_jmq
       fi
       ;;
*)
	echo "Usage: $0 { start | stop }"
        ;;
esac
