#
# Copyright (c) 2022-2023, Jesús Daniel Colmenares Oviedo <DtxdF@disroot.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * 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.
#
# * Neither the name of the copyright holder nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.

lib_load "${LIBDIR}/log"
lib_load "${LIBDIR}/sysexits"

lib_jail_exists()
{
	local jail_name="$1"

	if [ -z "${jail_name}" ]; then
		lib_err ${EX_USAGE} "usage: lib_jail_exists jail_name"
	fi

	if jls -j "${jail_name}" > /dev/null 2>&1; then
		return 0
	else
		return 1
	fi
}

lib_jail_created_by_appjail()
{
	local path
	local jail_name="$1"

	if [ -z "${jail_name}" ]; then
		lib_err ${EX_USAGE} "usage: lib_jail_created_by_appjail jail_name"
	fi

	path=`jls -j "${jail_name}" path`
	if [ "${path}" = "${JAILDIR}/${jail_name}/jail" ]; then
		return 0
	else
		return 1
	fi
}

lib_jailparam_name()
{
	local args="$1"
	local separator="$2"
	local parameter

	if [ -z "${args}" -o -z "${separator}" ]; then
		lib_err ${EX_USAGE} "usage: lib_jailparam_name args separator"
	fi

	args=`printf "%s" "${args}" | sed -Ee 's/^[[:space:]]*//'`
	parameter=`printf "%s" "${args}" | cut -s -d "${separator}" -f1`
	if [ -z "${parameter}" ]; then
		parameter="${args}"
	fi

	printf "%s" "${parameter}"
}

lib_jailparam_value()
{
	local args="$1"
	local separator="$2"

	if [ -z "${args}" -o -z "${separator}" ]; then
		lib_err ${EX_USAGE} "usage: lib_jailparam_value args separator"
	fi

	printf "%s" "${args}" | cut -s -d "${separator}" -f2-
}

lib_split_jailparams()
{
	local parameter="$1"

	if [ -z "${parameter}" ]; then
		lib_err ${EX_USAGE} "usage: lib_split_jailparams parameter"
	fi

	local errmsg ret

	errmsg=`"${UTILDIR}/appjail-config/tok" -- "${parameter}" 2>&1`
	ret=$?

	if [ ${ret} -ne 0 ]; then
		lib_err ${ret} -- "Tokenizer: ${errmsg}"
	fi

	printf "%s\n" "${errmsg}"
}

lib_split_ujailparams()
{
	local parameter="$1"

	if [ -z "${parameter}" ]; then
		lib_err ${EX_USAGE} "usage: lib_split_ujailparams parameter"
	fi

	local errmsg ret

	errmsg=`"${UTILDIR}/appjail-config/tok" -Q -- "${parameter}" 2>&1`
	ret=$?

	if [ ${ret} -ne 0 ]; then
		lib_err ${ret} -- "Tokenizer: ${errmsg}"
	fi

	printf "%s\n" "${errmsg}"
}

lib_ajconf()
{
    APPJAIL_CONFIG_JAILDIR="${JAILDIR}"; export APPJAIL_CONFIG_JAILDIR

    "${UTILDIR}/appjail-config/appjail-config" "$@"
}

lib_jail_setenv()
{
	local environment="$1"

	if [ -z "${environment}" ]; then
		lib_err ${EX_USAGE} "usage: lib_jail_setenv environment"
	fi

	local env_list env_total env_index=0
	local env_var env_name env_val

	env_list=`lib_split_jailparams "${environment}"` || exit $?
	env_total=`printf "%s\n" "${env_list}" | wc -l`

	while [ ${env_index} -lt ${env_total} ]; do
		env_index=$((env_index+1))
		env_var=`printf "%s\n" "${env_list}" | head -${env_index} | tail -n 1`

		env_name=`lib_jailparam_name "${env_var}" =`

		if lib_check_empty "${env_name}"; then
			lib_err ${EX_DATAERR} "Environment variable (${env_index}) is empty."
		fi

		env_val=`lib_jailparam_value "${env_var}" =`

		export "__APPJAIL_RUN_ENVIRONMENT_VAR__${env_name}=${env_val}"
	done
}
