#!/bin/sh

# Set default values
APPDIR=${APPDIR:-..}
VERBOSE=${VERBOSE:-0}

. ${APPDIR}/common.subr

sect "Operating system information"

cat <<EOF
Operating system release:	`uname -sr`
OS architecture:		`uname -m`
Hostname:			`hostname`
Kernel build dir location:	`sysctl kern.version | grep / | cut -d: -f2`
Currently booted kernel:	`sysctl -n kern.bootfile`
EOF

if is_verbose 1; then

cat <<EOF
Kernel release:			`sysctl -n kern.osreldate`
Kernel scheduler used:		`sysctl -n kern.sched.name`
Kernel secure level:		`sysctl -n kern.securelevel`
EOF

if ! is_jailed; then
	echo -e "Jailed system\t\t\tYes"
	warn "Running $0 from within a jail is not supported and some \
features may not work properly."
else
	echo -e "Jailed system\t\t\tNo"
fi

fi

# Kernel modules
MODS=`kldstat | grep -vw "Refs\|kernel" | awk '{print $5}'`
if [ -n "$MODS" ]; then
	echo -e "\nCurrently loaded kernel modules (kldstat(8)):"
	echo "$MODS"
	
	if check_privs /boot/loader.conf; then
		# XXX: this may reveal some false positives because of modules
		# loaded from rc.d scripts.
		for m in $MODS; do
			_m=$(echo $m | sed -e 's/\.ko$//')
			if ! grep -q "^${_m}_load" /boot/loader.conf; then
				warn "The $m module is loaded, however it is \
not being loaded upon the system boot time from /boot/loader.conf."
			fi
		done
	fi
else
	echo -e "\nNo additional kernel modules are currently loaded."
fi

echo

info "For security recommendations see the security(7) man page."
info "For system tuning advice, see the tuning(7) man page."
info "To view various system statistics use the systat(1) tool."

subsect "\nBootloader settings"
if check_privs /boot/loader.conf; then
	echo "The /boot/loader.conf has the following contents:"
	cat /boot/loader.conf | grep -v "^#"
	info "See the loader.conf(5) manual page for more information."
	echo
fi
if check_privs /boot.config; then
	echo "The /boot.config has the following contents:"
	cat /boot.config | grep -v "^#"
	info "See the boot.config(5) manual page for more information."
	echo
fi

subsect "System uptime:"
echo "System is up since `sysctl -n kern.boottime | awk '{print $9,$10,$11,$12,$13}'`"
uptime

subsect "\nProcess statistics:"
top -d 1 | grep "processes:"

if [ -d /var/crash ] && ls /var/crash/vmcore* >/dev/null 2>&1; then
	subsect "\nCrash dumps:"
	ls -l /var/crash/vmcore*
	echo
	info "See http://www.freebsd.org/doc/en/books/developers-handbook/kerneldebug.html \
to find out how to debug kernel crash dumps"
fi

# Display informations about jails
if is_jailed && [ "" != "`jls`" ]; then
	echo
	subsect "Jail related information:"
	jls
fi

exit 0
