# Get the number of avaialble cores (threads).
function _core_count() {
	cat /proc/cpuinfo | grep ^processor | wc -l
}

# Parse integers from mixed string.
function _parse_ints() {
	echo "${*}" | tr '\n' ' ' | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' | sed 's/ /\n/g'
}

# Function-wrapper to iterate with specified function with provided files.
# By default Iterates on all non-hidden files and directories.
# List of variables available to FUNCTION: target - current file, count - current item index, total - sum of targets, failed - count of previously failed items, skipped - count of skipped files, status - status line (not recommended to use).
# Usage: _iterate_targets <FUNCTION> [FILES]
function _iterate_targets() {
	local IFS=$'\n'
	local foo="${1}"
	local targets=("${@:2}")
	local total=${#targets[@]}
	local count=0
	local failed=0
	local skipped=0
	local code=0

	# set dafult value to target all supported archives.
	if [[ "${targets}" = "" ]]; then
		_error "No targets provided."
		return 1
	fi

	# iterate each target.
	for target in "${targets[@]}"; do
		# increment counter.
		((count++))

		# status info.
		local status="[${count}/${total}] ${target}"
		_info "${status}"

		# Call function.
		${foo} "${target}"

		# Show error.
		if [[ ${?} != 0 ]]; then
			((failed++))
			_error "${status}: Failed."
		fi

		# Add newline if not the last one.
		[[ "${count}" = "${total}" ]] || echo
	done

	# Show skipped.
	if [[ ${skipped} != 0 ]]; then
		echo
		echo -e "${color_byellow}Skipped: ${skipped}.${color_default}"
	fi

	# Show error.
	if [[ ${failed} != 0 ]]; then
		[[ "${skipped}" = 0 ]] && echo
		echo -e "${color_bred}Failed: ${failed}.${color_default}"
		false
	fi
}

# Report an error to stdout.
# Always returns code 1.
# Usage: _error <MESSAGE>
function _error() {
	echo -e "${color_bred}${*}${color_default}"
	return 1
}

# Report a warning to stdout.
# Usage: _warn <MESSAGE>
function _warn() {
	echo -e "${color_byellow}${*}${color_default}"
}

# Report a debug to stdout.
# Usage: _debug <MESSAGE>
function _debug() {
	echo -e "${color_bwhite}${*}${color_default}"
}

# Report an info to stdout.
# Usage: _info <MESSAGE>
function _info() {
	echo -e "${color_bwhite}${*}${color_default}"
}

# Skip current iteration.
# Usage: _skip [MESSAGE]
function _skip() {
	((skipped++))

	[[ "${*}" = "" ]] || echo -e "${color_byellow}${*}${color_default}"
}