#! /bin/sh
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#
#
ExitCode=0
# So far, so good.

# chk/add fp.conf entry
# 	(will not be removed on backout)

FPCONF=${ROOTDIR}/kernel/drv/fp.conf

grep 'scsi-binding-set=' ${FPCONF} > /dev/null 2>&1
if [ $? -ne 0 ] ; then
  if [ ! -w ${FPCONF} ] ; then
    echo "${FPCONF} not writeable."  >&2
    echo "Failed to update." >&2
    ExitCode=1
  else
    echo "\nscsi-binding-set=\"fcp\";" >> ${FPCONF}
    if [ $? -ne 0 ] ; then
      echo "Failed to update $FPCONF." >&2
      ExitCode=1
    fi
  fi
fi

# chk/add /etc/driver_aliases entries
# 	(will not be removed on backout)

DRVALIAS=${ROOTDIR}/etc/driver_aliases

update_alias ()
{
  alias=$1
  driver=$2
  
  DRVR=`basename $driver`

  grep "$DRVR[ 	][ 	]*$alias" $DRVALIAS > /dev/null 2>&1
  if [ $? -ne 0 ]; then
    if [ ! -w $DRVALIAS ] ; then
      if [ $ExitCode -eq 0 ] ; then
        echo "${DRVALIAS} not writeable."  >&2
        echo "Failed to update." >&2
        ExitCode=1
      fi
    else
      if [ -f $driver ]; then
        echo "$DRVR $alias" >> $DRVALIAS
        if [ $? -ne 0 ] ; then
          echo "Failed to update $DRVALIAS with $DRVR $alias." >&2
          ExitCode=1
        fi
      else
        echo "$DRVALIAS not updated for uninstalled driver $DRVR"  >&1
      fi
    fi
  fi
}

if [ ! -f $DRVALIAS ] ; then
  echo "$DRVALIAS does not exist"
  ExitCode = 1
else
  update_alias '"scsa,00.bfcp"' /kernel/drv/ssd
  update_alias '"scsa,00.bvhci"' /kernel/drv/ssd
  update_alias '"scsa,08.bfcp"' /kernel/drv/sgen
  update_alias '"scsa,08.bvhci"' /kernel/drv/sgen
  update_alias '"scsiclass,01"' /kernel/drv/st
  update_alias '"scsiclass,0d"' /kernel/drv/ses
fi


# Update San_drivers
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 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 


# Check for JFCA
[ -z "$ROOTDIR" ] && ROOTDIR=/

version=`/usr/sbin/patchadd -R $ROOTDIR -p | \
	nawk '$2 ~ /114877/{printf "%d\n", substr($2,8,2)}' | \
	sort | tail -1`

if [ -n "$version" ]; then
  # Patch 114877 is installed.

  # Check obsoleted field
  /usr/sbin/patchadd -R $ROOTDIR -p | \
	nawk -F: '{print $3}' | grep 114877 > /dev/null

  if [ $? -ne 0 ] ; then
    # Patch 114877 is not obsoleted by newer patch

    if [ $version -le 7 ] ; then
      echo 
      echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
      echo 
      echo "This patch, 111095, is not compatible with jfca driver patches" 
      echo "earlier than 114877-08. "
      echo
      echo "It is strongly recommended that all systems with patch"
      echo "114877-07 (or earlier) be upgraded to 114877-08 (or later)."
      echo 
      echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
      echo 
    fi

  fi
fi

exit $ExitCode 

