"""
This file is used to generate the files from template
"""

import sys
import os

from jinja2 import Template

TEMPLATES_DIR = "templates/"


def template(filename, template_file=None, template_args=None):
    """Function helping to generate final files from templates"""
    if template_file is None:
        template_file = filename + ".j2"

    template_file = TEMPLATES_DIR + template_file

    content = "# This file is autogenerated, do not edit; " \
              "changes may be undone by the next 'make gen-manifest'\n"
    with open(template_file) as tmp_file:
        content += tmp_file.read()

    Template(content).stream(
        **template_args).dump(filename)


if __name__ == "__main__":
    DOCKER_USER = os.environ.get("DOCKER_USER", "kadalu")
    KADALU_VERSION = os.environ.get("KADALU_VERSION", "latest")
    OPENSHIFT = bool(int(os.environ.get("OPENSHIFT", 0)))
    TEMPLATE_ARGS = {
        "namespace": "kadalu",
        "kadalu_version": KADALU_VERSION,
        "docker_user": DOCKER_USER,
        "openshift": OPENSHIFT
    }

    template(sys.argv[1], template_file="operator.yaml.j2", template_args=TEMPLATE_ARGS)
