#!/bin/sh
#
#    Copyright (c) 1996-2001 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    script to setup IP address of the host
#    in the absence of fabos
#
#    This is available only for factory
#

#check number of arguments. if zero, print usage and return
if [ "$#" = 0 ]; then
	echo "Usage host_setup ip_address host_name"
	exit
fi

# check the Hardware Model
# Hardware Model equals 10, it is ulysses. so check CPID.
# Hardware Model equals 12, it is terminator. so sw0 entry in /etc/hosts.
# Check the CPID if it is ulysses.
# CPID equals 0 if cp is in slot 5. so modify cp0 entry in /etc/hosts.
# CPID equals 1 if cp is in slot 6. so modify cp1 entry in /etc/hosts.

HWMODEL=`sin | grep "Platform" | sed -e"s/[    ][       ]*//g" | cut -d, -f1 | cut -d: -f2 2> /dev/null`
CPID=`sin | grep "Processor No" | sed -e"s/[  ][       ]*/ /g" | cut -d: -f2 2> /dev/null`

if [ "$HWMODEL" = "SWBD10" ]; then
        echo Model is Ulysses
	if [ "$CPID" -eq 0 ]; then
		echo CPID is zero
		OLDIP=`grep "#cp0" /etc/hosts | sed -e"s/[   ][       ]*/ /g" | cut -d' ' -f1`
		OLDHOSTNAME=`grep "#cp0" /etc/hosts | sed -e"s/[   ][       ]*/ /g" | cut -d' ' -f2`
	elif [ "$CPID" -eq 1 ]; then
		OLDIP=`grep "#cp1" /etc/hosts | sed -e"s/[   ][       ]*/ /g" | cut -d' ' -f1`
		OLDHOSTNAME=`grep "#cp1" /etc/hosts | sed -e"s/[   ][       ]*/ /g" | cut -d' ' -f2`
	fi

elif [ "$HWMODEL" = "SWBD12" ]; then
        echo Model is Terminator
	OLDIP=`grep "#sw0" /etc/hosts | sed -e"s/[   ][       ]*/ /g" | cut -d' ' -f1`
	OLDHOSTNAME=`grep "#sw0" /etc/hosts | sed -e"s/[   ][       ]*/ /g" | cut -d' ' -f2`
fi

sed -e"s/$OLDIP/$1/g" /etc/hosts > /etc/hosts.1
sed -e"s/[ ][ ]*$OLDHOSTNAME[ ][ ]*/ $2 /g" /etc/hosts.1 > /etc/hosts.2
rm /etc/hosts.1
mv /etc/hosts /etc/hosts.0
mv /etc/hosts.2 /etc/hosts

