#!/bin/sh
#set -x
# 
#  	Driver load script for SunPCi 2 Solaris driver. 
#
GREP="/bin/grep"
ID="/bin/id"
ADD_DRV="$PKG_INSTALL_ROOT/usr/sbin/add_drv"
REM_DRV="$PKG_INSTALL_ROOT/usr/sbin/rem_drv"
ECHO="/bin/echo"
AWK="/bin/awk"
CP="/bin/cp"
RM="/usr/bin/rm"
MV="/usr/bin/mv"
OSREVINFO=`$PKG_INSTALL_ROOT/usr/bin/uname -r`
FDX=`uname -r | sed 's/.\.//'`
arg0basedir=` dirname $0 `

FNAME="sunpcidrv."2${FDX}0""
OSREV="2${FDX}0"

DEVLINKS_TAB="$PKG_INSTALL_ROOT/etc/devlink.tab"

#
# Must be root to run, test that right away.
#
ISROOT=`${ID} | ${AWK} '/root/ {print}'`
if [ -z "$ISROOT" ]
then
	${ECHO} "\nMust be root to install the SunPCi 2 driver."
	exit 1
fi

#
# Complain and refuse to load if the SunPCi 1.X driver is installed.
#
PENGUIN_INSTALLED=`${GREP} sunpcidrv $PKG_INSTALL_ROOT/etc/driver_aliases`
if [ -n "$PENGUIN_INSTALLED" ]
then
	${ECHO} "\nYou Must remove the SunPCi driver/package before installing SunPCi 2"
	exit 1
fi

#
# See if the driver is already installed.
#
SUNPCDRV_INSTALLED=`${GREP} sunpci2drv $PKG_INSTALL_ROOT/etc/driver_aliases`
if [ -n "$SUNPCDRV_INSTALLED" ]
then
	${REM_DRV} sunpci2drv
fi
#
# I think this is a good place to add this
# remove the devlinks because the board may
# have moved or some other reason that we are getting
# multiple instances of the driver
#

${RM} -rf $PKG_INSTALL_ROOT/dev/sunpci2drv*
${RM} -rf $PKG_INSTALL_ROOT/dev/sunpcidrv*

#
# Copy the driver files to the kernel driver area.
#
if [ -f $arg0basedir/${FNAME} ]
then

	${CP} $arg0basedir/sunpcidrv.conf $PKG_INSTALL_ROOT/kernel/drv/sunpci2drv.conf
	${CP} $arg0basedir/${FNAME} $PKG_INSTALL_ROOT/kernel/drv/sunpci2drv

	# Install the 64 bit version
	if [ "${OSREV}" -gt "260" ]
	then
		${CP} $arg0basedir/sunpcidrv.conf $PKG_INSTALL_ROOT/kernel/drv/sparcv9/sunpci2drv.conf
		${CP} $arg0basedir/${FNAME}.64 $PKG_INSTALL_ROOT/kernel/drv/sparcv9/sunpci2drv
	fi

else
	${ECHO} "\nThe SunPCi 2 driver installation has failed. "
	${ECHO} "  Cannot find the driver file: ${FNAME}"
	exit 2
fi

#
# Check if devlink.tab file already has an entry. If not, add it. See man
# devlinks for syntax.
#

DEVLINK_MODIFIED=`${GREP} sunpci2drv ${DEVLINKS_TAB}`
if [ -z "$DEVLINK_MODIFIED" ]
then
	${CP} ${DEVLINKS_TAB} ${DEVLINKS_TAB}.sunpci2
	echo "#The following lines added for the SunPCi 2 Device Driver" >> ${DEVLINKS_TAB} 
	echo "type=ddi_pseudo;name=pci108e,7063	sunpci2drv\N0" >> ${DEVLINKS_TAB}
fi

#this is for jump start without it we will break
if [ "$PKG_INSTALL_ROOT" ]
then
	$PKG_INSTALL_ROOT/usr/sbin/add_drv -m '* 0666 root root' -i '"pci108e,7063"' -b $PKG_INSTALL_ROOT sunpci2drv
else
	$PKG_INSTALL_ROOT/usr/sbin/add_drv -m '* 0666 root root' -i '"pci108e,7063"' sunpci2drv
fi


#
# Now run makemaster to generate the Master.ini file. This should only be run
# if add_drv didn't fail.
#
if [ -f $PKG_INSTALL_ROOT/etc/Master.ini ]
then

#
# Copy the old file to Master.bak
#
${CP} -f $PKG_INSTALL_ROOT/etc/Master.ini $PKG_INSTALL_ROOT/etc/Master.bak

fi

#
# Make a new master.ini file
#
$arg0basedir/makemaster -f $PKG_INSTALL_ROOT/etc/Master.ini

#
# Make sure that umask hasn't screwed up the permissions.
#
chmod 644 $PKG_INSTALL_ROOT/etc/Master.ini

