Files
TRIMSP/scripts/deploy-web.sh
T

86 lines
3.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# Copyright (C) 2026 Zaher Salman
# SPDX-License-Identifier: GPL-2.0-only
set -euo pipefail
project_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
web_target="${TRIMSP_WEB_TARGET:-/var/www/html/TRIMSP-NL}"
cgi_target="${TRIMSP_CGI_TARGET:-/var/www/cgi-bin/singleTrimSP.cgi}"
trimsp_target="${TRIMSP_BIN_TARGET:-/usr/local/bin/trimspNL}"
web_owner="${TRIMSP_WEB_OWNER:-apache}"
web_group="${TRIMSP_WEB_GROUP:-apache}"
build_static="${TRIMSP_BUILD_STATIC:-1}"
binary_source="${TRIMSP_BINARY_SOURCE:-$project_root/fortran/trimspNL-static}"
plotly_source="$project_root/node_modules/plotly.js-dist/plotly.js"
if [[ "$build_static" == "1" ]]; then
make -C "$project_root/fortran" trimspNL-static
elif [[ "$build_static" != "0" ]]; then
printf 'TRIMSP_BUILD_STATIC must be 0 or 1, not %s\n' "$build_static" >&2
exit 2
fi
if [[ ! -f "$binary_source" ]]; then
printf 'TRIM.SP-NL binary not found: %s\n' "$binary_source" >&2
printf 'Build it first or set TRIMSP_BINARY_SOURCE.\n' >&2
exit 1
fi
if [[ ! -f "$plotly_source" ]]; then
printf 'Plotly browser bundle not found: %s\n' "$plotly_source" >&2
printf 'Run npm install before deploying.\n' >&2
exit 1
fi
perl -c "$project_root/singleTrimSP.cgi" >/dev/null
# Install only files used by the browser application. Electron-specific code,
# package metadata, tests, and Fortran sources stay outside the document root.
install -d -m 0755 "$web_target" "$(dirname "$cgi_target")" "$(dirname "$trimsp_target")"
install -m 0644 \
"$project_root/TrimSP.html" \
"$project_root/TrimSPlib.js" \
"$project_root/TrimSPweb.js" \
"$project_root/myplots.js" \
"$project_root/ZGUI.css" \
"$project_root/plotly.css" \
"$project_root/icon.png" \
"$web_target/"
install -m 0644 "$plotly_source" "$web_target/plotly.js"
ln -sfn TrimSP.html "$web_target/index.html"
install -m 0755 "$binary_source" "$trimsp_target"
# Keep the repository CGI generic while embedding the selected installed
# binary path in the deployed copy.
cgi_stage="$(mktemp)"
trap 'rm -f "$cgi_stage"' EXIT
escaped_trimsp_target="${trimsp_target//\\/\\\\}"
escaped_trimsp_target="${escaped_trimsp_target//&/\\&}"
escaped_trimsp_target="${escaped_trimsp_target//|/\\|}"
sed "s|^\\\$trimbin=.*|\$trimbin=\"timeout 300 $escaped_trimsp_target\";|" \
"$project_root/singleTrimSP.cgi" > "$cgi_stage"
install -m 0755 "$cgi_stage" "$cgi_target"
if getent passwd "$web_owner" >/dev/null 2>&1 && getent group "$web_group" >/dev/null 2>&1; then
chown -R "$web_owner:$web_group" "$web_target"
chown "$web_owner:$web_group" "$cgi_target"
else
printf 'Warning: owner/group %s:%s does not exist; ownership was not changed.\n' \
"$web_owner" "$web_group" >&2
fi
if command -v restorecon >/dev/null 2>&1; then
restorecon -RF "$web_target" "$cgi_target" "$trimsp_target" || true
fi
printf 'Installed TRIMSP-NL web assets in %s\n' "$web_target"
printf 'Installed TRIMSP-NL CGI runner at %s\n' "$cgi_target"
printf 'Installed TRIM.SP-NL engine at %s\n' "$trimsp_target"
printf '\nThe web server must provide:\n'
printf ' /cgi-bin/singleTrimSP.cgi -> %s\n' "$cgi_target"
printf ' /tmp/ -> /tmp/ (simulation results and archives)\n'
printf 'For SELinux deployments, verify CGI execution and write access with:\n'
printf ' ausearch -m avc -ts recent\n'