#!/bin/sh
# set -x
# sunpcload
#  	Driver load script for SunPC svr4 driver. Modeled after
#	the 4.x sunpcload script and the svr4 'add' script (thanks, Mike).
#
DEVLINKS="/usr/sbin/devlinks"
ADD_DRV="/usr/sbin/add_drv"
REM_DRV="/usr/sbin/rem_drv"
MODLOAD="/usr/sbin/modload"
MODUNLOAD="/usr/sbin/modunload"
DRVALIASES="/etc/driver_aliases"

DEVLINKS_TAB="/etc/devlink.tab"

CKYORN="/usr/bin/ckyorn"
UNAME="/usr/bin/uname"
CHMOD="/usr/bin/chmod"
CUT="/usr/bin/cut"

AWK="/bin/awk"
GREP="/bin/grep"
ID="/bin/id"
ECHO="/bin/echo"
CP="/bin/cp"
MODINFO="/usr/sbin/modinfo"
ARCH=`${UNAME} -m`
OSREVINFO=`/usr/bin/uname -r`

#
# Get OS release to use in the driver name.
OSREVLEVEL=`echo "$OSREVINFO"| sed 's/\.//'`
# We only need to differentiate between 5.1, 5.2, and 5.3.
# We only ship driver for OS Versions 5.1 (or below), 5.2,and 5.3 (or greater).
if [ $OSREVLEVEL = "50" -o $OSREVLEVEL = "51" ]
then
    DRIVER_OSREVLEVEL="51"
elif [ $OSREVLEVEL = "52" ]
then
	DRIVER_OSREVLEVEL="52"
elif [ $OSREVLEVEL = "53" ]
then
	DRIVER_OSREVLEVEL="53"
else
    DRIVER_OSREVLEVEL="54"
fi

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

#
# check if have an alternate vmunix file to load against
#
if [ $# -eq 2 ]
then
    if [ $1 = "-A" ]
    then
	VMUNIX_FILE=$2
    else
	if [ $1 = "" ]
	then
		${ECHO} "\nusage: sunpcload -A vmunix_file"
		exit 1
	fi
	VMUNIX_FILE=/kernel/unix
    fi
else
	if [ $# -eq 0 ]
	then
		VMUNIX_FILE=/kernel/unix
	else
		echo "\nusage: sunpcload -A vmunix_file"
		exit 1
	fi
fi

#echo VMUNIX_FILE=${VMUNIX_FILE}

#
# Check if driver is already installed.
# Check out the /etc/driver_aliases file for the sunpcdrv alias.
#
SUNPCDRV_INSTALLED=`${GREP} sunpcdrv /etc/driver_aliases`
if [ -n "$SUNPCDRV_INSTALLED" ]
then
	${REM_DRV} sunpcdrv
fi

#
# Copy driver files into kernel driver area.
# Make sure files exist first.....
#
if [ -f sunpcmod.${ARCH}_${DRIVER_OSREVLEVEL}.o ]
then
	${CP} sunpcmod.${ARCH}_${DRIVER_OSREVLEVEL}.o /kernel/drv/sunpcdrv
	${CP} sunpcdrv.conf /kernel/drv/sunpcdrv.conf
else
	${ECHO} "\nThe SunPC driver installation has failed. This may be due to"
	${ECHO} "your system having an unsupported SunOS version or system "
	${ECHO} "architecture. Your are currently running SunOS ${OSREVINFO}"
	${ECHO} "on a ${ARCH} system."
	exit 2
fi

#
# Check if devlink.tab file already has a sunpc entry.
# (if not, add the sunpc entry.)
#
DEVLINK_MODIFIED=`${GREP} sunpcdrv ${DEVLINKS_TAB}`
if [ -z "$DEVLINK_MODIFIED" ]
then
	${CP} ${DEVLINKS_TAB} ${DEVLINKS_TAB}.sunpc
	echo "#The following lines added for the SunPC kernel driver" >> ${DEVLINKS_TAB} 
	echo "type=ddi_pseudo;name=sunpcdrv;minor=0	sdos0" >> ${DEVLINKS_TAB}
	echo "type=ddi_pseudo;name=sunpcdrv;minor=1	sdos1" >> ${DEVLINKS_TAB}
	echo "type=ddi_pseudo;name=sunpcdrv;minor=2	sdos2" >> ${DEVLINKS_TAB}
	echo "type=ddi_pseudo;name=sunpcdrv;minor=3	sdos3" >> ${DEVLINKS_TAB}
	echo "type=ddi_pseudo;name=sunpcdrv;minor=128	ereg0" >> ${DEVLINKS_TAB}
#	echo "type=sbus@1,f8000000;name=sdos	\M" >> ${DEVLINKS_TAB}
fi
${ADD_DRV} -m '* 0666 root sys' -i 'SUNW,SunPC SUNW,sdos' sunpcdrv
#
# At least one version of add_drv is broken when it comes to adding multiple
# aliases for a driver.  This hack checks if the /etc/driver_aliases file
# has a broken entry, and if it does it removes the broken entry and replaces
# it with a proper entry. (ama)
#
if ${GREP} "SUNW,SunPC SUNW,sdos" ${DRVALIASES} > /dev/null 2>&1
then
    mv /etc/driver_aliases /etc/da.sunpc
    ${GREP} -v "SUNW,SunPC SUNW,sdos" /etc/da.sunpc > /etc/driver_aliases
    echo "sunpcdrv SUNW,SunPC" >> /etc/driver_aliases
    echo "sunpcdrv SUNW,sdos" >> /etc/driver_aliases
    rm -f /etc/da.sunpc
fi

# Set up storekernname so it can be run after a system reset. The 
# rest of this file only needs to be run once.
${CP} -p ./storekernname.${ARCH} /kernel/drv
${CP} -p ./S10storekernname /etc/rc2.d

# save the kernel name in the driver so we can patch the vm functions
#
	./storekernname.${ARCH} ${VMUNIX_FILE}
	sync

	exit 0
