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

# pragma ident	"$Id: set_component_status.sh,v 1.2.2.4 2005/04/07 21:14:52 sd148285 Exp $"
##############################################################################

################################################################################
#  Function of the set_component_status
#  1.  Look up the status entry for the component passed in and set is value.
#################################################################################

###
## Return 0 indicating a valid status
## Return 1 indication an invalid status
##
##  Valid status strings are one of 'enabled','disabled','started','stopped'
##  Corresponding int values are 1 2 3 and 4.
###

validate_status() {
    ### 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

    valid_status_strings="$ENABLE_STATUS_STRING $DISABLE_STATUS_STRING $START_STATUS_STRING $STOP_STATUS_STRING"
    valid_status_values="$ENABLE_STATUS_VALUE $DISABLE_STATUS_VALUE $START_STATUS_VALUE $STOP_STATUS_VALUE"

    for nextentry in ${valid_status_strings}
    do
        if [ ${1} = ${nextentry} ] ; then
            return 0
            break   
        fi
    done

    for nextentry in ${valid_status_values}
    do  
        if [ ${1} -eq ${nextentry} ] ; then
            convert_status_to_string $1
            return 0
        fi
    done
    
    return 1
}

###
##  Convert the status represented as int into a string to store in CCR
###

convert_status_to_string() {
    case $1 in
        $ENABLE_STATUS_VALUE) 
           status_value=$ENABLE_STATUS_STRING
           ;;
        $DISABLE_STATUS_VALUE) 
           status_value=$DISABLE_STATUS_STRING
           ;;
        $START_STATUS_VALUE) 
           status_value=$START_STATUS_STRING
           ;;
        $STOP_STATUS_VALUE) 
           status_value=$STOP_STATUS_STRING
           ;;
        *)
           $log $1 is not a valid status
           exit
           ;;
    esac
      
}

######## Main Processing #######

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

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

## Make sure that a valid status is passed in ###
validate_status  $status_value
if [ $? -ne 0 ] ; then
    $log $status_value is not a valid status.
    $log Use valid strings $valid_status_strings OR valid integers $valid_status_values
    exit 1
fi

### 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

## 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, so creating it.
    touch $prop_file 
fi

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

fgrep "$service_status_key.default" $prop_file > /dev/null
if [ $? -ne 0 ] ; then
    $log "Adding the key $service_status_key.default to CCR"
    echo "$service_status_key.default" >> $prop_file 
    $log "Adding the key $service_status_key.secret to CCR"
    echo "$service_status_key.secret=false" >> $prop_file 
fi

fgrep "$component_status_key.default" $prop_file > /dev/null
if [ $? -ne 0 ] ; then
    $log "Adding the key $component_status_key.default to CCR"
    echo "$component_status_key.default" >> $prop_file 
    $log "Adding the key $component_status_key.secret to CCR"
    echo "$component_status_key.secret=false" >> $prop_file 
fi

## Set the status using ccr client interface ##
$cli -p $component_status_key -v $status_value 



