# Copyright 1999-2000 Sun Microsystems, Inc. All Rights Reserved.
# @(#)postinstall	1.7 00/04/05
#

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 Sun Ray, utadem, utserial and utparallel pseudo drivers
#

SUNRAY="sunray"
UTADEM="utadem"
UTSERIAL="utserial"
UTPARALLEL="utparallel"
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=$SUNRAY[ 	]/d" \
	 -e "/name=$UTADEM[ 	]/d" \
	 -e "/name=$UTSERIAL[ 	]/d" \
	 -e "/name=$UTPARALLEL[ 	]"/d \
	$DEVTAB > $NEWFILE

# add these drivers to the end of the link table
echo "type=ddi_pseudo;name=$SUNRAY	\M0" >> $NEWFILE
echo "type=ddi_pseudo;name=$UTADEM	\M0" >> $NEWFILE
echo "type=ddi_pseudo;name=$UTSERIAL	\D"  >> $NEWFILE
echo "type=ddi_pseudo;name=$UTPARALLEL	\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
#
# for each of the drivers to be installed, first remove the old version,
# then check that the 32-bit version of the driver and the conf file are
# both there before calling add_drv.
#

#
# utadem
#
set +e
$REM_DRV $UTADEM >/dev/null 2>&1
set -e

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

#
# utserial
#
set +e
$REM_DRV $UTSERIAL >/dev/null 2>&1
set -e

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

#
# utparallel
#
set +e
$REM_DRV $UTPARALLEL >/dev/null 2>&1
set -e

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

#
# sunray
#
set +e
$REM_DRV $SUNRAY >/dev/null 2>&1
set -e

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

exit $ret
