package de.skeletoneye.bungee.scale;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.commons.io.FileUtils;

import lombok.Getter;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;

@Getter
public class BungeeScale extends Plugin
{
    private static @Getter BungeeScale instance;

    private Configuration networkConfig;
    private Path runtimeDir;

    // TODO To be moved somewhere else
    public Configuration checkConfig(Path path) throws IOException
    {
        if (!Files.exists(path)) {
            this.getLogger().info("Unable to find " + path.getFileName().toString() + ", creating it.");

            try (InputStream stream = this.getResourceAsStream("templates/" + path.getFileName().toString())) {
                Files.copy(stream, path);
            }
        }

        return ConfigurationProvider.getProvider(YamlConfiguration.class).load(path.toFile());
    }

    @Override
    public void onEnable()
    {
        try {
            // Check whether servers directory exists
            final Path serversDir = Paths.get("servers");
            Files.createDirectories(serversDir);

            // Check whether configuration file exists and read it
            this.networkConfig = this.checkConfig(serversDir.resolve("network.yml"));

            // Check whether other required directories exist
            final Path staticDir = serversDir.resolve("static");
            Files.createDirectories(staticDir);

            final Path imagesDir = serversDir.resolve("images");
            Files.createDirectories(imagesDir);

            final Path includesDir = serversDir.resolve("includes");
            Files.createDirectories(includesDir);

            // Make sure runtime/ is existing and empty
            this.runtimeDir = serversDir.resolve("runtime");

            if (Files.exists(this.runtimeDir)) {
                this.getLogger().info("Clearing runtime directory");
                FileUtils.deleteDirectory(this.runtimeDir.toFile());
            }

            Files.createDirectory(this.runtimeDir);

            // Singleton
            BungeeScale.instance = this;
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}

@@ -1908,32 +1908,340 @@
 (includesDir);%0A%0A
+            // Check whether driver.jar exists%0A            final Path driverJar = imagesDir.resolve(%22driver.jar%22);%0A%0A            if (!Files.exists(driverJar)) %7B%0A                this.getLogger().severe(%22Unable to find %22 + driverJar.toString() + %22, not going to start.%22);%0A                return;%0A            %7D%0A%0A
             // M

Add does-file-exist-check for driver.jar