package com.kylinolap.job.deployment;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import com.kylinolap.job.tools.LZOSupportnessChecker;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.util.CompressionTest;

/**
 * Created by honma on 9/30/14.
 * <p/>
 * This class is assumed to be run by
 * "hbase org.apache.hadoop.util.RunJar kylin-job-0.5.7-SNAPSHOT-job.jar com.kylinolap.job.deployment.HadoopConfigPrinter "
 * in the shell, so that hbase and hadoop related environment variables will be
 * visible to this class.
 */
public class HbaseConfigPrinter {
    public static void main(String[] args) throws IOException {
        if (args.length != 1) {
            System.out.println("Usage: hbase org.apache.hadoop.util.RunJar kylin-job-0.5.7-SNAPSHOT-job.jar com.kylinolap.job.deployment.HadoopConfigPrinter targetFile");
            System.exit(1);
        }

        printConfigs(args[0]);
    }

    private static void printConfigs(String targetFile) throws IOException {

        File output = new File(targetFile);
        if (output.exists() && output.isDirectory()) {
            throw new IllegalStateException("The output file: " + targetFile + " is a directory");
        }

        StringBuilder sb = new StringBuilder();

        sb.append("export KYLIN_LZO_SUPPORTED=" + ConfigLoader.LZO_INFO_LOADER.loadValue() + "\r\n");
        sb.append("export KYLIN_LD_LIBRARY_PATH=" + ConfigLoader.LD_LIBRARY_PATH_LOADER.loadValue() + "\r\n");
        sb.append("export KYLIN_HBASE_CLASSPATH=" + ConfigLoader.HBASE_CLASSPATH_LOADER.loadValue() + "\r\n");
        sb.append("export KYLIN_HBASE_CONF_PATH=" + ConfigLoader.HBASE_CONF_FOLDER_LOADER.loadValue() + "\r\n");
        sb.append("export KYLIN_ZOOKEEPER_QUORUM=" + ConfigLoader.ZOOKEEP_QUORUM_LOADER.loadValue() + "\r\n");
        sb.append("export KYLIN_ZOOKEEPER_CLIENT_PORT=" + ConfigLoader.ZOOKEEPER_CLIENT_PORT_LOADER.loadValue() + "\r\n");
        sb.append("export KYLIN_ZOOKEEPER_ZNODE_PARENT=" + ConfigLoader.ZOOKEEPER_ZNODE_PARENT_LOADER.loadValue() + "\r\n");

        FileUtils.writeStringToFile(output, sb.toString());
    }

    @SuppressWarnings("unused")
    private static void printAllEnv() {
        for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
            System.out.println("Key: " + entry.getKey());
            System.out.println("Value: " + entry.getValue());
            System.out.println();
        }
    }

    enum ConfigLoader {

        LZO_INFO_LOADER {
            @Override
            public String loadValue() {
                return LZOSupportnessChecker.getSupportness() ? "true" : "false";
            }
        },

        ZOOKEEP_QUORUM_LOADER {
            @Override
            public String loadValue() {
                Configuration conf = HBaseConfiguration.create();
                return conf.get(HConstants.ZOOKEEPER_QUORUM);
            }
        },

        ZOOKEEPER_ZNODE_PARENT_LOADER {
            @Override
            public String loadValue() {
                Configuration conf = HBaseConfiguration.create();
                return conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT);
            }
        },

        ZOOKEEPER_CLIENT_PORT_LOADER {
            @Override
            public String loadValue() {
                Configuration conf = HBaseConfiguration.create();
                return conf.get(HConstants.ZOOKEEPER_CLIENT_PORT);

            }
        },

        LD_LIBRARY_PATH_LOADER {
            @Override
            public String loadValue() {
                return System.getenv("LD_LIBRARY_PATH");
            }
        },

        HBASE_CLASSPATH_LOADER {
            @Override
            public String loadValue() {
                return System.getenv("CLASSPATH");
            }
        },

        HBASE_CONF_FOLDER_LOADER {
            @Override
            public String loadValue() {
                String output = HBASE_CLASSPATH_LOADER.loadValue();
                String[] paths = output.split(":");
                StringBuilder sb = new StringBuilder();

                for (String path : paths) {
                    path = path.trim();
                    File f = new File(path);
                    if (StringUtils.containsIgnoreCase(path, "conf") && f.exists() && f.isDirectory() && f.getName().equalsIgnoreCase("conf")) {
                        sb.append(":" + path);
                    }
                }
                return sb.toString();
            }
        };

        public abstract String loadValue();
    }

}

@@ -1591,34 +1591,32 @@
 loadValue() + %22%5C
-r%5C
 n%22);%0A        sb.
@@ -1700,34 +1700,32 @@
 loadValue() + %22%5C
-r%5C
 n%22);%0A        sb.
@@ -1809,34 +1809,32 @@
 loadValue() + %22%5C
-r%5C
 n%22);%0A        sb.
@@ -1920,34 +1920,32 @@
 loadValue() + %22%5C
-r%5C
 n%22);%0A        sb.
@@ -2029,34 +2029,32 @@
 loadValue() + %22%5C
-r%5C
 n%22);%0A        sb.
@@ -2154,26 +2154,24 @@
 Value() + %22%5C
-r%5C
 n%22);%0A       
@@ -2285,10 +2285,8 @@
 + %22%5C
-r%5C
 n%22);

refine newline char