#!/bin/sh
#
# Component Name: TivoliPlus
#
# $Source: /tivoli/development/src/2.0/plus/Link/src/wcrtsprfl,v $
#
# $Revision: 1.0 $
#
# 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 link views of a file package 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`
USAGE=`wbindmsg LinkCmdCat 17 "Usage: %1\\$s [-s state] plus-collection-name profile-type profile-name profile-manager-name link-name [subscriber ...]" "$THIS_CMD" 2> /dev/null`
if [ ! "$USAGE" ]; then
	USAGE="Usage: $THIS_CMD [-s state] plus-collection-name profile-type profile-name profile-manager-name link-name [subscriber ...]"
fi


STATE=""
# Hmmm, it seems that getopts doesn't set OPTARG in bash, so we'll do this by hand
#while getopts "s" opt; do
#  case $opt in
#      s) STATE=$OPTARG;;
#     *?) echo "$USAGE"; exit 1;;
#  esac
#done
#shift `expr $OPTIND - 1`

if [ "$1" = "-s" ]; then
  shift
  STATE=$1
  shift
fi

ARGCOUNT=5
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 [ "$APP_PR_NAME" ]; then
  PRNAME="$APP_PR_NAME"
else
  PRNAME="$1"
fi
SOLNAME=$1
shift
TYPE=$1
shift
PRFNAME=$1
shift
PMNAME=$1
shift
LNNAME=$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 [ $? -ne 0 ] ; then
  if [ ! "$WINSTRUCTED" = "true" ]; then
	wcrtprfmgr @PolicyRegion:"$PRNAME#$IRONAME" "$PMNAME" > /dev/null
	if [ $? -ne 0 ]; then
	  exit 1
	fi
  else
	MSG=`wbindmsg LinkCmdCat 18 "winstruct should have already created profile manager %1\\$s" "$PMNAME" 2> /dev/null`
	if [ ! "$MSG" ]; then
		MSG="winstruct should have already created profile manager $PMNAME!"
	fi
	echo $MSG
	exit 1
  fi
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

if [ "$TYPE" != "OmniFilePackage" ] ; then
  PROFILE=`wlookup -r "$TYPE" "$PRFNAME#$IRONAME" 2> /dev/null`
  if [ $? -ne 0 ]; then
	wcrtprf @ProfileManager:"$PMNAME#$IRONAME" "$TYPE" "$PRFNAME" > /dev/null
	if [ $? -ne 0 ]; then
	  exit 1
	fi 
  fi
else
	wcrtcoll "$LNNAME" "TS Hidden Collections"
fi

#Add Subscribers

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

#Add to the proper solutions collection

wcrtslink "$SOLNAME" "$TYPE" "$PRFNAME" "$PMNAME" "$LNNAME" "$STATE"

exit 0
