libk  build-manpage.sh at [34c625a47b]

File global/build-manpage.sh artifact b795aa8a1c part of check-in 34c625a47b


#!/usr/bin/env sh
(test -d global && test -f build.sh) || {
	echo >&2 "($0) run $me from root of libk source directory"
	exit 1
}

. global/common.sh
reqpack cmark "generate documentation"


file="$1"
filename="$(basename "$file")"
stem=${filename%%.md}
mandest="$out/doc/man"
section=${section:-4}
out="$mandest/$stem.$section"
desc_rec="$(grep -m1 "^$stem:" global/modules)"
if test "$desc_rec" != ""; then
	desc=" - $(echo $desc_rec | cut -d: -f4)"
else
	desc=""
fi
date="$(date -r"$file" "+%d %A %Y")"

mkdir -p "$mandest"

echo  >"$out" ".TH ${stem^^} \"$section\" \"$date\" \"hale.su libk [r$build]\" \"modules\""

echo >>"$out" ".SH NAME"
echo >>"$out" "$stem$desc"
echo >>"$out" ".SH SYNOPSIS" 

dhead=$(grep -m1 -n "^# description$" "$file")
if test $? -eq 0; then
	descline=$(echo "$dhead" | cut -d: -f1)
	offset=1
else
	descline=$(grep -m2 -n "^#" "$file" | tail -n1 | cut -d: -f1)
	offset=0
fi

tail -n+2 $file | head -n$(expr $descline - 2) | cmark -t man >>"$out"

echo >>"$out" ".SH DESCRIPTION"

tail -n+$(expr $descline + $offset) "$file"  | cmark -t man >> "$out"

if has gzip; then
	gzip -f "$out"
	out="$out.gz"
fi

say "wrote manpage for $stem to $out"