#!/bin/sh

# Copyright (c) 2015-2017 Ad Schellevis <ad@opnsense.org>
# Copyright (c) 2015-2018 Franco Fichtner <franco@opnsense.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

RCORDER="rcorder -s nostart -s firstboot"

# check which services to enable
if [ -f /etc/rc.conf ]; then
	. /etc/rc.conf
fi
if [ -f /etc/rc.conf.local ]; then
	. /etc/rc.conf.local
fi
for RC_CONF in $(find /etc/rc.conf.d -type f); do
	. ${RC_CONF}
done

rc_enabled()
{
	rc_filename=${1}
	name=${2}

	# check if service has a name
	if [ -z "${name}" ]; then
		echo "Error: no name set in ${rc_filename}"
		return 1
	fi

	# check if service has a variable
	rcvar=
	eval "$(grep "^rcvar[[:blank:]]*=" ${rc_filename})"
	if [ -z "${rcvar}" ]; then
		# FreeBSD does this, leave here for debugging
		#echo "Error: no rcvar set in $rc_filename"
		return 1
	fi

	# check if service is enabled
	eval "enabled=\$${rcvar}"
	if [ "${enabled}" != "YES" ]; then
		return 1
	fi

	return 0
}

rc_filenames="$(${RCORDER} /etc/rc.d/[a-z]* /usr/local/etc/rc.d/[a-z]* 2> /dev/null)"
rc_filenames_defer="
/etc/rc.d/ipfw
/usr/local/etc/rc.d/captiveportal
"

for rc_filename in ${rc_filenames_defer}; do
	# exclude deferred scripts from first pass, appended last instead
	rc_filenames=$(echo "${rc_filenames}" | grep -v "^${rc_filename}$")
done

if [ -z "${1}" ]; then
	echo "Error: no action argument given"
	exit 1
fi

# run our bootstrap command on startup
if [ "${1}" == "start" ]; then
	for rc_filename in ${rc_filenames}; do
		eval "$(grep "^name[[:blank:]]*=" ${rc_filename})"

		if ! rc_enabled ${rc_filename} ${name}; then
			continue
		fi

		pre_run_var="${name}_var_script"
		eval "pre_run_cmd=\$${pre_run_var}"
		${pre_run_cmd}
	done
fi

# pass all commands to script now
for rc_filename in ${rc_filenames} ${rc_filenames_defer}; do
	eval "$(grep "^name[[:blank:]]*=" ${rc_filename})"

	if ! rc_enabled ${rc_filename} ${name}; then
		continue
	fi

	${rc_filename} ${1}
done
