#!/bin/sh
#
# Component Name: TivoliPlus
#
# $Source: /tivoli/development/src/2.0/plus/Link/src/wcrtspr,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 script will create a solutions policy region with the name passed in
#If the TivoliPlus Policy Region doesn't exist, it will be created

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

THIS_CMD=`echo $0 | tr "\\\\\\\\" "/"`; THIS_CMD=`basename $THIS_CMD`
USAGE=`wbindmsg LinkCmdCat 14 "Usage: %1\\$s region-name" "$THIS_CMD" 2> /dev/null`
if [ ! "$USAGE" ]; then
	USAGE="Usage: $THIS_CMD region-name"
fi

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

NAME=$1

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

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

#Check to see which applications are installed
SENTRY_OID=`wlookup -r Classes SentryProfile 2> /dev/null`
if [ x"$SENTRY_OID" = x ] ; then
	SENTRY_INSTALLED=0
else
	SENTRY_INSTALLED=1
fi

COURIER_OID=`wlookup -r Classes FilePackage 2> /dev/null`
if [ x"$COURIER_OID" = x ] ; then
	COURIER_INSTALLED=0
else
	COURIER_INSTALLED=1
fi

TEC_OID=`wlookup -r Classes EventServer 2> /dev/null`
if [ x"$TEC_OID" = x ] ; then
	TEC_INSTALLED=0
else
	TEC_INSTALLED=1
fi

TAPM_OID=`wlookup -r Classes MarProfile 2> /dev/null`
if [ x"$TAPM_OID" = x ] ; then
	TAPM_INSTALLED=0
else
	TAPM_INSTALLED=1
fi

INVENTORY_OID=`wlookup -r Classes InventoryProfile 2> /dev/null`
if [ x"$INVENTORY_OID" = x ] ; then
	INVENTORY_INSTALLED=0
else
	INVENTORY_INSTALLED=1
fi

ACP_OID=`wlookup -r Classes ACP 2> /dev/null`
if [ x"$ACP_OID" = x ] ; then
	ACP_INSTALLED=0
else
	ACP_INSTALLED=1
fi

TS_REGION=`wlookup -r PolicyRegion "$TIVOLI_SOLUTIONS#$IRONAME" 2> /dev/null`

if [ x"$TS_REGION" = x ] ; then
	wcrtpr "$TIVOLI_SOLUTIONS"
fi

SOL_REGION=`wlookup -r PolicyRegion "$NAME#$IRONAME" 2> /dev/null`

if [ x"$SOL_REGION" = x ] ; then
	# temp name will be unique in inter-region
	TEMP_NAME="${NAME}_${IRONAME}" #temp name will be unique in inter-region
	wcrtpr -s @PolicyRegion:"$TIVOLI_SOLUTIONS#$IRONAME" "$TEMP_NAME"

	# now rename it to what we really wanted
	NEW_PR_OBJ=`wlookup -r PolicyRegion "$TEMP_NAME"`
	idlcall $NEW_PR_OBJ _set_label '"'$NAME'" {0}'
fi

#We have to determine which resources are already managed by the region
FILEPACKAGE=0
INDCOLL=0
SENTRYPROFILE=0
PROFILEMANAGER=0
TASKLIBRARY=0
SENTRYPROXY=0
MARPROFILE=0
INVENTORYPROFILE=0
ACPPROFILE=0
for RESOURCE in `wgetpr @PolicyRegion:"$NAME#$IRONAME"`
do
	case $RESOURCE in
		FilePackage)
			FILEPACKAGE=1
		;;
		IndicatorCollection)
			INDCOLL=1
		;;
		SentryProfile)
			SENTRYPROFILE=1
		;;
		ProfileManager)
			PROFILEMANAGER=1
		;;
		TaskLibrary)
			TASKLIBRARY=1
		;;
		SentryProxy)
			SENTRYPROXY=1
		;;					
		MarProfile)
			MARPROFILE=1
		;;					
		InventoryProfile)
			INVENTORYPROFILE=1
		;;					
		ACP)
			ACPPROFILE=1
		;;					
	esac
done

#Now we need to add the appropriate resources to the region

if [ $TASKLIBRARY -eq 0 ] ;then
	wsetpr TaskLibrary @PolicyRegion:"$NAME#$IRONAME"
fi

if [ $PROFILEMANAGER -eq 0 ] ;then
	wsetpr ProfileManager @PolicyRegion:"$NAME#$IRONAME"
fi

if [ $SENTRY_INSTALLED -eq 1 ] ; then
	if [ $INDCOLL -eq 0 ] ; then
		wsetpr IndicatorCollection @PolicyRegion:"$NAME#$IRONAME"
	fi
	if [ $SENTRYPROFILE -eq 0 ] ; then
		wsetpr SentryProfile @PolicyRegion:"$NAME#$IRONAME"
	fi
	if [ $SENTRYPROXY -eq 0 ] ; then
		wsetpr SentryProxy @PolicyRegion:"$NAME#$IRONAME"
	fi
fi

if [ $COURIER_INSTALLED -eq 1 ] ; then
	if [ $FILEPACKAGE -eq 0 ] ; then
		wsetpr FilePackage @PolicyRegion:"$NAME#$IRONAME"
	fi
fi

if [ $TAPM_INSTALLED -eq 1 ] ; then
	if [ $MARPROFILE -eq 0 ] ; then
		wsetpr MarProfile @PolicyRegion:"$NAME#$IRONAME"
	fi
fi

if [ $INVENTORY_INSTALLED -eq 1 ] ; then
	if [ $INVENTORYPROFILE -eq 0 ] ; then
		wsetpr InventoryProfile @PolicyRegion:"$NAME#$IRONAME"
	fi
fi

if [ $ACP_INSTALLED -eq 1 ] ; then
	if [ $ACPPROFILE -eq 0 ] ; then
		wsetpr ACP @PolicyRegion:"$NAME#$IRONAME"
	fi
fi

exit 0

