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

#This is a script which will configure a Link object for a Task and a
#Job

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

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

#
#	New modules set a variable called MODULE_VERSION to 2.0.  This allows
#	this script to check for the right number of arguments.
#
if [ "$MODULE_VERSION" = "2.0" \
     -o "$MODULE_VERSION" = "3.0" ]; then
	ARGCOUNT=5
	USAGE=`wbindmsg LinkCmdCat 8 "Usage: %1\\$s collection-name task-library-name task-name link-name job-name [state-name]" "$THIS_CMD" 2> /dev/null`
	if [ ! "$USAGE" ]; then
		USAGE="Usage: $THIS_CMD collection-name task-library-name task-name link-name job-name [state-name]"
	fi

else
	ARGCOUNT=4
	USAGE=`wbindmsg LinkCmdCat 9 "Usage: %1\\$s collection-name task-library-name task-name job-name [state-name]" "$THIS_CMD" 2> /dev/null`
	if [ ! "$USAGE" ]; then
		USAGE="Usage: $THIS_CMD collection-name task-library-name task-name job-name [state-name]"
	fi

fi

if [ $# -lt $ARGCOUNT ] ; then 
	echo $USAGE
	exit 1
fi

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

PRNAME=$1
COLLNAME=$1
shift
TLNAME=$1
shift
TASKNAME=$1
if [ $ARGCOUNT -gt 4 ] ; then
	shift
fi
LINKNAME=$1
shift;
JOBNAME=$1
shift;

#See if the state name was passed as a fifth parameter

if [ $# -gt 0 ] ; then
	STATE_NAME=$1
fi

#Get the region name to avoid collisions
IRO=`wlookup InterRegion`
IRONAME=`idlattr -t -g $IRO name string`
IRONAME=`eval echo $IRONAME`

TL=`wlookup -r TaskLibrary "$TLNAME#$IRONAME" 2> /dev/null`
FAUX_JOB=`wlookup -r Job "$JOBNAME#$IRONAME"`
if [ $? -ne 0 ] ; then
	#Something is amiss, stop here
		exit 1
fi
FAUX_TASK=$TL"$TASKNAME"
#Check for unrecoverables
if [ x"$FAUX_JOB" = x ] ; then
	#Something is amiss, stop here
		exit 1
fi
if [ x"$TL" = x ] ; then
	#Something is amiss, stop here
		exit 1
fi



#Add to the proper solutions collection

LIBRARY=`wlookup Library`
COLLECTIONGUI=`idlcall $LIBRARY lookup_object '"imp_TMF_TGC::CollectionGUI" {0}'`
COLLOBJ=`idlcall $COLLECTIONGUI lookup_object '"'$COLLNAME'" {0}'`

LINK_CO=`wlookup -r Classes SolutionsLink`

LINK=`idlcall $COLLOBJ lookup_object '"'$LINKNAME'" {0}'`
if [ $? -ne 0 ] ; then
	LINK=`idlcall $LINK_CO create_instance 'OBJECT_NIL OBJECT_NIL "'$LINKNAME'"'`
fi

idlattr -t -s $LINK faux_job Object "$FAUX_JOB"
#This is a hack to work around a platform bug
#The parser for the idl stuff eats spaces in object references which
#screws up pseudo objects with spaces in the names.
#So we'll use a string here until it's fixes
idlattr -t -s $LINK faux_task_string string '"'$FAUX_TASK'"'

if [ x"$STATE_NAME" != x ] ; then
	STATE=$STATE_NAME
else
	STATE=JLINK
fi

idlcall $LINK  _set_state '"'$STATE'"' 
gcoadd $COLLOBJ $LINK

exit 0
