#!/bin/sh

# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#
# Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
#
# The contents of this file are subject to the terms
# of the Common Development and Distribution License
# (the License). You may not use this file except in
# compliance with the License.
#
# You can obtain a copy of the License at
# https://opensso.dev.java.net/public/CDDLv1.0.html or
# opensso/legal/CDDLv1.0.txt
# See the License for the specific language governing
# permission and limitations under the License.
#
# When distributing Covered Code, include this CDDL
# Header Notice in each file and include the License file
# at opensso/legal/CDDLv1.0.txt.
# If applicable, add the following below the CDDL Header,
# with the fields enclosed by brackets [] replaced by
# your own identifying information:
# "Portions Copyrighted [year] [name of copyright owner]"
#
# $Id: amsfo,v 1.6 2009/04/16 15:35:58 subashvarma Exp $
#
#

CONF_FILE=/opt/SUNWam/lib/amsfo.conf
CONF_FILE_LINUX=/opt/sun/identity/lib/amsfo.conf
CONF_FILE_HPUX=/opt/sun/identity/lib/amsfo.conf

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

OS_ARCH=`/bin/uname -s`

if [ "$OS_ARCH" = "Linux" ]; then
    CONF_FILE=${CONF_FILE_LINUX}
elif [ "$OS_ARCH" = "HP-UX" ]; then
    CONF_FILE=${CONF_FILE_HPUX}
fi

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

AM_BIN_DIR=$AM_HOME_DIR/bin
if [ "$OS_ARCH" = "Linux" ]; then
    AM_BIN_DIR=$AM_HOME_LINUX/bin
elif [ "$OS_ARCH" = "HP-UX" ]; then
    AM_BIN_DIR=$AM_HOME_HPUX/bin
fi
JMQ_BIN_DIR=/usr/bin
JMQ_PID_FILE="$LOG_DIR/jmq.pid"
AM_PID_FILE="$LOG_DIR/amdb.pid"

#####hpux-dev#####
if [ "$OS_ARCH" = "Linux" ]; then
    JMQ_BIN_DIR=/opt/sun/mq/bin
elif [ "$OS_ARCH" = "HP-UX" ]; 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 "JMQ Broker 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 "JMQ Broker 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 "AM Session DB client 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 -m $CONF_FILE"
        fi        
           
        mkdir -p $DATABASE_DIR
        $AMEXECUTABLE -a $CLUSTER_LIST -u $USER_NAME -f $PASSWORDFILE -b $DATABASE_DIR $AMSESSIONDB_ARGS -m $CONF_FILE >> $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 ## 
       sleep 5
       if [ "$START_BROKER" = "true" ]; then
           stop_jmq
       fi
       ;;
'start-jmq')
           start_jmq
           sleep 5
       ;;
'stop-jmq')
           stop_jmq
           sleep 5
       ;;
'start-amsdb')
           start_am
           sleep 5
       ;;
'stop-amsdb')
           stop_am
           sleep 5
       ;;
*)
	echo "Usage: $0 { start | stop | start-jmq | stop-jmq | start-amsdb | stop-amsdb}"
        ;;
esac
