1#!/bin/sh
2# SPDX-License-Identifier: LGPL-2.1-or-later
3
4set -e
5
6if [ -z "$1" ]; then
7    echo "Use: $0 page-name (with no section suffix)"
8    exit 1
9fi
10
11# make sure the rules have been regenerated (in case update-man-rules was just run)
12ninja -C "@BUILD_ROOT@" version.h
13
14page="$(echo "$1" | sed 's/\./\\./')"
15target=$(ninja -C "@BUILD_ROOT@" -t query man/man | grep -E -m1 "man/$page\.[0-9]$" | awk '{print $2}')
16if [ -z "$target" ]; then
17    echo "Cannot find page $1"
18    exit 1
19fi
20ninja -C "@BUILD_ROOT@" "$target"
21
22fullname="@BUILD_ROOT@/$target"
23redirect="$(sed -n -r '1 s|^\.so man[0-9]/(.*)|\1|p' "$fullname")"
24if [ -n "$redirect" ]; then
25    ninja -C "@BUILD_ROOT@" "man/$redirect"
26
27    fullname="@BUILD_ROOT@/man/$redirect"
28fi
29
30exec man "$fullname"
31