#!/bin/sh
#############################################################################
# Copyright (C)2004-2005, Sun Microsystems Inc. All rights reserved.
# Use is subject to license terms.
#
# Name: cns_client_master
# Description: Master script dealing with the installed client side proxy and agentry

# pragma ident 
##############################################################################

################################################################################
#  Function of the cns_client master script 
#  1.  Run initscript for a specific run level
#  2.  Look up the registration entries for the registered proxy and agentry
#  3.  Invoke the appropriate scripts based on the params passed in.
#################################################################################

######################################################################
##  Function to run initscript for a specific run level
#
#   Expects $1 to be the desired run level
#   Expects $2 to be the parameter to pass to the initscript
#      (ie start/stop)
#
#   If no run level is found, it is set to maxRunLevel
######################################################################

doRunLevel() {

    for entry in $script_entries
    do
        dir_name=`dirname $entry`
	runlevel_file=$dir_name/runlevel
	if [ -f $runlevel_file ] ; then
	    runlevel_value=`cat $runlevel_file`
	else
	    runlevel_value=$maxRunLevel
	fi
        if [ $1 = $runlevel_value ]; then
            $log Invoking the script $entry
            $entry
        fi
    done
  
}

######################################################################
##  Function to look up the registration entries for the init.d scripts in the 
##  CCS registry and start/stop them based on the run_levels
##
##  This functional needs to prepare the runlevel entries for the coomponents to be
##  started and call a doRunLevel with the correct argument.
######################################################################

cns_client_start_stop() {
    script_entries=""
   
   for service in $service_names
   do
       if [ "${service}" != "framework" ] ; then
           if [ -z "${component_names}" ] ; then
               $log Starting the components of the service $service.
               service_dir="/usr/lib/cc-cfw/$service"
               $log Looking up the $1 entries in $service_dir

               case "$1" in
                    'start')         
			script_entries=`find $service_dir -name start -print`
                        doRunLevel 1  "start" 
                        doRunLevel 2  "start" 
                        doRunLevel 3  "start" 
                        ;;
                    'stop')
			script_entries=`find $service_dir -name stop -print`
                        doRunLevel 3   "stop"
                        doRunLevel 2   "stop"
                        doRunLevel 1   "stop"
                        ;;
                    *)
                       $log "ERROR : cns_client_start_stop called with invalid parameter - $1 - "
                       ;;
               esac

           else
               for component in $component_names
               do
                   component_dir="/usr/lib/cc-cfw/$service/$component" 
                   $log Looking up the $1 entries in $component_dir

                  case "$1" in
                    'start')         
			script_entries=`find $component_dir -name start -print`
                        doRunLevel 1  "start" 
                        doRunLevel 2  "start" 
                        doRunLevel 3  "start" 
                        ;;
                    'stop')
			script_entries=`find $component_dir -name stop -print`
                        doRunLevel 3   "stop"
                        doRunLevel 2   "stop"
                        doRunLevel 1   "stop"
                        ;;
                    *)
                       $log "ERROR : cns_client_start_stop called with invalid parameter - $1 - "
                       ;;
                  esac

               done
           fi
       fi
   done
    
}

######################################################################
##  Function to look up the registration entries in the CCS registry and
##  invoke the enable scripts.
##  
## If service_names is empty, it means no services are passed in as arguments.  
##     In this case, lookup all the components registered and enable/disable all of the 
##     components.
##
##
## If service_names is not empty, it means one or more services are passed in as arguments.
##     In this case, go through the list of the services passed in, for each service
##     if component_names is empty, lookup all the components belonging to the service and 
##     enable/disable them.  If component_names is not empty, lookup just the components passed in
##     and enable/disable them.
######################################################################


