#!/usr/bin/sh
#
# Path:
# /opt/mx/lbin/new_tool
# Description:
# Script to add a new tool by creating a tool definition file and loading it

#
# Usage
#
Usage()
{
echo '
new_tool -t "<tool name>" [ -c | -x | -w ] "<string>" <options...>
    -t "<tool name>":         : add or modify tool name
    -c "<command string>"     : add command line ssa-command-tool
    -x "<x application path>" : add X app ssa-command-tool
    -w "<url path>"           : add web-launch-tool
    -f "<tool file path>"     : save tool file at path (defaul is a tmp file)
    -m "<menu path>"          : menu path (default is Tools|Command Line Tools)
    -n "<menu path web tools> : menu path (default is Tools|Custom Command)
    -p "<prompt string>"      : add if command string contains "%1" parameter
    -g "<description string>" : tool description (default is blank)
    -h "<comment string>"     : tool comment on how to use (default is blank)
    -e root                   : execute as "root" (default is as user)
    -q <target filter>        : "HPUX" or "LINUX" (default is all targets)
    -s <source_path>          : Path to source file to copy
    -d <dest_path>            : Path to destination file of copy (must be different)
    -u <windowname>           : force url tool into new window (no spaces)
    -j <accept-targets>       : accepts-targets=yes or no for web tools (default false)
    '
exit 2
}

#
# Initialize values
#
TOOL=
COMMAND=
URL=
TYPE=
MENU=
DEFAULTCLIMENU="@MENU_PATH_COMMAND_LINE_TOOLS"
DEFAULTWEBMENU="@CUSTOM_COMMANDS"
MENU_BLOCK=
PROMPT=
PROMPT_BLOCK=
DESCRIPTION="No description provided."
COMMENT="No comments provided."
ROOT=
EXECUTE_BLOCK=
FILTER=
FILTER_BLOCK=
SOURCE=
TARGET=
COPY_BLOCK=
WINDOW=
ACCEPTS=
ENCODING=
if [ -z "$MX_TOOL" ]
then
    MX_TOOL=/opt/mx/lbin/new_tool
fi
if [ -z "$MX_USER" ]
then
    MX_USER=$(whoami)
fi
TDEF=/var/opt/mx/data/localtool$$.xml
SAVEPATH=


#
# Debug options
#
Show()
{
echo TOOL=$TOOL
echo COMMAND=$COMMAND
echo URL=$URL
TYPE=$TYPE
echo MENU=$MENU
echo PROMPT=$PROMPT
echo DESCRIPTION=$DESCRIPTION
echo COMMENT=$COMMENT
echo ROOT=$ROOT
echo FILTER=$FILTER
echo SOURCE=$SOURCE
echo TARGET=$TARGET
echo WINDOW=$WINDOW
echo ACCEPTS=$ACCEPTS
echo ENCODING=$ENCODING
}



#
# Parse Options
#
while [ $# != 0 ]
do
    case $1 in
       -t) if [ "$TOOL" != "" ]
            then
                echo "ERROR: Tool name already set: $TOOL"
                Usage
            fi
            # Use echo to get rid of trailing spaces
            TOOL=$(echo $2); shift 2;;
       -c) if [ "$TYPE" != "" ]
            then
                echo "ERROR: Tool type already set: $TYPE"
                Usage
            fi
            TYPE=stdout; COMMAND=$2; shift 2;;
       -x) if [ "$TYPE" != "" ]
            then
                echo "ERROR: Tool type already set: $TYPE"
                Usage
            fi
            TYPE=x-window; COMMAND=$2; shift 2;;
       -w) if [ "$TYPE" != "" ]
            then
                echo "ERROR: Tool type already set: $TYPE"
                Usage
            fi
            TYPE=url; URL=$2; shift 2;;
       -f) if [ -n "$2" ]
           then
               # use echo to get rid of trailing spaces
               SAVEPATH=$(echo $2); TDEF=$(echo $2)
           fi
           shift 2;;
       -m) if [ -n "$2" ]
              then
                 MENU=$(echo $2 | sed s/:/\|/)
              else
                 MENU=$DEFAULTCLIMENU
            fi
            shift 2;;
       -n)  if [ -n "$2" ]
              then
                 MENU=$(echo $2 | sed s/:/\|/)
              else
                 MENU=$DEFAULTWEBMENU
            fi
            shift 2;;
       -p) PROMPT=$2; shift 2;;
       -g) if [ "$2" != "" ]
           then
                DESCRIPTION=$2
           fi
           shift 2;;
       -h) if [ "$2" != "" ]
           then
                COMMENT=$2
           fi
           shift 2;;
       -e) if [ "$2" != root -a "$2" != "" ]
            then
                echo "ERROR: Only acceptable execution user: root"
                Usage
            fi
            ROOT=$2; shift 2;;
       -q) if [ "$2" != HPUX -a "$2" != Linux ]
            then
                echo "ERROR: Only acceptable filters: HPUX or Linux"
                Usage
            fi
            FILTER=$2; shift 2;;
           # use echo to get rid of trailing white space
       -s) SOURCE=$(echo $2); shift 2;;
       -d) TARGET=$(echo $2); shift 2;;
       -u) WINDOW=$2; shift 2;;
       -j) if [ "$2" != yes -a "$2" != no ]
             then
                echo "ERROR: Only acceptable answers: yes or no"
                Usage
              fi
             if [ "$2" = yes ]
              then
                ACCEPTS=true
              else
                ACCEPTS=false
              fi
              shift 2;;
         *) echo "ERROR: illegal option: $1"
           Usage;
    esac
