#!/bin/sh 
#set -x
#
#	sunpc:	SunPCi startup script.
#
#	@(#)sunpci	1.33     07/14/99
#
#	Copyright (c) 1998, 1999, 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.
#
# Set the search path.
PATH=/bin:/usr/bin:/usr/ucb:/etc:/usr/etc

# Swap space required to start a SunPCi session, 11M bytes.
SWAPREQD=12000
BASESWAP=12000

# /tmp space required to start a SunPCi session, 1M byte.
TMPREQD=1000

# variable used to turn off version checking for shared libraries...
LD_NOVERSION=1
export LD_NOVERSION

# Record the starting point so we can return after determining $SUNPC2HOME.
INITIAL_DIR=`pwd`

#       If sunpc is invoked from a symbolic link, find out where it points to and use
#       the pointer to determine the distribution location. This works for one level of
#       symlink only.
IF_LINK=`/bin/ls -l "$0" | /bin/grep "lrwxrwxrwx"`
if [ -n "$IF_LINK" ]
then
        LINK_PATH=`/usr/bin/echo ${IF_LINK} | /bin/awk '{ printf "%s", $NF }'`
else
        LINK_PATH=$0
fi

#       Find out where this script lives, and determine $SUNPCIIHOME.
FROM_PATH=`/usr/bin/echo "$LINK_PATH" | /bin/grep "/"`

# Check for Solaris 2
#
if [  -f /usr/bin/uname ]
then
    OSREVINFO=`/usr/bin/uname -r`
    OSREVLEVEL=`/usr/bin/echo "$OSREVINFO"|/bin/grep "^5."`
    if [ -z "$OSREVLEVEL" ]
    then
        /usr/bin/echo `gettext "The SunPCi software only runs on Solaris 2.5.1 or later."`
        exit 1
    fi

    #
    # Change OSREVLEVEL from 5.x format to 5x format.
    #
    OSREVLEVEL=`/usr/bin/echo "$OSREVINFO"| sed 's/\.//'`

    if [ "$OSREVLEVEL" -lt "55" ]
    then
        /usr/bin/echo `gettext "The SunPCi software only runs on Solaris 2.5.1 or later."`
        exit 1
    fi

fi

