#!/usr/bin/env sh

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

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

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

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

    if [ "$prefix" = "telodendria" ]; then
        if [ "$1" = "dev" ]; then
            continue
        fi
    else
        if [ "$1" = "user" ]; then
            continue
        fi
    fi

    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>"