#!/bin/sh
#
#	sunpc_install:	SunPC 4.0 startup program.
#
#	sunpc_install:	Must be run as 'root' on sun4, sun4c, or sun4m
#			architecture systems.
#
#			Must be running SunOS 5.1 or greater.
#
#	sunpc_install:
#
#		Installs the SunPC 4.0 loadable module into /kernel/drv and
#		activates the module.  
#
#		Creates or appends entries to /etc/magic and /etc/filetype to
#		iconically identify SunPC 4.0 filetypes from the
#		filemgr(1) and file(1) programs.
#
#		Copies SunPC 4.0 filemgr icons into /usr/include/images.
#		
#       Copyright (c) 1993, Sun Microsystems, Inc.  All Rights Reserved
#       Sun considers its source code as an unpublished, proprietary
#       trade secret, and it is available only under strict license
#       provisions.  This copyright notice is placed here only to protect
#       Sun in the event the source is deemed a published work.  Dissassembly,
#       decompilation, or other means of reducing the object code to human
#       readable form is prohibited by the license agreement under which
#       this code is provided to the user or company in possession of this
#       copy.
#
#       RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the
#       Government is subject to restrictions as set forth in subparagraph
#       (c)(1)(ii) of the Rights in Technical Data and Computer Software
#       clause at DFARS 52.227-7013 and in similar clauses in the FAR and
#       NASA FAR Supplement.
#

#	The DISKREQD and SWAPREQD values are in K bytes, 1000K is equal to 1 megabyte.

# Swap space required to run sunpc_install, 5M bytes.
SWAPREQD=5000

# Disk space required in /, 100K bytes.
RDISKREQD=100

# Disk space required in /usr, 10K bytes.
UDISKREQD=10

# Disk space required in /etc, 200K bytes.
EDISKREQD=200

# Default kernel pathname.  Must be overidden with 'sunpc_install -A kernel_pathname'
# if the kernel in use is other than /kernel/unix
KERNEL_PATH=/kernel/unix

# Volume Manager Configuration file
VOLD="/etc/vold.conf"
VOLMGT_CHANGED=0
MUST_PATCH=0
OVERRIDE=0

# Set the path 

PATH=/bin:/usr/bin:/usr/ucb:/etc:/usr/etc

#
# Check for usage and process arguments
#

# max command line arguments: "-OVERRIDE -A kernel_pathname" (3)
if [ "$#" -gt "3" ]
then
	echo "usage: sunpc_install -A[ kernel_pathname ]"
	echo "                     -OVERRIDE_PATCH_CHECK"
	exit 1
fi

if [ "$#" -eq "1" ]
then
	if [ "$1" = "-OVERRIDE_PATCH_CHECK" ]
	then
		OVERRIDE=1
	else
		echo "usage: sunpc_install -A[ kernel_pathname ]"
		echo "                     -OVERRIDE_PATCH_CHECK"
		exit 1
	fi

elif [ "$#" -eq "2" ]
then
	if [ "$1" != "-A" ]
	then
		echo "usage: sunpc_install -A [ kernel_pathname ]"
		echo "                     -OVERRIDE_PATCH_CHECK"
		exit 1
	else
		KERNEL_PATH="$2"
	fi
	
elif [ "$#" -eq "3" ]
then
	if [ "$1" = "-OVERRIDE_PATCH_CHECK"  -a "$2" = "-A" ]
	then
		OVERRIDE=1
		KERNEL_PATH="$3"

	elif [ "$1" = "-A" -a "$3" = "-OVERRIDE_PATCH_CHECK" ]
	then
		OVERRIDE=1
		KERNEL_PATH="$2"

	else
		echo "usage: sunpc_install -A [ kernel_pathname ]"
		echo "                     -OVERRIDE_PATCH_CHECK"
		exit 1
	fi
fi

#       Find out where this script lives to determine the distribution location.
FROM_PATH=`echo "$0" | /bin/grep "/"`
 
if [ -n "$FROM_PATH" ]
then
	#in this case $0 is a directory component.
	FIRSTARG=`echo ${FROM_PATH} | /bin/awk -F/ '{ print $1 }'`
	
	if [ -z "$FIRSTARG" ]; then
		SOURCE3=`echo ${0} | /bin/awk -F/ '{ for (i = 1; i < NF; i++)  { printf "%s", $i; printf "/" } }'`
	else
        	SOURCE3=`echo ${0} | /bin/awk -F/ '{ if ($1 == "") printf "/"; for (i = 1; i < NF; i++)  { printf "%s", $i; printf "/" } }'`
	fi
else
	#in this case $0 is not a directory component.
	SOURCE3="./"
fi

cd $SOURCE3
cd ..
SUNPCHOME=`pwd`
export SUNPCHOME

