#!/sbin/sh
#Tag 0x00000800
#
# Perform a crash dump save.
# "$Revision: 1.14 $"

#
# Get the crash directory from the savecore.options file (if any).
#
OPTIONS=`cat /etc/config/savecore.options 2> /dev/null`
CRASHDIR=""
if [ ! "${OPTIONS}" = "" ] ; then
	for DIR in ${OPTIONS} ; do
		if [ -d "${DIR}" ] ; then
			CRASHDIR="${DIR}"
		fi
	done
	if [ "${CRASHDIR}" = "" ] ; then
		CRASHDIR="/var/adm/crash"
		OPTIONS="${OPTIONS} ${CRASHDIR}"
	fi
else
	OPTIONS="/var/adm/crash"
	CRASHDIR=${OPTIONS}
fi

#
# Set the current bounds value.
#
BOUNDS=`cat ${CRASHDIR}/bounds 2> /dev/null`
BOUNDS=${BOUNDS:=0}

#
# Assign filenames appropriately for the files in ${CRASHDIR}
#
FRU=${CRASHDIR}/fru.${BOUNDS}
UNIX=${CRASHDIR}/unix.${BOUNDS}
VMCORE=${CRASHDIR}/vmcore.${BOUNDS}.comp
SUMMARY=${CRASHDIR}/summary.${BOUNDS}
ANALYSIS=${CRASHDIR}/analysis.${BOUNDS}

CONFIG=/etc/config
IS_ON=/sbin/chkconfig

if $IS_ON verbose ; then
	LOGGER='lfmt -l savecore -s info'
else
	LOGGER='lfmt -l savecore -s info -G 1'
fi

NOMSGSEVERITY=1 export NOMSGSEVERITY

#
# If the savecore option is on, run 'savecore', then run an icrash
# report on the core dumps saved if this is successful.
# Also run savecore if /usr/bin/icrash is not found on the system.
#
if $IS_ON savecore || [ ! -f /usr/bin/icrash ] ; then
	/usr/etc/savecore ${OPTIONS}
	if [ $? -eq 0 -a -f /usr/bin/icrash ] ; then
		/bin/rm -f ${SUMMARY} ${ANALYSIS}
		/usr/bin/icrash -r -a ${SUMMARY} -w ${ANALYSIS} \
			${UNIX} ${VMCORE} 2> /dev/null
		if [ $? -eq 0 ] ; then
			${LOGGER} "Created crash report.\n"
			if [ -f /usr/bin/fru ] ; then
				/bin/rm -f ${FRU}
				/usr/bin/fru -a -w ${FRU} ${UNIX} ${VMCORE} 2> /dev/null
				if [ $? -eq 0 ] ; then
					${LOGGER} "Created fru report.\n"
				else
					/bin/rm -f ${FRU}
				fi
			fi
		else
			/bin/rm -f ${SUMMARY} ${ANALYSIS}
		fi
	fi
else
	if [ -f /usr/bin/icrash ] ; then
		/bin/rm -f ${SUMMARY} ${ANALYSIS}
		/usr/bin/icrash -c -r -a ${SUMMARY} -w ${ANALYSIS} \
			/unix /dev/swap 2> /dev/null

		if [ $? -eq 0 ] ; then
			${LOGGER} "Created crash report.\n"
			if [ -f /usr/bin/fru ] ; then
				/bin/rm -f ${FRU}
				/usr/bin/fru -a -w ${FRU} ${UNIX} ${VMCORE} 2> /dev/null
				if [ $? -eq 0 ] ; then
					${LOGGER} "Created fru report.\n"
				else
					/bin/rm -f ${FRU}
				fi
			fi
			BOUNDS=`expr ${BOUNDS} + 1`
			echo ${BOUNDS} > ${CRASHDIR}/bounds
		else
			/bin/rm -f ${SUMMARY} ${ANALYSIS}
		fi
	fi
fi
