#!/usr/bin/env python
import os
import sys
import json
import argparse
import awscli.clidriver
from awscli.help import HelpRenderer

REF_PATH = 'reference'
TUT_PATH = 'tutorial'


class FileRenderer(HelpRenderer):

    def __init__(self, file_path):
        self._file_path = file_path

    def render(self, contents):
        fp = open(self._file_path, 'wb')
        fp.write(contents)
        fp.close()


def do_operation(driver, service_path, operation_name, operation_command):
    file_path = os.path.join(service_path,
                             operation_name + '.rst')
    help_command = operation_command.create_help_command()
    if help_command is None:
        # Do not document anything that does not have a help command.
        return
    help_command.doc.target = 'html'
    help_command.renderer = FileRenderer(file_path)
    help_command(None, None)


def do_service(driver, ref_path, service_name, service_command):
    print('...%s' % service_name)
    service_path = os.path.join(ref_path, service_name)
    if not os.path.isdir(service_path):
        os.mkdir(service_path)
    index_path = os.path.join(service_path, 'index.rst')
    help_command = service_command.create_help_command()
    if help_command is None:
        # Do not document anything that does not have a help command.
        return
    help_command.doc.target = 'html'
    help_command.renderer = FileRenderer(index_path)
    help_command(None, None)
    for operation_name in help_command.command_table:
        if operation_name == 'help':
            continue
        operation_command = help_command.command_table[operation_name]
        do_operation(driver, service_path, operation_name, operation_command)


def do_provider(driver):
    help_command = driver.create_help_command()
    help_command.doc.target = 'html'
    help_command.renderer = FileRenderer(os.path.join(REF_PATH, 'index.rst'))
    help_command(None, None)
    services = sorted(help_command.command_table)
    for service_name in services:
        if service_name == 'help':
            continue
        service_command = help_command.command_table[service_name]
        do_service(driver, REF_PATH, service_name, service_command)


def build_service_list(tut_path, ref_path, driver):
    file_path = os.path.join(tut_path, 'services.rst')
    fp = open(file_path, 'w')
    fp.write('\n')
    l = []
    help_command = driver.create_help_command()
    for service_name in help_command.command_table:
        if service_name == 'help':
            continue
        service_command = help_command.command_table[service_name]
        if not hasattr(service_command, '_service_object'):
            continue
        service = service_command._service_object
        l.append((service.service_full_name, service_name))
    l = sorted(l, key=lambda x: x[1])
    for full_name, service_name in l:
        service_ref_path = os.path.join(ref_path, service_name)
        service_ref_path = os.path.join(service_ref_path, 'index')
        fp.write('* :doc:`%s <..%s%s>`\n' % (full_name,
                                             os.path.sep,
                                             service_ref_path))
    fp.write('\n')
    fp.close()


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-s', '--service',
                        help='Name of service, or else all services')
    parser.add_argument('-o', '--operations',
                        help='Name of operations, or else all operations',
                        nargs='*')
    args = parser.parse_args()
    driver = awscli.clidriver.create_clidriver()
    if not os.path.isdir(REF_PATH):
        os.mkdir(REF_PATH)
    if not os.path.isdir(TUT_PATH):
        os.mkdir(TUT_PATH)
    print('Generating ReST documents for all services...')
    do_provider(driver)
    print('Generating service list ReST document...')
    build_service_list(TUT_PATH, REF_PATH, driver)
    print('Done!')
