#
# ident "@(#)postinstall.src	1.2 04/04/13 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
# Use is subject to license terms.
#
# NOTE:
# utdisk depends on utdiskctl, so utdiskctl should be installed first
# and removed last
#

PATH=/bin:/sbin:/usr/sbin

# if PKG_INSTALL_ROOT is not assigned from the env then
# set it to an empty string, set -u in effect below
#
if [ -z "$PKG_INSTALL_ROOT" ]; then
  PKG_INSTALL_ROOT=""
  REM_DRV="/usr/sbin/rem_drv"
  ADD_DRV="/usr/sbin/add_drv"
else
  REM_DRV="/usr/sbin/rem_drv -b ${PKG_INSTALL_ROOT}"
  ADD_DRV="/usr/sbin/add_drv -b ${PKG_INSTALL_ROOT}"
fi

set -eu
ret=0

#
# Install the utdiskctl and utdisk pseudo drivers
#

UTDISK="utdisk"
UTDISKCTL="utdiskctl"
DEVTAB="$PKG_INSTALL_ROOT/etc/devlink.tab"
NEWFILE="${DEVTAB}.new"
TARGDIR="$PKG_INSTALL_ROOT/usr/kernel/drv"

/bin/rm -f $NEWFILE

# save a backup copy of the driver link table
/usr/bin/cp -f $DEVTAB $DEVTAB.bak

# remove any existing driver with these names
/bin/sed -e "/name=$UTDISK[ 	]/d" \
	 -e "/name=$UTDISKCTL[ 	]/d" \
	$DEVTAB > $NEWFILE

# add these drivers to the end of the link table
# utdiskctl should be added first so that it'll be loaded first on reboot
echo "type=ddi_pseudo;name=$UTDISKCTL	\D"  >> $NEWFILE
echo "type=ddi_pseudo;name=$UTDISK	\D"  >> $NEWFILE

# Atomic update; rename is supposed to be atomic so a crash will
# either leave the old file or the new file as $DEVTAB
/bin/sync
/bin/rm -f "${DEVTAB}.old"
/bin/ln "${DEVTAB}" "${DEVTAB}.old"
/bin/sync
/bin/mv -f "${NEWFILE}" "${DEVTAB}"
/bin/sync

cd $TARGDIR
#
# First remove older versions, removing utdisk first, before utdiskctl
# then check that the 32-bit version of the driver and the conf file are
# both there before calling add_drv.
# When adding drv, do utdiskctl first
#

set +e
$REM_DRV $UTDISK >/dev/null 2>&1
$REM_DRV $UTDISKCTL >/dev/null 2>&1
set -e

if [ -f "${TARGDIR}/${UTDISKCTL}" -a -f "${TARGDIR}/${UTDISKCTL}.conf" ]; then
  $ADD_DRV -m '* 0600 bin bin' $UTDISKCTL
  echo "### successfully installed Sun Ray disk controller pseudo driver ($UTDISKCTL)"
else
  echo "Error:  Sun Ray disk controller pseudo driver ($UTDISKCTL) files not in $TARGDIR"
  ret=1
fi

if [ -f "${TARGDIR}/${UTDISK}" -a -f "${TARGDIR}/${UTDISK}.conf" ]; then
  $ADD_DRV -m '* 0600 bin bin' $UTDISK
  echo "### successfully installed Sun Ray disk pseudo driver ($UTDISK)"
else
  echo "Error:  Sun Ray disk pseudo driver ($UTDISK) files not in $TARGDIR"
  ret=1
fi

exit $ret
