#!/usr/bin/env bash
# Copyright: 2012 Brian Harring <ferringb@gmail.com>
# License: GPL2/BSD 3 clause

# 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_ENV}" || {
	echo "failed to load ebd env: ${PKGCORE_EBD_ENV}" >&2
	exit -127
}

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

if [[ $# -lt 1 ]]; then
	die "ebuild-helper invoked without a target helper; no args given."
fi

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

failed=false
_helper_issues=()
error() {
	echo "${HELPER_ERROR_PREFIX}: ${@-no message given}" >&2
	_helper[${#_helper[@]}]=${*-no message given}
	failed=true
}

warn() {
	echo "${HELPER_ERROR_PREFIX}: warning, ${@-no message given}" >&2
}

info() {
	echo "${HELPER_ERROR_PREFIX}: $@" >&2
}

check_args() {
	local tense="argument"
	local min=$(( $1 ))
	local max
	[[ ${min} -gt 1 ]] && tense="arguments"
	if [[ $2 == '-' ]]; then
		max=${HELPER_ARG_COUNT}
	elif [[ -z $2 ]]; then
		max=$1
	fi
	max=$(( max ))

	if [[ ${HELPER_ARG_COUNT} -ge ${min} && ${HELPER_ARG_COUNT} -le ${max} ]]; then
		return 0
	fi
	if [[ ${min} -eq ${max} ]]; then
		die "${HELPER_ERROR_PREFIX}: requires exactly ${min} ${tense}, got ${HELPER_ARG_COUNT}"
	elif [[ $2 == '-' ]]; then
		die "${HELPER_ERROR_PREFIX}: requires at least ${min} ${tense}, got ${HELPER_ARG_COUNT}"
	else:
		die "${HELPER_ERROR_PREFIX}: requires at least ${min} ${tense}, and at most ${max} arguments, got ${HELPER_ARG_COUNT}"
	fi
}

check_command() {
	local ret
	"$@"
	ret=$?
	[[ ${ret} == 0 ]] && return 0
	error "exitcode ${ret} from $*"
	return $(( ret ))
}

check_command_or_stop() {
	check_command "$@"
	__helper_check_exit $? "$@ failed, cannot continue"
	return 0
}

MASTER_HELPER_NAME=${1##*/}
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; failing."
	exit -1
fi

invoke_script() {
	[[ $# -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
		# note this can daftly go find a binary...
		HELPER_PATH=$(type -p "${HELPER_NAME}")
		[[ -z ${HELPER_PATH} ]] && die "couldn't find implementation of ${HELPER_NAME}!?"
	fi

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

	source "${HELPER_PATH}"

	if ${failed}; then
		if ${PKGCORE_NONFATAL}; then
			echo "WARNING: nonzero exitcode from ${HELPER_ERROR_PREFIX}" >&2
			# need to track the exit code here...
			return 1
		fi
		kill -s SIGUSR1 ${PKGCORE_EBUILD_PROCESS_PID}
		exit 1
	fi
	return 0
}
invoke_script "$@"
exit $(( $? ))
