#!/bin/sh
# set -x
# sunpcload
#
#  	Driver load script for SunPC driver. 
#
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"

UNAME="/usr/bin/uname"
CHMOD="/usr/bin/chmod"

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

#
# Get OS release to use in the driver name.
#
OSREVLEVEL=`echo "$OSREVINFO"| sed 's/\.//'`
if [ $OSREVLEVEL = "53" ]
then
	DRIVER_OSREVLEVEL="530"
elif [ $OSREVLEVEL = "54" ]
then
	DRIVER_OSREVLEVEL="540"
elif [ $OSREVLEVEL = "55" ]
then
	DRIVER_OSREVLEVEL="550"
elif [ $OSREVLEVEL = "55.1" ]
then
	DRIVER_OSREVLEVEL="551"
else
	DRIVER_OSREVLEVEL="560"
fi

#
# Get Platform name (uname -i didn't exist until 2.5)
#
if [ "${DRIVER_OSREVLEVEL}" -gt "540" ]
then
	PLATFORM=`/usr/bin/uname -i`
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

# 
# first set Unix kernel variable to default depending on
# OS rev and system architecture
#
if [ $OSREVLEVEL = "53" -o $OSREVLEVEL = "54" ]
then
	VMUNIX_FILE=/kernel/unix

elif [ $OSREVLEVEL = "55" -o $OSREVLEVEL = "55.1" ]
then

	MODEL=` uname -a | awk '{print $7}' `
	if [ "x$MODEL" = "x" ]
	then
		MODEL=$ARCH
	fi
	VMUNIX_FILE="/platform/$MODEL/kernel/unix"
else

	# Have to search for the kernel in 2.6 The
	# search path is:
	#
	#	/platform/`uname -i`/kernel
	#	/platform/`uname -m`/kernel
	#	/kernel
	#	/usr/kernel

	if [ -x "/platform/`uname -i`/kernel/unix" ] 
	then 
		VMUNIX_FILE="/platform/`uname -i`/kernel/unix"

	elif [ -x "/platform/`uname -m`/kernel/unix" ]
	then
		VMUNIX_FILE="/platform/`uname -m`/kernel/unix"

	elif [ -x "/kernel/unix" ]
	then
		VMUNIX_FILE="/kernel/unix"

	elif [ -x "/usr/kernel/unix" ]
	then
		VMUNIX_FILE="/usr/kernel/unix"
	else
		VMUNIX_FILE="Cant find 2.6 kernel"
	fi 

fi
#
# next 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
# pphilbin	VMUNIX_FILE=/kernel/unix
    fi
else
	if [ $# -ne 0 ]
	then
		echo "\nusage: sunpcload -A vmunix_file"
		exit 1
	fi
fi
#
# make sure selected kernel is valid
#
if [ ! -x $VMUNIX_FILE ] 
then 
        echo "ERROR: Specified Unix kernel, $VMUNIX_FILE is not valid."
	exit 1
fi 

#echo VMUNIX_FILE=${VMUNIX_FILE}

#
# Setup the appropriate driver files given this architecture and O/S version.
# Note: If this is a SS20 running 2.5 or 2.5.1, use the ddi version of the driver
#
#if [ $PLATFORM = "SUNW,SPARCstation-20" ] && [ $OSREVLEVEL = "55" ] || [ $OSREVLEVEL = "55.1" ]

if [ x$PLATFORM = x"SUNW,SPARCstation-20" ] 
then
	if [ $OSREVLEVEL = "55" -o $OSREVLEVEL = "55.1" ]
	then
		DRIVERFILE="sunpcmod.${ARCH}_${DRIVER_OSREVLEVEL}_ddi.o"
		STOREKFILE="storekernname.${ARCH}_ddi"
	else
		DRIVERFILE="sunpcmod.${ARCH}_${DRIVER_OSREVLEVEL}.o"
		STOREKFILE="storekernname.${ARCH}"
	fi
 
else

	DRIVERFILE="sunpcmod.${ARCH}_${DRIVER_OSREVLEVEL}.o"
	STOREKFILE="storekernname.${ARCH}"
fi

#
# Matrix corner case.  4M/5.6 uses ddi driver so pickup ddi version of storekernname.
#
if [ $ARCH = "sun4m" -a $DRIVER_OSREVLEVEL = "560" ]
then
	STOREKFILE="storekernname.${ARCH}_ddi"
fi

#
# Copy driver files into kernel driver area.
# Make sure files exist first.....
#

if [ -f $DRIVERFILE ]
then

	${CP} sunpcdrv.conf /kernel/drv/sunpcdrv.conf
	${CP} $DRIVERFILE /kernel/drv/sunpcdrv
else
	${ECHO} "\nThe SunPC driver installation has failed. This may be due to your "
	${ECHO} "system having an unsupported SunOS version or system architecture. "
	${ECHO} "Your are currently running SunOS ${OSREVINFO} on a ${ARCH} system."
	${ECHO} "File $DRIVERFILE is missing"
	exit 2
fi

#
# 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
#
# If OS rev level > 5.4, send ioctl to allow driver to be loaded/unloaded
#
	if [ "${DRIVER_OSREVLEVEL}" -gt "540" ]
	then
		./cleardriverunload
	fi
	${REM_DRV} sunpcdrv
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

# fb needs to be accessible. 
${CHMOD} +rw /dev/fb

# 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 $STOREKFILE /kernel/drv/storekernname.${ARCH} 

rm -f /etc/rc2.d/S10storekernname
echo "/kernel/drv/storekernname.${ARCH} ${VMUNIX_FILE}" > /etc/rc2.d/S10storekernname
echo "/usr/sbin/sync" >> /etc/rc2.d/S10storekernname

# save the kernel name in the driver so we can patch the vm functions (or not if ddi)

/kernel/drv/storekernname.${ARCH} ${VMUNIX_FILE}
sync
exit 0
