import argparse
from pathlib import Path
from jinja2 import Template


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='Write the latest.json file')
    requiredNamed = parser.add_argument_group('Required named arguments')
    requiredNamed.add_argument("--version", required=True, help="The full semantic version with build id as the 4th item")
    args = parser.parse_args()

    v = args.version.split('.')
    url_stem = "https://bigrep.com/wp-content/uploads/Blade"
    file = Path(__file__).parent / "templates/latest.json.jinja"
    file_out = Path(__file__).parent.parent / "latest.json"

    templated = Template(file.read_text())
    rendered = templated.render(
        major=v[0],
        minor=v[1],
        patch=v[2],
        id=v[3],
        alpha_url=f"{url_stem}/alpha/blade-installer-alpha-{v[3]}.exe",
        fleet_url=f"{url_stem}/fleet/blade-installer-fleet-{v[3]}.exe",
        internal_url=f"{url_stem}/internal/blade-installer-internal-{v[3]}.exe",
    )
    file_out.write_text(rendered)
