#! /sbin/sh
# @(#) $Revision: 1.2.98.1 $
###############################################################################
# Usage_exit ()
#
# Function to print a Usage message to standard out, then exit.
# The message is not needed if this is invoked from swinstall.
#
Usage_exit ()
{
	echo "Usage:  $CMD [-k <kernel path>] [-v]"
    	exit $ERROR_EXIT
}

#
######## 
# Parse_cmd
#
# Function to parse the command invocation.  Exit with the Usage message if 
# an error is found, otherwise return 0 and proceed.  Cannot depend on the
# presence of the getopt(1) command.
#
# Note that command line arguments supersede environment variables as well
# as default values.
#
Parse_cmd ()
{

    [[ "$1" = "-?" ]] && Usage_exit	# Explicit request for usage
    while [[ $# -ne 0 ]]
    do
	case $1 in
	-k)	# kernel path
	    shift
	    if [ $# -eq 0 -o -z "$1" ]
	    then
		echo "ERROR:   Improper syntax for $CMD"
		echo "         The -k option requires an argument."
		echo "         \c"
		Usage_exit
	    fi

	    KERNEL_PATH=$1
	    shift
	    ;;
	-v)	# verbose
	    shift
	    VERBOSE=1
	    ;;
	*)	# anything else is bogus
	    echo "ERROR:   Improper syntax for $CMD"
	    echo "         $1 not valid"
	    echo "         \c"
	    Usage_exit
	    ;;
	esac
    done
    return $SUCCESS
}

#############################################################################
#
# MAIN
#
# is_lite returns :
#			0 - if the kernel is considered lite
#			1 - if the kernel is NOT considered lite
#			2 - if an error was encountered during processing
#
	CMD=${0##*/}
	ERROR_EXIT=2
	ISLITE=1	
	VERBOSE=0
	SUCCESS=0
	GENFILE="/tmp/sysfile$$"
	LITE_TUNABLES="/tmp/tuneparms$$"
	KERNEL_PATH="/stand/vmunix"
	UTILS="/usr/lbin/sw/control_utils"
	GET_KDFILE="/usr/lbin/sysadm/get_kdfile"
	GET_SYSFILE="/usr/lbin/sysadm/get_sysfile"

	if [[ $# -gt 0 ]]
	then
		Parse_cmd $@	
	fi

#
# Setup critial SD environment variables (if not already setup by SD).
# Because the sourcing of control_utils *resets* $PATH to $SW_PATH:... ,
# $SW_PATH needs to be properly set-up here.
#
	[[ -z "${SW_ROOT_DIRECTORY}" ]] && SW_ROOT_DIRECTORY="/"
	[[ -z "${SW_PATH}" ]] && \
	SW_PATH="/usr/lbin/sw/bin:/usr/bin:/usr/ccs/bin"

#
# Source control_utils function library
#

	if [[ ! -f $UTILS ]]
	then
		print "ERROR:   Cannot find $UTILS"
    		exit $ERROR_EXIT
	fi
	. $UTILS

#
# get a copy of the systems genfile
#

	if [[ $VERBOSE -ne 0 ]] 
	then
		print "Checking system \"$KERNEL_PATH\" for \"lite\" tuning."
	fi

	#
	# Extract the system file.
	# Run get_kdfile if its present 
	# (for either pre-10.0 or pre-cycleR kernels).
	# If it isn't present (or it fails), then run get_sysfile.
	#
	retval=0
	rm -f $GENFILE 2>/dev/null
	if [[ -x $GET_KDFILE ]]; then
		$GET_KDFILE $KERNEL_PATH > $GENFILE 2>/dev/null
		retval=$?
	fi
	if [[ ! -x $GET_KDFILE || $retval -ne 0 ]]; then
		$GET_SYSFILE $KERNEL_PATH > $GENFILE 2>/dev/null
		retval=$?
	fi

    	if [ $retval -ne $SUCCESS ]
	then
		print "ERROR:   The extraction of a system file from \"$KERNEL_PATH\" failed."
		print "         Verify that \"$KERNEL_PATH\" is a properly built kernel file"
		print "         before trying again."

		rm -f $GENFILE 2>/dev/null
		exit $ERROR_EXIT
    	fi

    	if [[ $retval -eq $SUCCESS && $VERBOSE -eq 1 ]]
	then
		print "* The existing kernel's template file has been extracted."
    	fi


#
# The list of tunables that define a lite kernel.
#
#
	cat > $LITE_TUNABLES <<End-Of-Tunables
nproc		120
ninode 		250
nfile 		400
nflocks		50
npty 		30
msgmni 		20
semmni 		20
shmmni 		50
shmseg 		50
dbc_max_pct	25
dbc_min_pct	2
scroll_lines	70
maxusers	8
End-Of-Tunables


	#
	# Check each tunable against the kernel to see if it is lite. It will
	# be considered lite if it is less than or equal to the lite tunable
	# value.
	#
	# if at least one tunable is lite, the system is considered lite.
	#
	while read param val junk
	do
		#
		# Query system file for parameter.
		#
		curval=`query_systemfile $GENFILE $param`
        	if [[ $? -ne 0 ]]; then
            		print "WARNING: Query of \"$GENFILE\" for"
			print "         \"$param\" failed."
			rm -f $LITE_TUNABLES 2>/dev/null
			rm -f $GENFILE 2>/dev/null
            		exit $ERROR_EXIT
		fi

		if [[ $VERBOSE -eq 1 && -n "$curval" ]]; then
			print "\tTunable \"$param\" is currently \"$curval\", it's \"lite\" value is \"$val\""
		fi

		if [[ -n "$curval" ]]; then
			if [ $curval -le $val ]; then
				ISLITE=0
			fi
		fi

	done < $LITE_TUNABLES

	if [[ $VERBOSE -eq 1 ]]; then
	    if [[ $ISLITE -eq 0 ]]; then
		print "\n* The kernel \"$KERNEL_PATH\" is considered \"lite\""
	    else print "\n* The kernel \"$KERNEL_PATH\" is not considered \"lite\""
	    fi
	fi
	
	rm -f $LITE_TUNABLES 2>/dev/null
	rm -f $GENFILE 2>/dev/null

	exit $ISLITE
