#! /bin/sh 
#
# Copyright (c) 2001-2002 by Sun Microsystems, Inc.
# All rights reserved.
#
#
#

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

# remove deleted link generator #
osd_line=`grep \^/usr/lib/devfsadm/linkmod/SUNW_fp_link.so\:.\*111846 $Dst`
if [ -n "$osd_line" ] ; then
   echo "$osd_line" >> $Sav
   grep -v $osd_line $Dst  > $TMPF
   mv $TMPF $Dst
fi
 
# 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 

exit 0 

