#!/usr/bin/ksh
#
# Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
#
# @(#)check     1.9 03/04/06
#

# 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

. $COMPONENT_DIR/component_lib.ksh
. $COMPONENT_DIR/dt_lib.ksh
. $COMPONENT_DIR/pt_lib.ksh
. $COMPONENT_DIR/sv_lib.ksh
. $COMPONENT_DIR/ih_lib.ksh

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

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

    where options are:

        <no options>      Display all information about component
        -e | --external   Display information about external dependencies
        -i | --internal   Display information about internal files/pkgs
        -p | --processes  Display the status of the component processes
        -l | --logs       Display the size of the component logs
        -I | --install    Return install status (0=not,1=full, 2=partial)
        -s | --space      Return space required to install this component
        -? | -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 external dependencies
# Arguments: 
#    none
#---------------------------------------
l_ck_external () {
        el_secho 1 $ESM_LOG n "===== $SCRNAME: l_external_space () - needs to be implemented"
}
#---------------------------------------
# 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 sizes of the component's logs
# Arguments: 
#    none
#---------------------------------------
l_ck_logs () {
    el_secho 1 $ESM_LOG n "===== $SCRNAME: l_ck_logs () - needs to be implemented"
}

#---------------------------------------
# Display the status the component's processes
# Arguments: 
#    none
#---------------------------------------
l_ck_processes () {
    el_secho 1 $ESM_LOG n "===== $SCRNAME: l_ck_processes () - needs to be implemented"
}

#---------------------------------------
# Return the space required for the component
# Arguments: 
#    none
#---------------------------------------
l_ck_space () {
    el_secho 1 $ESM_LOG n "===== $SCRNAME: l_ck_space () - needs to be implemented"
}

#---------------------------------------
# Display all information about the component
# Skip the space and install checks.  They
# only have meaning in the context of
# installation.
# Arguments: 
#    none
#---------------------------------------
l_check_all () {
#jw     dt_ck_external
#jw     sv_ck_external
#jw     pt_ck_external
#jw     ih_ck_external
#jw 
     _add_locale_pkgs


     el_check_internal "$CL_NAME" "$CL_PACKAGES"
#jw 
#jw     dt_ck_processes
#jw     sv_ck_processes
#jw     pt_ck_processes
#jw     ih_ck_processes
#jw 
#jw     dt_ck_logs
#jw     sv_ck_logs
#jw     pt_ck_logs
#jw     ih_ck_logs
}

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

          el_check_internal "$CL_NAME" "$CL_PACKAGES"
	  ;;
      external)
#TBD         dt_ck_external
#TBD         sv_ck_external
#TBD         pt_ck_external
#TBD         ih_ck_external
          ;;
      install)
	  l_ck_install
          ;;
      internal)
          el_check_internal "$CL_NAME" "$CL_PACKAGES"
          ;;
      logs)
#TBD         dt_ck_logs
#TBD         sv_ck_logs
#TBD         pt_ck_logs
#TBD         ih_ck_logs
	  ;;
      none)
	  l_check_all
          ;;
      processes)
#TBD          dt_ck_processes
#TBD          sv_ck_processes
#TBD          pt_ck_processes
#TBD          ih_ck_processes
          ;;
      space)
          dt_ck_space
          sv_ck_space
          pt_ck_space
          ih_ck_space
          ;;
      *)
	  usage
	  ;;
    esac
}

#---------------------------------------
# Parse compound command line arguments (-azbD)
# Arguments: 
#  $1 set of compound arguments
#---------------------------------------
lgetopts () {
    actionarg=$2
    while getopts :ceIplis gopt
    do
        case $gopt in
        c)
            action=components
                        ;;
        e)
            action=external
                        ;;
        i)
            action=internal
                        ;;
        p)
            action=processes
                        ;;
        l)
            action=logs
                        ;;
        I)
            action=install
                        ;;
        s)
            action=space
                        ;;
        *)
            action=help
                        ;;
        esac
        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 | grep '\-\-' > /dev/null
                        if [ $? -eq 0 ]; then 
                                optaction `echo $opt | tr -d '-'` $optarg
                        else
                                lgetopts $opt $optarg
                        fi
                        shift
                done
        else
                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
