# 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_init_defines core_variable_isSet
core_init_defines core_variable_isUnset
core_init_defines core_variable_indirectValue
core_init_defines core_variable_unset

core_variable_setVariable()
{
	eval "$1"'=$2'
}

core_variable_setAndExportVariable()
{
	eval export "$1"'=$2'
}

core_variable_isUnsetOrEmpty()
{
	local variableName="$1"
	if core_variable_isUnset "$variableName"; then
		return 0
	fi
	
	local core_variable_indirectValue_result
	core_variable_indirectValue "$variableName"
	if [ -z "$core_variable_indirectValue_result" ]; then
		return 0
	fi
	return 1
}

core_variable_setVariableIfUnset()
{
	local variableName="$1"
	local variableDefaultValue="$2"
	
	# This is not whitespace-safe: eval "$variableName=\"\${$variableName-$variableDefaultValue}\""
	if core_variable_isUnset "$variableName"; then
		eval "$variableName='$variableDefaultValue'"
	fi
}

core_variable_contains()
{
	local value="$1"
	local contains="$2"
	case "$value" in
		
		*"$contains"*)
			return 0
		;;
		
	esac
	
	return 1
}

core_variable_matches()
{
	local value="$1"
	local match="$2"
	case "$value" in
		
		$match)
			return 0
		;;
		
	esac
	
	return 1
}

#if core_variable_startsWith "/etc/path/to/file" "/home/"; then
#	echo 'yes'
#fi
core_variable_startsWith()
{
	local value="$1"
	local contains="$2"
	case "$value" in
		
		"$contains"*)
			return 0
		;;
		
	esac
	
	return 1
}

core_variable_doesNotStartWith()
{
	! core_variable_startsWith "$@"
}

#if core_variable_endsWith "/etc/path/to/file" "/file"; then
#	echo 'yes'
#fi
core_variable_endsWith()
{
	local value="$1"
	local contains="$2"
	case "$value" in
		
		*"$contains")
			return 0
		;;
		
	esac
	
	return 1
}

core_variable_doesNotEndWith()
{
	! core_variable_endsWith "$@"
}

core_variable_firstCharacter()
{
	local value="$1"
	local suffix="${value#?}"
	printf '%s' "${value%$suffix}"
}

core_variable_lastCharacter()
{
	local value="$1"
	local prefix="${value%?}"
	printf '%s' "${value#$prefix}"
}

core_variable_trimSpaceAndHorizontalTab()
{
	local value="$1"
	
	# Remove trailing characters
	value="${value#"${value%%[![:blank:]]*}"}"
	
	# Remove leading characters
	printf '%s' "${value%"${value##*[![:blank:]]}"}"
}

core_variable_trimWhitespace()
{
	local value="$1"
	
	# Remove trailing characters
	value="${value#"${value%%[![:space:]]*}"}"
	
	# Remove leading characters
	printf '%s' "${value%"${value##*[![:space:]]}"}"
}

_core_variable_copy()
{
	local valueToCopy="$1"
	local copies=$2
	
	local count=0
	while [ $count -lt $copies ]
	do
		copy="${copy}${valueToCopy}"
		count=$((count + 1))
	done
}

core_variable_allButLastN()
{
	local value="$1"
	local numberToOmit=$2
	
	local length=${#value}
	local endLength=$((length - numberToOmit))
	if [ $endLength -lt 1 ]; then
		return
	fi
	
	local count=0
	local match=''
	local copy=''
	_core_variable_copy '?' $endLength
	
	local suffix="${value#$copy}"
	printf '%s' "${value%$suffix}"
}

core_variable_allButLast()
{
	core_variable_allButLastN "$1" 1
}

core_variable_allButFirstN()
{
	local value="$1"
	local numberToOmit=$2
	
	local length=${#value}
	local endLength=$((length - numberToOmit))
	if [ $endLength -lt 1 ]; then
		return
	fi
	
	local count=0
	local match=''
	local copy=''
	_core_variable_copy '?' $endLength
	
	local prefix="${value%$copy}"
	printf '%s' "${value#$prefix}"
}

core_variable_allButFirst()
{
	core_variable_allButFirstN "$1" 1
}

_core_variable_characterByCharacter()
{
	local index=0
	local core_variable_character
	while IFS='' read -r core_variable_character
	do
		if [ -z "$character" ]; then
			character="
"
		fi
		$callback
		index=$((index+1))
	done
}

core_variable_characterByCharacter()
{
	local value="$1"
	local callback="$2"
	
	local TMP_FILE
	core_temporaryFiles_newFileToRemoveOnExit
	local inputFile="$TMP_FILE"
	prinf '%s' "$value" >"$inputFile"
	
	core_file_characterByCharacter "$inputFile" _core_variable_characterByCharacter
	
	rm -f "$inputFile"
}

core_variable_isTrue()
{
	_core_variable_parseBoolean "$1" 0 1 2
}

core_variable_isFalse()
{
	_core_variable_parseBoolean "$1" 1 0 2
}

core_variable_isInvalidBoolean()
{
	_core_variable_parseBoolean "$1" 1 1 0
}

_core_variable_parseBoolean()
{
	case "$1" in
		
		true|True|TRUE|T|yes|Yes|YES|Y|on|On|ON|1)
			return $2
		;;
		
		false|False|FALSE|F|no|No|NO|N|off|Off|OFF|0)
			return $3
		;;
		
		*)
			return $4
		;;
		
	esac
}

# eg local my_heredoc_value='$(core_variable_escapeForSingleQuotedLiteralInHeredoc "$my_value")'
core_dependency_requires '*' sed
core_variable_escapeForSingleQuotedLiteralInHeredoc()
{
	local value="$1"

	local singleQuote="'"
	local doubleQuote='"'
	
	# '"'"'
	local replacement="${singleQuote}${doubleQuote}${singleQuote}${doubleQuote}${singleQuote}"
	
	# Replaces any embedded ' symbols with '"'"'
	printf '%s' "$value" | sed "s/${singleQuote}/${replacement}/g"
}

core_variable_escapedCommandLine()
{
	{
		local afterFirst=false
		local argument
		for argument in "$@"
		do
			if $afterFirst; then
				printf ' '
			else
				afterFirst=true
			fi
			printf "'"
			core_variable_escapeSingleQuotes "$argument"
			printf "'"
		done
		printf '\n'
	}
}

core_variable_escapeSingleQuotes()
{
	core_variable_escape "$1" "'" "'\''"
}

core_variable_escape()
{
	local value="$1"
	local separator="$2"
	local replacement="$3"
	local result=''
	
	local split
	local remainder="$value"
	while [ ${#remainder} -ne 0 ]
	do
		IFS="$separator" read -r split remainder <<-EOF
			${remainder}
		EOF
		if [ -n "$result" ]; then
			result="${result}${replacement}"
		fi
		result="${result}${split}"
	done
	
	printf '%s' "$result"
}
