#!/bin/sh
# set -x
#
#	sunpc:	SunPC startup script.
#
#	sunpc:	Before running 'sunpc', the SunPC start up program
#		named 'sunpc_install' must be run to setup the system.
#
#		Creates the users pc directory if necessary, $HOME/pc.
#
#		Creates an emulated hard disk drive in the $HOME/pc
#		directory called C.sunpc3.
#
#	Copyright (c) 1997, 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`

# Determine the LANG then set DFBOVERRIDE if ja ko zh zh_TW

LOCALE=`locale | grep LANG | sed -e 's/LANG=//'`
case $LOCALE in
	ja* | ko* | zh* )
		DFBOVERRIDE=1;
		export DFBOVERRIDE;;
esac

#       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 $SUNPCHOME.
FROM_PATH=`/usr/bin/echo "$LINK_PATH" | /bin/grep "/"`

# Check for SunOS 5.x (vpc2148)
#
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 "The SunPC software only runs on Solaris 2.4 and greater."
        /usr/bin/echo "You must install SunPC 3.x for use on Solaris 1.x."
        exit 1
    fi

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

    if [ "$OSREVLEVEL" -lt "54" ]
    then
        /usr/bin/echo "The SunPC software only runs on Solaris 2.4 and greater."
        exit 1
    fi

fi

#
# Should NOT be root to run SunPC. 
#
ISROOT=`/bin/id | /bin/awk '/root/ {print}'`
if [ -n "$ISROOT" ]
then
	/usr/bin/echo "SunPC may NOT run correctly as root. Please run in user mode"
	/usr/bin/echo "SunPC script is exiting."
	exit 1
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
# 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 to see what version is this and set DOS_VERSION to that

if [ -f $SUNPCHOME/defaults/C.7.01.sunpc3 ]
then
	DOS_VERSION=7.01
	export DOS_VERSION
elif [ -f $SUNPCHOME/defaults/C.6.22.sunpc3 ]
then
	DOS_VERSION=6.22
	export DOS_VERSION
fi


#	Process arguments and set flags
ARGS="-visual PseudoColor -depth 8"
HFLAG=0
PFLAG=0
FFLAG=0
WFLAG=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
	elif [ $1 = "-w" ]
	then
		WFLAG=1
		$SUNPCHOME/bin/readwabimap
		shift
	else
		ARGS="$ARGS $1"
		shift
	fi
done

if [ $WFLAG -eq 1 ]
then
	if [ $CFLAG -eq 1 ]
	then
		/usr/bin/echo "you can not use the -c <command> switch with the -w switch"
	fi

	ARGS="$ARGS -c r:\tmp\wabi.bat"
#	/usr/bin/echo $ARGS
fi

LS_DEV=`/usr/bin/ls -l /devices/pseudo`
MODULE_IN=`/usr/bin/echo "$LS_DEV"|/bin/grep "sunpcdrv"`
if [ -z "$MODULE_IN" ]
then
        /usr/bin/echo "You must run the startup program before using the SunPC software for the first time."
        /usr/bin/echo " "
        /usr/bin/echo "To run the startup program:"
        /usr/bin/echo " "
        /usr/bin/echo "    1. Type: su, then enter your superuser password."
        /usr/bin/echo "    2. Type: cd $SUNPCHOME/bin"
        /usr/bin/echo "    3. Type: sunpc_install"
        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 "There is not enough space in your /tmp directory to run the SunPC software."
	/usr/bin/echo ""$TMPAVAIL" Kbytes are available, "$TMPREQD" 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 | /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 "SunPC: This machine has multiple network interfaces. If you are using"
			/usr/bin/echo "       SunPC networking, you need to set the environment variable NVL_INTERFACE"
			/usr/bin/echo "       to indicate the desired network interface. Type netstat -i for a list "
			/usr/bin/echo "       of interfaces on this machine."
		fi
	else
		name=`/usr/bin/netstat -i | /usr/bin/tail -1 | /usr/bin/cut -f1 -d' '`
		NVL_INTERFACE=$name
	fi

	export NVL_INTERFACE

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.
/usr/bin/echo "Opening a SunPC 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.
# Copy the default .sunpcrc 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=`/usr/bin/echo $DF | /bin/awk -F" " '{ print $(NF - 2) }'`

	if [ "$HOMEAVAIL" -lt "$HDISK_SIZE" ]
	then
		/usr/bin/echo "There is not enough space in the $HOME directory to run the SunPC software."
		/usr/bin/echo " "
		/usr/bin/echo ""$HOMEAVAIL" Kbytes are available, "$HDISK_SIZE" Kbytes are required."
		exit 1
	else
		/usr/bin/echo "Creating the SunPC emulated hard drive. \c"
		/usr/bin/echo "One minute, please..."
		$SUNPCHOME/bin/create_disk $HOME/pc/C.sunpc3 20
		/usr/bin/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 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=`/usr/bin/echo $DF | /bin/awk -F" " '{ print $(NF - 2) }'`
		if [ "$HOMEAVAIL" -lt "$HDISK_SIZE" ]
        	then
			/usr/bin/echo "There is not enough space in the $HOME directory to run the SunPC software."
			/usr/bin/echo " "
			/usr/bin/echo ""$HOMEAVAIL" Kbytes are available, "$HDISK_SIZE" Kbytes are required."
			exit 1
        	else
			/usr/bin/echo "Creating the SunPC emulated hard drive. \c"
			/usr/bin/echo "One minute, please..."
			$SUNPCHOME/bin/create_disk $HOME/pc/C.sunpc3 20
			/usr/bin/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=`/bin/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
	/usr/bin/echo "There is not enough swap space on your system to run the SunPC software."
	/usr/bin/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 $?
