LibrePortal/scripts/source/loading/initilize_files.sh
librelad 9ffc8e4924 chore(cleanup): retire two stale leftovers from the lazy-load + subdomain work
initilize_files.sh's load-comment said "Phase 6 will tackle this with a
precompiled cache file" — but Phase 6 deliberately shipped a different
(simpler) win, and Phase 7 was reconsidered and rejected (the ~30 ms
saving doesn't justify the invalidation complexity across every CFG
write site). Rewrite the comment to describe current behaviour: the two
config scans run because bash can't lazy-load variables, and the
precompile was looked at and dropped.

app_generate.sh had a `read -p "" host_name` inside a `while true` with
no break — anyone who actually ran `libreportal app generate` would
have hung at the hostname prompt forever. The value was then fed into
a `sed 's/HOST_NAME=test/HOST_NAME='"$host_name"'/g'` against the new
app's .config file, but the post-2e4f420 template no longer carries
HOST_NAME=test (per-port subdomains in the PORT row drive routing now).
So the prompt was infinite-looping to gather a value that fed a no-op
sed. Drop both — the function loses no real capability since the
subdomain field is set per-port via the standard PORT row.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Signed-off-by: librelad <librelad@digitalangels.vip>
2026-05-26 23:42:12 +01:00

156 lines
7.0 KiB
Bash
Executable File

