#!/usr/bin/env python3

"""Create an ecosnail project"""

import argparse
import os

ECOSNAIL_ROOT = os.path.dirname(os.path.realpath(__file__))

PROJECT_CMAKE_TEMPLATE = """cmake_minimum_required (VERSION {0.cmake_version})

project ({0.project_name})

set (CMAKE_CXX_STANDARD {0.cxx_standard})
set (CMAKE_CXX_STANDARD_REQUIRED TRUE)

add_library ({0.project_name}
)
target_include_directories ({0.project_name} PUBLIC include)

add_subdirectory (examples)

enable_testing ()
add_subdirectory (tests)
"""

LINK_LIBRARIES_CMAKE_TEMPLATE = "link_libraries ({0.project_name})\n"


def write_hierarchy(root, hierarchy):
    if isinstance(hierarchy, dict):
        os.mkdir(root)
        for directory_name, inner_hierarchy in hierarchy.items():
            directory = os.path.join(root, directory_name)
            write_hierarchy(directory, inner_hierarchy)
    else:
        with open(root, "w") as file:
            file.write(hierarchy)


def main():
    parser = argparse.ArgumentParser(description="Create an ecosnail project")
    parser.add_argument("project_name")
    parser.add_argument("--cmake-version", default="3.8")
    parser.add_argument("--cxx-standard", default="17")
    args = parser.parse_args()

    write_hierarchy(
        os.path.join(ECOSNAIL_ROOT, args.project_name),
        {
            "include": {
                "ecosnail": {
                    args.project_name: {},
                    "{}.hpp".format(args.project_name): "#pragma once\n",
                },
            },
            "examples": {
                "CMakeLists.txt": LINK_LIBRARIES_CMAKE_TEMPLATE.format(args),
            },
            "tests": {
                "CMakeLists.txt": LINK_LIBRARIES_CMAKE_TEMPLATE.format(args),
            },
            "CMakeLists.txt": PROJECT_CMAKE_TEMPLATE.format(args),
        })


if __name__ == "__main__":
    main()
