#!/bin/bash

set -eu
cd "$(dirname "$0")"

COMMANDS=("sigsum-key" "sigsum-monitor" "sigsum-verify" "sigsum-submit" "sigsum-token" "sigsum-policy")

declare -A SUBCOMMANDS
SUBCOMMANDS["sigsum-key"]="generate verify sign to-hash to-hex to-vkey from-hex from-vkey"
SUBCOMMANDS["sigsum-token"]="create record verify"
SUBCOMMANDS["sigsum-policy"]="list show"

version=$1; shift
for cmd in "${COMMANDS[@]}"; do
    echo "INFO: generating doc/$cmd.1" >&2

    # Include ENVIRONMENT and FILES sections only for those commands that need those
    env_and_files_includes="--include=environment.help2man --include=files.help2man"
    if [[ $cmd == "sigsum-key" || $cmd == "sigsum-token" ]] ; then
	env_and_files_includes=""
    fi

    help2man \
        --no-info \
        --version-string "$cmd $version" \
        --include="../../cmd/$cmd/help2man/name.help2man" \
        --include="../../cmd/$cmd/help2man/see-also.help2man" \
        $env_and_files_includes \
        --include="return-codes.help2man" \
        --include="reporting-bugs.help2man" \
        --include="contact.help2man" \
        -o "../$cmd.1" "./wrapper $cmd no"

    # Generate subcommand man pages
    if [[ -n "${SUBCOMMANDS[$cmd]:-}" ]]; then
        for subcmd in ${SUBCOMMANDS[$cmd]}; do
            echo "INFO: generating doc/$cmd-$subcmd.1" >&2
            help2man \
                --no-info \
                --version-string "$cmd $subcmd $version" \
                --include="../../cmd/$cmd/help2man/$subcmd.help2man" \
                --include="../../cmd/$cmd/help2man/see-also.help2man" \
                $env_and_files_includes \
                --include="return-codes.help2man" \
                --include="reporting-bugs.help2man" \
                --include="contact.help2man" \
                -o "../$cmd-$subcmd.1" "./wrapper $cmd $subcmd"
        done
    fi
done