#
# Check to see if we are running S2.4 (By checking the server version of 3400). If
# its running, we must not allow sunpc_install to run because loading the driver
# will cause the servers hwc streams module to be popped and repushed resulting
# in the loss of the system cursor. ServerVersion Exit status codes are:
#
#	Exit code of 0: Server is running and the version matches the argument.
#	Exit code of 1: No Server Running.
#	Exit code of 2: Server is running but does not match the rev level.
#	Exit code of 3: Non-Sun Server is running.
#
$SUNPCHOME/bin/ServerVersion -revge 3300
SERVERVERSION_RET=$?

if [ "$SERVERVERSION_RET" -eq "0" ]
then
	echo "sunpc_install is unable to install the SunPC package."
	echo "You must first exit OpenWindows and then rerun sunpc_install."
	exit 1
fi

#	Check that default or specified KERNEL_PATH exists.
if [ ! -f $KERNEL_PATH ]
then
	echo "$KERNEL_PATH: not found"
	echo " "
	echo "Did you rename your kernel?"
	echo " "
	echo "Try running the startup program again, using the following command:"
	echo " "
	echo "sunpc_install -A[ kernel_pathname ]"
	echo " "
	echo "Substitute the complete path to your kernel for kernel_pathname."
	echo " "
	echo "Example: sunpc_install -A /mykernel"
        exit 1
fi

#       Check for required swap space to run sunpc_install.
SWAPAVAIL=`/usr/sbin/swap -l | /bin/awk -F" " '
 BEGIN	{ s = 0
	  NR = 2 }  			# skip header line
	{ s += $NF }			# sum free blocks column
 END	{ print s/2 }'`		# convert from 512-byte blocks to kbytes

if [ "$SWAPAVAIL" -lt "$SWAPREQD" ]
then
	echo "You need additional swap space to install and run the SunPC software."
	echo " "
	echo ""$SWAPREQD" Kbytes are required, "$SWAPAVAIL" Kbytes are available."
	echo " "
	echo "See the SunPC Users's Guide for information on adding swap space."
	exit 1
fi

#	Check for and set sun4 or sun4c or sun4m kernel architecture.
KARCH=`/bin/uname -m`
if [ "$KARCH" != "sun4" -a "$KARCH" != "sun4c" -a "$KARCH" != "sun4m" ]
then
	echo "You must have a SPARCstation, or a system with sun4, sun4c, or sun4m kernel"
	echo "architecture to install and run the SunPC software."
	exit 1
fi

#	Check for SunOS 5.x or greater
if [ ! -f /usr/bin/uname ]
then
	echo "You must have SunOS version 5.1 or greater to install and"
	echo "run the SunPC 4.0.1 software."
	exit 1
else
	OSREVINFO=`/usr/bin/uname -r`

	OSREVLEVEL=`echo "$OSREVINFO"|/bin/grep "^5."`
	if [ -z "$OSREVLEVEL" ]
	then
		echo "You must have SunOS version 5.1 or greater to install and"
		echo "run the SunPC 4.0.1 software."
		exit 1
	fi
fi

#	Check that user 'root' has invoked sunpc_install.
ISROOT=`/bin/id | /bin/awk '/root/ {print}'`
if [ -z "$ISROOT" ]
then
	echo "You must be a superuser to run the SunPC startup program."
	exit 1
fi

#	 Check the / filesystem for required disk space.
DF=`/bin/df -k /`
DFROOT=`echo $DF | /bin/awk -F" " '{ print $(NF - 2) }'`
if [ "$DFROOT" -lt "$RDISKREQD" ]
then
	echo "There is not enough disk space to run the SunPC startup program."
	echo " "
	echo ""$RDISKREQD" Kbytes are required in the / (root) file system."
        exit 1
fi

#	 Check the /usr filesystem for required disk space.
DF=`/bin/df -k /usr`
DFUSR=`echo $DF | /bin/awk -F" " '{ print $(NF - 2) }'`
if [ "$DFUSR" -lt "$UDISKREQD" ]
then
	echo "There is not enough disk space to run the SunPC startup program."
	echo " "
	echo ""$UDISKREQD" Kbytes are required in the /usr file system."
        exit 1
fi

# 	Check the /etc filesystem for required disk space.
DF=`/bin/df -k /etc`
DFETC=`echo $DF | /bin/awk -F" " '{ print $(NF - 2) }'`
if [ "$DFETC" -lt "$EDISKREQD" ]
then
	echo "There is not enough disk space to run the SunPC startup program."
	echo " "
	echo ""$EDISKREQD" Kbytes are required in the /etc file system."
        exit 1
fi

