#!/bin/sh -e
#
# SYNOPSIS
#
# make_man [OPTIONS...] INPUT-FILE...
#
# OPTIONS
#
# --protos FILE     (default: protos)
# --section NUM     (default: 3)
# --manual TEXT
#
# DESCRIPTION
#
# Generate man pages from each specially marked section of documentation
# files.  Sections can be marked:
#
#   # API: foo
#
# Pages are generated in the current directory.
#
# ENVIRONMENT VARIABLES
#
# - PANDOC
# - PROTOS
# - SECTION
# - MANUAL
#
# (For portability, stick to POSIX shell constructs in this script.)
#

# Passed down to make_man.awk.
export PANDOC=${PANDOC:-pandoc}
export PROTOS=${PROTOS:-protos}
export SECTION=${SECTION:-3}
export MANUAL=${MANUAL:-Allegro reference manual}

SPLIT_API_DOCS=$(dirname "$0")/split_api_docs

# Speed up awk.
export LC_ALL=C

while true; do
    case $1 in
        --protos)
            PROTOS=$2
            shift 2
            ;;
        --section)
            SECTION=$2
            shift 2
            ;;
        --manual)
            MANUAL=$2
            shift 2
            ;;
        *)
            break
            ;;
    esac
done

TEMPDIR=make_man.tmp.$$_$RANDOM
mkdir $TEMPDIR
trap 'rm -rf $TEMPDIR' 0 1 2 3 13 15

awk -f "$SPLIT_API_DOCS" $TEMPDIR "$@"
awk -f "$0.awk" $TEMPDIR/*

# vim: set et:
