#!/usr/bin/env python
# automatically generated by auxilium
import collections
import os
import sys

from _aux_ci import JobManager


def main():
    jman = JobManager()
    jman.load_env(
        slug="{{ aux.project.slug }}",
        verbose=True,
    )
    if len(sys.argv) > 1 and ":" not in sys.argv[1]:
        if sys.argv[1] == "push":
            sys.argv[1] = "push-no-mr"
        for reason in sys.argv[1].split(","):
            jman.env[reason] = True
        sys.argv[1:] = sys.argv[2:]


    def env(key: str) -> str:
        return os.environ.get(key, "")

    {%- for name, form in ci_rules %}
    jman.env.setdefault("{{ name }}", {{ form }})
    {%- endfor %}

    {% for job, opts in aux.ci.jobs.items() %}
    {%- if opts.build -%}
    jman.add_docker_build_job(
        "{{ job }}",
        {%-if opts["build-stage"] %}
        stage="{{ opts['build-stage'] }}",
        {%- endif -%}
        {%-if opts["build-deps"] %}
        parents=[
            {%- for dep in opts["build-deps"] %}
            jman.name2build("{{ dep }}"),
            {%- endfor%}
            {%- for dep in opts["deps"] %}
            jman.name2run("{{ dep }}"),
            {%- endfor%}
        ],
        {%- endif %}
        {%-if opts["images"] %}
        images=[
            {%- for image in opts["images"] %}
            "{{ image }}",
            {%- endfor%}
        ],
        {%- endif %}
        {%-if "branch_match" in opts and opts["branch_match"] %}
        branch_match = {{ opts.branch_match }},
        {%- endif %}
        {%-if opts.files %}
        files={{ opts.files | list | replace("'", '"')}},
        {%- endif %}
        trigger=collections.OrderedDict(
            [("env_bool", {{  (opts["build-rules"] or opts.rules) | list | replace("'", '"')}})],
        ),
    )
    {%-if opts.get("always-build") %}
    jman.name2build("{{ job }}").dependent["up_to_date"] = False
    {%- endif %}
    {% endif -%}
    {%- endfor -%}

    {% for job, opts in aux.ci.jobs.items() -%}
    {%- if opts.run -%}
    jman.add_{{opts.function}}_run_job(
        "{{ job }}",
        {%-if opts["run-stage"] %}
        stage="{{ opts["run-stage"] }}",
        {%- endif %}
        parents=[
            {%- for dep in opts["run-deps"] %}
            jman.name2build("{{ dep }}"),
            {%- endfor %}
            {%- for dep in opts["deps"] %}
            jman.name2run("{{ dep }}"),
            {%- endfor%}
        ],
        trigger=collections.OrderedDict(
            [("env_bool", {{ (opts["run-rules"] or opts.rules) | list | replace("'", '"')}})],
        ),
    )
    {% endif -%}
    {%- endfor -%}

    if len(sys.argv) > 1:
        assert len(sys.argv) == 2
        ident = sys.argv[1]
        if ident == "echo_env":
            for key, val in jman.env.items():
                print(f"{key:>30} {val}")
            return
        if ident == "dry":
            jman.prune_unincluded()
            return
        else:
            jman.run_single(ident)
    else:
        jman.run_pipeline()
        jman.show_result()

    if jman.has_failed_jobs():
        sys.exit(1)


if __name__ == "__main__":
    main()
