#!/bin/sh 
#
# Component Name: TivoliPlus
#
# $Source: /tivoli/development/src/2.0/plus/Link/src/cfgappicon,v $
#
# $Revision: 1.1 $
#
# Description:
#
# (C) COPYRIGHT Tivoli Systems, Inc. 1995-2000
# Unpublished Work
# All Rights Reserved
# Licensed Material - Property of Tivoli Systems, Inc.
#

#
# Create an object, define a method on it, and add the object 
# to the desktop
#
# The object is created on the local machine, not on the ALI
#

[ "$DEBUG" = true ] && set -xv

THIS_CMD=`echo $0 | tr "\\\\\\\\" "/"`; THIS_CMD=`basename $THIS_CMD`

USAGE=`wbindmsg LinkCmdCat 1 "Usage: %1\\$s sol-name app-name bitmap.xpm dialog.d \
		[method-name method-path]..." "$THIS_CMD" 2> /dev/null`
if [ ! "$USAGE" ]; then
	USAGE="Usage: $THIS_CMD sol-name app-name bitmap.xpm dialog.d \
		[method-name method-path]..."
fi
if [ $# -lt 6 ]; then
	echo $USAGE
	exit 1
fi

#
# Set up the search PATH to find the testmicon program
#
BASE=0.0.0
OSERV=`objcall $BASE getattr oserv`
INSTALL_DIR=`objcall $OSERV query install_dir`
#PATH=${PATH}:${INSTALL_DIR}/${INTERP}/ADE

SOL_NAME=$1
APPNAME=$2
BITMAP=$3
DIALOG=$4

#Determine the name of the top level PolicyRegion to use
if [ x"$TOP_PR_TO_USE" = x ] ; then
	TIVOLI_SOLUTIONS="TivoliPlus"
else
	TIVOLI_SOLUTIONS="$TOP_PR_TO_USE"
fi

MNOID=`objcall 0.0.0 get_host_location`
eval HOSTNAME=`idlattr -t -g $MNOID label string`
#HOSTNAME=`uname -n`
PRESNAME="$APPNAME"_"$HOSTNAME"_"$SOL_NAME"
OBJ_NAME="$APPNAME"@"$HOSTNAME"
# Create an object.  It doesn't matter how as long as 
# the object's # interface derives from \
# TMF_SysAdmin::CollectionMember.  I picked the
# TMF_imp_TSysAdmin::PolicyDrivenBase class at random.
#
#echo "Creating a new object"
CLO=`wlookup Library`
CO=`idlcall $CLO TMF_SysAdmin::Library::lookup_object \
		'"TMF_imp_TSysAdmin::PolicyDrivenBase" {0}'`

#See if we already have one of these objects, nested under the correct Solution
COLLECTIONGUI=`idlcall "$CLO" lookup_object '"imp_TMF_TGC::CollectionGUI" {0}'`
TOPOBJ=`idlcall "$COLLECTIONGUI" lookup_object '"'$TIVOLI_SOLUTIONS'" {0}'`
COLLOBJ=`idlcall "$TOPOBJ" lookup_object '"'$SOL_NAME'" {0}'`
if [ $? -eq 0 -a "$COLLOBJ" ]; then
	YOUR_OBJECT=`idlcall "$COLLOBJ" lookup_object '"'$OBJ_NAME'" {0}'`
	if [ $? -ne 0 ]; then
		YOUR_OBJECT=""
	fi
fi
if [ ! "$YOUR_OBJECT" ]; then
	YOUR_OBJECT=`idlcall $CO TMF_SysAdmin::InstanceManager::create_instance \
				OBJECT_NIL $MNOID \"$OBJ_NAME\"`
fi

shift;shift;shift;shift

#
# Create a new pres object
#
#echo "Adding an icon state"

initpres "$PRESNAME" -pres-name -create > /dev/null 2>&1
ICON=`wlookup -r Presentation "$PRESNAME"`


#
# Set the object's TMF_SysAdmin::CollectionMember::pres_object 
# attribute to the state name so that the object's icon will 
# use the bitmap and descriptor that we just installed.
#
idlattr -t -s $YOUR_OBJECT state string \"$PRESNAME\"
idlattr -t -s $YOUR_OBJECT pres_object Object $ICON

#
# Add a callback method to the application object
#

while [ $# -gt 0 ] 
do
	METHOD_NAME=$1
	METHOD_PATH=$2
	if [ "$METHOD_PATH" = "" ]
	then
	   MSG=`wbindmsg LinkCmdCat 2 "Missing path for method %1\\$s" "$METHOD_NAME" 2> /dev/null`
	   if [ ! "$MSG" ]; then
	      MSG="Missing path for method $METHOD_NAME"
	   fi
	   echo $MSG
	   break
	fi
#
# Define the method
#
	objcall $YOUR_OBJECT om_create $METHOD_NAME TRUE > /dev/null 2>&1
	objcall $YOUR_OBJECT om_define $METHOD_NAME native $METHOD_PATH > /dev/null 2>&1
	objcall $YOUR_OBJECT om_set_acl $METHOD_NAME any
	objcall $YOUR_OBJECT om_enable $METHOD_NAME TRUE TRUE
	objcall $YOUR_OBJECT om_set_id $METHOD_NAME \* ""
	shift;shift
done

wputicon -r "$PRESNAME" "$PRESNAME" $DIALOG $BITMAP

#
# Add your object to Solution Collection
#

gcoadd "$COLLOBJ" "$YOUR_OBJECT"


exit 0


