aboutsummaryrefslogtreecommitdiff
path: root/tools/tool2man.sh
blob: 9111243c5b409ba3499b49d925f1cdf2e070fc09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/sh -e
# Convert 'contrib' scripts to manual pages.

# This script basically converts some of the comments inside scripts to manual
# page format. The intention behind this utility is to generate manual pages for
# contrib scripts without much hassle.

# Syntax:
# Every line starts with two hashes and a space ('## '). If the line ends with a
# colon (':'), it is assumed to be a section header. Subsections follow the same
# convention, but uses three hashes instead of two. An empty '## ' line will
# start a new paragraph (.Pp).  Otherwise, mdoc(7) syntax is used as is.
# Headers are generated using the script's name, and the docstring is used from
# the line following the shebang.
#
# 'See also' section is pregenerated and only points the user to cpt(1). Others
# can be added with the following line and format:
#     ## see: cpt-fork.1 this-manpage.5

case "$1" in
    --help|-h|'')
        printf 'usage: %s [file]\n' "${0##*/}" >&2
        exit 1
esac

out() { printf '%s\n' "$@"; }

file=$1
filename=${file##*/}
see=''
date=$(date "+%b %d, %Y")
docstr=$(sed -n '2s/# //p' "$file" | tr '[:upper:]' '[:lower:]')

out \
    ".Dd $date" \
    ".Dt $filename 1" \
    ".Sh NAME" \
    ".Nm $filename" \
    ".Nd $docstr"

while read -r line; do
    case $line in
        '###'*:)
            line=${line%:}
            out ".Ss ${line#'### '}"
            ;;
        '## see':*)
            line=${line##*:}
            see=$line
            ;;
        '##'*:)
            line=${line%:}
            out ".Sh ${line#'## '}"
            ;;
        '##'|'###')
            out ".Pp"
            ;;
        '##'*)
            line=${line#'##'}
            line=${line#'#'}
            line=${line# }
            out "$line"
            ;;
    esac
done < "$file"

out ".Sh AUTHOR" ".An Cem Keylan Aq Mt cem@ckyln.com"
out ".Sh LICENSE" "See LICENSE for copyright information."
out ".Sh SEE ALSO" ".Xr cpt 1"
[ "$see" ] &&
    for man in $see; do
        out ".Xr ${man%.[0-9]} ${man##*.}"
    done
out ".Pp"
out "The full documentation of cpt is available as an info page."
out "If either" ".Ic info" or ".Ic texinfo"
out "package is installed on your system, you can run"
out ".Bd -literal -offset indent" "info cpt" ".Ed"
out .Pp "to learn more about the package manager."