#!/bin/sh
#############################################################################
# Copyright (C)2004-2005, Sun Microsystems Inc. All rights reserved.
# Use is subject to license terms.
#
# Name: get_component_status
# Description: Common status retrieving function for the client components

# pragma ident	"$Id: get_component_status.sh"
##############################################################################

################################################################################
#  Function of the get_component_status
#  1.  Look up the status entry for the component passed in and return its value.
#################################################################################
#
# control logging method
#
#log=echo
log="logger -pdaemon.err"

if [ $# -ne 2 ] ; then
    $log  "Usage : `basename $0` <service_name> <component_name>" 
    exit 1
fi

## Assign arguments passed in ##
service_name=$1
component_name=$2

### Validate the service ###
svc_dir=/usr/lib/cc-cfw/$service_name
if [ ! -d $svc_dir ] ; then
    $log $service_name not a valid service
    exit 1
fi

### Validate the component ###
comp_dir=/usr/lib/cc-cfw/$service_name/$component_name
if [ ! -d $comp_dir ] ; then
    $log Invalid component $component_name
    $log $component_name does not belong to the service $service_name
    exit 1
fi

### load the ENUM file ##
### Get the int values of the command and status here. ###
enum_file=/usr/lib/cc-cfw/framework/lib/status_enum
if [ -f $enum_file ] ; then
    . $enum_file
else
    $log status_enum interface not found. 
    exit 1
fi


## Check for a valid ccr installation ##
/usr/lib/cc-cfw/framework/lib/get_interface CHECK_CCR > /dev/null
if [ $? -ne 0 ] ; then
    $log "The CCR package installation could not be verified.  Exiting."
    exit 1
fi
cli="/usr/lib/cc-ccr/bin/ccr"

prop_file=/var/cc-ccr/$service_name.properties

if [ ! -f $prop_file ] ; then
    #$log The property file $prop_file does not exist, the service $service_name not properly registered.
    #exit 1
    status_str="$STATUS_NOT_FOUND"
fi

## The status key string in the ccr ##
service_status_key="cns.service.$service_name.status"
component_status_key="cns.component.$component_name.status"

## Get the status using ccr client interface ##
status_str=`$cli -g $component_status_key` 
if  [ $? -ne 0 ] ; then
    status_str="$STATUS_NOT_FOUND"
fi

case $status_str in
$ENABLE_STATUS_STRING)
    echo $ENABLE_STATUS_VALUE
    ;;
$DISABLE_STATUS_STRING)
    echo $DISABLE_STATUS_VALUE
    ;;
$START_STATUS_STRING)
    echo $START_STATUS_VALUE
    ;;
$STOP_STATUS_STRING)
    echo $STOP_STATUS_VALUE
    ;;
*)
    echo $STATUS_NOT_FOUND
esac




