#!/usr/bin/env python3
#!/usr/bin/env python
# Copyright (c) 2016-2019 The UUV Simulator Authors.
# All rights reserved.
#
# 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.
import argparse
import sys
import os
import code
import uuv_docs
import yaml


if __name__ == '__main__':
    parser = argparse.ArgumentParser('Generate documentation for a catkin package')
    parser.add_argument('--config_file', type=str, help='YAML file with the documentation configuration for the catkin packages')
    args = parser.parse_args()

    with open(args.config_file, 'r') as config_file:
        config = yaml.load(config_file)

    print(config)

    for pkg_config in config['catkin_pkg_docs']:
        print('Repository name=', pkg_config['repository_name'])
        for catkin_pkg in pkg_config['pkg_names']:
            print('\t Generating documentation for=', catkin_pkg)
            
            config_gen = uuv_docs.CatkinPkgConfigGenerator(
                catkin_pkg, 
                pkg_config['repository_name'], 
                config['git_root_url'])

            if not os.path.isdir(os.path.join(pkg_config['output_dir'])):
                os.makedirs(os.path.join(pkg_config['output_dir']))

            with open(os.path.join(pkg_config['output_dir'], catkin_pkg + '.md'), 'w+') as md_file:
                md_file.write(config_gen.get_markdown())

            print('\t Output filename=', os.path.join(pkg_config['output_dir'], catkin_pkg + '.md'))

    
    