#!/usr/bin/env python3

import fileinput
import os

import ci.util

COMPONENT_SPEC_PYPI_PROJECT_NAME = 'gardener-component-model'
COMPONENT_SPEC_COMPONENT_NAME = 'github.com/gardener/component-spec'


def update_project_version(project_name: str, project_version):
    path_to_requirements_txt = os.path.join(
            os.path.abspath(ci.util.check_env('MAIN_REPO_DIR')),
            'requirements.txt',
        )
    for line in fileinput.input(path_to_requirements_txt, inplace=True):
        if line.startswith(project_name):
            print(f'{project_name}=={project_version}')
        else:
            print(line.rstrip())


ci.util.check_env('MAIN_REPO_DIR')

component_name = ci.util.check_env('DEPENDENCY_NAME')
component_version = ci.util.check_env('DEPENDENCY_VERSION')

if component_name == COMPONENT_SPEC_COMPONENT_NAME:
    update_project_version(COMPONENT_SPEC_PYPI_PROJECT_NAME, component_version)
else:
    raise NotImplementedError(f"Don't know how to upgrade '{component_name}'")
