#!/bin/sh

if [ -x /usr/bin/pandoc ]
then
    pandoc "$1" -t man -s -o "$2"
    exit 0
fi

get_header() {
    grep "$2:" "$1" | cut -d ' ' -f 2-
}

sed -e '/---/,/---/d' \
    -e '/## NAME/,+2 {/## NAME/d; s/^.*-/# /}' "$1" | \
    ronn --roff --pipe \
        --manual="$(get_header "$1" header)" \
        --name "$(get_header "$1" title)" \
        --organization "$(get_header "$1" footer)" \
        --section "$(get_header "$1" section)" > "$2"
