#!/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 local file_list=$(find "$file_list_directory" -type f -name "*.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 `\t` 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. These define vars or run # commands at top level; lazy stubs would skip those side effects. local _eager for _eager in "${LP_EAGER_FILES[@]}"; do [[ -f "${install_scripts_dir}${_eager}" ]] || continue if [[ "$LP_LOAD_TRACE" == "1" ]]; then local _t0=$EPOCHREALTIME source "${install_scripts_dir}${_eager}" 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" "${_eager} (LAZY-EAGER)" >> "$LP_LOAD_TRACE_FILE" else source "${install_scripts_dir}${_eager}" 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 sourceScanFiles "libreportal_configs"; sourceScanFiles "app_configs"; sourceScanFiles "containers"; }