#!/usr/bin/env python3

import argparse
import json
import os
from datetime import datetime
from jinja2 import Environment, FileSystemLoader
from typing import NamedTuple
from urllib.error import URLError
from urllib.request import urlopen, Request

JDK_URLS = {
    (12, 0, 1): 'https://download.java.net/java/GA/jdk12.0.1/69cfe15208a647278a19ef0990eea691/12/GPL/openjdk-12.0.1_linux-x64_bin.tar.gz',
    (11, 0, 1): 'https://download.java.net/java/GA/jdk11/13/GPL/openjdk-11.0.1_linux-x64_bin.tar.gz'
}


class Version(NamedTuple):
    major: int
    minor: int
    hotfix: int

    @classmethod
    def parse(cls, s: str):
        return Version(*map(int, s.split('.', maxsplit=2)))

    def __str__(self) -> str:
        return f'{self.major}.{self.minor}.{self.hotfix}'


def latest_crash():
    with urlopen('https://crate.io/versions.json') as r:
        d = json.load(r)
        return Version.parse(d['crash'])


def jdk_url_and_sha(jdk_version):
    if jdk_version not in JDK_URLS:
        raise ValueError(f'No URL for JDK version {jdk_version} found.')
    url = JDK_URLS[jdk_version]
    with urlopen(url + '.sha256') as r:
        sha256 = r.read().decode('utf-8')
    return url, sha256


def url_exists(url: str):
    try:
        with urlopen(Request(url, method='HEAD')):
            return True
    except URLError:
        return False


def ensure_existing_crash(crash_version: Version):
    if not crash_version:
        return latest_crash()
    url = f'https://cdn.crate.io/downloads/releases/crash_standalone_{crash_version}'
    if url_exists(url):
        return crash_version
    else:
        raise ValueError(f'No release found for crash {crash_version}')


def ensure_existing_cratedb(cratedb_version: Version):
    url = f'https://cdn.crate.io/downloads/releases/crate-{cratedb_version}.tar.gz'
    if url_exists(url):
        return cratedb_version
    else:
        raise ValueError(f'No release found for CrateDB {cratedb_version}')


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--cratedb-version', type=Version.parse, required=True)
    parser.add_argument('--crash-version', type=Version.parse)
    parser.add_argument('--jdk-version', type=Version.parse)
    parser.add_argument('--template', type=str, default='Dockerfile.j2')
    args = parser.parse_args()

    cratedb_version = ensure_existing_cratedb(args.cratedb_version)
    jdk_version_default = Version(12, 0, 1) if cratedb_version.major >= 4 else Version(11, 0, 1)
    jdk_version = args.jdk_version or jdk_version_default
    jdk_url, jdk_sha256 = jdk_url_and_sha(jdk_version)

    env = Environment(loader=FileSystemLoader(os.path.dirname(__file__)))
    template = env.get_template(args.template)
    print(template.render(
        CRATE_VERSION=ensure_existing_cratedb(args.cratedb_version),
        CRASH_VERSION=ensure_existing_crash(args.crash_version),
        JDK_VERSION=str(jdk_version),
        JDK_URL=jdk_url,
        JDK_SHA256=jdk_sha256,
        BUILD_TIMESTAMP=datetime.utcnow().isoformat()
    ))


if __name__ == "__main__":
    main()

@@ -788,18 +788,29 @@
 _crash()
+ -%3E Version
 :%0A
-
     with
@@ -1228,24 +1228,32 @@
 ts(url: str)
+ -%3E bool
 :%0A    try:%0A 
@@ -1415,24 +1415,35 @@
 on: Version)
+ -%3E Version
 :%0A    if not
@@ -1760,24 +1760,35 @@
 on: Version)
+ -%3E Version
 :%0A    url = 
@@ -2000,24 +2000,265 @@
 ersion%7D')%0A%0A%0A
+def find_template_for_version(cratedb_version: Version) -%3E str:%0A    v = cratedb_version%0A    versioned_template = f'Dockerfile_%7Bv.major%7D.%7Bv.minor%7D.j2'%0A    return versioned_template if os.path.exists(versioned_template) else 'Dockerfile.j2'%0A%0A%0A
 def main():%0A
@@ -2550,33 +2550,8 @@
 =str
-, default='Dockerfile.j2'
 )%0A  
@@ -2857,16 +2857,91 @@
 version)
+%0A    template = args.template or find_template_for_version(cratedb_version)
 %0A%0A    en
@@ -3008,16 +3008,16 @@
 le__)))%0A
+
     temp
@@ -3040,21 +3040,16 @@
 emplate(
-args.
 template

Add version based template selection logic to update.py