/*
 * Copyright 1&1 Internet AG, https://github.com/1and1/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.oneandone.stool.cli.command;

import net.oneandone.inline.Console;
import net.oneandone.stool.cli.Context;
import net.oneandone.stool.cli.Globals;
import net.oneandone.stool.Main;
import net.oneandone.stool.core.LocalSettings;
import net.oneandone.stool.core.Settings;
import net.oneandone.sushi.fs.ExistsException;
import net.oneandone.sushi.fs.FileNotFoundException;
import net.oneandone.sushi.fs.World;
import net.oneandone.sushi.fs.file.FileNode;

import java.io.IOException;

public class Setup {
    public static FileNode cisotools(World world) {
        String path;

        path = System.getenv("CISOTOOLS_HOME");
        return path == null ? null : world.file(path);
    }

    private final World world;
    private final FileNode home;
    private final String toolkit;
    private final String lib;
    private final String registryCredentials;
    private final Console console;
    private final String version;
    private final String spec;

    public Setup(Globals globals, String toolkit, String lib, String registryCredentials, String spec) {
        this.world = globals.getWorld();
        this.home = globals.home();
        this.toolkit = toolkit;
        this.lib = lib;
        this.registryCredentials = registryCredentials;
        this.console = globals.getConsole();
        this.version = Main.versionString(world);
        this.spec = spec;
    }

    public void run() throws IOException {
        Settings settings;

        if (home.exists()) {
            throw new IOException("Stool is already set up in " + home.getAbsolute());
        }
        settings = settings();
        home.mkdir();
        create("lib", lib);
        if (registryCredentials != null) {
            settings.local.registryCredentials.putAll(LocalSettings.parseRegistryCredentials(registryCredentials));
        }
        settings.save(Settings.settingsYaml(home));
        console.info.println("Done - created " + home.getAbsolute() + " for Stool version " + version);
        console.info.println("Available contexts:");
        for (Context c : settings.proxies.values()) {
            console.info.println("  " + c.name + " " + c.url);
        }
        console.info.println();
        console.info.println("Use 'sc context <name>' to choose a current context.");
        console.info.println();
        console.info.println();
        console.info.println("If you want some rough shell completion: ");
        console.info.println("  Add 'eval \"$(sc shell-inc)\"' in your shell profile.");
        console.info.println("  Don't forget to restart your terminal.");
    }

    private void create(String name, String linkTo) throws IOException {
        FileNode dir;

        dir = home.join(name);
        if (linkTo != null) {
            world.file(home, linkTo).checkDirectory().link(dir);
        } else {
            dir.mkdir();
        }
    }

    private Settings settings() throws IOException {
        int idx;
        Settings result;
        String name;
        String url;

        result = initialSettings();
        if (toolkit != null) {
            result.local.toolkit = toolkit;
        }
        if (spec != null) {
            idx = spec.indexOf('=');
            if (idx == -1) {
                throw new IllegalStateException("missing '=': " + spec);
            }
            name = spec.substring(0, idx);
            url = spec.substring(idx + 1);
            result.proxies.clear();
            result.addContext(name, url, null);
        }
        return result;
    }

    private Settings initialSettings() throws IOException {
        FileNode template;

        template = cisotoolsEnvironment(world);
        return template == null ? Settings.create(world) : Settings.load(home, template);
    }

    public static FileNode cisotoolsEnvironment(World world) throws FileNotFoundException, ExistsException {
        FileNode cisotools;

        cisotools = cisotools(world);
        return cisotools == null ? null : cisotools.join("stool/environment.yaml").checkFile();
    }
}

@@ -899,108 +899,8 @@
 gs;%0A
-import net.oneandone.sushi.fs.ExistsException;%0Aimport net.oneandone.sushi.fs.FileNotFoundException;%0A
 impo
@@ -1032,192 +1032,8 @@
 p %7B%0A
-    public static FileNode cisotools(World world) %7B%0A        String path;%0A%0A        path = System.getenv(%22CISOTOOLS_HOME%22);%0A        return path == null ? null : world.file(path);%0A    %7D%0A%0A
     
@@ -2007,177 +2007,8 @@
 b);%0A
-        if (registryCredentials != null) %7B%0A            settings.local.registryCredentials.putAll(LocalSettings.parseRegistryCredentials(registryCredentials));%0A        %7D%0A
     
@@ -3297,32 +3297,199 @@
 lkit;%0A        %7D%0A
+        if (registryCredentials != null) %7B%0A            result.local.registryCredentials.putAll(LocalSettings.parseRegistryCredentials(registryCredentials));%0A        %7D%0A
         if (spec
@@ -3935,25 +3935,19 @@
     
-FileNode template
+String path
 ;%0A%0A 
@@ -3957,45 +3957,48 @@
     
-template = cisotoolsEnvironment(world
+path = System.getenv(%22SC_SETUP_SETTINGS%22
 );%0A 
@@ -4011,24 +4011,20 @@
  return 
-template
+path
  == null
@@ -4075,283 +4075,23 @@
 me, 
-template);%0A    %7D%0A%0A    public static FileNode cisotoolsEnvironment(World world) throws FileNotFoundException, ExistsException %7B%0A        FileNode cisotools;%0A%0A        cisotools = cisotools(world);%0A        return cisotools == null ? null : cisotools.join(%22stool/environment.yaml%22
+world.file(path
 ).ch
@@ -4095,22 +4095,23 @@
 .checkFile()
+)
 ;%0A    %7D%0A%7D%0A

fix cisotools dependency