package de.tblsoft.solr.util;

import com.google.common.io.Files;

import de.tblsoft.solr.http.HTTPHelper;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * Created by tblsoft
 */
public class IOUtils {

    public static String getAbsoluteFile(String directory, String fileName) {
        if(fileName.toLowerCase().startsWith("http")) {
            return fileName;
        } else if(fileName.toLowerCase().startsWith("c:\\")) {
    		return fileName;
    	} else if(fileName.startsWith("/")) {
            return fileName;
    	} else if(fileName.startsWith("./")) {
            return fileName;
        } else {
            return directory + "/" + fileName;
        }
    }

    public static File getAbsoluteFileAsFile(String directory, String fileName) {
        return new File(getAbsoluteFile(directory, fileName));
    }

    public static String getDirectoryForFile(String file) {
        File f = new File(file);
        File currentPath = new File(f.getParent());

        return currentPath.getName();
    }

    public static List<String> getFiles(String path) {
        List<String> fileList = new ArrayList<String>();
        
        File root = new File(path);
        
		if(path.contains("*")) {
			IOFileFilter fileFilter = new WildcardFileFilter(root.getName());
			IOFileFilter dirFilter = new WildcardFileFilter("*");
			Collection<File> files = FileUtils.listFiles(root.getParentFile(), fileFilter, dirFilter);
			for(File file:files) {
				fileList.add(file.getAbsolutePath());
			}
			return fileList;
		}
        
        if(root.isFile()) {
            fileList.add(path);
            return fileList;
        }

        if(root.isDirectory()) {
            for (File file : Files.fileTraverser().breadthFirst(root)) {
                if (file.isFile()) {
                    fileList.add(file.getAbsolutePath());
                }
            }
            return fileList;
        }

        throw new RuntimeException("The file or path does not exists: " + path);
    }


    public static InputStream getInputStream(String inputFileName) throws IOException {
        if(inputFileName.startsWith("http")) {
            return HTTPHelper.getAsInputStream(inputFileName);
        }

        InputStream fileStream = new FileInputStream(inputFileName);
        if (inputFileName.endsWith(".gz")) {
            return new GZIPInputStream(fileStream);
        }
        if(inputFileName.endsWith(".bz2")) {
            return new BZip2CompressorInputStream(fileStream);
        }
        return fileStream;
    }

    public static String getString(String location) throws IOException {
        if(location.startsWith("http")) {
            return HTTPHelper.get(location);
        }
        return FileUtils.readFileToString(new File(location));
    }


    public static OutputStream getOutputStream(String outputFileName) throws IOException {
        if("stdout".equals(outputFileName)) {
            return System.out;
        } else if("stderr".equals(outputFileName)) {
            return System.err;
        } else if (outputFileName.endsWith(".gz")) {
            OutputStream out = new FileOutputStream(outputFileName);
            return new GZIPOutputStream(out);
        } else {
            return new FileOutputStream(outputFileName);
        }
    }

    public static void appendToOutputStream(OutputStream out, String value ) throws IOException {
        out.write(value.getBytes("UTF-8"));

    }
}

@@ -2445,32 +2445,65 @@
 s IOException %7B%0A
+        InputStream inputStream;%0A
         if(input
@@ -2537,38 +2537,46 @@
 ) %7B%0A            
-return
+inputStream = 
  HTTPHelper.getA
@@ -2613,26 +2613,34 @@
        %7D
-%0A%0A
+ else %7B%0A  
         
 InputStr
@@ -2623,33 +2623,35 @@
 lse %7B%0A          
-I
+  i
 nputStream fileS
@@ -2649,19 +2649,8 @@
 eam 
-fileStream 
 = ne
@@ -2675,32 +2675,43 @@
 inputFileName);%0A
+        %7D%0A%0A
         if (inpu
@@ -2770,36 +2770,37 @@
 GZIPInputStream(
-file
+input
 Stream);%0A       
@@ -2897,20 +2897,21 @@
 tStream(
-file
+input
 Stream);
@@ -2936,20 +2936,21 @@
  return 
-file
+input
 Stream;%0A

allow streaming of gzip files of http locations