#!/usr/bin/env python
import os
import sys
from urllib.request import urlopen

import semver

default_branch = os.environ["CI_DEFAULT_BRANCH"]
project_path = os.environ["CI_PROJECT_PATH"]
version_url = f"https://gitlab.com/{project_path}/-/raw/{default_branch}/VERSION"


def main():
    with urlopen(version_url) as handle:
        main_ver = handle.read().decode("utf-8").strip()
    with open("VERSION") as handle:
        branch_ver = handle.read().strip()

    print(f"main: {main_ver}, branch {branch_ver}")
    if semver.compare(branch_ver, main_ver) != 1:
        print(
            f"Branch version has not been incremented!\nmain: {main_ver}, branch {branch_ver}"
        )
        sys.exit(1)


if __name__ == "__main__":
    # execute only if run as a script
    main()