#!/bin/bash
sourceInitilize()
{
local flag="$1"
# We will only show the header for the full app
if [[ $flag == "run" ]]; then
isHeader "Loading LibrePortal Startup Files"
isNotice "If you are experiencing loading issues..."
isNotice "Please run the following : 'libreportal reset'"
fi
# Directory containing the files to source recursively
local file_list_directory="${install_scripts_dir}source/files"
# Check if the directory exists
if [ -d "$file_list_directory" ]; then
# Use find to get a list of all files (excluding directories) in the
# directory and its subdirectories. The function_manifest.sh file is
# excluded — it carries ~860 stub function definitions (~50 ms parse
# cost) that are only needed in lazy mode, where it's sourced
# explicitly. Including it here makes every eager-mode invocation pay
# for lazy infrastructure it never uses.
local file_list=$(find "$file_list_directory" -type f -name "*.sh" \
! -name "function_manifest.sh")
# Loop through each file in the file list
while IFS= read -r file; do
# Source the file
source "$file"
done <<< "$file_list"
else
echo "Directory $file_list_directory does not exist. Unable to start!"
fi
# For loading files needed for the full app or CLI
if [[ $flag == "run" ]]; then
source "${install_scripts_dir}source/files/app_files.sh"
files_to_source=("${files_libreportal_app[@]}")
elif [[ $flag == "cli" ]]; then
source "${install_scripts_dir}source/files/cli_files.sh"
files_to_source=("${files_libreportal_cli[@]}")
fi
# Trace setup — LP_LOAD_TRACE=1 logs `<ms>\t<file>` to LP_LOAD_TRACE_FILE
# for `libreportal debug load-trace`. Zero overhead when unset.
if [[ "$LP_LOAD_TRACE" == "1" ]]; then
: "${LP_LOAD_TRACE_FILE:=/tmp/libreportal-load-trace.$$.log}"
export LP_LOAD_TRACE_FILE
: > "$LP_LOAD_TRACE_FILE"
fi
# LP_LAZY=1: defer the bulk of file sourcing. Install autoload stubs from
# function_manifest.sh, source only the files with top-level side effects
# (LP_EAGER_FILES — set by the manifest), and skip the rest. Each stub
# sources its real file on first call; subsequent calls hit the real
# function directly (the source replaced the stub).
#
# Default (LP_LAZY unset or 0): the historical behaviour — source every
# file in files_to_source up front. Long-running processes (task
# processor, WebUI) want this so their first call to anything is hot.
if [[ "$LP_LAZY" == "1" ]]; then
local manifest="${install_scripts_dir}source/files/arrays/function_manifest.sh"
if [[ -f "$manifest" ]]; then
# Sourcing the manifest defines LP_FN_MAP, LP_EAGER_FILES, AND
# the autoload stubs (precompiled by the generator so we pay one
# parse cost instead of evaling ~700 snippets at startup).
if [[ "$LP_LOAD_TRACE" == "1" ]]; then
local _t0=$EPOCHREALTIME
source "$manifest"
local _t1=$EPOCHREALTIME
local _ms
_ms=$(awk -v a="$_t0" -v b="$_t1" 'BEGIN{printf "%.3f", (b-a)*1000}')
printf '%s\t%s\n' "$_ms" "source/files/arrays/function_manifest.sh (LAZY-manifest)" >> "$LP_LOAD_TRACE_FILE"
else
source "$manifest"
fi
# Eager-source the side-effect files. Entries are `<root>:<relpath>`
# where root selects the base dir ("scripts" → install_scripts_dir,
# "containers" → install_containers_dir). Pre-Phase-5 manifests
# without a `:` default to scripts/.
local _eager _root _path _base
for _eager in "${LP_EAGER_FILES[@]}"; do
if [[ "$_eager" == *:* ]]; then
_root="${_eager%%:*}"
_path="${_eager#*:}"
else
_root="scripts"; _path="$_eager"
fi
case "$_root" in
containers) _base="$install_containers_dir" ;;
*) _base="$install_scripts_dir" ;;
esac
[[ -f "${_base}${_path}" ]] || continue
if [[ "$LP_LOAD_TRACE" == "1" ]]; then
local _t0=$EPOCHREALTIME
source "${_base}${_path}"
local _t1=$EPOCHREALTIME
local _ms
_ms=$(awk -v a="$_t0" -v b="$_t1" 'BEGIN{printf "%.3f", (b-a)*1000}')
printf '%s\t%s\n' "$_ms" "${_root}/${_path} (LAZY-EAGER)" >> "$LP_LOAD_TRACE_FILE"
else
source "${_base}${_path}"
fi
done
else
# No manifest present — fall back to eager loading so we never
# leave the user with a broken install just because regen
# hasn't run. Silently — this is the safety net, not the path.
export LP_LAZY=0
fi
fi
# Eager path (default OR lazy-with-no-manifest fallback). Sources every
# file in files_to_source. Skipped entirely when LP_LAZY=1 succeeded.
if [[ "$LP_LAZY" != "1" ]]; then
for file_to_source in "${files_to_source[@]}"; do
if [ ! -f "${install_scripts_dir}${file_to_source}" ]; then
isNotice "Missing file: ${install_scripts_dir}${file_to_source}"
else
if [[ "$LP_LOAD_TRACE" == "1" ]]; then
local _t0=$EPOCHREALTIME
source "${install_scripts_dir}${file_to_source}"
local _t1=$EPOCHREALTIME
local _ms
_ms=$(awk -v a="$_t0" -v b="$_t1" 'BEGIN{printf "%.3f", (b-a)*1000}')
printf '%s\t%s\n' "$_ms" "$file_to_source" >> "$LP_LOAD_TRACE_FILE"
else
source "${install_scripts_dir}${file_to_source}"
fi
fi
done
fi
# Loading of all files. Three scans:
# libreportal_configs CFG_* vars from configs/* (~180 ms)
# app_configs CFG_<APP>_* vars from containers/* (~37 ms)
# containers Per-app installer functions (~70 ms)
#
# Under LP_LAZY=1 with a present manifest, the container scan is replaced
# by autoload stubs in function_manifest.sh — sourcing every container
# installer eagerly would defeat the lazy optimisation. Both config scans
# still run because bash can't lazy-load variables; precompiling them was
# considered (Phase 7) and rejected — the ~30 ms saving doesn't justify
# the invalidation complexity (updateConfigOption writes happen across
# many code paths).
sourceScanFiles "libreportal_configs";
sourceScanFiles "app_configs";
if [[ "$LP_LAZY" == "1" ]] && declare -p LP_FN_MAP >/dev/null 2>&1; then
: # containers covered by autoload stubs; skip the eager scan
else
sourceScanFiles "containers";
fi
}