#!/bin/sh

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

. ${APPDIR}/common.subr

#
# run_rc_script file arg
#	Start the script file with arg, and correctly handle the
#	return value from the script. If file appears to be a
#	backup or scratch file, ignore it. Otherwise if it is
#	executable run as a child process.
#
run_rc_script()
{
	local _file _arg
	_file=$1
	_arg=$2

	case "$_file" in
	*[~#]|*.OLD|*.bak|*.orig|*,v)	# scratch file - skip it
		warn "Ignoring scratch file $_file - you may want to delete it."
		;;
	# Ignore a few scripts that are too verbose or are not working well with
	# sysinfo (side effects e.g. creating md devices etc)
	*/netoptions|*/newsyslog|*/ip6addrctl|*/pf|*/power_profile|*/securelevel|*/tmp)
		;;
	*)
		if [ -x $_file ]; then
			( trap "echo Script $_file interrupted; kill -QUIT $$" 3
			  trap "echo Script $_file interrupted; exit 1" 2
			  set $_arg; . $_file )
		fi
		;;
	esac
}

sect "Information related to services"

if ! is_root; then
	warn "Running $0 as an unprivileged user may result in incorrect information."
	echo
fi

# Load the system configuration files
getsysconf

debug "Local startup: $local_startup"
startup_dirs="$local_startup /etc/rc.d"

RUN=""
NOTRUN=""

for dir in ${startup_dirs}; do
	if [ -d "${dir}" ]; then
		for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
			debug "Processing file $file."

			case "$file" in
			/usr/local*)
				STAT=`run_rc_script $file onestatus 2>/dev/null`
				;;
			*)
				STAT=`run_rc_script $file status 2>/dev/null`
				;;
			esac

			if [ -n "$STAT" ]; then
				# XXX: 8.0 is too verbose for scripts not set to YES
				if echo "$STAT" | grep -wq "Cannot"; then
					continue
				fi

				# Sort services to running and not running sections
				if echo "$STAT" | grep -q "is running"; then
					RUN="$RUN
$STAT"
				else
					eval `grep name= $file`
					if checkyesno ${name}_enable; then
						warn "${name} is supposed to be running, but it isn't."
					fi
					NOTRUN="$NOTRUN
$STAT"
				fi
			fi
		done
	fi
done

subsect "\nRunning services"

# workaround for scripts being present twice
RUN=$(echo "$RUN" | sort | uniq)

echo "$RUN" | while read line; do
	subsubsect "$line"

	if is_verbose 1; then
		pid=$(echo $line | awk '{print $6}' | tr -d ".")
		if [ -n "$pid" ]; then
			ps -ww -p $pid -o command | tail -n 1

			sockstat -l46 | grep -w $pid | awk '{print $1,$5,$6}'
			echo
		fi
	fi
done

echo

subsect "Not running services"

# workaround for scripts being present twice
NOTRUN=$(echo "$NOTRUN" | sort | uniq)

echo "$NOTRUN" | while read line; do
	subsubsect "$line"
done

echo

info "Configuration of system-wide services is located in the /etc/ directory."
info "Configuration of 3rd party services can be found in the /usr/local/etc/ directory."

# SSH sanity check
if check_privs /etc/ssh/sshd_config; then
	if grep -Eq "^PermitRootLogin[[:space:]]+yes" /etc/ssh/sshd_config; then
		echo
		warn "OpenSSH is configured to allow remote root logins."
		warn "You should consider turning this option off in /etc/ssh/sshd_config."
	fi
fi

exit 0
