#!/bin/sh
# set -x
#
#	sunpc:	SunPC 4.0 startup script.
#
#	sunpc:	Before running 'sunpc', the SunPC 4.0 start up program
#		named 'sunpc_install' must be run to setup the system.
#
#		Creates the users pc directory if necessary, $HOME/pc.
#
#		If a DOS Windows 1.0 or SoftPC properties file exists, the
#		SunPC properties file called .sunpcrc will be derived from
#		it automatically.
#
#		Creates an emulated hard disk drive in the $HOME/pc
#		directory called C.sunpc3.
#
#	Copyright (c) 1991, 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 SunPC session, 11M bytes.
SWAPREQD=30000
BASESWAP=12000

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

# Size of the default hard disk drive in K bytes, 1M byte.
HDISK_SIZE=1000

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

# Set the kernel architecture in KARCH for use with follow on programs.
KARCH=`/bin/uname -m`

#       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=`echo ${IF_LINK} | /bin/awk '{ printf "%s", $NF }'`
else
        LINK_PATH=$0
fi

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

# Check for SunOS 5.x (vpc2148)
#
if [  -f /usr/bin/uname ]
then
    OSREVINFO=`/usr/bin/uname -r`
    OSREVLEVEL=`echo "$OSREVINFO"|/bin/grep "^5."`
    if [ -z "$OSREVLEVEL" ]
    then
        echo "The SunPC 4.x software only runs on Solaris 2.x."
        echo "You must install SunPC 3.x for use on Solaris 1.x."
        exit 1
    fi
fi

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 ${LINK_PATH} | /bin/awk -F/ '{ for (i = 1; i < NF; i++)  { printf "%s", $i; printf "/" } }'`
	else
		SOURCE3=`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
# by 256. This may need to be further tuned as appropriate. 
# The bugs that this fixes (vpc2025 and vpc2028) are actually kernel
# bugs that we cannot have fixed properly.
# 
oldfdlimit=`ulimit -n`
oldhardfd=`ulimit -H -n`
newfdlimit=`expr $oldfdlimit + 256`
if [ $newfdlimit -le $oldhardfd ]
then
    ulimit -S -n $newfdlimit
else
    ulimit -S -n $oldhardfd
fi

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

#
# Check what SunOS patches the user may have to install
#
#$SUNPCHOME/bin/check_patches
#if [ "$?" -eq 1 ]
#then
#	exit 0
#fi

#	Process arguments and set flags
ARGS=""
HFLAG=0
PFLAG=0
FFLAG=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
	else
		ARGS="$ARGS $1"
		shift
	fi
done

LS_DEV=`/usr/bin/ls -l /devices/pseudo`
MODULE_IN=`echo "$LS_DEV"|/bin/grep "sunpcdrv"`
if [ -z "$MODULE_IN" ]
then
        echo "You must run the startup program before using the SunPC software for the first time."
        echo " "
        echo "To run the startup program:"
        echo " "
        echo "    1. Type: su, then enter your superuser password."
        echo "    2. Type: cd $SUNPCHOME/bin"
        echo "    3. Type: sunpc_install"
        exit 1
fi
 

#	Check for minimum amount of space in /tmp.
DF=`/bin/df -k /tmp`
TMPAVAIL=`echo $DF | /bin/awk -F" " '{ print $(NF - 2) }'`
if [ "$TMPAVAIL" -lt "$TMPREQD" ]
then
	echo "There is not enough space in your /tmp directory to run the SunPC software."
	echo ""$TMPAVAIL" Kbytes are available, "$TMPREQD" Kbytes are required."
	exit 1
fi

# if -h is passed, do not echo startup message.
if [ $HFLAG -eq 1 ]
then
	$SUNPCHOME/bin/spc.sva_svr4_prod -h
	exit $?
fi

# if -h is not passwd, display startup message and continue on.
echo "Opening a SunPC 4.0.1 window..."

# if -p is passed, they know what they want, just do it.
# eject floppy unless FFLAG is set or floppy drive is busy.
if [ $PFLAG -eq 1 ]
then
	if [ $FFLAG -eq 0 ]
	then
		$SUNPCHOME/bin/fdbusy
		if [ $? -eq 0 ]
		then
		    /usr/bin/eject floppy > /dev/null 2>&1
		fi
	fi

	cd $INITIAL_DIR
	exec $SUNPCHOME/bin/spc.sva_svr4_prod $ARGS &
	exit $?
