LibrePortal/scripts/source/files/generate_arrays.sh
librelad cd4fd55a6d feat(desudo): helper-ize backup-engine + app-config installs; retire standalone WireGuard
Bring the remaining deferred subsystems under the scoped sudoers, and drop
the one that's redundant.

Backup engines + app configs -> root-owned helpers (same pattern as
ownership/dns/ssh/socket/svc):
- scripts/system/libreportal-bininstall: install <restic|kopia> — does the
  whole pkg-manager/signed-download install itself for a fixed, validated
  engine name (no blanket sudo apt-get/install). restic_install/kopia_install
  call it.
- scripts/system/libreportal-appcfg: {adguard-auth <user> <bcrypt>|
  crowdsec-priority|owncloud-config <public> <host> <ip> <public_ip>} —
  faithful ports of the AdGuard yaml / CrowdSec bouncer / ownCloud config.php
  rewrites, fixed paths + validated args. adguard_auth/crowdsec_fix_priority/
  owncloud_setup_config call it.
- run_privileged: runBinInstall / runAppCfg; init.sh installs + allowlists both.

Retire standalone (host-level) WireGuard — it's a duplicate of the
containerized containers/wireguard app (+ headscale mesh), its slirp4netns
speed rationale is largely moot with a better rootless net backend / typical
WAN-bound throughput, and it was the heaviest host-root subsystem (apt +
sysctl + iptables + /etc/wireguard), the worst fit for the rootless/
least-privilege direction:
- moved scripts/wireguard/ + manage_wireguard.sh + check_wireguard.sh to
  scripts/unused/; dropped the install-path call, the Tools menu 'w' entry,
  and the requirement check; removed the half-built libreportal-wg helper.
- generate_arrays.sh now also skips system/ (root-owned helpers, never
  sourced); arrays regenerated (files_wireguard.sh pruned).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Signed-off-by: librelad <librelad@digitalangels.vip>
2026-05-24 19:22:22 +01:00

141 lines
5.2 KiB
Bash
Executable File

#!/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 - 2))
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 hbar
hbar=$(printf '═%.0s' $(seq 1 "$inner"))
printf '\n╔%s╗\n' "$hbar"
printf '║%*s%s%*s║\n' "$left_pad" '' "$title" "$right_pad" ''
printf '╚%s╝\n\n' "$hbar"
}
hbar=$(printf '═%.0s' $(seq 1 50))
printf '\n╔%s╗\n' "$hbar"
printf '║%6s%s%8s║\n' '' '╦ ┬┌┐ ┬─┐┌─┐ ╭─╮ ╔═╗┌─┐┬─┐┌┬┐┌─┐┬' ''
printf '║%6s%s%8s║\n' '' '║ │├┴┐├┬┘├┤ │◉│ ╠═╝│ │├┬┘ │ ├─┤│' ''
printf '║%6s%s%6s║\n' '' '╩═╝┴└─┘┴└─└─┘ ╨─╨ ╩ └─┘┴└─ ┴ ┴ ┴┴─┘' ''
printf '╚%s╝\n\n' "$hbar"
isHeader "Regenerating Array Files"
isNotice "Scanning scripts/ for subfolder arrays..."
# Create arrays directory if it doesn't exist
mkdir -p "$ARRAYS_DIR"
# Prune arrays whose source folder no longer exists, so a removed area (e.g.
# scripts/ssh/) doesn't linger in the sourced set as a stale files_*.sh.
for existing in "$ARRAYS_DIR"/files_*.sh; do
[ -f "$existing" ] || continue
pruned_name=$(basename "$existing"); pruned_name=${pruned_name#files_}; pruned_name=${pruned_name%.sh}
if [ ! -d "$SCRIPTS_DIR/$pruned_name" ]; then
rm -f "$existing"
isNotice "Pruned stale files_${pruned_name}.sh (no scripts/$pruned_name/)"
fi
done
# Get all directories in scripts folder
for folder in "$SCRIPTS_DIR"/*; do
if [ -d "$folder" ]; then
folder_name=$(basename "$folder")
# Skip folders that aren't sourced function libraries: the dead-code
# graveyard, and system/ (standalone root-owned helpers installed to
# /usr/local/sbin and invoked via sudo — never sourced into the runtime).
if [ "$folder_name" = "unused" ] || [ "$folder_name" = "system" ]; 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