#!/usr/bin/env bash

# protect against env screwups.
if [[ -z ${PKGCORE_EBD_PATH} ]]; then
	PKGCORE_EBD_PATH=$(readlink -f "${0}")
	# and go up 3, out of helpers.
	PKGCORE_EBD_PATH=${PKGCORE_EBD_PATH%/*}
	PKGCORE_EBD_PATH=${PKGCORE_EBD_PATH%/*}
	PKGCORE_EBD_PATH=${PKGCORE_EBD_PATH%/*}
fi
export PKGCORE_EBD_PATH

source "${PKGCORE_EBD_PATH}"/exit-handling.bash || {
	echo "failed to load exit-handling library: PKGCORE_EBD_PATH=${PKGCORE_EBD_PATH}" >&2
	exit -127
}

if [[ $# -lt 1 ]]; then
	die "ipc-helper invoked without a target helper arg"
fi

source "${PKGCORE_EBD_PATH}"/ebuild-daemon-lib.bash || \
	die "failed to load ebuild-daemon-lib.bash"
source "${PKGCORE_EBD_PATH}"/isolated-functions.bash || \
	die "failed to load isolated-functions.bash"
source "${PKGCORE_EBD_PATH}"/eapi/depend.bash >&2 || \
	die "failed sourcing eapi/depend.bash"
source "${PKGCORE_EBD_PATH}"/eapi/common.bash >&2 || \
	die "failed sourcing eapi/common.bash"
source "${PKGCORE_EBD_PATH}"/helpers/internals/helper-lib.bash >&2 || \
	die "failed sourcing helpers/internals/helper-lib.bash"

HELPER_ERROR_PREFIX=

if ! ${PKGCORE_PREFIX_SUPPORT:=false}; then
	export ED=${D}
elif [[ ${ED:-unset} == "unset" ]]; then
	error "The variable ED is missing from the environment, but is required for prefix mode."
	exit -1
fi

run_ipc_helper() {
	[[ $# -eq 0 ]] && die "${FUNCNAME}: missing required arguments"
	local HELPER_PATH=$1
	local HELPER_NAME=${1##*/}
	local HELPER_EAPI=${1%/*}
	HELPER_EAPI=${HELPER_EAPI##*/}
	shift
	local HELPER_ARG_COUNT=$#
	if [[ ! -e ${HELPER_PATH} ]]; then
		HELPER_PATH=$(type -p "${HELPER_NAME}")
		[[ -z ${HELPER_PATH} ]] && die "${HELPER_NAME} command not found"
	fi

	local OLD_ERROR_PREFIX=${HELPER_ERROR_PREFIX}
	local HELPER_ERROR_PREFIX=${OLD_ERROR_PREFIX:+${OLD_ERROR_PREFIX}: }${HELPER_NAME}

	local IPC_CMD=${HELPER_NAME}
	source "${HELPER_PATH}"

	# load all parent command opts when running a subcommand that is possibly nested
	local orig_cmd=${HELPER_NAME}
	while [[ ${orig_cmd} != ${IPC_CMD} ]]; do
		orig_cmd=${IPC_CMD}
		source "$(type -p ${IPC_CMD})" >&2 || die "failed sourcing ${IPC_CMD}"
	done

	__ebd_ipc_cmd ${IPC_CMD} "${OPTIONS[*]}" "$@"
}
run_ipc_helper "$@"
exit $(( $? ))
