package lombok.website;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FetchCurrentVersion {
	private FetchCurrentVersion() {}
	
	private static final Pattern VERSION_PATTERN = Pattern.compile("^.*<\\s*span\\s+id\\s*=\\s*[\"'](currentVersion|currentVersionFull)[\"'](?:\\s+style\\s*=\\s*[\"']display\\s*:\\s*none;?[\"'])?\\s*>\\s*([^\t<]+)\\s*<\\s*/\\s*span\\s*>.*$");
	
	public static void main(String[] args) throws IOException {
		System.out.print(fetchVersionFromSite(args.length == 0 || args[0].equals("full")));
	}
	
	public static String fetchVersionFromSite(boolean fetchFull) throws IOException {
		InputStream in = new URL("https://projectlombok.org/download").openStream();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
			for (String line = br.readLine(); line != null; line = br.readLine()) {
				Matcher m = VERSION_PATTERN.matcher(line);
				if (m.matches() && m.group(1).equals("currentVersionFull") == fetchFull) return m.group(2);
			}
			throw new IOException("Expected a span with id 'currentVersion'");
		} finally {
			in.close();
		}
	}
}

@@ -1155,16 +1155,40 @@
 group(2)
+.replace(%22&quot;%22, %22%5C%22%22)
 ;%0A%09%09%09%7D%0A%09

build script fix for website-only: It was adding an &amp; to the full version string every call.