#
# Disable Volume Floppy Management for Solaris 5.2 and up.
#
if [ -f ${VOLD} ]
then
    if [ ! -f ${VOLD}.beforesunpc ]
    then
        VOLMGT_CHANGED=1
        cp -p ${VOLD} ${VOLD}.beforesunpc
	sed  '/^use floppy drive/ i\
# The next line is commented out to disable Floppy Volume Management for SunPC' ${VOLD} > ${VOLD}.temp1
	sed  '/^use floppy drive/ s/^/#/' ${VOLD}.temp1 > ${VOLD}.temp2

	rm ${VOLD}
	rm ${VOLD}.temp1
	mv ${VOLD}.temp2 ${VOLD}
	/etc/init.d/volmgt stop
	/etc/init.d/volmgt start
        echo "**************************************************************"
        echo ""
	echo "This script has modified the Volume Manager configuration files"
	echo "to disable Volume Manager support for the system floppy drives."
	echo "See the SunPC4.0.1 product notes for instructions if you want"
	echo "to re-enable Volume Manager for the floppy drive."
    fi
fi

#
# Check for patches
# 
if [ "$OVERRIDE" -eq "1" ]
then
    $SUNPCHOME/bin/check_patches override
else
    $SUNPCHOME/bin/check_patches
fi
MUST_PATCH=$?
if [ "$MUST_PATCH" -eq "1" ]
then
    exit 0
fi

cd $SUNPCHOME/drvr

#       Tell what sunpc_install does in a nutshell.
echo	" "
echo	"This program installs the SunPC 4.0.1 device driver and modifies"
echo	"the following system files:"
echo	"	/etc/magic"
echo	"Your original files will be saved under a new name. For example,"
echo	"/etc/magic will become /etc/magic-".
echo    " "
echo    "The SunPC 4.0.1 device driver is now being installed..."


# Load or reload the driver even if its already running (could be a previous version).
./sunpcload -A "$KERNEL_PATH" 
SUNPCLOAD_RET=$?
cd $SUNPCHOME
#
# Exit code of 1 indicates that the SunPC kernel device driver could not be loaded.
#
# Exit code 2 indicates that we do not currently support this OS level.
#
# Other error codes are non fatal, so we can continue.
#
if [ $SUNPCLOAD_RET -eq 1 ]
then
	echo "Unable to install the SunPC device driver."
	echo " "
	echo "Try running the startup program again, using the following command:"
	echo " "
	echo "$SUNPCHOME/bin/sunpc_install -A [ kernel_pathname ]"
	echo " "
	echo "Substitute the complete path to your kernel for kernel_pathname."
	echo " "
	echo "Example: $SUNPCHOME/bin/sunpc_install -A /mykernel"
	exit 1
elif [ $SUNPCLOAD_RET -eq 2 ]
then
    echo "Sunpc_install exiting without successfully installing SunPC."
    echo " "
fi


# Copy SunPC 4.0 filemgr icons to /usr/include/images.
if [ ! -d "/usr/include/images" ]
then
	/bin/mkdir -p /usr/include/images
fi

/bin/cp $SUNPCHOME/images/SunPC.disk.filemgr.icon	/usr/include/images
/bin/cp $SUNPCHOME/images/sunpcrc.filemgr.icon		/usr/include/images

# Update /etc/magic with SunPC 4.0 file information.
if [ ! -f "/etc/magic" ] 
then 
        /bin/touch /etc/magic
fi

MAGIC_IN=`/bin/grep "SunPC 3.0" /etc/magic`
if [ -z "$MAGIC_IN" ]
then
	MAGIC_IN=`/bin/grep "SunPC 4.0" /etc/magic`
	if [ -z "$MAGIC_IN" ]
	then	
		/bin/cp /etc/magic /etc/magic-
		echo "0	long	0xfa33c08e	SunPC 4.0 Hard Disk"		>> /etc/magic
		echo "0	string	#SUNPC_CONFIG	SunPC 4.0 Properties Values"	>> /etc/magic
	fi
else
	/bin/cp /etc/magic /etc/magic-
        /bin/ed /etc/magic > /dev/null <<END
1,\$s/SunPC 3.0/SunPC 4.0/gp
w
q
END

fi

echo " "
echo "The SunPC startup program is finished. Type: exit"
echo "Once you have updated your path, Type: sunpc"
echo ""

if [ "$VOLMGT_CHANGED" -eq "1" ]
then
    echo "---------------------------------------------------------"
    echo "YOU MUST REBOOT BEFORE USING THE FLOPPY DRIVE WITH SUNPC."
    echo "---------------------------------------------------------"
fi

#
# make node to /devices/pseudo/openeepr@0:openprom that SunPC will have
# access to - provided it doesn't already exist.

if [ ! -c /dev/sunpcopenprom ]
then
	/usr/sbin/mknod /dev/sunpcopenprom c 38 0
	/usr/bin/chmod 644 /dev/sunpcopenprom
fi
