armbian_build/lib/functions/configuration/aggregation.sh

104 lines
3.2 KiB
Bash

#!/usr/bin/env bash
# Expected variables
# - aggregated_content
# - potential_paths
# - separator
# Write to variables :
# - aggregated_content
aggregate_content() {
display_alert "Aggregation: aggregate_content" "potential_paths: '${potential_paths}'" "aggregation"
for filepath in ${potential_paths}; do
if [[ -f "${filepath}" ]]; then
display_alert "Aggregation: aggregate_content" "HIT: '${filepath}'" "aggregation"
aggregated_content+=$(cat "${filepath}")
aggregated_content+="${separator}"
fi
done
}
get_all_potential_paths() {
display_alert "Aggregation: get_all_potential_paths" "${*}" "aggregation"
local root_dirs="${AGGREGATION_SEARCH_ROOT_ABSOLUTE_DIRS}"
local rel_dirs="${1}"
local sub_dirs="${2}"
local looked_up_subpath="${3}"
for root_dir in ${root_dirs}; do
for rel_dir in ${rel_dirs}; do
for sub_dir in ${sub_dirs}; do
potential_paths+="${root_dir}/${rel_dir}/${sub_dir}/${looked_up_subpath} "
done
done
done
}
# Environment variables expected :
# - aggregated_content
# Arguments :
# 1. File to look up in each directory
# 2. The separator to add between each concatenated file
# 3. Relative directories paths added to ${3}
# 4. Relative directories paths added to ${4}
#
# The function will basically generate a list of potential paths by
# generating all the potential paths combinations leading to the
# looked up file
# ${AGGREGATION_SEARCH_ROOT_ABSOLUTE_DIRS}/${3}/${4}/${1}
# Then it will concatenate the content of all the available files
# into ${aggregated_content}
#
# TODO :
# ${4} could be removed by just adding the appropriate paths to ${3}
# dynamically for each case
# (debootstrap, cli, desktop environments, desktop appgroups, ...)
aggregate_all_root_rel_sub() {
display_alert "Aggregation: aggregate_all_root_rel_sub" "${*}" "aggregation"
local separator="${2}"
local potential_paths=""
get_all_potential_paths "${3}" "${4}" "${1}"
aggregate_content
}
aggregate_all_debootstrap() {
display_alert "Aggregation: aggregate_all_debootstrap" "${*}" "aggregation"
local sub_dirs_to_check=". "
if [[ ! -z "${SELECTED_CONFIGURATION+x}" ]]; then
sub_dirs_to_check+="config_${SELECTED_CONFIGURATION}"
fi
aggregate_all_root_rel_sub "${1}" "${2}" "${DEBOOTSTRAP_SEARCH_RELATIVE_DIRS}" "${sub_dirs_to_check}"
}
aggregate_all_cli() {
display_alert "Aggregation: aggregate_all_cli" "${*}" "aggregation"
local sub_dirs_to_check=". "
if [[ ! -z "${SELECTED_CONFIGURATION+x}" ]]; then
sub_dirs_to_check+="config_${SELECTED_CONFIGURATION}"
fi
aggregate_all_root_rel_sub "${1}" "${2}" "${CLI_SEARCH_RELATIVE_DIRS}" "${sub_dirs_to_check}"
}
aggregate_all_desktop() {
display_alert "Aggregation: aggregate_all_desktop" "${*}" "aggregation"
aggregate_all_root_rel_sub "${1}" "${2}" "${DESKTOP_ENVIRONMENTS_SEARCH_RELATIVE_DIRS}" "."
aggregate_all_root_rel_sub "${1}" "${2}" "${DESKTOP_APPGROUPS_SEARCH_RELATIVE_DIRS}" "${DESKTOP_APPGROUPS_SELECTED}"
}
one_line() {
local aggregate_func_name="${1}"
local aggregated_content=""
shift 1
$aggregate_func_name "${@}"
cleanup_list aggregated_content
}
cleanup_list() {
local varname="${1}"
local list_to_clean="${!varname}"
list_to_clean="${list_to_clean#"${list_to_clean%%[![:space:]]*}"}"
list_to_clean="${list_to_clean%"${list_to_clean##*[![:space:]]}"}"
echo ${list_to_clean}
}