cns_client_enable_disable() {

    for service in $service_names
    do 
        if [ "${service}" != "framework" ] ; then
            if [ -z "${component_names}" ] ; then
                service_dir="/usr/lib/cc-cfw/$service"

                $log Looking up the $1 entries in $service_dir
                entries=`find $service_dir -name $1 -print`
                for nextentry in ${entries}
                do  
                    comp_dir=`dirname $nextentry`
                    comp=`basename $comp_dir`
                    comp_status=`/usr/lib/cc-cfw/framework/lib/get_component_status $service $comp` 
                    if [ ${1} = ${ENABLE_COMMAND} -a  ${comp_status} -eq ${ENABLE_STATUS_VALUE} ] ; then
                        $log Component $comp of the service $service is already ${ENABLE_STATUS_STRING}.
                    elif [ ${1} = ${DISABLE_COMMAND} -a  ${comp_status} -eq ${DISABLE_STATUS_VALUE} ] ; then
                        $log Component $comp of the service $service is already ${DISABLE_STATUS_STRING}.
                    else 
                        $log Invoking the $nextentry
                        $nextentry
                        ####### Need to set the status of the component to enabled/disabled here. #########
                        if [ $? -eq 0 ] ; then
                            if [ ${1} = ${ENABLE_COMMAND} ] ; then
                                `/usr/lib/cc-cfw/framework/lib/get_interface SET_COMPONENT_STATUS` $service $comp $ENABLE_STATUS_VALUE
                            else
                                `/usr/lib/cc-cfw/framework/lib/get_interface SET_COMPONENT_STATUS` $service $comp $DISABLE_STATUS_VALUE
                            fi
                        fi
                    fi

                done
                
            else
               for component in $component_names
               do
                   component_dir="/usr/lib/cc-cfw/$service/$component" 
                   $log Looking up the $1 entries in $component_dir
                   entries=`find $component_dir -name $1 -print`
                   for nextentry in ${entries}
                   do  
                       comp_status=`/usr/lib/cc-cfw/framework/lib/get_component_status $service $component` 
                       if [ ${1} = ${ENABLE_COMMAND} -a  ${comp_status} -eq ${ENABLE_STATUS_VALUE} ] ; then   
                           $log Component $comp of the service $service is already ${ENABLE_STATUS_STRING}.
                       elif [ ${1} = ${DISABLE_COMMAND} -a  ${comp_status} -eq ${DISABLE_STATUS_VALUE} ] ; then
                           $log Component $comp of the service $service is already ${DISABLE_STATUS_STRING}.
                       else 
                           $log Invoking the $nextentry
                           $nextentry
                       fi
                       ####### Need to set the status of the component to enabled/disabled here. #########
                       if [ $? -eq 0 ] ; then
                           if [ ${1} = ${ENABLE_COMMAND} ] ; then
                                `/usr/lib/cc-cfw/framework/lib/get_interface SET_COMPONENT_STATUS` $service $component $ENABLE_STATUS_VALUE
                           else
                                `/usr/lib/cc-cfw/framework/lib/get_interface SET_COMPONENT_STATUS` $service $component $DISABLE_STATUS_VALUE
                           fi
                       fi
                   done
               done
           fi

           ####### Need to set the status of the service to enabled/disabled here. #########
           `/usr/lib/cc-cfw/framework/lib/get_interface SET_SERVICE_STATUS` $service 
           
        fi
    done
    
}



######################################################################
##  Function to look up the registration entries in the CCS registry and
##  list the registered agents.
##
######################################################################

cns_client_list_components() {
   /usr/lib/cc-cfw/framework/lib/list_components $service_names
}

cns_client_list_services() {
   /usr/lib/cc-cfw/framework/lib/list_services
}

getServiceAndComponentNames() {
    args=$*
    service_names=""
    component_names=""
    component_args=""

    for arg in $args
    do
        if [ "${arg}" = "-s" ] ; then
            addServiceName=true;
        elif [ "${arg}" = "-c" ] ; then
            if [  "${addServiceName}" != "true" ] ;  then
                $log "ERROR  : Cannot pass in a component name without a service name"
                $log "Usage : `basename $0` { start | stop | enable | disable | list } -s <service-name[s]>  -c <component-name[s]> "
                echo "ERROR  : Cannot pass in a component name without a service name"
                echo "Usage : `basename $0` { start | stop | enable | disable | list } -s <service-name[s]>  -c <component-name[s]> "
                exit 1
            fi
            addServiceName=false;
            addComponentName=true;
        elif [ "${addServiceName}" = "true" ] ; then
            #
            # Validate that the service is registered with the framework.
            #
            if [ ! -d "/usr/lib/cc-cfw/${arg}" ] ; then
                $log "ERROR : The service ${arg} is not properly registered with the framework."
                exit 1
            fi

            #
            # The service is registered, add it to the set.
            #
            service_names="${service_names} ${arg}"
        elif [ "${addComponentName}" = "true" ] ; then
            component_args="${component_args} ${arg}"

            #
            # Validate that the component is registered with the framework.
            #
            for svc in $service_names
            do
                if [ -d "/usr/lib/cc-cfw/${svc}/${arg}" ] ; then
                    #
                    # The component is registered, add it to the set.
                    #
                    component_names="${component_names} ${arg}"
                fi
            done
        fi
    done
    
    #
    # Make sure all of the component args passed are in the registered list.
    #
    if [ "${addComponentName}" = "true" ] ; then
        for component in $component_args
        do
            /usr/bin/echo "${component_names}" | /usr/bin/grep "${component}"
            if [ $? -ne 0 ] ; then
                $log "ERROR : One or more of the components are not properly registered with the framework."
                exit 1
            fi
        done
    fi

    if [  -z "${service_names}" ] ; then
        service_names=`ls /usr/lib/cc-cfw`
    fi
}

