#!/bin/sh -eu
#
# Helper to generate a manpage from a markdown file.
#
# Usage:
#   make-manpage <src> <dst>

if [ "$#" -ne 2 ]; then
    echo "Usage: $0 <src> <dst>" >&2
    exit 1
fi

SRC="$1"
DST="$2"

# This pipeline converts a markdown file into a manpage:
# 1. Delete custom <?tabs> and </tabs?> tags lines entirely.
# 2. Remove ANY markdown image `![alt](url)`.
# 3. Replace internal `[text](rel/path.md)` links with just `text`.
# 4. Pandoc converts the rest to man format.
sed -e '/<?tabs>/d' \
    -e '/<\/tabs?>/d' \
    -e 's/!\[[^]]*\]([^)]*)//g' \
    -e 's/\[\([^]]*\)\]([^)]*\.md[^)]*)/\1/g' \
    "${SRC}" | pandoc -s -t man --shift-heading-level-by=-1 -o "${DST}"