#!/sbin/sh
#
# @(#) $Revision: 76.2 $
#
# NOTE:    This script is not configurable!  Any changes made to this
#          scipt will be overwritten when you upgrade to the next
#          release of HP-UX.
#
# WARNING: Changing this script in any way may lead to a system that
#          is unbootable.  Do not modify this script.

#
# Save kernel core dumps
#

PATH=/usr/sbin:/usr/bin:/sbin
export PATH

typeset -r DEFAULT_SAVECORE_DIR=/var/adm/crash

rval=0
set_return() {
	x=$?
	if [ $x -ne 0 ]; then
		echo "EXIT CODE: $x"
		rval=1
	fi
}

case $1 in
start_msg)
	echo "Save system core image if needed"
	;;

'start')
	if [ -f /etc/rc.config.d/savecore ] ; then
	    . /etc/rc.config.d/savecore
	else
	    echo "ERROR: /etc/rc.config.d/savecore defaults file MISSING"
	fi
	#
	# Determine action from runlevel information
	#
	set `who -r` x
	if [ "$9" = 'S' ] || [ "$9" -lt '2' ]; then
	    if [ "$SAVECORE" -eq 1 -a -x /sbin/savecore ]; then
		if [ X$SAVECORE_DIR = X ]; then
		    SAVECORE_DIR="$DEFAULT_SAVECORE_DIR"
		    echo "Savecore directory not set;" \
			 "defaulting to: $SAVECORE_DIR"
		fi

		[ ! -d "$SAVECORE_DIR" ] && mkdir -p "$SAVECORE_DIR"

		case $COMPRESS in
		0)	COMPRESS_OPTS='-Z'	
			;;
		1)	COMPRESS_OPTS='-z'
			;;
		*)	COMPRESS_OPTS=''
			;;
		esac
		{
		    typeset SAVECORE_OPTS=
		    if [ -n "$CHUNK_SIZE" ] ; then
			SAVECORE_OPTS='-s'${CHUNK_SIZE}
	       	    fi
		    if [ -n "$COMPRESS_OPTS" ] ; then
			SAVECORE_OPTS="${SAVECORE_OPTS} ${COMPRESS_OPTS}"
		    fi
		    if [ -n "$MIN_FREE" ] ; then
			SAVECORE_OPTS="${SAVECORE_OPTS} -m${MIN_FREE}"
		    fi
		    if [ -n "$SWAP_LEVEL" ] ; then
			SAVECORE_OPTS="${SAVECORE_OPTS} -w${SWAP_LEVEL}"
		    fi
		    if [ "$FOREGRD" -eq 1 ] ; then
			SAVECORE_OPTS="${SAVECORE_OPTS} -f"
		    fi
		    /sbin/savecore $SAVECORE_OPTS $SAVECORE_DIR
		    rval=$?
		    unset SAVECORE_OPTS
		}

		case $rval in
		3)	echo "EXIT CODE: $rval - " \
			     "Insufficient space: savecore saved partial dump"
			;;
		2)	echo "EXIT CODE: $rval - " \
			     "savecore found no core dump to save"
			;;
		0)	echo "EXIT CODE: $rval"
			;;
		*)	echo "EXIT CODE: $rval"
			rval=1
			;;
		esac
	    else
		rval=2
	    fi
	fi
	;;
*)
	echo "usage: $0 start"
	rval=1
	;;
esac

exit $rval