register_flash()
{
    if [ -z "$1" ]
    then
        ###
        ## Sym-link the client_cleanup script to /etc/flash/postdeployment for 
        ## flash archive tool registration.
        ###

        if [ -d $flashdir ]
        then
            if [ ! -f ${flashdir}/cc-client_cleanup ]
            then
                /usr/bin/ln -s /usr/lib/cc-cfw/framework/lib/client_cleanup "${flashdir}/cc-client_cleanup"
            fi
        else
            $log "The flash archive tool directory does not exist."
            $log "Cancelling flash archive tool registration."
        fi
    fi
}

register_sysuncfg()
{
    if [ -z "$1" ]
    then
        ###
        ## Run sysidconfig with the '-a' option to get the client_cleanup script added to
        ## the list of defined applications.
        ###

        defined_app=`/usr/sbin/sysidconfig -l | /usr/bin/fgrep "client_cleanup"`

        if [ -z "$defined_app" ]
        then
            /usr/sbin/sysidconfig -a /usr/lib/cc-cfw/framework/lib/client_cleanup
        fi
    fi
}

unregister_flash()
{
    if [ -z "$1" ]
    then
        ###
        ## Remove the sym-link to the client_cleanup script in 
        ## /etc/flash/postdeployment.
        ###

        if [ -f "${flashdir}/cc-client_cleanup" ]
        then
            /usr/bin/rm "${flashdir}/cc-client_cleanup"
        else
            $log "cc-client_cleanup is not registered with the flash archive tool."
        fi
    fi
}

unregister_sysuncfg()
{
    if [ -z "$1" ]
    then
        ###
        ## Run sysidconfig with the '-r' option to get the client_cleanup script removed
        ## from the list of defined applications.
        ###

        /usr/sbin/sysidconfig -r /usr/lib/cc-cfw/framework/lib/client_cleanup
    fi
}

######################################################################
###  Main functionality of the script
######################################################################


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

/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


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


maxRunLevel=3
flashdir="/etc/flash/postdeployment"

case "$1" in
'start')
    getServiceAndComponentNames $*
    cns_client_start_stop $1
    ;;
'stop')
    getServiceAndComponentNames $*
    cns_client_start_stop $1
    ;;
'enable')
    register_flash $2
    register_sysuncfg $2
    getServiceAndComponentNames $*
    cns_client_enable_disable $1
    ;;
'disable')
    unregister_flash $2
    unregister_sysuncfg $2
    getServiceAndComponentNames $*
    cns_client_enable_disable $1
    ;;
'list_components')
    getServiceAndComponentNames $*

    if [ ! -z "${component_names}" ] ; then
        echo "Component names are not accepted for the list_components command"
        echo "Usage : `basename $0` list_components [ -s <service_name(s)> ] "
        exit 1
    fi
    cns_client_list_components 
    ;;
'list_services')
    if [ $# -ne 1 ] ; then
        echo "No arguments are accepted for the list_services command."
        echo "Usage : `basename $0` list_services"
        exit 1
    fi
    cns_client_list_services
    ;;
 *)
    echo "Usage : `basename $0` { start | stop | enable | disable | list_components | list_services } -s <service-name[s]>  -c <component-name[s]> "
    ;;
esac

exit 0

