#!/bin/sh
#
# Component Name: TivoliPlus
#
# $Source: /tivoli/development/src/2.0/plus/Link/src/wcrtsprfmgr,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 will create an instance of a profile manager with the specified
#state.  If no state is specified, the TSPM state is used.  If state is
#"dataless", then the Plus version of a dataless profile manager is used.
#The script takes two arguments the  PolicyRegion to create the ProfileManager
#in and the name of the profile manager

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

THIS_CMD=`echo $0 | tr "\\\\\\\\" "/"`; THIS_CMD=`basename $THIS_CMD`
USAGE=`wbindmsg LinkCmdCat 19 "Usage: %1\\$s region-name profile-manager-name [ { dataless | state [ dataless-boolean ] } ]" "$THIS_CMD" 2> /dev/null`
if [ ! "$USAGE" ]; then
	USAGE="Usage: $THIS_CMD region-name profile-manager-name [ { dataless | state [ dataless-boolean ] } ]"
fi

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

PRNAME=$1
SOLNAME=$1
PMNAME=$2
STATE=$3
DATALESS=$4

if [ -z "$STATE" ]; then
	STATE=TSPM
elif [ "$STATE" = dataless ]; then
	STATE=TSPMD
	DATALESS=true
fi

OSERV=`objcall 0.0.0 get_oserv`
export OSERV
INST_DIR=`objcall $OSERV query install_dir`
INST_DIR=`echo $INST_DIR | tr "\\\\\\\\" "/"`
export INST_DIR

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

if [ "$APP_PR_NAME" ]; then
  PRNAME="$APP_PR_NAME"
fi

PRFMGR=`wlookup -r ProfileManager "$PMNAME#$IRONAME" 2> /dev/null`
if [ $? -ne 0 ] ; then
	wcrtprfmgr @PolicyRegion:"$PRNAME#$IRONAME" "$PMNAME" > /dev/null
	if [ $? -ne 0 ]; then
		exit 1
	fi
	PRFMGR=`wlookup -r ProfileManager "$PMNAME#$IRONAME"`
fi

PMDSL=$INST_DIR/generic_unix/TME/PLUS/LINK/PM_icon.d
if [ "$DATALESS" = true -a "$STATE" = TSPMD ]; then
  PMXPM=$INST_DIR/generic_unix/TME/PLUS/LINK/PM_icon_dataless.xpm
else
  PMXPM=$INST_DIR/generic_unix/TME/PLUS/LINK/PM_icon.xpm
fi

if wlsicon -r ProfileManager | grep "^$STATE$" > /dev/null; then
  :
else
  wputicon -r ProfileManager $STATE $PMDSL $PMXPM
fi


if [ "$DATALESS" = true ]; then
	wsetpm -d  "@ProfileManager:$PMNAME#$IRONAME"
  idlcall "$PRFMGR" _set_flags 1
fi

idlcall "$PRFMGR"  _set_state '"'$STATE'"'

#Add to the proper solutions collection

LIBRARY=`wlookup Library`

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

gcoadd "$COLLOBJ" "$PRFMGR"
idlcall "$COLLOBJ" refresh_collection

exit 0
