#!/bin/sh
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"@(#)i.mptsasconf	1.1	09/07/13 SMI"
#

PATH=/usr/bin:/usr/sbin:$PATH; export PATH
PREFIX=/tmp/mptsas.conf.$$

add_comment_for_vhci_class()
{
	if grep "^# The mpt_sas driver, as a pHCI driver" $1 > /dev/null 2>&1; then
		return
	fi

	cat >> $1 << EOF

#
# The mpt_sas driver, as a pHCI driver, must specify the vHCI class it
# belongs to(scsi_vhci).
#
EOF
}

add_comment_for_mpxio_disable()
{

	if grep "^# Global mpxio-disable property:" $1 > /dev/null 2>&1; then
		return
	fi

	cat >> $1 << EOF

#
# I/O multipathing feature (MPxIO) can be enabled or disabled using
# mpxio-disable property. Setting mpxio-disable="no" will activate
# I/O multipathing; setting mpxio-disable="yes" disables the feature.
#
# Global mpxio-disable property:
#
# To globally enable MPxIO on all LSI MPT SAS 2.0 controllers set:
# mpxio-disable="no";
#
# To globally disable MPxIO on all LSI MPT SAS 2.0 controllers set:
# mpxio-disable="yes";
#
# You can also enable or disable MPxIO on a per HBA basis. 
# Per HBA settings override the global setting for the specified HBAs.
# To disable MPxIO on a controller whose parent is /pci@7c0/pci@0/pci@9
# and the unit-address is "0" set:
# name="mpt_sas" parent="/pci@7c0/pci@0/pci@9" unit-address="0" mpxio-disable="yes";
#
EOF
}

add_comment_for_tape_property()
{

	if grep "^# The property tape" $1 > /dev/null 2>&1; then
		return
	fi

	cat >> $1 << EOF

#
# The property tape is only used for X86
#
EOF
}

update_mptsasconf()
{
	NEWHDR1=$PREFIX.hdr1
	NEWHDR2=$PREFIX.hdr2
	TMPFILE=$PREFIX.tmp
	
	# replace old copyright with new one	
	HEADER="^#.* Copyright.*Sun Microsystems.*$"
	if grep "$HEADER" $1 > $NEWHDR1 2>/dev/null; then
		# replace / by \/
		sed "s/\\//\\\\\\//g" $NEWHDR1 > $NEWHDR2 2>/dev/null
		if sed "s/$HEADER/`cat $NEWHDR2`/" $2 > $TMPFILE 2>/dev/null
		then
			cp $TMPFILE $2
			if [ $? -gt 0 ]; then
				rm -f $NEWHDR1 $NEWHDR2 $TMPFILE
				return 1
			fi
		fi
	fi

	if [ "$ARCH" = "i386" ]; then
		grep 'tape="sctp"' $2 > /dev/null 2>&1
		if [ $? != 0 ] ; then
		        add_comment_for_tape_property $2
			echo 'tape="sctp";' >> $2
		fi
	fi

	add_comment_for_vhci_class $2

	# check for property named ddi-vhci-class
	grep '^ddi-vhci-class' $2 > /dev/null 2>&1
	if [ $? != 0 ] ; then
		echo 'ddi-vhci-class="scsi_vhci";' >> $2
	fi

	add_comment_for_mpxio_disable $2
	# check for property named mpxio-disable

	grep '^mpxio-disable' $2 > /dev/null 2>&1
	if [ $? != 0 ] ; then
		echo 'mpxio-disable="no";' >> $2
	fi


	rm -f $NEWHDR1 $NEWHDR2 $TMPFILE
	return 0
}

while read src dest; do
	if [ ! -f $dest ]; then
		cp $src $dest
		if [ $? -gt 0 ]; then
			rm -f $dest
			exit 0
		fi
		if [ "$ARCH" = "i386" ]; then
			# add property tape for i386 platform
		        add_comment_for_tape_property $dest
			echo 'tape="sctp";' >> $dest
		fi	
	else
		cp $dest $dest.$$
		if [ $? -gt 0 ]; then
			rm -f $dest.$$
		else
			# upgrading solaris
			update_mptsasconf $src $dest.$$
			if [ $? -gt 0 ]; then
				rm -f $dest.$$
				exit 0
			fi
			mv $dest.$$ $dest
		fi
	fi

done

exit 0
