#!/bin/ksh
#-------------------------------------------------------------------------------
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
#
# @(#)esm_cimom_tool.ksh	1.3 04/04/01
#
#-------------------------------------------------------------------------------

APP_NAME=$(basename $0)
APP_HOME=$(cd `dirname $0`; pwd)

# source the common library
. $APP_HOME/esm_cimom.ksh


usage()
{
    cat <<EOF
USAGE:
    $APP_NAME <subcommand> [options] [operands]

SYNOPSIS:
    $APP_NAME list
    $APP_NAME dump
    $APP_NAME display <host>
    $APP_NAME remove <host>
    $APP_NAME setup <host>
    $APP_NAME --help

SUBCOMMANDS:
    list
	Lists the CIMOM addresses, either manually setup or dynamically
	discovered by SLP.
    dump
	Dumps the files and directories in the CIMOM persistence directory.
    display <host>
	Displays the details of the specified CIMOM, including the service
	properties discovered by SLP, the configuration settings and whether
	or not the CIMOM secrets are setup.
    remove <host>
	Removes the specified CIMOM, including the persisted data and the
	CIMOM secrets.
    setup <host>
	Setup the specified CIMOM, which implies storing the CIMOM secrets.

OPERANDS:
    <host>
	The CIMOM host, either an IP address or a hostname may be used.

EOF
#MAYBE# Type "$APP_NAME <subcommand> --help" for detailed usage information.

    exit 0
}


# Product setup
checkRoot
checkEsm


ESM_CIMOM_DIRNAME=/var$ESM_BASE/sssm/cimom
ESM_CIMOM_SECRETS=$APP_HOME/esm_cimom_secrets

if [ ! -d $ESM_CIMOM_DIRNAME ]; then
    error 2 "Unable to execute -- $ESM_CIMOM_DIRNAME directory not found."
fi
cd $ESM_CIMOM_DIRNAME


# Utility Function
find_cimom()
{
    host=$1
    required=$2
    hostname=$(to_hostname $1)
    ipaddr=$(to_ipaddr $1)

    if [ -n "$ipaddr" ]; then
	if [ ! -d $ipaddr -a "$required" = "true" ]; then
	    error 2 "CIMOM ($host) directory not found: $ipaddr"
	fi
    else
	#NO# if [ "$required" = "true" ]; then
	#FYI: this failure is because the host is unknown in the /etc/hosts or
	# NIS maps.  It does not make sense to continue in this case, as the
	# host simply does not exist.
	error 2 "CIMOM host not found: $host"
    fi
}

