#!/bin/sh

set -eu

# Generate manpages from the program invocation. Unfortunately,
# help2man is not good enough for the job here.

# Usage: generate-manpages <output-directory>

version="$(dpkg-parsechangelog --show-field Version | sed -e 's/+ds.*//')"

output_dir="$1"

mkdir -p "$output_dir"

convert() {
    local binary program line output_mode
    binary="$1"
    line="$2"
    output_mode="${3-stderr}"

    program="$(basename "$binary")"

    help_output="$output_dir/$program.help"
    # The programs dump their usage text in different forms ...
    case "$output_mode" in
        stdout)
            "$binary" -? >"$help_output" 2>/dev/null || :
            ;;
        stderr)
            "$binary" -? >/dev/null 2>"$help_output" || :
            ;;
    esac

    # Drop everything until and including "Available (options)"
    sed -n -i -e '/^Available /,$p' "$help_output"
    sed -i -e '1d' "$help_output"

    scdoc="$output_dir/$program.scd"
    manpage="$output_dir/$program.1"

    cat <<__EOS__ >"$scdoc"
$program(1) "libEST $version" "libEST Documentation"

# NAME

$program - $line

# OPTIONS

__EOS__

    # Convert usage output into markdown, basically:
    # * "--option <param> <description>" =>
    #   "*--option* <param>\n\t<description>"
    # * "--option <description>" =>
    #   "*--option::\n\t<description>"
    # * Indent following lines with tab
    sed -e "
s/^ *\(--*[a-z][^ ]*\) \{1,\}<\([a-z][^>]\{1,\}\)> */\\n*\1* _\2_\\n\\t/ ;
s/^ *\(--*[a-z][^ ]*\) \{1,\}<\([A-Z][^>]\{1,\}\)> */\\n*\1* \2\\n\\t/ ;
s/^ *\(--*[a-z6\?][^ ]*\) \{1,\}/\\n*\1*\\n\\t/ ;
s/^ */\t/
" \
        <"$help_output" >>"$scdoc"

    cat <<__EOS__ >>"$scdoc"

# COPYRIGHT & LICENSE

Copyright (c) 2012-2018 Cisco Systems, Inc.  All rights reserved.

License (BSD-3-Clause):

\`\`\`
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the following
  disclaimer in the documentation and/or other materials provided
  with the distribution.
Neither the name of the Cisco Systems, Inc. nor the names of its
  contributors may be used to endorse or promote products derived
  from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
\`\`\`

# AUTHOR

This manpage is based on $program's usage output and the included
documentation. It was written for the Debian project by Christoph Biedl
<debian.axhn@manchmal.in-ulm.de> but may be used by others.
__EOS__

    scdoc <"$scdoc" >"$manpage"
}

convert \
    example/client/estclient \
    'example EST client application using the granular API'

convert \
    example/client-simple/estclient_simple \
    'example EST client application using the easy provision API' \
    stdout

convert \
    example/client-brski/estclient_brski \
    'example EST client application using Bootstrapping Remote Secure Key Infrastructure' \
    stdout

convert \
    example/proxy/estproxy \
    'example EST proxy application acting as an RA'

convert \
    example/server/estserver \
    'example EST server application using OpenSSL CA'