done

#
# Check for mandatory values
#
if [ "$TOOL" = "" ]
then
    echo "ERROR: Must specify a tool name with -a or -r"
    Usage
fi

if [ "$TYPE" = ""  ]
then
    echo "ERROR: Must specify -c, -x or -w"
    Usage
fi



##############################################################################
#
# Main: Create and load tool definition file
#
##############################################################################

if [ -n "$FILTER" ]
then
    FILTER_BLOCK="        <include-filter type=\"os\">
            <node-filter name=\"OSName\" operator=\"eq\" value=\"$FILTER\" />
        </include-filter>"
fi

if [ "$ROOT" = root ]
then
    EXECUTE_BLOCK="        <execute-as-user>root</execute-as-user>"
fi

if [ -n "$MENU" ]
then
    MENU_BLOCK="        <attribute name=\"menu-path\">$MENU</attribute>"
fi

if [ -n "$PROMPT" ]
then
    PROMPT_BLOCK="        <parameter index=\"1\" prompt=\"$PROMPT\" required=\"false\" />"
fi

if [ -n "$ACCEPTS" ]
then
    ACCEPTS_BLOCK="accepts-targets=\"$ACCEPTS\""
fi

if [ -n "$WINDOW" ]
NO_SPACE_WINDOW=`echo $WINDOW | tr -d ' '`
then
    WINDOW_BLOCK="        <attribute name=\"target-frame\">$NO_SPACE_WINDOW</attribute>"
fi


ENCODING="ISO-8859-1"

if [ ${LANG%_*} = "ja" ]
then
    ENCODING="EUC-JP"
fi

if [ $LANG = "ja_JP.SJIS" ] || [ $LANG = "ja_JP.PCK" ]
then
    ENCODING="Shift_JIS"
fi

if [ ${LANG#*.} = "iso885915" ] || [ ${LANG#*.} = "iso885915@euro" ]
then
    ENCODING="ISO-8859-15"
fi

if [ ${LANG#*.} = "utf8" ] || [ ${LANG#*.} = "UTF-8" ]
then
    ENCODING="UTF-8"
fi


if [ -n "$SOURCE" ]
then
    if [ -n "$TARGET" ]
    then
        COPY_BLOCK="                    <copy-block>
                    <source>$SOURCE</source>
                    <destination>$TARGET</destination>
                    </copy-block>"
    else
        echo "ERROR: Must specify both source and destination to copy."
    fi
fi

if [ "$TYPE" = stdout -o $TYPE = x-window ]
then

echo "<?xml version=\"1.0\" encoding=\"$ENCODING\"?>
<!-- File: $TDEF -->
<!-- Created on: $(date) -->
<!-- By Tool: $MX_TOOL -->
<!-- By User: $MX_USER -->
<tool-list>
    <ssa-command-tool name=\"$TOOL\" revision=\"1.0\" visible=\"true\">
        <description><![CDATA[
            $DESCRIPTION
        ]]></description>
        <comment><![CDATA[
            $COMMENT
        ]]></comment>
$EXECUTE_BLOCK
        <include-filter type=\"protocol\">
            <node-filter name=\"SSH\" operator=\"ge\" value=\"1.0\" />
        </include-filter>
$FILTER_BLOCK
        <ssa-block>
            <command command-type=\"$TYPE\" log=\"false\"><![CDATA[
                $COMMAND
            ]]></command>
$COPY_BLOCK
$PROMPT_BLOCK
        </ssa-block>
$MENU_BLOCK
    </ssa-command-tool>
</tool-list>
" >$TDEF

else

echo "<?xml version=\"1.0\" encoding=\"$ENCODING\"?>
<!-- File: $TDEF -->
<!-- Created on: $(date) -->
<!-- By Tool: $MX_TOOL -->
<!-- By User: $MX_USER -->
<tool-list>
    <web-launch-tool name=\"$TOOL\" revision=\"1.0\" visible=\"true\">
        <description><![CDATA[
            $DESCRIPTION
        ]]></description>
        <comment><![CDATA[
            $COMMENT
        ]]></comment>
$EXECUTE_BLOCK
        <include-filter type=\"protocol\">
            <node-filter name=\"SSH\" operator=\"ge\" value=\"1.0\" />
        </include-filter>
$FILTER_BLOCK
        <web-block $ACCEPTS_BLOCK>
            <main-url><![CDATA[
                $URL
            ]]></main-url>
        </web-block>
$WINDOW_BLOCK
$MENU_BLOCK
    </web-launch-tool>
</tool-list>
" > $TDEF

fi

echo Creating tool definition file...
echo
cat $TDEF
echo
echo
echo Removing any previous definition of \"$TOOL\"...
/opt/mx/bin/mxtool -r -t "$TOOL" 2>/dev/null
echo Tool definition file saved as: $TDEF
echo Adding \"$TOOL\" to HP Systems Insight Manager...
/opt/mx/bin/mxtool -af $TDEF

if [ -n "$SAVEPATH" ]
then
    echo To modify this tool, you may edit the saved tool definition file,
    echo then reload with \"mxtool -mf $TDEF -x force\".
    echo Alternatively, you can re-run \"$MX_TOOL\".
else
    echo To modify this tool, you can re-run \"$MX_TOOL\".
    rm -f $TDEF
fi
