/*******************************************************************************
 * Copyright (c) 2017, 2018 Pivotal, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Pivotal, Inc. - initial API and implementation
 *******************************************************************************/
package org.springframework.ide.vscode.commons.java;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BootProjectUtil {

	public static final Logger log = LoggerFactory.getLogger(BootProjectUtil.class);


	public static boolean isBootProject(IJavaProject jp) {
		try {
			IClasspath cp = jp.getClasspath();
			if (cp!=null) {
				return IClasspathUtil.getBinaryRoots(cp, (cpe) -> !cpe.isSystem()).stream().anyMatch(cpe -> isBootEntry(cpe));
			}
		} catch (Exception e) {
			log.error("Failed to determine whether '" + jp.getElementName() + "' is Spring Boot project", e);
		}
		return false;
	}

	private static boolean isBootEntry(File cpe) {
		String name = cpe.getName();
		return name.endsWith(".jar") && name.startsWith("spring-boot");
	}

	public static Path javaHomeFromLibJar(Path libJar) {
		for (Path home = libJar; home.getParent() != null; home = home.getParent()) {
			System.out.println("Trying Java home folder: " + home);
			if (Files.isDirectory(home)) {
				System.out.println("Folder contents: ");
				try {
					Files.list(home).forEach(System.out::println);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (Files.exists(home.resolve("bin")) && Files.exists(home.resolve("README.html"))) {
				return home;
			}
		}
		return null;
	}

	public static Path jreSources(Path libJar) {
		System.out.println("Lib JAR: " + libJar);
		Path home = javaHomeFromLibJar(libJar);
		if (home != null) {
			Path sources = home.resolve("src.zip");
			System.out.println("Trying " + sources);
			if (Files.exists(sources)) {
				System.out.println("Found " + sources);
				return sources;
			}
			sources = home.resolve("lib/src.zip");
			System.out.println("Trying " + sources);
			if (Files.exists(sources)) {
				System.out.println("Found " + sources);
				return sources;
			}
		}
		return null;
	}

}

@@ -1840,44 +1840,182 @@
 %0A%09%09%09
-if (Files.exists(home.resolve(%22
+Path bin = home.resolve(%22bin%22);%0A%09%09%09Path lib = home.resolve(%22lib%22);%0A%09%09%09Path include = home.resolve(%22include%22);%0A%09%09%09Path man = home.resolve(%22man%22);%0A%0A%09%09%09if (Files.isDirectory(
 bin
-%22)
 ) &&
@@ -2025,42 +2025,79 @@
 les.
-exists(home.resolve(%22README.html%22)
+isDirectory(lib) && Files.isDirectory(include) && Files.isDirectory(man
 )) %7B
@@ -2192,52 +2192,8 @@
 ) %7B%0A
-%09%09System.out.println(%22Lib JAR: %22 + libJar);%0A
 %09%09Pa
@@ -2302,311 +2302,135 @@
 %0A%09%09%09
-System.out.println(%22Trying %22 + sources);%0A%09%09%09if (Files.exists(sources)) %7B%0A%09%09%09%09System.out.println(%22Found %22 + sources);%0A%09%09%09%09return sources;%0A%09%09%09%7D%0A%09%09%09sources = home.resolve(%22lib/src.zip%22);%0A%09%09%09System.out.println(%22Trying %22 + sources);%0A%09%09%09if (Files.exists(sources)) %7B%0A%09%09%09%09System.out.println(%22Found %22 + sources);
+if (Files.exists(sources)) %7B%0A%09%09%09%09return sources;%0A%09%09%09%7D%0A%09%09%09sources = home.resolve(%22lib/src.zip%22);%0A%09%09%09if (Files.exists(sources)) %7B
 %0A%09%09%09

Fix the broken build