#!/usr/bin/env bash # Copyright (C) 2026 Zaher Salman # SPDX-License-Identifier: GPL-3.0-or-later set -euo pipefail project_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" web_target="${MUSRFLOW_WEB_TARGET:-/var/www/html/muSRFlow}" cgi_target="${MUSRFLOW_CGI_TARGET:-/var/www/cgi-bin/single_musrfit.cgi}" # Server-owned startup XML read by the CGI to build portable RUN names. It is # part of this deployment (not a system config file) and lives beside the CGI, # outside the web document root so it is never served to browsers. startup_target="${MUSRFLOW_STARTUP_XML_TARGET:-/var/www/cgi-bin/musrfit_startup.xml}" docs_source="$project_root/docs/_build/html" # Build as the normal user beforehand, not inside a possibly privileged deploy. # Check before changing the installed application so missing docs fail cleanly. if [ ! -s "$docs_source/index.html" ]; then printf 'HTML manual missing. Run make -C "%s/docs" html before deploying.\n' "$project_root" >&2 exit 1 fi # Install only assets used by the browser build. Parameter plotting and ROOT # rendering are loaded as separate browser modules. install -d -m 0755 "$web_target" "$web_target/assets" install -m 0644 \ "$project_root/index.html" \ "$project_root/quick-start.html" \ "$project_root/ZGUI.css" \ "$project_root/muSRFitweb.js" \ "$project_root/muSRFitRoot.js" \ "$project_root/muSRFitParameterPlot.js" \ "$project_root/muSRFitlib.js" \ "$web_target/" install -m 0644 \ "$project_root/assets/musrflow-favicon.png" \ "$project_root/assets/musrflow-icon.png" \ "$web_target/assets/" # Copy the entire Sphinx site, including search data, styles, and source links. # Build-machine permissions must not prevent Apache from reading the manual. install -d -m 0755 "$web_target/docs" cp -R "$docs_source/." "$web_target/docs/" find "$web_target/docs" -type d -exec chmod 0755 {} + find "$web_target/docs" -type f -exec chmod 0644 {} + # Stamp the deployed help with reproducible application and runtime identity. # The Git revision identifies the exact deployed source; musrfit reports both # its own and ROOT's versions using the same executable configured for the CGI. if command -v node >/dev/null 2>&1; then version="$(node -p 'require(process.argv[1]).version' "$project_root/package.json")" revision="${MUSRFLOW_BUILD_REVISION:-}" if [[ "$revision" =~ ^[0-9a-fA-F]{7,40}$ ]]; then revision="${revision:0:12}" elif git_revision="$(git -C "$project_root" rev-parse --short=12 HEAD 2>/dev/null)"; then revision="$git_revision" else # git archive expands the export-subst placeholder in this file. Gitea # source archives therefore retain the exact commit without a .git tree. archive_revision="$(sed -n 's/^muSRFlow=//p' "$project_root/build-revision.txt" 2>/dev/null || true)" if [[ "$archive_revision" =~ ^[0-9a-fA-F]{40}$ ]]; then revision="${archive_revision:0:12}" else # Third-party tar programs do not expand git placeholders. The # release version is still more useful and honest than "unrecorded". revision="v${version}-archive" fi fi modified="" if [ -n "$(git -C "$project_root" status --porcelain 2>/dev/null || true)" ]; then modified=" (modified)" fi musrfit_bin="${MUSRFIT_BIN:-/usr/local/bin/musrfit}" musrfit_output="" if [ -x "$musrfit_bin" ]; then musrfit_output="$("$musrfit_bin" --version 2>/dev/null || true)" fi musrfit_version="$(printf '%s\n' "$musrfit_output" | sed -n 's/.*musrfit version:[[:space:]]*\([^,[:space:]]*\).*/\1/p' | head -n 1)" root_version="$(printf '%s\n' "$musrfit_output" | sed -n 's/.*ROOT version:[[:space:]]*\([^,[:space:]]*\).*/\1/p' | head -n 1 | tr / .)" musrfit_version="${musrfit_version:-not reported}" root_version="${root_version:-not reported}" tmp_qs="$(mktemp)" sed -e "s#__MUSRFLOW_VERSION__#$version#g" \ -e "s#__MUSRFLOW_EDITION__#Web#g" \ -e "s#__MUSRFLOW_REVISION__#$revision#g" \ -e "s#__MUSRFLOW_MODIFIED__#$modified#g" \ -e "s#__MUSRFLOW_RUNTIME__#server tools#g" \ -e "s#__MUSRFIT_VERSION__#$musrfit_version#g" \ -e "s#__ROOT_VERSION__#$root_version#g" \ "$web_target/quick-start.html" > "$tmp_qs" install -m 0644 "$tmp_qs" "$web_target/quick-start.html" rm -f "$tmp_qs" printf 'Branded %s with muSRFlow %s · Web · build %s\n' \ "$web_target/quick-start.html" "$version" "$revision" else printf 'node not found; quick-start.html keeps its development-source identity\n' fi # Apache executes the runner through the existing /cgi-bin ScriptAlias. install -m 0755 "$project_root/cgi/single_musrfit.cgi" "$cgi_target" # Install the server-owned startup XML beside the CGI. The CGI reads it to # resolve portable RUN names; it must be readable by Apache but is not under # the document root. install -m 0644 "$project_root/cgi/musrfit_startup.xml" "$startup_target" # The startup XML names each instrument's data root as an absolute . # The shipped file targets the PSI layout. On a machine whose data tree lives # under a different root (e.g. /mnt/fake), the portable-RUN resolver would # reject every local path as "not configured". When MUSRFLOW_DATA_ROOT is set, # rewrite the prefixes in the installed copy to that root so the # resolver matches the local tree. The edit is applied to the installed file # only, never the repository source, and is idempotent: re-running on an already # rewritten file matches none of the old prefixes. if [ -n "${MUSRFLOW_DATA_ROOT:-}" ]; then data_root="$(printf '%s' "$MUSRFLOW_DATA_ROOT" | sed 's/[&/\\]/\\&/g')" tmp_xml="$(mktemp)" sed -e "s#/psi.ch/group/lmu/public/data/#$data_root/#g" \ -e "s#/mnt/data/nemu/his<#$data_root/nemu/his<#g" \ "$startup_target" > "$tmp_xml" install -m 0644 "$tmp_xml" "$startup_target" rm -f "$tmp_xml" printf 'Rewrote roots in %s to %s\n' "$startup_target" "$MUSRFLOW_DATA_ROOT" fi # The browser assets and the CGI must be readable (and, for the CGI, executable) # by the web server. On the live server the web space is owned by the apache # user; on a developer machine the files may instead be owned by the deploying # user and served straight from a user-owned tree. Determine the owner to hand # the files to, preferring an explicit override, then a real web-server account, # and keeping the current owner when it is already the deploying user (the # common developer-machine case, where the deploying user simply owns the web # space and chowning to a nonexistent apache user would fail). web_owner="${MUSRFLOW_WEB_OWNER:-}" web_group="${MUSRFLOW_WEB_GROUP:-$web_owner}" if [ -z "$web_owner" ]; then # Detect from the target filesystem: if the deployed tree is already owned # by the invoking user, leave ownership untouched. if [ "$(stat -c %u "$web_target")" = "$(id -u)" ]; then web_owner="" web_group="" else # Prefer a real web-server account when one exists. for cand in apache www-data httpd; do if id "$cand" >/dev/null 2>&1; then web_owner="$cand" web_group="$cand" break fi done # With no matching account, fall back to the invoking user (the trees # were written by this deploy, so they are already owned by them). [ -z "$web_owner" ] && web_owner="$(id -un)" && web_group="$(id -gn)" fi fi if [ -n "$web_owner" ]; then chown -R "$web_owner:$web_group" "$web_target/" chown "$web_owner:$web_group" "$cgi_target" "$startup_target" chmod -R u+rX "$web_target/" 2>/dev/null || true fi printf 'Installed muSRFlow web assets in %s\n' "$web_target" printf 'Installed muSRFlow HTML manual in %s/docs/\n' "$web_target" printf 'Installed muSRFlow CGI runner at %s\n' "$cgi_target" printf 'Installed muSRFlow startup XML at %s\n' "$startup_target" if [ -n "$web_owner" ]; then printf 'Ownership set to %s:%s\n' "$web_owner" "$web_group" else printf 'Ownership left unchanged (deployed by the owning user)\n' fi