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


#This script takes four arguments
#1. The name of the Collection to create the link in
#2. The type of link to create "SentryProfile", "FilePackage", "MarProfile" or "InventoryProfile"
#3. The name of the element to link to
#4. The name of the link

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

THIS_CMD=`echo $0 | tr "\\\\\\\\" "/"`; THIS_CMD=`basename $THIS_CMD`
USAGE=`wbindmsg LinkCmdCat 10 "Usage: %1\\$s collection-name \"[SentryProfile|FilePackage]\" element-name link-name [state]" "$THIS_CMD" 2> /dev/null`
if [ ! "$USAGE" ]; then
	USAGE="Usage: $THIS_CMD collection-name \"[SentryProfile|FilePackage|MarProfile|InventoryProfile]\" element-name link-name [state]"
fi

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

COLLNAME=$1
TYPE=$2
ELEMENT=$3
LINKNAME=$4
STATE=$5

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

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


if [ "$TYPE" != "OmniFilePackage" ] ; then
	FAUX_PROFILE=`wlookup -r $TYPE "$ELEMENT#$IRONAME"`
	if [ $? -ne 0 ] ;then
		#Something is amiss, stop here
		exit 1
	fi
	FAUX_PROFILE_MANAGER=`idlattr -t -g $FAUX_PROFILE profile_organizer Object`
else
	FAUX_PROFILE=`idlcall $COLLECTIONGUI lookup_object '"'$ELEMENT'" {0}'`
	if [ $? -ne 0 ] ; then
		#Something is amiss, stop here
		exit 1
	fi
	FAUX_PROFILE_MANAGER=`wlookup -r ProfileManager "$ELEMENT#$IRONAME"`
	if [ $? -ne 0 ] ; then
		#Something is amiss, stop here
		exit 1
	fi
fi

#Check for unrecoverables
if [ x"$FAUX_PROFILE" = x ] ; then
	#Something is amiss, stop here
		exit 1
fi
if [ x"$FAUX_PROFILE_MANAGER" = x ] ; then
	#Something is amiss, stop here
		exit 1
fi



LINK_CO=`wlookup -r Classes SolutionsLink`

LINK=`idlcall $LINK_CO 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_profile Object $FAUX_PROFILE
idlattr -t -s $LINK faux_profile_manager Object $FAUX_PROFILE_MANAGER

if [ "$STATE" = "dataless" ]; then
	case $TYPE in
		FilePackage)
			STATE=CLINKLCF;;
		OmniFilePackage)
			STATE=NCLINKLCF;;
		SentryProfile)
			STATE=SLINKLCF;;
		MarProfile)
			STATE=MLINKLCF;;
		*)
			STATE=normal;;
	esac
elif [ -z "$STATE" ]; then
	case $TYPE in
		FilePackage)
			STATE=CLINK;;
		OmniFilePackage)
			STATE=NCLINK;;
		SentryProfile)
			STATE=SLINK;;
		MarProfile)
			STATE=MLINK;;
		InventoryProfile)
			STATE=ILINK;;
		*)
			STATE=normal;;
	esac
fi

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


exit 0
