#!/usr/bin/env python

##############################################################################
# Medical Image Registration ToolKit (MIRTK)
#
# Copyright 2013-2015 Imperial College London
# Copyright 2013-2015 Andreas Schuh
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################

"""
Generates an overview table of all commands with brief description formatted in reStructuredText.
"""

# ==============================================================================
# Imports
# ==============================================================================

import argparse
import os
import sys

# ==============================================================================
# Main
# ==============================================================================

# ------------------------------------------------------------------------------
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('command_names', nargs='*', help='Names of MIRTK commands')
    args = parser.parse_args()
    print('.. Auto-generated by Documentation/tools/' + os.path.basename(sys.argv[0]) + ' during CMake configure step')
    # toctree
    print("""
.. toctree::
   :hidden:
   :maxdepth: 1
""")
    for arg in args.command_names:
        print('   /commands/' + arg)
    # table
    header1 = 'Command'
    header2 = 'Description'
    max_length = len(header1)
    for arg in args.command_names:
        max_length = max(max_length, len(arg))
    brief_description_suffix = '-brief-description'
    separator = '=' * (max_length + 2) + ' ' + '=' * (max_length + len(brief_description_suffix) + 2)
    print('\n' + separator)
    print(header1 + (' ' * (max_length - len(header1) + 3)) + header2)
    print(separator)
    for arg in args.command_names:
        print('|' + arg + '|' + (' ' * (max_length - len(arg))) + ' |' + arg + brief_description_suffix + '|')
    print(separator + '\n')
    for arg in args.command_names:
        print('.. |' + arg + '|' + (' ' * (max_length - len(arg))) + ' replace:: :doc:`/commands/' + arg + '`')
    sys.stdout.write('\n')
    for arg in args.command_names:
        print('.. include:: /commands/_summaries/' + arg + '.rst')
    sys.exit(0)