fi

# If -p is not passed do the normal checking and initial setup if needed.

# If the user does not have a home/pc directory, create it.
# If the file ~/.SoftPC exists, convert it to ~/pc/.sunpcrc,
# otherwise a copy of the default .sunpcrc is copied to ~/pc.
# In either case, the default hard drive is created in
# ~/pc if there is enough disk space.

if [ ! -d $HOME/pc ]
then
	/bin/mkdir -p $HOME/pc

	/bin/cp $SUNPCHOME/defaults/.sunpcrc $HOME/pc

	DF=`/bin/df -k  $HOME`
	HOMEAVAIL=`echo $DF | /bin/awk -F" " '{ print $(NF - 2) }'`

	if [ "$HOMEAVAIL" -lt "$HDISK_SIZE" ]
	then
		echo "There is not enough space in the $HOME directory to run the SunPC software."
		echo " "
		echo ""$HOMEAVAIL" Kbytes are available, "$HDISK_SIZE" Kbytes are required."
		exit 1
	else
		echo "Creating the SunPC emulated hard drive. \c"
		echo "One minute, please..."
		$SUNPCHOME/bin/create_disk $HOME/pc/C.sunpc3 20
		echo "Done."
	fi
fi

# The user may already have a home/pc directory.
# Check for a SunPC configuration file.  If it does not exist, check 
# for a ~/pc/setup.pcs file and convert it to ~/pc/.sunpcrc.  If ~/pc/setup.pcs
# does not exist, check for a ~/.SoftPC configuration file and convert it
# to ~/pc/.sunpcrc.  If no configuration files exist copy the default .sunpcrc
# from the distribution to the ~/pc directory.

if [ ! -f $HOME/pc/.sunpcrc ]
then
	/bin/cp $SUNPCHOME/defaults/.sunpcrc $HOME/pc

	if [ ! -f $HOME/pc/C.sunpc3 ]
	then
		DF=`/bin/df -k $HOME`
		HOMEAVAIL=`echo $DF | /bin/awk -F" " '{ print $(NF - 2) }'`
		if [ "$HOMEAVAIL" -lt "$HDISK_SIZE" ]
        	then
			echo "There is not enough space in the $HOME directory to run the SunPC software."
			echo " "
			echo ""$HOMEAVAIL" Kbytes are available, "$HDISK_SIZE" Kbytes are required."
			exit 1
        	else
			echo "Creating the SunPC emulated hard drive. \c"
			echo "One minute, please..."
			$SUNPCHOME/bin/create_disk $HOME/pc/C.sunpc3 20
			echo "Done."
        	fi
	fi
fi

# Remove cmos.ram file
#	This could be in either Home or Home/pc, check for
#	both and remove.

if [ -f $HOME/cmos.ram ]
then
	/bin/rm $HOME/cmos.ram
fi

if [ -f $HOME/pc/cmos.ram ]
then
	/bin/rm $HOME/pc/cmos.ram
fi

# 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
      
PCMEM=`grep PC_MEMORY_SIZE $HOME/pc/.sunpcrc | /bin/awk '{ print $2 }'`

# 2MB default
if [ -z "$PCMEM" ]
then
    PCMEM=2
fi
SWAPREQD=`expr $PCMEM "*" 1000 + $BASESWAP`

if [ "$SWAPAVAIL" -lt "$SWAPREQD" ]
then
	echo "There is not enough swap space on your system to run the SunPC software."
	echo ""$SWAPAVAIL" Kbytes are available, "$SWAPREQD" Kbytes are required."
        exit 1
fi

# Run it.
# Eject floppy unless FFLAG is set or floppy drive is busy.

if [ $FFLAG -eq 0 ]
then  
	$SUNPCHOME/bin/fdbusy
	if [ $? -eq 0 ]
	then
	    /usr/bin/eject floppy > /dev/null 2>&1
	fi
fi

cd $INITIAL_DIR
exec $SUNPCHOME/bin/spc.sva_svr4_prod $ARGS &
exit $?
