# 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 base64/decode variable temporaryFiles

# Unfortunately, strips trailing LFs
core_dependency_requires '*' cat
core_snippet_raw_encoder()
{
	# "$(<FILE)" doesn't work in all situations
	core_variable_setVariable _core_snippet_embeddedData_${snippetName} "$(cat "$snippetFilePath")"
}

core_snippet_raw_decoder()
{
	local core_variable_indirectValue_result
	core_variable_indirectValue _core_snippet_embeddedData_${snippetName}
	if core_variable_isTrue "$snippetAppend"; then
		printf '%s' "$core_variable_indirectValue_result" >>"$snippetFilePath"
	else
		printf '%s' "$core_variable_indirectValue_result" >"$snippetFilePath"
	fi
}

# Different base64 implementations wrap differently and with different switches
core_dependency_requires '*' base64 tr
core_snippet_base64_encoder()
{
	_core_snippet_embeddedData_${snippetName}="$(base64 "$snippetFilePath" | tr -d '\n')"
}

core_snippet_base64_decoder()
{
	core_base64_decode_string "$(core_variable_indirectValue _core_snippet_embeddedData_${snippetName})" "$snippetFilePath" "$snippetAppend" '+' '/'
}

core_usesIn core variable
core_snippet_embed()
{
	local codecName="$1"
	shift 1
	
	local encoder="core_snippet_${codecName}_encoder"
	
	local snippetName
	local snippetFilePath
	for snippetName in "$@"
	do
		if core_variable_isSet _core_snippet_embeddedData_${snippetName}; then
			continue
		fi
		
		snippetFilePath="$_program_libPath"/"$core_libraryName"/"${_program_namespace}"/"$snippetName".snippet
		if ! core_path_isReadableNonEmptyFilePath "$snippetFilePath"; then
			core_exitError $core_commandLine_exitCode_SOFTWARE "Snippet '$snippetName' does not exist as a readable, non-empty file at '$snippetFilePath'"
		fi
		
		${encoder}
		core_dependency_declares _core_snippet_embeddedData_${snippetName}
		
		core_variable_setVariable _core_snippet_embeddedCodec_${snippetName} $codecName
		core_dependency_declares _core_snippet_embeddedCodec_${snippetName}
	done
}

core_snippet_retrieve()
{
	local snippetName="$1"
	local snippetAppend="$2"
	local snippetFilePath="$3"
	local core_variable_indirectValue_result
	local codec
	local decoder

	core_variable_indirectValue _core_snippet_embeddedCodec_${snippetName}
	codec="$core_variable_indirectValue_result"
	decoder="core_snippet_${codec}_decoder"
	${decoder}
}

core_dependency_requires '*' cat rm
core_snippet_retrieveAndSourceAsHereDoc()
{
	local snippetName="$1"
	
	local TMP_FILE
	core_temporaryFiles_newFileToRemoveOnExit
	local snippetFilePath="$TMP_FILE"
	
	printf '%s\n' 'cat <<EOF' >"$snippetFilePath"
	core_snippet_retrieve "$snippetName" yes "$snippetFilePath"
	printf '\nEOF' >>"$snippetFilePath"
	
	. "$snippetFilePath"
	rm "$snippetFilePath" || true
}
