# 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 functions

core_init_defines core_compatibility_basename
core_init_defines core_compatibility_which
core_init_defines core_compatibility_whichNoOutput

core_compatibility_dirname()
{
	local converted="${1%/*}"
	if [ "$converted" = "$1" ]; then
		printf '%s' '.'
	else
		printf '%s' "$converted"
	fi
}

_core_compatibility_builtInDoesNotExist()
{
	local builtInName="$1"
	local value="$(PATH='' core_compatibility_which "$builtInName")"
	if [ "$builtInName" = "$value" ]; then
		return 1
	fi
	return 0
}

# dash by default escapes backslashes, other echos behave differently. This way is defensive.
core_compatibility_echo()
{
	printf '%s\n' "$1"
}

core_dependency_requires '*' sleep
core_compatibility_sleepSupportsFractionalSeconds()
{
	sleep 0.0001 2>/dev/null
}

core_usesIn core functions

core_compatibility_execute()
{
	core_functions_execute _core_compatibility_functions
}

core_compatibility_setSaneShellOptions()
{
	:
}
core_functions_register _core_compatibility_functions core_compatibility_setSaneShellOptions

core_compability_installPushdAndPopd()
{
	# bash, zsh are known to support this
	if _core_compatibility_builtInDoesNotExist pushd; then
		
		if ! _core_compatibility_builtInDoesNotExist popd; then
			core_exitError $core_commandLine_exitCode_SOFTWARE "Weird shell does not have pushd but does have popd (?feature detection bug?)!"
		fi
		
		_core_init_compatibility_pushdCount=0

		pushd()
		{
			local path="$1"
			eval "core_init_pushdStack${_core_init_compatibility_pushdCount}='$(pwd)'"
			_core_init_compatibility_pushdCount=$((_core_init_compatibility_pushdCount+1))
			cd "$path" 1>/dev/null
		}

		popd()
		{
			_core_init_compatibility_pushdCount=$((_core_init_compatibility_pushdCount-1))
			eval "local path=\"\$core_init_pushdStack${_core_init_compatibility_pushdCount}\""
			eval "unset core_init_pushdStack${_core_init_compatibility_pushdCount}"
			cd "$path" 1>/dev/null
		}
	
	else
		
		pushd()
		{
			builtin pushd "$@" 1>/dev/null
		}
	
		popd()
		{
			builtin popd "$@" 1>/dev/null
		}
		
	fi
}
core_functions_register _core_compatibility_functions core_compability_installPushdAndPopd

core_compability_installSource()
{
	# bash, zsh are known to support this
	if _core_compatibility_builtInDoesNotExist source; then
		
		# Done using as eval to workaround a bug in BusyBox's ash shell when the 'source' directive is compiled in
		# BusyBox's ash parser evaluates this code not at 'if-time' but at evaluation of the surrounding code
		# Yuck
		eval 'source()
{
. "$@"
}'
		
	fi
}
core_functions_register _core_compatibility_functions core_compability_installSource