# Subcommand Function
list_cimoms()
{
    IPS=$(ls -1d [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*)

    if [ -n "$IPS" ]; then
	for IP in $IPS ; do
	    echo "$IP\t$(to_hostname $IP)"
	done
    else
	#warning "no CIMOM"
	echo none
    fi
}

# Subcommand Function
dump_cimoms()
{
    find $ESM_CIMOM_DIRNAME -print
}

# Subcommand Function
remove_cimom()
{
    find_cimom $1 true
    #assert: CIMOM_HOSTNAME and CIMOM_IPADDR are setup and CIMOM exists.

    #TODO: ask user if they are sure?
    #TODO: accept a -f FORCE option to avoid interaction

    REMOVE_CIMOM="rm -fr $CIMOM_IPADDR"
    echo "Removing persistence...\c"
    echo " $REMOVE_CIMOM \c"
    $REMOVE_CIMOM
    echo "done."

    # remove the secrets as well
    setup=$($ESM_CIMOM_SECRETS getCredential --hostname $CIMOM_IPADDR)
    if [ -n "$setup" ]; then
	echo "Removing secrets...\c"
	echo " x\c"
	$ESM_CIMOM_SECRETS setCredential --hostname $CIMOM_IPADDR \
	    --credential ''
	echo "x \c"
	$ESM_CIMOM_SECRETS setPrincipal --hostname $CIMOM_IPADDR \
	    --principal ''
	echo "done."
    fi
}

# Utility Function
display_properties()
{
    title=$1
    file=$2
    shift 2

    echo "    $title:"
    if [ -f $file ]; then
	cat $file |sed -e 's/^/	/' |cut -c1-80
    else
	echo "\t$*"
    fi
}

# Subcommand Function
display_cimom()
{
    find_cimom $1 true
    #assert: CIMOM_HOSTNAME and CIMOM_IPADDR are setup and CIMOM exists.

    SLP_PROPERTIES=$CIMOM_IPADDR/cimom.properties
    CF_PROPERTIES=$CIMOM_IPADDR/config.properties

    echo "$CIMOM_IPADDR\t$CIMOM_HOSTNAME"
    display_properties "Service Properties" $SLP_PROPERTIES "NOT DISCOVERED"
    display_properties "Configuration Properties" $CF_PROPERTIES "NOT CONFIGURED"

    # indicate whether the user/password are setup or not
    echo "    CimomSecrets:"
    setup=$($ESM_CIMOM_SECRETS getCredential --hostname $CIMOM_IPADDR)
    if [ -n "$setup" ]; then
	echo "\tYES"
    else
	echo "\tNO"
    fi
}

# Subcommand Function
# prompt for user/password and delegate to the esm_cimom_secrets command
#
setup_cimom()
{
    find_cimom $1 false
    #assert: CIMOM_HOSTNAME and CIMOM_IPADDR are setup and CIMOM exists.

    #TODO: determine if the CIMOM exists already?  ask if change.
    #TODO: accept a -f FORCE option to avoid interaction

    setup_secret "principal" 0
    setup_secret "credential" 1

    # add a helpful touch-file that shows the hostname
    FILE="$ESM_CIMOM_DIRNAME/$CIMOM_IPADDR/$CIMOM_HOSTNAME"
    if [ ! -f $FILE ]; then
	DIR=$(dirname $FILE)
	mkdir -p $DIR
	touch $FILE
    fi
}

setup_secret()
{
    _secret=$1
    _passwd=$2
    _value=""

    # Catch certain signals to ensure that terminal echo is restored
    # in case we're interrupted during no-echo input (eg, passwords).
    #
    trap '/usr/bin/stty echo; exit 3' 2 15

    while [ -z "$_value" ]
    do
        [ $_passwd -eq 1 ] && /usr/bin/stty -echo
        echo
        echo "Please enter CIMOM $_secret: \c"

        read _value
        [ $_passwd -eq 1 ] && /usr/bin/stty echo && print

        if [ -n "$_value" ]; then
            if [ $_passwd -eq 1 ]; then
                print -n "Please re-enter the CIMOM $_secret for confirmation: "
                /usr/bin/stty -echo
                read -u0 _value2
                /usr/bin/stty echo && print
                if [ "$_value" != "$_value2" ] ; then
                    warn "The ${_secret}s do not match.  Please try again."
                    _value=""
                    _value2=""
                fi
            fi
        else
            warn "The $_secret must be a non-null value."
        fi
    done

    # delegate to the esm_cimom_secrets script to store the values
    if [ $_passwd -eq 1 ]; then
	$ESM_CIMOM_SECRETS setCredential --hostname $CIMOM_IPADDR \
		--principal $CIMOM_PRINCIPAL --credential $_value
	[ $? -ne 0 ] && error 2 "Failed to setup CIMOM credential"
    else
	$ESM_CIMOM_SECRETS setPrincipal --hostname $CIMOM_IPADDR \
		--principal $_value
	[ $? -ne 0 ] && error 2 "Failed to setup CIMOM principal"
	CIMOM_PRINCIPAL=$_value
    fi
}


# option processing
if [ $# -eq 0 ]; then
    error 1 "missing required subcommand"
fi

SUBCOMMAND=
case $1 in
--help | -h | -?) usage ;;
list | dump | remove | display | setup) SUBCOMMAND=$1 ;;
*) error 1 "unknown subcommand: $1" ;;
esac
shift 1

#TODO: consider using getopts here, then shift and process operand

if [ $# -gt 0 ]; then
    CIMOM_HOST=$(find_host $1)
    CIMOM_IPADDR=$(echo $CIMOM_HOST   |awk - '{print $1}')
    CIMOM_HOSTNAME=$(echo $CIMOM_HOST |awk - '{print $2}')
fi

case $SUBCOMMAND in
list)
    checkExtraArguments $*
    list_cimoms
    ;;

dump)
    checkExtraArguments $*
    dump_cimoms
    ;;

display)
    checkRequiredOperand $*
    display_cimom $1
    ;;

remove)
    checkRequiredOperand $*
    remove_cimom $1
    ;;

setup)
    checkRequiredOperand $*
    setup_cimom $1
    ;;
esac

