#! /usr/bin/env python3

# ICON
#
# ------------------------------------------
# Copyright (C) 2004-2026, DWD, MPI-M, DKRZ, KIT, ETH, MeteoSwiss
# Contact information: icon-model.org
# See AUTHORS.TXT for a list of authors
# See LICENSES/ for license information
# SPDX-License-Identifier: BSD-3-Clause
# ------------------------------------------

import pickle
import sys
from pathlib import Path

import click

# -*- coding: utf-8 -*-
# ==============================================================================
# create an experiment list
# ==============================================================================
from buildbot_config import BuildbotConfig
from icon_paths import buildbot_list_path
from util import WhitespaceSeperatedDict, WhitespaceSeperatedList


@click.command()
@click.argument("builder_name", type=str)
@click.option("--build_script", type=str, help="the configure wrapper file")
@click.option(
    "--configureflags",
    type=WhitespaceSeperatedDict(),
    help="the configure flags for the builder",
)
@click.option(
    "--builderflags",
    "--builderflag",
    "builderflag",
    type=click.Choice(["Active", "Inactive", "build_only"]),
    help="the builder flag (Active, Inactive, build_only)",
    required=True,
)
@click.option(
    "--machine",
    type=str,
    help="the machine name to add the builder",
    required=True,
)
@click.option("--list", "list_name", type=str, help="the list", required=True)
def addbuilder(
    builder_name, build_script, configureflags, builderflag, machine, list_name
):

    full_list_name = buildbot_list_path / list_name

    if Path(full_list_name).exists():
        thisList = BuildbotConfig.from_pickle(full_list_name)
    else:
        print("did not find experiment list {}".format(full_list_name))
        sys.exit(1)

    thisList.add_builders(
        builder_name,
        machine,
        build_script,
        config=configureflags,
        flag=builderflag,
    )

    thisList.to_pickle(full_list_name)


if __name__ == "__main__":
    addbuilder()
