#!/bin/sh
#
# prepatch script for Sun patch(es)
#
# 108556-04        109089-02        109466-01        XXXXX-XX
# SBU 5.1.1 US     SBU 5.5.1 JA     SBU 5.1.1 US     SBU 5.1.1 JA
# SUNWsbus2        LGTOdrvr         SUNWsbus2        LGTOdrvr
#
# XXXXXX-XX        XXXXXX-XX
# SBU 5.1 US       SBU 5.1 JA
# SUNWsbus2        LGTOdrvr
#
# Copyright (c) 1990 - 2000 Sun Microsystems, Inc.
# Portions Copyright (c) 1999 Legato Systems, Inc.
#
# This script fixes two problems: 
#
# o As of Solaris 7 11/99 the file  /etc/devlink.tab cannot contain 
#   any excess whitespace or add_drv(1M) will fail to create the /dev/lus 
#   link. The function fix_lus() addresses this.
#
# o NSR 5.5.1 & 5.1.1 don't recognize Solaris 8 and therefore fail to properly
#   install the libscsi.so.2 library over the libscsi.so.1 library. The
#   function fix_libscsi() addresses this.
#

# Set the PATH for this shell script
#
PATH=$PATH:/sbin:/usr/sbin:/etc:/usr/etc

# ROOTDIR Specifies an alternate root. 
# if necessary apply default here
#
if [ "${ROOTDIR}." = "." ]; then
  ROOTDIR="/"
fi

# Determine Solaris Version
#
SOLARIS_VERSION=`grep Solaris $ROOTDIR/etc/release|awk '{print $2}'`

# Determine package name (LGTOdrvr or SUNWsbus2)
#
PKG=`pkginfo|grep "(Backup/Recover) Device Drivers"|awk '{print $2}'`

# Determine NSR version
#
NSR_VERSION=`pkginfo -l ${PKG}|grep VERSION|awk '{print $2}'|awk -F. '{print $1"."$2}'`

# Define functions
#

fix_libscsi() {
# fix_libscsi function.
# o save a copy of libscsi.so.1
# o move libscsi.so.2 over libscsi.so.1 which is pointed to by libscsi.so
# o update package database

# save a copy of libscsi.so.1
#
cp -p $ROOTDIR/usr/lib/nsr/libscsi.so.1 $ROOTDIR/usr/lib/nsr/libscsi.so.1.save

# move libscsi.so.2 over libscsi.so.1 which is pointed to by libscsi.so
#
mv $ROOTDIR/usr/lib/nsr/libscsi.so.2 $ROOTDIR/usr/lib/nsr/libscsi.so.1

# update package database
#
removef -R $ROOTDIR $PKG /usr/lib/nsr/libscsi.so.1 >/dev/null
removef -R $ROOTDIR $PKG /usr/lib/nsr/libscsi.so.2 >/dev/null
installf -R $ROOTDIR $PKG /usr/lib/nsr/libscsi.so.1 f
removef -R $ROOTDIR -f $PKG
installf -R $ROOTDIR -f $PKG
}


fix_lus() {
# fix_lus function. 
# o remove Legato entries in /etc/devlink.tab
# o create Legato entries in /etc/devlink.tab
# o remove lus driver module from memory
# o load lus driver module and create /dev/lus

# remove Legato entries in /etc/devlink.tab
#
FILE=$ROOTDIR/etc/devlink.tab
if [ -f ${FILE} ]; then
  cp ${FILE} ${FILE}.save || exit 2
  sed1='/^#Legato Juke begin/,/^#Legato Juke end/d'
  sed2='/^#Legato uscsi begin/,/^#Legato uscsi end/d'
  sed3='/^#Legato Optical begin/,/^#Legato Optical end/d'
  sed -e "${sed1}" -e "${sed2}" -e "${sed3}" < ${FILE} > /tmp/q$$ && \
  rm -f ${FILE} && mv /tmp/q$$ ${FILE}
  if [ $? -ne 0 ]
  then
    rm -f ${FILE}
    mv ${FILE}.save ${FILE}
    rm -f /tmp/q$$
    exit 2
  fi

  # create Legato entries in /etc/devlink.tab
  #
  echo "#Legato uscsi begin - added by ${PKG} install - do not EDIT" >> $FILE
  echo 'type=ddi_pseudo;name=lus;minor=0	lus' >> $FILE
  echo 'type=ddi_pseudo;name=op	optical\N0' >> $FILE
  echo "#Legato uscsi end - added by ${PKG} install - do not EDIT" >> $FILE

  # remove lus driver module from memory 
  # (only use -b for alternate $ROOTDIR)
  #
  if [ "${ROOTDIR}" = "/" ]; then
    /usr/sbin/rem_drv lus
  else
    /usr/sbin/rem_drv -b $ROOTDIR lus
  fi

  # load lus driver module and create /dev/lus
  # (only use -b for alternate $ROOTDIR)
  #
  if [ "${ROOTDIR}" = "/" ]; then
    /usr/sbin/add_drv lus
  else 
    /usr/sbin/add_drv -b $ROOTDIR lus
  fi
  
  # in case /etc/devlink.tab is missing
  #
  else
  echo "ERROR: file (${FILE}) is missing"
  exit 2
fi
}

# Main routine
#

# If Solaris 8 then fix libscsi.so.2 placement
#
if [ $SOLARIS_VERSION = 8 ]; then
  fix_libscsi
fi

# In all cases we need to fix_lus
#
fix_lus
