import re

from versions.software.utils import get_command_stderr, get_soup, \
    get_text_between


def name():
    """Return the precise name for the software."""
    return 'Zulu OpenJDK'


def installed_version():
    """Return the installed version of the jdk, or None if not installed."""
    try:
        version_string = get_command_stderr(('java', '-version'))
        # "1.8.0_162" or "9.0.4.1" for example
        return get_text_between(version_string, '"', '"')
    except FileNotFoundError:
        pass


def latest_version():
    """Return the latest version of Zulu OpenJDK available for download."""
    installed = installed_version()
    soup = get_soup('http://www.azul.com/downloads/zulu/zulu-windows/')
    if soup:
        zip_filename = re.compile('\.zip$')
        for tag in soup.find_all('a', class_='r-download', href=zip_filename):
            filename = tag.attrs['href']
            zulu = get_text_between(filename, 'bin/zulu', '-')
            jdk = get_text_between(filename, 'jdk', '-')
            if (installed is None) or (installed[0] == '9' and zulu[0] == '9'):
                return zulu
            elif installed[0] == '1' and jdk[0] == installed[2]:
                version, update = jdk.rsplit('.', 1)
                return f'1.{version}_{update}'
    return 'Unknown'

@@ -255,19 +255,19 @@
  of the 
-jdk
+JDK
 , or Non
@@ -378,44 +378,70 @@
   # 
-%221.8.0_162%22 or %22
+Examples: 1.6.0-107, 1.7.0_181, 1.8.0_172, 
 9.0.
-4.1%22 for example
+7.1, 10, 10.0.1
 %0A   
@@ -540,16 +540,764 @@
  pass%0A%0A%0A
+def jdk_version():%0A    %22%22%22Return the installed JDK main version string: 1.6, 1.7, 1.8, 9, 10...%22%22%22%0A    installed = installed_version()%0A    if installed is None:%0A        return None%0A    elif re.search('%5E1%5C.%5B678%5D', installed):%0A        return installed%5B:3%5D%0A    return installed.split('.')%5B0%5D%0A%0A%0Adef url_version(url):%0A    %22%22%22Given a Zulu download URL, return the JDK version.%22%22%22%0A    jdk = get_text_between(url, 'jdk', '-')%0A    if jdk%5B0%5D == '6':%0A        version, update = jdk.rsplit('.', 1)%0A        return f'1.%7Bversion%7D-%7Bupdate%7D'%0A    elif jdk%5B0%5D in ('7', '8'):%0A        version, update = jdk.rsplit('.', 1)%0A        return f'1.%7Bversion%7D_%7Bupdate%7D'%0A    elif jdk%5B0%5D == '9':%0A        return get_text_between(url, 'bin/zulu', '-')%0A    else:%0A        return jdk%0A%0A%0A
 def late
@@ -1390,44 +1390,8 @@
 %22%22%22%0A
-    installed = installed_version()%0A
     
@@ -1479,27 +1479,22 @@
         
-zip_
 file
-nam
+_r
 e = re.c
@@ -1501,16 +1501,19 @@
 ompile('
+x64
 %5C.zip$')
@@ -1525,18 +1525,17 @@
     
-for tag in
+matches =
  sou
@@ -1580,27 +1580,17 @@
 ref=
-zip_filename):%0A    
+file_re)%0A
     
@@ -1601,15 +1601,13 @@
 file
-name
+s
  = 
+%5B
 tag.
@@ -1623,355 +1623,327 @@
 ef'%5D
-%0A            zulu = get_text_between(filename, 'bin/zulu', '-')%0A            jdk = get_text_between(filename, 'jdk', '-')%0A            if (installed is None) or (installed%5B0%5D == '9' and zulu%5B0%5D == '9'):%0A                return zulu%0A            elif installed%5B0%5D == '1' and jdk%5B0%5D == installed%5B2%5D:%0A                version, update = jdk.rsplit('.', 1)%0A
+ for tag in matches%5D%0A        if files:%0A            version_names = %5Burl_version(url) for url in files%5D%0A            version = jdk_version()%0A            if version is None:%0A                return version_names%5B0%5D%0A            else:%0A                for v in version_names:%0A                    if v.startswith(version):%0A    
     
@@ -1958,38 +1958,20 @@
     
+    
 return 
-f'1.%7Bversion%7D_%7Bupdate%7D'
+v
 %0A   

Support all available downloads of Zulu OpenJDK.