#!/usr/bin/env sh

printf "<table>"
printf "<tr>"
printf "<th>Man Page</th>"
printf "<th>Description</th>"
printf "</tr>"

find man build/man -type f -name '*.[1-9]' | sort -r | while IFS= read -r file; do
    base=$(basename "$file")

    name=$(echo "$base" | rev | cut -d '.' -f 2- | rev)
    section=$(echo "$base" | rev | cut -d '.' -f 1)
    description=$(grep '^\.Nd' "$file" | cut -d ' ' -f 2-)

    prefix=$(echo "$name" | cut -d '-' -f 1)

    case "$prefix" in
        "telodendria"*)
            if [ "$1" = "dev" ]; then
                continue
            fi
            ;;
        *)
            if [ "$1" = "user" ]; then
                continue
            fi
            ;;
    esac

    printf "<tr>"
    printf "<td><a href=\"man/man$section/$name.$section.html\">$name($section)</a></td>"
    printf "<td>$description</td>"
    printf "</tr>"
done

printf "</table>"