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

# pragma ident	"$Id: set_service_status.sh,v 1.2.2.6 2005/05/24 21:42:13 jr150870 Exp $"
##############################################################################

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

###
##  Verify the status of all the components of this service.
##  Serivce status can be set to enabled, only if atleast on of its component is enabled.
##  Service status can be set to disabled, only if all of its components are disabled.
###

determine_service_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

    components_enabled=0
    component_names=`ls /usr/lib/cc-cfw/$service_name`
   
    for component in $component_names
    do
        comp_status=`/usr/lib/cc-cfw/framework/lib/get_component_status $service_name $component` 
        if [ $comp_status = $ENABLE_STATUS_VALUE ]  ; then
            components_enabled=1
            break  
        fi
    done

    if [ $components_enabled -eq 0 ] ; then
        $log All the components of the service $service are disabled.
        $log Setting the service status to disabled.
        status_value=$DISABLE_STATUS_STRING  
    else
        $log Atleast one of the components of the service $service is enabled.
        $log Setting the service status to enabled.
        status_value=$ENABLE_STATUS_STRING 
    fi
  
}


##### Main processing logic ###########

#
# control logging method
#
log=echo
#log="logger -pdaemon.err"

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

# The package which installs this file.
PKGNAME="SUNWccfw"
# The package which should own /var/cc-ccr/<service_name>.properties files.
# These are property files created just for the use of this Framework.
#   This needs some background: We run removef/installf to tell the package
#   system we are adding and removing the service CCR property files.
#   removef thinks the only files you could ever remove are files below the
#   owning package's BASEDIR. So the package which owns these property files
#   must have a BASEDIR of / or /var for removef to work correctly.
#   PKG_WHICH_OWNS_CCR_FILES="SUNWccccrr"
#   However, We have found that if you leave the files in place once you
#   have created them and used installf to associate them with a package,
#   even one with a BASEDIR=/usr, then when you remove the package, the files
#   are correctly removed. So that is what we do.
PKG_WHICH_OWNS_CCR_FILES="$PKGNAME"

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

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

determine_service_status

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

###  
##  Check to see if the service has been disabled. If so, remove its associated
##  properties file.
###
prop_file=/var/cc-ccr/$service_name.properties

if [ $status_value = $DISABLE_STATUS_STRING ] ; then
    ## Set the status using ccr clie ##
    $cli -p $service_status_key -v $status_value

    #$log The service $service_name is disabled, removing $prop_file now.
    # Used to remove the $prop_file here. Now we associate the file
    # with the Framework package so that, when that package is removed,
    # the system takes care of removing the file for us.
    exit 0
elif [ ! -f $prop_file ] ; then
    # We need to tell the packaging system we are adding a file to this pkg.
    #    This file is only supposed to be executable by root so setting the
    #    owner of the file to root is OK.
    installf $PKG_WHICH_OWNS_CCR_FILES $prop_file f 0644 root sys
    touch $prop_file
    chmod 644 $prop_file
    installf -f $PKG_WHICH_OWNS_CCR_FILES
    $log The property file $prop_file does not exist, so creating it.
else
    # There seems to be no way to ask if a file belongs to a package
    # so lets make sure the right package owns this file.
    installf $PKG_WHICH_OWNS_CCR_FILES $prop_file
    installf -f $PKG_WHICH_OWNS_CCR_FILES
    $log The property file $prop_file exists.
fi

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

## Set the status using ccr clie ##
$cli -p $service_status_key -v $status_value 




