From 2fc1af63b20ab67683b74e6bb8f2a3edf5ef2a3d Mon Sep 17 00:00:00 2001 From: _ov4 Date: Tue, 13 Oct 2026 14:23:18 +0000 Subject: Install manuals in an independent directory and register manpath Co-Authored-By: Claude Fable 5.1 --- install.sh | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 106 insertions(+), 9 deletions(-) (limited to 'install.sh') diff --git a/install.sh b/install.sh index 43a4a42..fbe98a2 100755 --- a/install.sh +++ b/install.sh @@ -5,11 +5,12 @@ usage() { cat <<'EOF' Usage: install.sh [--prefix DIR] [--source-dir DIR] -Install cpsc and its man page to DIR/bin and DIR/share/man/man1. +Install cpsc to DIR/bin and its man pages to DIR/share/cpsc/man/man1 and man7. The default prefix is /usr/local. The source directory defaults to the -directory containing this script and must contain cpsc and doc/cpsc.1. +directory containing this script and must contain cpsc, doc/cpsc.1 and +doc/language/*.7. Installation registers the independent manual search path. Use --source-dir build when running this script from the source checkout. -DESTDIR may be set to stage an installation without changing the prefix. +DESTDIR may be set to stage files without changing host manpath configuration. EOF } @@ -45,18 +46,108 @@ source_dir=$(CDPATH= cd -- "$source_dir" && pwd) || die "cannot open source dire || die "missing readable executable: $source_dir/cpsc; build cpsc first" [ -f "$source_dir/doc/cpsc.1" ] && [ -r "$source_dir/doc/cpsc.1" ] \ || die "missing readable manual: $source_dir/doc/cpsc.1; run make doc first" +set -- "$source_dir"/doc/language/cpsc-*.7 +[ -f "$1" ] || die "missing language manuals: $source_dir/doc/language; run make doc first" +for manual do + [ -f "$manual" ] && [ -r "$manual" ] || die "missing readable manual: $manual" +done command -v install >/dev/null 2>&1 || die "required command not found: install" +platform=$(uname -s) +man_root=${prefix%/}/share/cpsc/man +case "$man_root" in + *:*) die "--prefix cannot contain a colon (manpath separator)" ;; + *' +'*) die "--prefix cannot contain a newline" ;; +esac +if [ "$platform" = Linux ] && [ -z "$destdir" ]; then + case "$man_root" in + *[[:space:]]*) die "Linux man-db registration requires a --prefix without whitespace" ;; + esac +fi + +# Append only our entry; leave existing system/user configuration intact. +register_line() { + config=$1 + entry=$2 + if [ -e "$config" ]; then + [ -f "$config" ] && [ -r "$config" ] && [ -w "$config" ] \ + || die "cannot update manpath configuration: $config" + if grep -Fqx -- "$entry" "$config"; then return; fi + fi + printf '\n# cpsc manual: %s\n%s\n' "$man_root" "$entry" >> "$config" \ + || die "cannot register manuals in $config" + printf 'Registered manuals in %s\n' "$config" +} + +register_manpath() { + case "$platform" in + Linux) + if [ "$(id -u)" -eq 0 ]; then + if [ -f /etc/manpath.config ]; then + config=/etc/manpath.config + elif [ -f /etc/man_db.conf ]; then + config=/etc/man_db.conf + else + die "cannot locate man-db configuration; register $man_root manually" + fi + else + [ -n "${HOME:-}" ] || die "HOME is unset; cannot locate user manpath configuration" + config=$HOME/.manpath + fi + register_line "$config" "MANDATORY_MANPATH $man_root" + ;; + Darwin) + if [ "$(id -u)" -eq 0 ]; then + install -d /usr/local/etc/man.d || die "cannot create macOS man configuration directory" + register_line /usr/local/etc/man.d/cpsc.conf "MANPATH $man_root" + else + [ -n "${HOME:-}" ] || die "HOME is unset; cannot locate user shell configuration" + case "${SHELL:-}" in + */zsh) config=$HOME/.zprofile ;; + */bash) config=$HOME/.bash_profile ;; + *) + printf 'Set MANPATH manually for your shell: %s:\n' "$man_root" + config= + ;; + esac + # Quote a literal path inside double quotes in the shell snippet. + shell_man_root=$(printf '%s' "$man_root" | sed 's/[\\`"$]/\\&/g') + if [ -n "$config" ]; then + marker="# cpsc manual: $man_root" + if ! grep -Fqx -- "$marker" "$config" 2>/dev/null; then + printf '\n%s\n%s\n%s\n%s\n%s\n' "$marker" \ + 'case ":${MANPATH:-}:" in' \ + " *\":$shell_man_root:\"*) ;;" \ + " *) export MANPATH=\"$shell_man_root:\${MANPATH:-}\" ;;" \ + 'esac' >> "$config" || die "cannot update shell configuration: $config" + fi + printf 'Registered manuals in %s; open a new login terminal.\n' "$config" + fi + printf '%s\n' 'For optional global registration, run manually:' \ + ' sudo mkdir -p /usr/local/etc/man.d' \ + " printf '%s\\n' \"MANPATH $shell_man_root\" | sudo tee -a /usr/local/etc/man.d/cpsc.conf" + fi + ;; + *) printf 'Register %s in your system or user manpath configuration.\n' "$man_root" ;; + esac +} + bin_dir=$destdir${prefix%/}/bin -man_dir=$destdir${prefix%/}/share/man/man1 -install -d "$bin_dir" "$man_dir" \ +man_dir=$destdir$man_root +install -d "$bin_dir" "$man_dir/man1" "$man_dir/man7" \ || die "cannot create installation directories; check permissions or choose a writable --prefix" install -m 755 "$source_dir/cpsc" "$bin_dir/cpsc" \ || die "cannot install cpsc to $bin_dir; check permissions" -install -m 644 "$source_dir/doc/cpsc.1" "$man_dir/cpsc.1" \ +install -m 644 "$source_dir/doc/cpsc.1" "$man_dir/man1/cpsc.1" \ || die "cannot install the manual to $man_dir; check permissions" -if [ "$(uname -s)" = Darwin ]; then +for manual do + install -m 644 "$manual" "$man_dir/man7/$(basename -- "$manual")" \ + || die "cannot install language manual: $manual" +done + +if [ "$platform" = Darwin ]; then command -v xattr >/dev/null 2>&1 || die "required command not found: xattr" attributes=$(xattr "$bin_dir/cpsc") \ || die "cannot read extended attributes of $bin_dir/cpsc" @@ -66,11 +157,17 @@ if [ "$(uname -s)" = Darwin ]; then fi fi -printf 'Installed %s\nInstalled %s\n' "$bin_dir/cpsc" "$man_dir/cpsc.1" +printf 'Installed %s\nInstalled manuals in %s\n' "$bin_dir/cpsc" "$man_dir" if [ -z "$destdir" ]; then + register_manpath case ":${PATH:-}:" in *":${prefix%/}/bin:"*) ;; *) printf 'Add %s/bin to PATH to run cpsc by name.\n' "${prefix%/}" ;; esac - printf 'Read the manual with: man cpsc\n' + printf 'Read manuals with: man 1 cpsc; man 7 cpsc-language-overview\n' + if [ -n "${MANPATH:-}" ]; then + printf 'Your explicit MANPATH overrides configuration; also include %s (retain a colon for defaults).\n' "$man_root" + fi +else + printf 'Staged files only; register %s in manpath when deploying.\n' "$man_root" fi -- cgit v1.3.1-4-g156e