#!/usr/bin/env python3

import os
from datetime import datetime, timedelta

import click

directory = os.path.dirname(__file__)
with open(os.path.join(directory, "tool_sample_plugin.yaml"), "r") as file:
    initial_plugin_contents = file.read()

with open(os.path.join(directory, "tool_sample.test.ts"), "r") as file:
    initial_test_contents = file.read()


@click.group()
def cli():
    pass


@cli.command()
@click.argument("workspace")
def scan(workspace):
    tool_dir = os.path.join(workspace, "tools")
    generated_files = False
    for tool_name in os.listdir(tool_dir):
        subdir_path = os.path.join(tool_dir, tool_name)
        if os.path.isfile(subdir_path):
            continue

        subdir_contents = os.listdir(subdir_path)
        # If this is a newly created, empty directory, dump template files
        if (
            len(subdir_contents) == 0
            and os.stat(subdir_path).st_ctime
            > (datetime.now() - timedelta(seconds=10)).timestamp()
        ):
            generated_files = True
            # Write plugin.yaml
            with open(os.path.join(subdir_path, "plugin.yaml"), "w") as plugin_file:
                plugin_file.write(
                    initial_plugin_contents.replace("NAME_HERE", tool_name)
                )

            # Write test file
            with open(
                os.path.join(
                    subdir_path, "{}.test.ts".format(tool_name.replace("-", "_"))
                ),
                "w",
            ) as test_file:
                test_file.write(initial_test_contents.replace("NAME_HERE", tool_name))

            print("Created starter files in {}", subdir_path)

    if not generated_files:
        print("No generated files")


if __name__ == "__main__":
    cli()
