#!/bin/ksh

# Copyright 03/05/02 Sun Microsystems, Inc. All Rights Reserved.
#pragma ident  "@(#)es-tool	1.10 02/03/05 Sun Microsystems"

#-------------------------------------------------------------------------------
# Description
#-------------------------------------------------------------------------------
# run this with ? as the only argument to read the description

#-------------------------------------------------------------------------------
# Constants
#-------------------------------------------------------------------------------
    BASEDIR=`/usr/bin/pkgparam SUNWescom BASEDIR`
    CONFIGFILE=/var/opt/SUNWsymon/cfg/console-tools.cfg
    OPTTOOLFILE=${BASEDIR}/SUNWsymon/classes/base/console/cfg/tools-extension-j.x
    TOOLFILE=/var/opt/SUNWsymon/cfg/tools-extension-j.x

    ID=`/usr/bin/id`
    USER=`/usr/bin/expr "${ID}" : 'uid=\([^(]*\).*'`

    PROGRAM=$0

#-------------------------------------------------------------------------------
# Functions
#-------------------------------------------------------------------------------

findline()
{
    CMD_GREP=/usr/bin/egrep
    pattern=$1
    file=$2
    found=`$CMD_GREP -n -e "$pattern" $file`
    if [ -z "$found" ] ; then
        return 1
    else
        return 0
    fi
}

usage()
{
    print 'Usage:' $PROGRAM [config_file]
    print
    print 'This script reads in the configuration file, if specified, or '
    print $CONFIGFILE', and installs the tool'
    print 'configuration with the SyMON server.'
    print 'The configuration file contains one or more lines of:'
    print 
    print 'menu_label,class [arguments]'
    print ''
    print 'where menu_label is what appears in the Tools menu'
    print '      class is the name of the Java class to create'
    print '      arguments are optional user defined arguments to be passed to the Java class'
    print 
    print 'There may be quotes in the arguments.'
    print 'Lines beginning with # are considered as comments.'
    print 
    print 'For example:'
    print
    print '# example'
    print 'Example GUI,exampleApp.ExampleGUI'
    print 'Telnet,com.sun.symon.base.client.console.SMSystemCommand "/usr/openwin/bin/xterm -e telnet $host"'
    print 'FTP,com.sun.symon.base.client.console.SMSystemCommand "/usr/openwin/bin/xterm -e ftp $host"'

    exit 1
}

# $1 item number 
# $2 menu 
# $3 args
genItem()
{
    typeset item
    typeset menu
    typeset args

    item=$1
    menu=$2
    args=$3

    print 'Tool'$item' = { [ use MENU-ITEM ]'
    print 'res:label =' $menu

    print '    awx:services = action'
    print '    actionService = Launcher'
    print '    actionCommand = launch' $args
    print }
    print
}

printHeader()
{
    print '# YOU MUST NOT HAND EDIT THIS FILE.'
    print '# THIS FILE IS GENERATED BY' $PROGRAM 'script'
    print
    print 'Launcher = { [ use BEAN-CONNECTOR ]'
    print '   awx:bean = com.sun.symon.base.client.console.SMToolLauncher'
    print }
    print
}

readFile()
{
    item=1
    while read line
    do
	firstChar=`print $line | /usr/bin/cut -c1`
	if [[ $firstChar != '#' && ${#line} -ne 0 ]] ; then
	    menu=`print $line | /usr/bin/cut -d, -f1`
	    args=`print $line | /usr/bin/cut -d, -f2`
	    # Add menu item if it is not already existing
	    findline $menu $1
	    if [ $? -eq 1 ] ; then
		genItem $item "$menu" "$args"
	    fi
	    let item=item+1
	fi
    done
}

#-------------------------------------------------------------------------------
# Main
#-------------------------------------------------------------------------------

if [[ $# -eq 1 && $1 == "?" ]]; then
    usage
fi

if [[ "${USER}" != "0" ]]; then
    print 'Must be root to run '$PROGRAM
    exit 1
fi

if [[ $# -gt 1 ]]; then
   usage
fi

if [[ $# -eq 0 ]]; then
    infile=$CONFIGFILE
else
    infile=$1
fi

if [[ ! -r $infile  ]]; then
    eval echo `/usr/bin/gettext 'ERROR : Cannot read input file: $infile'`
    exit 2
fi

print Using $infile
if [[ -f "$OPTTOOLFILE" && ! -f "$TOOLFILE" ]]; then
   /bin/cp $OPTTOOLFILE $TOOLFILE
fi

if [[ -f $TOOLFILE ]]; then
   readFile $TOOLFILE < $infile >>  $TOOLFILE
else
   printHeader > $TOOLFILE 
   readFile < $infile >>  $TOOLFILE
fi
print $TOOLFILE updated.
exit 0
