#!/bin/sh
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)i.ibnexconf	1.1	06/03/27 SMI"

PATH=/usr/bin:$PATH

#
# The IB Nexus driver (ib(7d)), requires the following lines :
#       name="ib" class="root";
# in /kernel/drv/ib.conf file.
#
# This script adds the required line and relevent comments,
# if the lines are not already present in the ib.conf file.
#

#
# Search for the line containing
#	name="ib" class="root";
# in ib.conf file. This returns 0 on a successful search.
#
search_ibconf()
{
	TGT_IBCONFFILE=$1

	grep "$IB_SEARCH_STRING" $TGT_IBCONFFILE  > /dev/null 2>&1
	search_result=$?
	return $search_result
}

#
# Update ib.conf does two things :
# 1. Replace the old ident with new ident
# 2. Add the line containing :
#	name="ib" class="root";
#    and the adjoining comments in target ib.conf file.
#
update_ibconf()
{
	PKG_IBCONFFILE=$1
	TGT_IBCONFFILE=$2

	# Replace old ident with new ident
	ident_pattern="^#.*ident.*SMI\"$"
	ident_var=`grep $ident_pattern $PKG_IBCONFFILE`
	ed -s $TGT_IBCONFFILE <<-EOF
	/$ident_pattern/c
	$ident_var
	.
	w
	q
	EOF

	echo $IB_STR1 >> $TGT_IBCONFFILE
	echo $IB_STR2 >> $TGT_IBCONFFILE
	echo $IB_STR3 >> $TGT_IBCONFFILE
}

#
# Search Pattern and additional lines for ib.conf
#
IB_STR1="# The name and class property are required to indicate that IB nexus"
IB_STR2="# driver is a child of the root nexus driver. DO NOT DELETE LINE BELOW"
IB_STR3="name=\"ib\" class=\"root\";"
IB_SEARCH_STRING="^[ 	]*name=\"ib\"[ 	]*class=\"root\"[ 	]*;"

while read src dest; do
	if [ -f $dest ]; then
		#
		# 1. Search the $dest file for relevent line 
		# 2. If search fails, add the required lines to $dest
		#
		search_ibconf $dest
		if [ "$?" -ne 0 ]; then
			update_ibconf $src $dest
		fi

	else
		#
		# If no ib.conf file is present on the target system,
		# just copy over the one from the package.
		#
		cp -p $src $dest
	fi
done
exit 0
