#!/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.
##############################################################################

"""
Formats a table listing the names of all commands 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()
    sys.stdout.write('.. Auto-generated by Documentation/tools/' + os.path.basename(sys.argv[0]) + ' during CMake configure step\n\n')
    ncol = 4
    wcol = 0
    for arg in args.command_names:
        wcol = max(wcol, len(arg))
    wcol += 2 # "|<command_name>|"
    for i in range(0, ncol):
        if i > 0: sys.stdout.write(' ')
        sys.stdout.write('=' * wcol)
    row = 0
    sys.stdout.write('\n')
    for i in range(0, len(args.command_names)):
        if i > 0:
            if i % ncol == 0: sys.stdout.write('\n')
            else:             sys.stdout.write(' ')
        sys.stdout.write('|' + args.command_names[i] + '|')
        sys.stdout.write(' ' * (wcol - len(args.command_names[i]) - 2))
    sys.stdout.write('\n')
    for i in range(0, ncol):
        if i > 0: sys.stdout.write(' ')
        sys.stdout.write('=' * wcol)
    sys.stdout.write('\n\n')
    for i in range(0, len(args.command_names)):
        sys.stdout.write('.. |' + args.command_names[i] + '| replace:: :doc:`commands/' + args.command_names[i] + '`\n')
    sys.stdout.write('\n')
    sys.exit(0)