if [ -n "$FROM_PATH" ]
then
	#in this case $0 is a directory component.
	FIRSTARG=`/usr/bin/echo ${FROM_PATH} | /bin/awk -F/ '{ print $1 }'`
 
	if [ -z "$FIRSTARG" ]; then
		SOURCE3=`/usr/bin/echo ${LINK_PATH} | /bin/awk -F/ '{ for (i = 1; i < NF; i++)  { printf "%s", $i; printf "/" } }'`
	else
		SOURCE3=`/usr/bin/echo ${LINK_PATH} | /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

#
# Increase the number of file descriptors available to the process. Set the 
# soft limit to the hard limit.
# 
hardlimit=`ulimit -H -n`
ulimit -S -n $hardlimit

cd $SOURCE3
cd ..
SUNPCIIHOME=`pwd`
export SUNPCIIHOME
# check to see what version is this and set DOS_VERSION to that


#
# I18N stuff
#
TEXTDOMAIN=sunpci_script
TEXTDOMAINDIR=$SUNPCIIHOME/lib/locale
export TEXTDOMAIN TEXTDOMAINDIR

if [ -z "${LANG}" ] 
then
	LANG=C
fi
export LANG

if [ -f $SUNPCIIHOME/defaults/C.7.01.template ]
then
	DOS_VERSION=7.01
	export DOS_VERSION
else
        /usr/bin/echo `gettext "Can't find the SunPCi defaults directory"`
        exit 1
fi


#	Process arguments and set flags
ARGS=""
HFLAG=0
PFLAG=0
FFLAG=0
CFLAG=0
while [ $# -ne 0 ]; do

	if [ $1 = "-h" ]
	then
		HFLAG=1
		ARGS="$ARGS $1"
		shift
	elif [ $1 = "-p" ]
	then
		PFLAG=1
		ARGS="$ARGS $1"
		shift
	elif [ $1 = "-f" ]
	then
		FFLAG=1
		shift
	elif [ $1 = "-c" ]
	then
		CFLAG=1
		ARGS="$ARGS $1"
		shift
	else
		ARGS="$ARGS $1"
		shift
	fi
done

LS_DEV=`/usr/bin/ls -l /dev`
MODULE_IN=`/usr/bin/echo "$LS_DEV"|/bin/grep "sunpcidrv"`
if [ -z "$MODULE_IN" ]
then
        /usr/bin/echo `gettext "The SunPCi device driver is not installed."`
        /usr/bin/echo " "
        /usr/bin/echo `gettext "To run the startup program:"`
        /usr/bin/echo " "
        /usr/bin/echo "    " `gettext "1. Type: su, then enter your superuser password."`
        /usr/bin/echo "    " `gettext "2. Type: cd $SUNPCIIHOME/drivers/solaris"`
        /usr/bin/echo "    " `gettext "3. Type: ./sunpcload"`
        exit 1
fi
 
# Check for minimum amount of space in /tmp.
DF=`/bin/df -k /tmp`
TMPAVAIL=`/usr/bin/echo $DF | /bin/awk -F" " '{ print $(NF - 2) }'`
if [ "$TMPAVAIL" -lt "$TMPREQD" ]
then
	/usr/bin/echo `gettext "There is not enough space in your /tmp directory to run the SunPCi software."`
	/usr/bin/echo $TMPAVAIL `gettext "Kbytes are available,"`
	/usr/bin/echo $TMPREQD `gettext "Kbytes are required."`
	exit 1
fi

#
# If the NVL_INTERFACE environment variable is already set, assume that the user
# has set it correctly. Otherwise, check to see what network interfaces this
# machine has.  If its standalone, default to the loopback port. If there is
# only 1 interface, use it otherwise print a warning and default to the loopback
# port so that we don't select the wrong interface.
#
if [ "$NVL_INTERFACE" = "" ]
then

	lines=`/usr/bin/netstat -in | awk '{ if($0 == "") exit; print $0}'  | /usr/bin/wc -l`
	lines=`expr $lines`

	if [ $lines -eq 2 -o $lines -gt 3 ]
	then
		NVL_INTERFACE=lo0
		if [ $lines -gt 3 ]
		then
			/usr/bin/echo `gettext "SunPCi: This machine has multiple network interfaces. If you are using"`
			/usr/bin/echo "       " `gettext "SunPC networking, you need to set the environment variable NVL_INTERFACE"`
			/usr/bin/echo "       " `gettext "to indicate the desired network interface. Type netstat -i for a list "`
			/usr/bin/echo "       " `gettext "of interfaces on this machine."`
		fi
	else
name=`/usr/bin/netstat -i |  awk '{ if($0 == "") exit; print $0}' | /usr/bin/tail -1 |
/usr/bin/cut -f1 -d' '`
		NVL_INTERFACE=$name
	fi

	export NVL_INTERFACE

fi

# If the user does not have a home/pc directory, create it.
# Copy the default .sunpcrc to ~/pc.
# In either case, the default hard drive is created in
# ~/pc if there is enough disk space.

NEED_NEW_DRIVE=0

if [ ! -d $HOME/pc ]
then
	NEED_NEW_DRIVE=1
	/bin/mkdir -p $HOME/pc
fi

if [ ! -f $HOME/pc/SunPC.ini ]
then
	NEED_NEW_DRIVE=1
	/bin/cp $SUNPCIIHOME/defaults/SunPC.ini $HOME/pc
fi

if [ ! -f $HOME/pc/pc8x16s.bdf ]
then
	/bin/cp $SUNPCIIHOME/defaults/pc8x16s.bdf $HOME/pc
fi

if [ ! -f $HOME/pc/fonts.dir ]
then
	/bin/cp $SUNPCIIHOME/defaults/fonts.dir $HOME/pc
fi

XFILESEARCHPATH=$SUNPCIIHOME/lib/locale/$LANG/LC_MESSAGES/%N:$XFILESEARCHPATH
export XFILESEARCHPATH
  
# Check for minimum amount of swap space required to run SunPC.

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 [ -z "$SWAPAVAIL" ]
then
	SWAPAVAIL=0
fi

if [  ! -f /usr/lib/rmmount/action_sunpci.so.1  ]
then
	/usr/bin/echo `gettext "It appears that you have just installed the sunpci patch" `
	/usr/bin/echo `gettext "Please as root run" `
	/usr/bin/echo " "
	/usr/bin/echo `gettext "sunpciupdate" `
	/usr/bin/echo " "
	/usr/bin/echo `gettext "to complete the installation. It is located in ...SUNWspci/bin" `
	/usr/bin/echo `gettext "directory (if installed to default directory, /opt/SUNWspci/bin)" `
	/usr/bin/echo " "
	/usr/bin/echo `gettext "See the patch README for full instructions" `
	exit 1
BIOS_VERSION="`$SUNPCIIHOME/bin/sunpciflash -v -f $SUNPCIIHOME/bios/sunpci.bin `"
fi

RET="$?"
if [ "$RET" != "0" ]
then
	if [ "$RET" -eq "12" ]
	then
		/usr/bin/echo `gettext "It appears that your solaris drivers are out of date" `
		/usr/bin/echo `gettext "Please as root run" `
		/usr/bin/echo " "
		/usr/bin/echo `gettext "sunpcload" `
		/usr/bin/echo " "
		/usr/bin/echo `gettext "to complete the installation. It is located in ...SUNWspci/drivers/solaris" `
		/usr/bin/echo `gettext "directory (if installed to default directory, /opt/SUNWspci/bin)" `
		exit 1
	else
		/usr/bin/echo `gettext "The sunpci startup script is unable to obtain the BIOS version" `
		/usr/bin/echo `gettext "Your SunPCi card may not operate correctly." `
		/usr/bin/echo " "
		/usr/bin/echo `gettext "Please check for software updates at: http://www.sun.com/desktop/products/sunpci" `
		/usr/bin/echo " "
	fi

else

	FILE_VERSION="`echo $BIOS_VERSION|cut -f1 -d' '`"
	CARD_VERSION="`echo $BIOS_VERSION|cut -f2 -d' '`"
	FILE_VERSION_INT=$FILE_VERSION*1000
	CARD_VERSION_INT=$CARD_VERSION*1000

	if [ $FILE_VERSION_INT -gt $CARD_VERSION_INT ]
	then
		printf "`gettext \"Warning: BIOS version %s on the SunPCi card\"`" $CARD_VERSION
		printf "\n\t"
		printf "`gettext \"differs from BIOS version %s in %s/bios/sunpci.bin\"`" $FILE_VERSION $SUNPCIIHOME
		printf "\n\t"
		printf "`gettext \"Your SunPCi card may not operate correctly\"`"
		/usr/bin/echo "\n\n"
	fi
	
fi

#
# check to see if /etc/devlink.tab was updated correctly. If not,
# this might indicate the system has an old OPB
#

if [ -f /etc/devlink.tab ]
then
 if [ "`grep pci108e,5043 /etc/devlink.tab`" = "" -o ! -c /dev/sunpcidrv2 ]
 then
   echo "`gettext \"Warning: /etc/devlink.tab not updated correctly\"`"
   echo "\t`gettext \"Perhaps the system OBP needs to be updated\"`"
 fi
else
 echo "Warning: can not file /etc/devlink.tab"
fi

# display startup message.
/usr/bin/echo `gettext "Opening a SunPCi window..."`

cd $INITIAL_DIR
($SUNPCIIHOME/bin/sunpcbinary $ARGS ;
if [ -x /usr/openwin/bin/xset ]
then
	/usr/openwin/bin/xset r on
fi)&
