#! /usr/bin/ksh
#
# @(#)u4ft_setMBDLocation.sh	1.9	98/09/21 SMI
#
# Copyright (c) 1998, by Sun Microsystems, Inc.
# All rights reserved.
#
# u4ft_setMBDLocation name number location lastlocation
#
# Purpose:      When a FRU location is set to a motherboard, set up the 
#		relative constituent on the motherboard to point to this FRU.
# Result:	When a FRU location is set to NULL, delete the associated 
#		constituent value on the original motherboard. When a FRU 
# 		location is set to a motherboard and its last value was not 
#		NULL, delete the associated constituent value on the original
#		motherboard, prior to setting up a new constituent value on the
#		new motherboard.
#
# Mandatory Parameters
# name                 - class name of device
# number               - instance number of device
# location             - the location of the FRU that has just been set
# lastlocation         - the last location of the FRU
#
# Error Codes:
# Return 0 on success, non-zero otherwise
#

# Validate that there are 4 parameters to the function
if [[ $# -ne 4 ]]
then
        cmsdebug "u4ft_setMBDLocation: wrong no. of args $1 $2 $3 $4"
	exit 1
fi
name="$1"
number="$2"
location="$3"
lastlocation="$4"

# If the location is set from NULL to NULL, just return
if [[ "$location" = "NULL" && "$lastlocation" = "NULL" ]]
then
	cmsdebug "Location set from NULL to NULL"
	exit 0
fi

SWAP_LOC=""
if [[ "$location" = "NULL" ]]
then
	# If Location has been set to NULL need to delete constituent
	MYLOCATION="$lastlocation"
	PARENTARG="null 0"
else
	# Test if Location has been swapped from one motherboard to another
	if [[ "$lastlocation" != "NULL" && "$lastlocation" != "$location" ]]
	then
		SWAP_LOC="true"
	fi
	MYLOCATION="$location"
	PARENTARG="$name $number"
fi

# Work out which motherboard the location resides on
MBD="${MYLOCATION%%-*}-MBD"

# If a swap has taken place, delete constituent from last motherboard
if [[ -n "$SWAP_LOC" ]]
then
	OTHER_MBD="${lastlocation%%-*}-MBD"
	cmsreport $OTHER_MBD 0 ${location##*-} "null 0"
	cmslog 1 "$name $number has been moved from \
		location $lastlocation to $location"
elif [[ "$PARENTARG" = "null 0" ]]
then
	cmslog 1 "$name $number has been removed from location $lastlocation"
	cmsreport $name $number _show_by_default no
else
	cmslog 1 "$name $number has been added to location $location"
	cmsreport $name $number _show_by_default yes
	
fi

# Set up constituent of motherboard to be either null 0 or name of 
# FRU according to location value.
cmsreport $MBD 0 ${MYLOCATION##*-} "$PARENTARG"
echo $MBD 0
exit 0
