#!/bin/sh
#
# Component Name: TivoliPlus
#
# $Source: /tivoli/development/src/2.0/plus/Link/src/wcrtsprf,v $
#
# $Revision: 1.4 $
#
# 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 create a profile manager (if required)
#and a profile inside a
#solution policy region and add them to the proper collection

[ "$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 15 "Usage: %1\\$s state policy-region-name profile-type profile-name profile-manager-name [subscriber ...]" "$THIS_CMD" 2> /dev/null`
	if [ ! "$USAGE" ]; then
		USAGE="Usage: $THIS_CMD state policy-region-name profile-type profile-name profile-manager-name [subscriber ...]"
	fi
else
	ARGCOUNT=4
	USAGE=`wbindmsg LinkCmdCat 16 "Usage: %1\\$s policy-region-name profile-type profile-name profile-manager-name [subscriber ...]" "$THIS_CMD" 2> /dev/null`
	if [ ! "$USAGE" ]; then
		USAGE="Usage: $THIS_CMD policy-region-name profile-type profile-name profile-manager-name [subscriber ...]"
	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

if [ $ARGCOUNT -gt 4 ]; then
	STATE=$1
	shift
fi

PRNAME=$1
SOLNAME=$1
shift
TYPE=$1
shift
PRFNAME=$1
shift
PMNAME=$1
shift;

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


#Check to see if profile manager exists
PM_CLASS=`wlookup -r Classes ProfileManager`
PRFMGR=`idlcall $PM_CLASS lookup_object '"'$PMNAME'" {0}'`

#if [ x"$PRFMGR" = x ] ; then
if [ $? -ne 0 ] ; then
	wcrtprfmgr @PolicyRegion:"$PRNAME#$IRONAME" "$PMNAME" > /dev/null
elif wlookup -r ProfileManager "$PMNAME#$IRONAME" > /dev/null 2>& 1 ; then
	:
else
	#Make the existing one visible via wlookup
	wregister -r ProfileManager "$PMNAME" "$PRFMGR"
fi

PROFILE=`wlookup -r "$TYPE" "$PRFNAME#$IRONAME" 2> /dev/null`

if [ "$STATE" = "dataless" ]; then
	wsetpm -d @ProfileManager:"$PMNAME#$IRONAME"
fi

if [ "$TYPE" != "OmniFilePackage" ] ; then
if [ x"$PROFILE" = x ] ; then
	wcrtprf @ProfileManager:"$PMNAME#$IRONAME" "$TYPE" "$PRFNAME" > /dev/null
fi
else
	wcrtcoll "$PRFNAME" "TS Hidden Collections"
fi

#Add Subscribers

while [ "$1" ]
do
	wsub @ProfileManager:"$PMNAME#$IRONAME" "$1#$IRONAME"
	shift
done

#Add to the proper solutions collection

wcrtlink "$SOLNAME" "$TYPE" "$PRFNAME" "$PRFNAME" "$STATE"

exit 0
