# This file is part of shellfire core. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/core/master/COPYRIGHT. No part of shellfire core, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2014-2015 The developers of shellfire core. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/core/master/COPYRIGHT.


core_usesIn core compatibility
core_usesIn core/variable array

core_functions_register()
{
	local functionsVariableName="$1"
	shift 1
	
	core_variable_array_append "$functionsVariableName" "$@"
	core_dependency_declaresAsArray "$functionsVariableName"
}

core_functions_execute()
{
	local functionsVariableName="$1"
	shift 1
	
	if core_variable_isSet "$functionsVariableName"; then
		core_variable_array_iterateAsCallbacks "$functionsVariableName" "$@"
	fi
}

# Not perfect by any means; an expanded environment variable with content of 'a shell function', say, could fail this test
core_functions_exists()
{
	local functionName="$1"
	if ! type "$functionName" 1>/dev/null 2>/dev/null; then
		return 1
	fi
	
	local arg0
	local arg1
	local arg2
	local arg3
	local arg4
	IFS=' ' read -r arg0 arg1 arg2 arg3 arg4 <<-EOF
		$(type "$functionName")
	EOF
	
	case "$arg3" in
		
		# dash
		shell)
			if [ "$arg4" = 'function' ]; then
				return 0
			fi
		;;
		
		# bash
		function)
			return 0
		;;
		
		# AIX shell
		'function.')
			return 0
		;;
		
	esac
	
	return 1
}
