#!/bin/sh 
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#pragma ident	"@(#)postpatch	1.5	05/12/18 SMI"
# 

PATH="/usr/bin:/usr/sbin:${PATH}"
export PATH

# Driver definitions
DRVR_NAME=emlxs
DRVR_PERM="-m '* 0600 root sys'"
DRVR_CLASS="-c fibre-channel"
ALIAS_FILE=/etc/driver_aliases

SUPPORT_LIST="\
	pci10df,fc00 \
	pci10df,f800 \
	pci10df,f900 \
	pci10df,f980 \
	pci10df,fa00 \
	pci10df,fd00 \
	pci10df,fe00 \
	pci10df,fc10 \
	lpfs \
	"

update_sandrivers ()
{
	# Update san_drivers file

	TMPF=/tmp/sand_$$
	Dst=${ROOTDIR}/opt/SUNWsan/etc/san_drivers
	Sav=${PKGDB}/SUNWsan/${PatchNum}.sav 
	touch $Sav
	
	idate=`date +%y/%m/%d`
	
	while read driver dpkg desc ; do
	
   	dfile=${ROOTDIR}${driver}
	
   	if [ -f $dfile ] ; then
	
		siz=`ls -l ${dfile} | nawk '{print $5}'`
		cksum=`sum ${dfile} | nawk '{print $1}'`
		 
		sd_line=`printf "%s:%s:%s:%s:%s:%s\n" \
		"$driver" "$desc" "$siz" "$cksum" "$idate" "$PatchNum"`
		
		osd_line=`grep \^${driver}\: $Dst`
		 
		if [ -z "$osd_line" ] ; then
			echo $sd_line >> $Dst
		else
			echo "$osd_line" >> $Sav
			sed -e 's+^'$driver':.*+'$sd_line'+' $Dst > $TMPF
			mv $TMPF $Dst
		fi
   	fi
	
	done < ${patchdir}/san_drivers
}

 
archive_patch ()
{
	# Archive this patch
	# 
	# 
	if [ -f $ROOTDIR/opt/SUNWsan/patches/patchconf ]; then 

	 . $ROOTDIR/opt/SUNWsan/patches/patchconf 

	 if [ $patch_archive = yes ] ; then 
	   if [ ! -f $ROOTDIR/opt/SUNWsan/patches/${PatchNum}.tar.Z ]; then

	     cd `dirname ${patchdir}`
	     TMPF=/tmp/$$_${PatchNum}.tar

	     tar cf $TMPF `basename ${patchdir}`
	     compress $TMPF

	     # Remove older versions
	     for P in `ls $ROOTDIR/opt/SUNWsan/patches/${PatchBase}* 2> /dev/null`
	     do
		 rm -f $P
	     done

	     # check for space
	     FreeSP=`df -b $ROOTDIR/opt/SUNWsan/patches | tail -1 | nawk '{print $2}'`
	     TarSize=`du -k $TMPF.Z`

	     if [ $FreeSP -gt $TarSize ]; then
		mv $TMPF.Z $ROOTDIR/opt/SUNWsan/patches/${PatchNum}.tar.Z
	     else
		echo "Warning: Not enough space available in $ROOTDIR/opt/SUNWsan/patches to archive patch $PatchNum."
		echo "To avoid seeing this warning message in the future; either increase disk space or turn off patch archiving in $ROOTDIR/opt/SUNWsan/patches/patchconf"
		echo "Patch archiving is an optional service used for upgrade recovery"
	     fi
	   fi 
	  fi
	fi 
}


#######################################################################
# Update drivers subroutines
#######################################################################
CheckAlias()
#
#  Return Status = 0 if driver and alias found in driver_alias file
#
{
	DRIVER=$1
	PCI_STRING=$2
	
	grep "^${DRIVER}[ 	]\"*${PCI_STRING}\"*" ${ROOTDIR}${ALIAS_FILE} > /dev/null 2>&1
	STATUS=$?
	return $STATUS
}

GenerateSupportList()
#
# Create list of aliases to add to add_drivers
#
# Use the list of aliases from SUPPORT_LIST and adds the
# alias if it is not already being used by the lpfc driver.
# Result is store in GEN_LIST.
#
{

	LPFC_DRVR=lpfc
	GEN_LIST=""
	
	for HBA in ${SUPPORT_LIST}
	do
		CheckAlias ${LPFC_DRVR} ${HBA}
		if [ $STATUS -ne 0 ]; then
			if [ -z "${GEN_LIST}" ]; then
				GEN_LIST="\"$HBA\""
			else
				GEN_LIST="${GEN_LIST} \"$HBA\""
			fi
		fi
	done
}


update_drivers ()
#
# Replace driver, alias and class information with add_drv command.
#
{
	if [ -z "${ROOTDIR}" ]; then
		echo "\n$0 Failed: ROOTDIR is not set.\n" >&2
		exit 1
	fi

	# Determine list of aliases
	GenerateSupportList
	
	DRVR_ALIASES="-i '${GEN_LIST}'"

	# Remove existing definition, if it exists.
	/usr/sbin/rem_drv -b "${ROOTDIR}" ${DRVR_NAME} > /dev/null 2>&1

	if [ "$ROOTDIR" = "/" ] ; then
		ADD_DRV="add_drv -n"
	else
		ADD_DRV="add_drv -b ${ROOTDIR}"
	fi

	eval ${ADD_DRV} "${DRVR_PERM}" ${DRVR_CLASS} "${DRVR_ALIASES}" ${DRVR_NAME}
	if [ $? -ne 0 ]; then
		echo "\nCommand Failed:" >&2
		echo "${ADD_DRV} "${DRVR_PERM}" ${DRVR_CLASS} "${DRVR_ALIASES}" ${DRVR_NAME}\n" >&2
		exit 1
	fi

}

##########################################
#Main#####################################
##########################################

update_sandrivers
archive_patch
update_drivers

exit 0 

