#!/bin/sh
#
# $Header: whdev/2.0/owb/shiphome/owb/bin/unix/ccashut /tahoelc/1 2009/11/17 20:09:39 jinghe Exp $
#
# ccashut  
#
# Copyright (c) 2007, 2009, Oracle and/or its affiliates. All rights reserved. 
#
#    NAME
#      ccashut - Shutdown of OWB Control Center Agent
#
#    DESCRIPTION
#      This will shutdown OWB Control Center Agent gracefully.
#
#

# include OWB environment settings
DIR=`expr $0 : '\(.*\)/'`
if [ "x$DIR" != "x" ]; then
  . $DIR/owbenv
else
  . owbenv
fi

    # get the admin port
    port=`grep 'port=' $OWB_HOME/owb/jrt/config/rmi.xml | sed -e 's/[^0-9]//g'`
    echo "Checking admin port: $port"
    if [ "x$port" = "x" ]
    then
      echo "Failed in getting the admin port, use 23791 instead"
      port="23791"
     fi

if [ "x$1" != "x" ]; then
  OWB_JRT_ADMIN_PASSWORD=$1
fi

if  $JAVA_HOME/bin/java $JAVA64FLAG -Doracle.home=$OC4J_HOME -Doracle.j2ee.home=$OWB_JRT_RUN_HOME -jar $OC4J_HOME/j2ee/home/admin.jar ormi://localhost:$port oc4jadmin $OWB_JRT_ADMIN_PASSWORD -shutdown force
then
  echo ""
else
     echo "Exiting due to error."
     exit 1
fi

# check startup status from log file
COUNT=0
while [ $COUNT -lt 60 ]; do
# check whether Control Center Agent J2EE server is already running, we check port 23791
    if netstat -an | grep $port | grep -i listen
        then
        sleep 3
        COUNT=`expr $COUNT + 1`
    else
        echo "Control Center Agent shutdown successful."
        exit 0
    fi
done

echo "Control Center Agent shutdown failed due to unknown error."
exit 1
