import java.util.*;
import java.io.*;


public class CpLogParser
{

    static String [] files = {
        /*"102596.txt",
        "102696.txt",
        "102796.txt",
        "102896.txt",
        "102996.txt",
        "103096.txt",
        "103196.txt",
        "110196.txt",
        "110296.txt",
        "110396.txt",
        "110496.txt",
        "110596.txt",
        "110696.txt",
        "110796.txt",
        "110896.txt"
        */

        "access.0"
    };


	public static void main (String argv[])
	{
	    String fileName = "D:\\Mindspring Log Files\\access.0";
	    String databaseDir = "D:\\Mindspring Log Files\\database\\";

        switch (argv.length) {
        case 1:
	        fileName = argv[0];
	        break;
	    case 2:
	        fileName = argv[0];
	        databaseDir = argv[1];
	        break;
	    }

        try {

            openReportFile(databaseDir);
            for (int i = 0; i < files.length; i++) {
                fileName = "D:\\Mindspring Log Files\\" + files[i];
	            report("Processing file " + fileName);
                FileInputStream inputStream = new FileInputStream(fileName);
                addToDatabase(inputStream, databaseDir);
                inputStream.close();
            }

            closeReportFile();
        }
        catch (IOException e) {
            System.out.println(e);
            e.printStackTrace();
        }
	}

	static void addToDatabase(FileInputStream in, String databaseDir) throws IOException
	{
        DataInputStream dataIn = new DataInputStream(in);
        Hashtable logEntryDict = new Hashtable(5000);
        String hostName;
        Vector logEntries;
        CpLogEntry logEntry;
        File file;
        RandomAccessFile outputFile;

        int count = 0;
        int rejectedCount = 0;

        while (true) {
            logEntry = CpLogEntry.logEntryFromStream(dataIn);
            if (logEntry == null) {
                in.close();
                break;
            }
            count++;
            if (logEntry.isInterestingEntry()) {
                if ((logEntries = (Vector)logEntryDict.get(logEntry.getHostName())) == null) {
                    logEntries = new Vector();
                    logEntryDict.put(logEntry.getHostName(), logEntries);
                }
                logEntries.addElement(logEntry);
            } else {
                rejectedCount++;
            }
        }

        report("Read " + count + " lines, rejected " + rejectedCount + " lines, analyzing...");

        Enumeration keys = logEntryDict.keys();
        while (keys.hasMoreElements()) {
            hostName = (String)keys.nextElement();
            logEntries = (Vector)logEntryDict.get(hostName);
            logEntry = (CpLogEntry)logEntries.elementAt(0);
            report("Processing " + hostName + " (" + logEntries.size() + ") entries");

            file = new File(databaseDir + logEntry.getDirectoryPath());
            file.mkdirs();

            outputFile = new RandomAccessFile(databaseDir + logEntry.getFilePath(),"rw");
            long length = outputFile.length();
            if(length > 0) {
                outputFile.seek(length);
            }
            for (int i = 0; i < logEntries.size(); i++) {
                outputFile.writeBytes(logEntries.elementAt(i) + "\n");
            }
            outputFile.close();
        }
        report("Completed");
    }


     static Vector getLogNamesInDirectory(String dirPath)
     {
        File dir = new File(dirPath);
        Vector logNames = new Vector();

        if (!dir.isDirectory()) {
            throw new IllegalArgumentException("Directory does not exist " + dirPath);
        }

        String [] allFileNames = dir.list();

        for (int i=0; i < allFileNames.length; i++) {
            if (allFileNames[i].endsWith(".txt")) {
                logNames.addElement(allFileNames[i]);
            }
        }

        return logNames;
    }


    static RandomAccessFile reportOutputFile;

    static void openReportFile(String dirPath) throws IOException
    {
        reportOutputFile = new RandomAccessFile(dirPath + "output.txt","rw");
        long length = reportOutputFile.length();
        if(length > 0) {
            reportOutputFile.seek(length);
        }
    }

    static void closeReportFile() throws IOException
    {
        reportOutputFile.close();
    }

    static void report(String text) throws IOException
    {
        System.out.println(text);
        reportOutputFile.writeBytes(text + "\n");
    }

}