#! /usr/bin/env python3

import argparse
import sys
import os
from pathlib import Path


# TODO: add inline argument

from temporython import LIBRARY_PATH


def load_file(filename):
    with open(filename, 'r') as input_file:
        contents = input_file.read()
    return contents


IMPORT_LIBRARY_CODE = load_file(os.path.join(LIBRARY_PATH, 'temporython_import.py'))
INLINE_LIBRARY_CODE = load_file(os.path.join(LIBRARY_PATH, 'temporython_inline_process_files_library.py'))
IMPORT_PIPE_LIBRARY_CODE = load_file(os.path.join(LIBRARY_PATH, 'temporython_pipe_import.py'))
INLINE_PIPE_LIBRARY_CODE = load_file(os.path.join(LIBRARY_PATH, 'temporython_inline_process_pipe_library.py'))

def main():
    TEMPLATE_TYPES = {
        'lines': os.path.join(LIBRARY_PATH, 'templates', 'process_file_lines.py.template'),
        'files': os.path.join(LIBRARY_PATH, 'templates', 'process_files.py.template'),
        'pipe': os.path.join(LIBRARY_PATH, 'templates', 'process_file_lines_pipe_only.py.template'),
    }
    TEMPLATE_TYPE_CHOICES = sorted(TEMPLATE_TYPES.keys())

    # set up aguments ----
    parser = argparse.ArgumentParser()
    parser.add_argument('template_type', choices=TEMPLATE_TYPE_CHOICES)
    parser.add_argument('-i', '--inline', help='Inline the temporython library in the generated code instead of including it.', action='store_true')
    parser.add_argument('FILENAME', nargs='?', help='Name of file to generate')
    args = parser.parse_args()

    # load parameters ----
    template_type = args.template_type
    new_filename = args.FILENAME
    if not new_filename:
        new_filename = 'process_' + template_type + '.tmp'
    inline = args.inline

    # check if safe to write to that filename ----
    if (new_filename != '-') and Path(new_filename).exists():
        print('ERROR:' + repr(new_filename) + ' already exists!')
        sys.exit(1)

    # build the contents of the file that we want to generate ----
    template_filename = TEMPLATE_TYPES[template_type]
    with open(template_filename, 'r') as input_file:
        template_contents = input_file.read()
    if inline:
        import_file_contents = INLINE_LIBRARY_CODE
        import_pipe_contents = INLINE_PIPE_LIBRARY_CODE
    else:
        import_file_contents = IMPORT_LIBRARY_CODE
        import_pipe_contents = IMPORT_PIPE_LIBRARY_CODE
    rendered_contents = template_contents.replace('$INCLUDE_TEMPORYTHON_PROCESS_FILES_IN_ARGV', import_file_contents)
    rendered_contents = rendered_contents.replace('$INCLUDE_TEMPORYTHON_PROCESS_PIPE_LIBRARY', import_pipe_contents)

    # write the file (or to stdout) ----
    if new_filename == '-':  # stdout
        print(rendered_contents)
    else:
        with open(new_filename, 'w') as output_file:
            output_file.write(rendered_contents)
        # make the newly-generated file executable ----
        os.chmod(new_filename, 0o775)
        print('wrote file ' + repr(new_filename) + '.')

    # report success ----
    sys.exit(0)


if __name__ == '__main__':
    main()
