• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018-2023 Gavin D. Howard and contributors.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are met:
9#
10# * Redistributions of source code must retain the above copyright notice, this
11#   list of conditions and the following disclaimer.
12#
13# * Redistributions in binary form must reproduce the above copyright notice,
14#   this list of conditions and the following disclaimer in the documentation
15#   and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27# POSSIBILITY OF SUCH DAMAGE.
28#
29
30# Print the usage and exit with an error.
31usage() {
32	printf "usage: %s manpage\n" "$0" 1>&2
33	exit 1
34}
35
36# Generate a manpage and print it to a file.
37# @param md   The markdown manual to generate a manpage for.
38# @param out  The file to print the manpage to.
39gen_manpage() {
40
41	_gen_manpage_md="$1"
42	shift
43
44	_gen_manpage_out="$1"
45	shift
46
47	cat "$manualsdir/header.txt" > "$_gen_manpage_out"
48	cat "$manualsdir/header_${manpage}.txt" >> "$_gen_manpage_out"
49
50	pandoc -f commonmark_x -t man "$_gen_manpage_md" >> "$_gen_manpage_out"
51}
52
53# Generate a manual from a template and print it to a file before generating
54# its manpage.
55# param args  The type of markdown manual to generate. This is a string that
56#             corresponds to build type (see the Build Type section of the
57#             manuals/build.md manual).
58gen_manual() {
59
60	_gen_manual_args="$1"
61	shift
62
63	# Set up some local variables. $manualsdir and $manpage from from the
64	# variables outside the function.
65	_gen_manual_out="$manualsdir/$manpage/$_gen_manual_args.1"
66	_gen_manual_md="$manualsdir/$manpage/$_gen_manual_args.1.md"
67
68	# Remove the files that will be generated.
69	rm -rf "$_gen_manual_out" "$_gen_manual_md"
70
71	# Filter the text for the build type.
72	filter_text "$manualsdir/${manpage}.1.md.in" "$_gen_manual_md" "$_gen_manual_args"
73
74	# Generate the manpage.
75	gen_manpage "$_gen_manual_md" "$_gen_manual_out"
76}
77
78set -e
79
80script="$0"
81scriptdir=$(dirname "$script")
82manualsdir="$scriptdir/../manuals"
83
84. "$scriptdir/functions.sh"
85
86# Constants for use later. If the set of build types is changed, $ARGS must be
87# updated.
88ARGS="A E H N EH EN HN EHN"
89
90# Process command-line arguments.
91test "$#" -eq 1 || usage
92
93manpage="$1"
94shift
95
96if [ "$manpage" != "bcl" ]; then
97
98	# Generate a manual and manpage for each build type.
99	for a in $ARGS; do
100		gen_manual "$a"
101	done
102
103else
104	# For bcl, just generate the manpage.
105	gen_manpage "$manualsdir/${manpage}.3.md" "$manualsdir/${manpage}.3"
106fi
107