#!/bin/sh

# Copyright (c) 2024 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 ``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 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.

set -e

COMPONENT=sys/netpfil/pf

TESTDIR=/usr/src/tests/${COMPONENT}
DESTDIR=/usr/tests/${COMPONENT}

if [ "$(id -u)" != "0" ]; then
	echo "Must be root." >&2
	exit 1
fi

DO_ALL=
DO_BOOTSTRAP=
DO_DEBUG=
DO_LIST=
DO_INJECT=yes

while getopts abdlrV OPT; do
	case ${OPT} in
	a)
		DO_ALL="-a"
		;;
	b)
		DO_BOOTSTRAP="-b"
		;;
	d)
		DO_DEBUG="-d"
		;;
	l)
		DO_LIST="-l"
		;;
	r)
		# referenece testing via /usr/tests
		unset DO_INJECT
		;;
	V)
		DO_VERBOSE="-V"
		;;
	*)
		echo "Usage: man ${0##*/}" >&2
		exit 1
		;;
	esac
done

shift $((OPTIND - 1))

if [ -n "${DO_VERBOSE}" ]; then
	set -x
fi

if [ -n "${DO_BOOTSTRAP}" ]; then
	opnsense-update -Q

	# XXX needed for inject mode (default) but not sure if the best way
	#opnsense-code src
fi

if [ -n "${DO_INJECT}" -a ! -d "${TESTDIR}" ]; then
	echo "Source directory not found: ${TESTDIR}" >&2
	exit 1
fi

if [ ! -d "${DESTDIR}" ]; then
	echo "Target directory not found: ${DESTDIR}" >&2
	exit 1
fi

# clear from previous run
rm -rf ${DESTDIR}/_*

list()
{
	if [ -z "${DO_INJECT}" ]; then
		cat ${DESTDIR}/Kyuafile | tr '"' ' ' | while read PRE NAME MORE; do
			echo ${NAME}
		done
	else
		for TEST in $(find -s ${TESTDIR} -name "*.sh"); do
			TEST=$(basename ${TEST})
			echo ${TEST%.sh}
		done
	fi
}

if [ -n "${DO_LIST}" ]; then
	list
	exit 0
fi

TESTS=${@}
if [ -n "${DO_ALL}" ]; then
	TESTS=$(list)
fi

if [ -z "${TESTS}" ]; then
	echo "Nothing to do."
	exit 0
fi

# XXX kldload required things now as kyua is powerless (but complains)

# set up a shadow config
cat > ${DESTDIR}/_Kyuafile << EOF
-- Automatically generated by bsd.test.mk.

syntax(2)

test_suite("FreeBSD")

EOF

FINAL=

for TEST in ${TESTS}; do
	if [ -n "${DO_INJECT}" ]; then
		CASE=

		if [ -n "${DO_DEBUG}" ]; then
			if [ -n "${TEST%%*:*}" ]; then
				echo "No test case given in debug mode, use '${TEST}:case'" >&2
				exit 1
			fi

			CASE=:${TEST##*:}
			TEST=${TEST%%:*}
		fi

		if [ ! -f  ${TESTDIR}/${TEST}.sh ]; then
			echo "Source file not found: ${TESTDIR}/${TEST}.sh" >&2
			exit 1
		fi

		cat >> ${DESTDIR}/_Kyuafile << EOF
atf_test_program{name="_${TEST}", is_exclusive=true}
EOF

		cat > ${DESTDIR}/_${TEST} << EOF
#! /usr/libexec/atf-sh

$(cat ${TESTDIR}/${TEST}.sh)
EOF

		chmod 555 ${DESTDIR}/_${TEST}
	fi

	FINAL="${FINAL} ${DO_INJECT+_}${TEST}${CASE}"
done

if [ -z "${DO_DEBUG}" ]; then
	exec kyua test -k ${DESTDIR}/${DO_INJECT+_}Kyuafile ${FINAL}
else
	for TEST in ${FINAL}; do
		# only support first one
		exec kyua debug -k ${DESTDIR}/${DO_INJECT+_}Kyuafile ${TEST}
	done
fi
