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

# Define the paths used by this script
SCRIPT=$0
SCRNAME=`basename $SCRIPT`
COMPONENT_DIR=`dirname $0`

# Include function libraries
. $ESM_BASE/lib/esm_lib.ksh
. $ESM_BASE/lib/esm_globals.ksh
. $COMPONENT_DIR/component_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
        -I | --install    Return install status (0=full, 1=partial, 2=not)
		-o | --os         Return status if installable on host OS (0=yes, 1=no)
        -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 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 () {
    _add_locale_pkgs

    el_check_internal "$CL_NAME" "$CL_PACKAGES"
    l_ck_processes
}

#---------------------------------------
# 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)
	    l_ck_external
		;;
    install)
	    l_ck_install
		;;
    internal)
	    el_check_internal "$CL_NAME" "$CL_PACKAGES"
		;;
    none)
		l_check_all
		;;
    os)
		el_check_os "$CL_OS_DEP"
		;;
    processes)
	    l_ck_processes
		;;
    space)
	    l_ck_space
		;;
    *)
        usage
    ;;
    esac
}

#---------------------------------------
# Parse compound command line arguments (-azbD)
# Arguments: 
#  $1 set of compound arguments
#---------------------------------------
lgetopts () {
    actionarg=$2
    while getopts :ceIpios gopt
    do
        case $gopt in
        c)
            action=components
			;;
        e)
            action=external
			;;
        i)
            action=internal
			;;
        o)
            action=os
			;;
        p)
            action=processes
			;;
        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 | /bin/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
