// Path: src/main/java/crate/elasticsearch/export/OutputFile.java
// public class OutputFile extends Output {
// 
//     private Result result;
//     private final String path;
//     private OutputStream os;
//     private final boolean overwrite;
//     private final boolean compression;
// 
//     public OutputFile(String path, boolean overwrite, boolean compression) {
//         this.path = path;
//         this.overwrite = overwrite;
//         this.compression = compression;
//     }
// 
//     @Override
//     public void open() throws IOException {
//         File outFile = new File(path);
//         if (!overwrite && outFile.exists()){
//             throw new IOException("File exists: " +  path);
//         }
//         os = new FileOutputStream(outFile);
//         if (compression) {
//             os = new GZIPOutputStream(os);
//         }
//     }
// 
//     @Override
//     public void close() throws IOException {
//         result = new Result();
//         if (os != null) {
//             os.close();
//             result.exit = 0;
//         } else {
//             result.exit = 1;
//         }
//         os = null;
//     }
// 
//     @Override
//     public OutputStream getOutputStream() {
//         return os;
//     }
// 
//     @Override
//     public Result result() {
//         return result;
//     }
// }


import crate.elasticsearch.export.Output;
import crate.elasticsearch.export.OutputCommand;
import crate.elasticsearch.export.OutputFile;
import org.elasticsearch.cache.recycler.CacheRecycler;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.index.engine.Engine;
import org.elasticsearch.index.service.IndexService;
import org.elasticsearch.index.shard.service.IndexShard;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.search.SearchShardTarget;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.search.internal.ShardSearchRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
        for (String part : templateArray) {
            ret.add(applyVars(part));
        }
        return ret;
    }

    /**
     * Replaces variable placeholder with actual value
     *
     * @param template
     * @return
     */
    private String applyVars(String template) {
        template = template.replace(VAR_SHARD, String.valueOf(indexShard().shardId().getId()));
        template = template.replace(VAR_INDEX, indexShard().shardId().getIndex());
        template = template.replace(VAR_CLUSTER, clusterName());
        return template;
    }

    /**
     * Method to retrieve name of cluster
     *
     * @return name of cluster
     */
    private String clusterName() {
        return ClusterName.clusterNameFromSettings(this.indexShard().indexSettings()).value();
    }

    public Output createOutput() {
        if (outputFile()!=null){
// Path: src/main/java/crate/elasticsearch/export/OutputFile.java
// public class OutputFile extends Output {
// 
//     private Result result;
//     private final String path;
//     private OutputStream os;
//     private final boolean overwrite;
//     private final boolean compression;
// 
//     public OutputFile(String path, boolean overwrite, boolean compression) {
//         this.path = path;
//         this.overwrite = overwrite;
//         this.compression = compression;
//     }
// 
//     @Override
//     public void open() throws IOException {
//         File outFile = new File(path);
//         if (!overwrite && outFile.exists()){
//             throw new IOException("File exists: " +  path);
//         }
//         os = new FileOutputStream(outFile);
//         if (compression) {
//             os = new GZIPOutputStream(os);
//         }
//     }
// 
//     @Override
//     public void close() throws IOException {
//         result = new Result();
//         if (os != null) {
//             os.close();
//             result.exit = 0;
//         } else {
//             result.exit = 1;
//         }
//         os = null;
//     }
// 
//     @Override
//     public OutputStream getOutputStream() {
//         return os;
//     }
// 
//     @Override
//     public Result result() {
//         return result;
//     }
// }


// Path: src/main/java/crate/elasticsearch/action/export/ExportContext.java
import crate.elasticsearch.export.Output;
import crate.elasticsearch.export.OutputCommand;
import crate.elasticsearch.export.OutputFile;
import org.elasticsearch.cache.recycler.CacheRecycler;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.index.engine.Engine;
import org.elasticsearch.index.service.IndexService;
import org.elasticsearch.index.shard.service.IndexShard;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.search.SearchShardTarget;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.search.internal.ShardSearchRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

        for (String part : templateArray) {
            ret.add(applyVars(part));
        }
        return ret;
    }

    /**
     * Replaces variable placeholder with actual value
     *
     * @param template
     * @return
     */
    private String applyVars(String template) {
        template = template.replace(VAR_SHARD, String.valueOf(indexShard().shardId().getId()));
        template = template.replace(VAR_INDEX, indexShard().shardId().getIndex());
        template = template.replace(VAR_CLUSTER, clusterName());
        return template;
    }

    /**
     * Method to retrieve name of cluster
     *
     * @return name of cluster
     */
    private String clusterName() {
        return ClusterName.clusterNameFromSettings(this.indexShard().indexSettings()).value();
    }

    public Output createOutput() {
        if (outputFile()!=null){
            return new OutputFile(outputFile(), forceOverride(), compression);