import java.util.*;
import java.io.*;


public class CpLogParser2
{

	public static void main (String argv[])
	{
	    Vector fileNames;
	    String workingDir;

	    if (argv.length == 1) {
	        workingDir = argv[0];
	    } else {
	        workingDir = "D:\\Mindspring Log Files";
	    }
	    System.out.println("working dir " + workingDir);

        fileNames = getLogNamesInDirectory(workingDir);

		String fileName = workingDir + "\\" + "output.txt";
        try {
            PrintStream outputStream = new PrintStream(new FileOutputStream(fileName));
            System.out.println("# files: " + fileNames.size());
            for (int i=0; i < fileNames.size(); i++) {
                if (fileNames.elementAt(i).equals("output.txt")) {
                    continue;
                }
                System.out.println("processing file " + fileNames.elementAt(i));
                FileInputStream inputStream = new FileInputStream(workingDir + "\\" + fileNames.elementAt(i));
                performOperationsOn(inputStream, outputStream);
                inputStream.close();
            }
            outputStream.close();
        }
        catch (IOException e) {
            System.out.println(e);
            e.printStackTrace();
        }
	}

	static void performOperationsOn(FileInputStream in, PrintStream out) throws IOException
	{
        DataInputStream dataIn = new DataInputStream(in);
        Hashtable logEntries = new Hashtable(5000);
        String key;
        Integer value;

        //for (int i=0; i < 10; i++) {
        while (true) {
            CpLogEntry logEntry = CpLogEntry.logEntryFromStream(dataIn);
            if (logEntry == null) {
                in.close();
                break;
            }
            if (logEntry.isInterestingEntry()) {
                key = logEntry.getHostName() + " " + logEntry.getInstructionArg();
                if ((value = (Integer)logEntries.get(key)) == null) {
                    value = new Integer(0);
                }
                logEntries.put(key, new Integer(value.intValue() + 1));
                //out.println(logEntry.getHostName() + " [" + logEntry.getDateString() + "] " + logEntry.getInstructionArg());

            }


        }

        Enumeration keys = logEntries.keys();
        while (keys.hasMoreElements()) {
            key = (String)keys.nextElement();
            out.println(key + " [" + logEntries.get(key) + "]");
        }

    }



     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;
    }
}