#!/usr/bin/env python
import os

import yaml
from jinja2 import Template


def main():
    print('# !!! Do not edit this file manually, see ci-generator folder !!!')
    print(get_rendered_ci_template())


def get_rendered_ci_template():
    context = {
        'versions': get_unity_versions() or {},
        'components': get_unity_components()
    }
    return render_template(get_ci_yaml_template(), context)


def get_ci_yaml_template():
    script_dirname = os.path.dirname(os.path.realpath(__file__))
    return os.path.join(script_dirname, "gitlab-ci.jinja2")


def get_unity_versions():
    script_dirname = os.path.dirname(os.path.realpath(__file__))
    unity_versions = os.path.join(script_dirname, "unity_versions.yml")
    with open(unity_versions, 'r') as f:
        return yaml.load(f.read())


def get_unity_components():
    return {
        'unity': 'Unity',
        'windows': 'Unity,Windows,Windows-Mono',
        'mac': 'Unity,Mac,Mac-Mono',
        'ios': 'Unity,iOS',
        'android': 'Unity,Android',
        'webgl': 'Unity,WebGL',
        'facebook': 'Unity,Facebook-Games'
    }


def render_template(yaml_template, context):
    with open(yaml_template, 'r') as f:
        template = Template(f.read())
    return template.render(context)


if __name__ == "__main__":
    main()
