package io.quarkus.container.image.s2i.deployment;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.BooleanSupplier;
import java.util.function.Function;

import org.jboss.logging.Logger;

import io.quarkus.container.image.deployment.ContainerImageConfig;
import io.quarkus.deployment.util.ExecUtil;

public class S2iBuild implements BooleanSupplier {

    private static final Logger LOGGER = Logger.getLogger(S2iBuild.class.getName());

    private ContainerImageConfig containerImageConfig;

    S2iBuild(ContainerImageConfig containerImageConfig) {
        this.containerImageConfig = containerImageConfig;
    }

    @Override
    public boolean getAsBoolean() {
        OutputFilter filter = new OutputFilter();
        try {
            if (ExecUtil.exec(new File("."), filter, "oc", "version")) {
                Optional<String> version = getServerVersionFromOc(filter.getLines());
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }

    private static Optional<String> getServerVersionFromOc(List<String> lines) {
        return lines.stream()
                .filter(l -> l.startsWith("kubernetes"))
                .map(l -> l.split(" "))
                .filter(a -> a.length > 2)
                .map(a -> a[1])
                .findFirst();
    }

    private static class OutputFilter implements Function<InputStream, Runnable> {
        private final List<String> list = new ArrayList();

        @Override
        public Runnable apply(InputStream is) {
            return () -> {
                try (InputStreamReader isr = new InputStreamReader(is);
                        BufferedReader reader = new BufferedReader(isr)) {

                    for (String line = reader.readLine(); line != null; line = reader.readLine()) {
                        list.add(line);
                    }
                } catch (IOException e) {
                    throw new RuntimeException("Error reading stream.", e);
                }
            };
        }

        public List<String> getLines() {
            return list;
        }
    }
}

@@ -61,325 +61,35 @@
 ava.
-io.BufferedReader;%0Aimport java.io.File;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.Optional;%0Aimport java.util.function.BooleanSupplier;%0Aimport java.util.function.Function;%0A%0Aimport org.jboss.logging.Logg
+util.function.BooleanSuppli
 er;%0A
@@ -159,52 +159,8 @@
 fig;
-%0Aimport io.quarkus.deployment.util.ExecUtil;
 %0A%0Apu
@@ -213,94 +213,8 @@
  %7B%0A%0A
-    private static final Logger LOGGER = Logger.getLogger(S2iBuild.class.getName());%0A%0A
     
@@ -450,1466 +450,20 @@
     
-OutputFilter filter = new OutputFilter();%0A        try %7B%0A            if (ExecUtil.exec(new File(%22.%22), filter, %22oc%22, %22version%22)) %7B%0A                Optional%3CString%3E version = getServerVersionFromOc(filter.getLines());%0A                return true;%0A            %7D%0A        %7D catch (Exception e) %7B%0A            return false;%0A        %7D%0A        return false;%0A    %7D%0A%0A    private static Optional%3CString%3E getServerVersionFromOc(List%3CString%3E lines) %7B%0A        return lines.stream()%0A                .filter(l -%3E l.startsWith(%22kubernetes%22))%0A                .map(l -%3E l.split(%22 %22))%0A                .filter(a -%3E a.length %3E 2)%0A                .map(a -%3E a%5B1%5D)%0A                .findFirst();%0A    %7D%0A%0A    private static class OutputFilter implements Function%3CInputStream, Runnable%3E %7B%0A        private final List%3CString%3E list = new ArrayList();%0A%0A        @Override%0A        public Runnable apply(InputStream is) %7B%0A            return () -%3E %7B%0A                try (InputStreamReader isr = new InputStreamReader(is);%0A                        BufferedReader reader = new BufferedReader(isr)) %7B%0A%0A                    for (String line = reader.readLine(); line != null; line = reader.readLine()) %7B%0A                        list.add(line);%0A                    %7D%0A                %7D catch (IOException e) %7B%0A                    throw new RuntimeException(%22Error reading stream.%22, e);%0A                %7D%0A            %7D;%0A        %7D%0A%0A        public List%3CString%3E getLines() %7B%0A            return list;%0A        %7D
+return true;
 %0A   

remove call to oc version