#!/usr/bin/ksh
#
# Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
#
# @(#)check	1.14 03/10/20
#

# Define the paths used by this script
SCRIPT=$0
SCRNAME=`basename $SCRIPT`
COMPONENT_DIR=`dirname $0`
# Include function libraries
. $ESM_LIB/esm_lib.ksh
. $ESM_LIB/configvars_lib.ksh
. $ESM_LIB/depend_lib.ksh

. $COMPONENT_DIR/component_lib.ksh
. $COMPONENT_DIR/sstr_lib.ksh

#---------------------------------------
# Display the usage message
# Arguments: 
#---------------------------------------
usage () {

    echo $SCRIPT "INFO: usage:     $SCRNAME [options]

    where options are:

        <no options>      Display all information about component
        -i | --internal   Display information about internal files/pkgs
        -p | --processes  Display the status of the component processes
        -I | --install    Return install status (0=full, 1=partial, 2=not)
        -? | -h | --help  Display this message
"
    exit 0
}

_add_locale_pkgs ()
{
    if el_isEsmTrue `el_configvar_get Localization` ; then
        CL_PACKAGES="$CL_PACKAGES $CL_L10N_PACKAGES" 
        export CL_PACKAGES 
    fi 
}

#---------------------------------------
# Check for installed packages.
# Arguments: 
#    none
# Exits with the following:
#    EL_PK_ALL   Component is fully installed
#    EL_PK_SOME  Component is partially installed
#    EL_PK_NONE  Component is not installed
#    EL_PK_ERR   Error occured while checking
#---------------------------------------
l_ck_install () {

    el_check_pkg_list "$CL_PACKAGES"
    exit $?
}

#---------------------------------------
# Display the status the component's processes
# Arguments: 
#    none
#---------------------------------------
l_ck_processes () {
    _pid=

#jw IMQ is disabled for ESM 2.0
#jw     _pid=`cl_findprocjmq`
#jw     if [ -z "$_pid" ]; then 
#jw 	el_log 1 "iPlanet iMQ...........................not running."
#jw     else
#jw 	el_log 1 "iPlanet iMQ...........................running"
#jw 	el_log 1 "$_pid"
#jw     fi

    _pid=`cl_findprocpgsql`
    if [ -z "$_pid" ]; then
	el_log 1 "PostgreSQL for SSSM...................not running."
    else
	el_log 1 "PostgreSQL for SSSM...................running"
	el_log 1 "$_pid"
    fi

    _pid=`cl_findproccre`
    if [ -z "$_pid" ]; then
	el_log 1 "The CRE for SSSM......................not running."
    else
	el_log 1 "The CRE for SSSM......................running"
	el_log 1 "$_pid"
    fi
}

#---------------------------------------
# Display all information about the component
# Arguments: 
#    none
#---------------------------------------
l_check_all () {
    # Localization packages are currently not part of the check for
    # the installed component.

    _add_locale_pkgs

    el_check_internal "$CL_NAME" "$CL_PACKAGES"
    l_ck_processes
}

#---------------------------------------
# Parse word command line arguments (--word)
# Arguments: 
#  $1 action
#---------------------------------------
l_optaction () {
    case $1 in
	components)
            _add_locale_pkgs

	    el_check_internal "$CL_NAME" "$CL_PACKAGES"
	    ;;
	install)
		l_ck_install
		    ;;
	internal)
		el_check_internal "$CL_NAME" "$CL_PACKAGES"
		    ;;
	none)
		    l_check_all
		    ;;
	processes)
		l_ck_processes
		    ;;
	*)
	    usage
	;;
    esac
}

#---------------------------------------
# Parse compound command line arguments (-azbD)
# Arguments: 
#  $1 set of compound arguments
#---------------------------------------
l_getopts () {
    actionarg=$2
    while getopts :cipI gopt
    do
        case $gopt in
        c)
            action=components
			;;
        i)
            action=internal
			;;
        p)
            action=processes
			;;
        I)
            action=install
			;;
        *)
            action=help
			;;
        esac
        l_optaction $action $optarg
    done
}

#---------------------------------------
# Arguments: 
#  $1 command line arguments (see usage function above)
#---------------------------------------
main () {
	el_secho 1 $ESM_LOG y "=== $CL_NAME $CL_VERSION Start: Checking $CL_NAME"

    # Parse command line arguments.
    if [ $# -ne 0 ]; then
        while [ $# -ne 0 ];
            do
            opt=$1
            optarg=$2
            echo $opt | /bin/grep '\-\-' > /dev/null
            if [ $? -eq 0 ]; then
                l_optaction `echo $opt | tr -d '-'` $optarg
                [ $? -eq 1 ] && shift
            else
                echo $opt | /bin/grep '\-' > /dev/null
                if [ $? -ne 0 ]; then
                    _answerfile=$opt
                else
                    echo $optarg | /bin/grep '\-' > /dev/null
                    if [ $? -eq 0 ]; then
                        l_getopts $opt
                    else
                        l_getopts $opt $optarg
                        shift
                    fi
                fi
            fi
            [ $# -ne 0 ] && shift
        done
    else
        l_optaction none none
    fi

    el_secho 1 $ESM_LOG y "=== $CL_NAME $CL_VERSION End:   Checking $CL_NAME"
}

main $*

if [ "$DEBUG" = "1" ]; then
    el_debug
    cl_debug
    el_secho 1 $ESM_LOG n "Local Environment Variables:"
    el_secho 1 $ESM_LOG n "    COMPONENT_DIR |$COMPONENT_DIR|"
    el_secho 1 $ESM_LOG n "    SCRIPT        |$SCRIPT|"
    el_secho 1 $ESM_LOG n "    SCRNAME       |$SCRNAME|"
fi
