#!/bin/bash # Generate arrays script - automatically creates files_*.sh arrays # This script scans the scripts folder and generates array files for each subfolder # # Usage: ./generate_arrays.sh run # # SAFETY: This script only runs when executed directly with 'run' parameter # It will NOT run when sourced (which happens during LibrePortal startup) # Only run if executed directly (not sourced) AND run parameter is provided if [[ "${BASH_SOURCE[0]}" == "${0}" && "$1" == "run" ]]; then SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ARRAYS_DIR="$SCRIPT_DIR/arrays" SCRIPTS_DIR="$(dirname "$(dirname "$SCRIPT_DIR")")" # Standalone runner — duplicate the framework's tiny output helpers inline so # the script keeps its zero-dependency property but still matches the look of # the rest of the install pipeline. RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[0;33m'; NC='\033[0m' isSuccessful() { echo -e "${GREEN}SUCCESS:${NC} $1"; } isNotice() { echo -e "${YELLOW}NOTICE:${NC} $1"; } isHeader() { local title="$1" local width=52 local inner=$((width - 6)) local total_pad=$((inner - ${#title})) (( total_pad < 0 )) && total_pad=0 local left_pad=$((total_pad / 2)) local right_pad=$((total_pad - left_pad)) local bar bar=$(printf '%*s' "$width" '' | tr ' ' '#') echo "" echo "$bar" printf '###%*s%s%*s###\n' "$left_pad" '' "$title" "$right_pad" '' echo "$bar" echo "" } echo " ╦ ┬┌┐ ┬─┐┌─┐ ╭─╮ ╔═╗┌─┐┬─┐┌┬┐┌─┐┬ ║ │├┴┐├┬┘├┤ │◉│ ╠═╝│ │├┬┘ │ ├─┤│ ╩═╝┴└─┘┴└─└─┘ ╰─╯ ╩ └─┘┴└─ ┴ ┴ ┴┴─┘" echo "" isHeader "Regenerating Array Files" isNotice "Scanning scripts/ for subfolder arrays..." # Create arrays directory if it doesn't exist mkdir -p "$ARRAYS_DIR" # Get all directories in scripts folder for folder in "$SCRIPTS_DIR"/*; do if [ -d "$folder" ]; then folder_name=$(basename "$folder") # Skip unused folder if [ "$folder_name" = "unused" ]; then isNotice "Skipping $folder_name/" continue fi array_file="$ARRAYS_DIR/files_${folder_name}.sh" # Start the array file cat > "$array_file" << EOF #!/bin/bash # This file is auto-generated by generate_arrays.sh # Do not edit manually - run './scripts/source/files/generate_arrays.sh run' to regenerate ${folder_name}_scripts=( EOF # Find all .sh files in the folder and subdirectories find "$folder" -name "*.sh" -type f | sort | while read -r file; do # Get relative path from scripts directory rel_path=$(realpath --relative-to="$SCRIPTS_DIR" "$file") # Skip specific files for source folder if [ "$folder_name" = "source" ]; then case "$rel_path" in "source/files/app_files.sh"|"source/files/cli_files.sh"| \ "source/loading/check_files.sh"|"source/loading/initilize_files.sh"| \ "source/loading/scan_files.sh"|"source/load_sources.sh"| \ "source/files/generate_arrays.sh") continue ;; esac fi # Skip specific files for webui folder if [ "$folder_name" = "webui" ]; then case "$rel_path" in "webui/data/generators/webui_test_generate.sh") continue ;; esac fi # Skip specific files for headscale folder if [ "$folder_name" = "headscale" ]; then case "$rel_path" in "headscale/tailscale/tailscale.sh") continue ;; esac fi # Escape quotes and add to array echo " \"$rel_path\"" >> "$array_file" done # Close the array cat >> "$array_file" << EOF ) EOF isSuccessful "Generated files_${folder_name}.sh" fi done isHeader "Generation Complete" isSuccessful "$(find "$ARRAYS_DIR" -name "files_*.sh" | wc -l) array files written to $ARRAYS_DIR" echo "" fi