#!/bin/sh
#############################################################################
# Copyright (C)2004-2005, Sun Microsystems Inc. All rights reserved.
# Use is subject to license terms.
#
# Name: remove_service_entries
# Description: Common function to remove the service entries

# pragma ident	"$Id: remove_service_entries.sh,v 1.2.2.2 2005/03/15 16:46:20 bs20596 Exp $"
##############################################################################

################################################################################
#  Function of the remove_service_entries
#  Accepts either a component_name or a service_name but not both.
#  The service entries are removed after making sure that there are no components attached to this.
#################################################################################

removeServiceProperties() {

    tmpfile="/var/tmp/tmp_deregister.txt"
    touch $tmpfile
    
    grep "cns.service.$service_name" $registryfile 
    if [ $? -ne 0 ]; then
        $log cns.service.$service_name not found in the registry $registryfile
        exit 1
    fi

    ## Removing the component entries from the ccr registry ## 
    sed /$service_name/d $registryfile > $tmpfile
    cat $tmpfile > $registryfile
    
    ## Removing the component entries from the privileged.properties if they exist ## 
    privilegedproperties="/var/cc-ccr/privileged.properties" 
    if [ -f $privilegedproperties ]  ; then
        sed /$service_name/d $privilegedproperties > $tmpfile
        cat $tmpfile > $privilegedproperties
    fi 
    
    ## Removing the component entries from the nonprivileged.properties if they exist ## 
    nonprivilegedproperties="/var/cc-ccr/nonprivileged.properties" 
    if [ -f $privilegedproperties ]  ; then
        sed /$service_name/d $nonprivilegedproperties > $tmpfile
        cat $tmpfile > $nonprivilegedproperties
    fi 
 
    rm $tmpfile

}
########################################################
### Main processing logic ###
########################################################
#
# control logging method
#
log=echo
#log="logger -pdaemon.err"

#
# Make sure this script is invoked with the correct number of parameters.
# 

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

service_name=$1
registryfile="/var/cc-ccr/ccr.properties" 
cli="/usr/lib/cc-ccr/bin/ccr"

##Check if there are any components attached to this service##
componentserviceentries=`$cli -w cns.component.+.service`

if [ "$componentserviceentries" = "0" ] ; then
    $log No more components attached to the service $service_name. Deleting the service entries.
    removeServiceProperties
else
    $log Components attached to this service $service_name. Not deleting the service entries.
fi
