#!/bin/sh
#
#  Add entry for random driver to etc/devlink.tab file.
#  add_drv random if it isn't already installed. 
#
#

PATH="/usr/bin:/usr/sbin:${PATH}"
export PATH

DEVLINKTB="${ROOTDIR}/etc/devlink.tab"
ADD_DRV="${ROOTDIR}/usr/sbin/add_drv -b ${ROOTDIR}"
ADD_DRV_SERVER="/usr/sbin/add_drv -b ${ROOTDIR}"
NAMEMAJOR="${ROOTDIR}/etc/name_to_major"
DRV='random'

trap "rm -f /tmp/$$.*;exit" 0 1 2 3 13 15
TMP=/tmp/$$

#
# add_devlink - adds an entry to ${DEVLINKTB}
#
add_devlink()
{
        PATTERN="$1"
        LINK="$2"
        echo "$PATTERN\t$LINK" >>$TMP.devlink
        mv $TMP.devlink ${DEVLINKTB}
}

#
# 'grep -v' prints out contents without random entry if it is there. 
# This keeps from duplicating the line and works with subsequent 
# revs of this patch.

if test ! -f ${DEVLINKTB} -o ! -w ${DEVLINKTB}; then
  if [ "$ROOTDIR" = "/" ]; then
        echo "File ${DEVLINKTB} is missing/inaccessible." >&2
        exit 1
  fi
else
  grep -v '^type=ddi_pseudo;name=random[	 ]*\\M0$' ${DEVLINKTB} >$TMP.devlink
  add_devlink 'type=ddi_pseudo;name=random' '\\M0'
fi
 
if test ! -f ${NAMEMAJOR} -o ! -w ${NAMEMAJOR}; then
  if [ "$ROOTDIR" = "/" ]; then
        echo "File ${NAMEMAJOR} is missing/inaccessible." >&2
        exit 1
  fi
else
  grep "^${DRV} " $NAMEMAJOR > /dev/null 2>&1 
  if [ $? -ne 0 ]; then
    if [ -x ${ADD_DRV} ]; then
	${ADD_DRV} -n ${DRV} || exit 1
    else
	${ADD_DRV_SERVER} -n ${DRV} || exit 1
    fi
  fi
fi

touch ${ROOTDIR}/reconfigure

exit 0

