// Import
// **************************************
// Core
import java.io.*;
import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

// **************************************
public class IrcConvertor {
    // Version Control Tag
    private static String VC_NAME = new String("$Name: TrillianLogConvertor $");
    private static String VC_DATE = new String("$Date: 10/03/2003 23:31:45 $");
    
    private static int firstTimeStamp = 0;
    private String lastDate = new String();

    private String fileName;
    private Properties convertionTable;
		private static int lineNumber =0;

    /**
     * Constructor : Build a new FileCharacter Converter.
     * @@param sourceFileName Text source filename.
     * @@param defintionFileName Properties definition filename.
     */
    public IrcConvertor(String sourceFileName) {
	fileName = sourceFileName;
    }
        
    /**
     * Actionner : Convert source file using current definition, and display result.
     */
    public void convert() {
	DataInputStream dis=null;
	int car=0;

	try {
	    dis = new DataInputStream(new FileInputStream(fileName));
	} catch (FileNotFoundException e) { }

	BufferedReader d = new BufferedReader(new InputStreamReader(dis));

	String currentLine = new String();
	String currentConvertedLine = new String();

	try {
	    while ((currentLine=d.readLine())!=null) {
		currentConvertedLine = convertLine(currentLine);
		if (currentConvertedLine!=null)
		    System.out.println(currentConvertedLine);
	    }
	    System.out.println(lastDate);
	}
	catch (EOFException e) {
	}
	catch (IOException e) { 
	    System.out.println("err");
	}

	try {
	    dis.close();
	}
	catch (IOException e) { 
	    System.out.println("err");
	}
    }

    /**
     * Actionner : Convert source file using current definition file, and write output.
     * @@param destinationFileName Destination filename.
     */
    public void convert(String destinationFileName) {
	DataInputStream dis=null;
	DataOutputStream dos=null;
	int car=0;

	try {
	    dis = new DataInputStream(new FileInputStream(fileName));
	} catch (FileNotFoundException e) { }

	try {
	    dos = new DataOutputStream(new FileOutputStream(destinationFileName));
	} catch (FileNotFoundException e) { }

	
	BufferedReader d = new BufferedReader(new InputStreamReader(dis));
	String currentLine = new String();
	String currentConvertedLine = new String();

	try {
	    while ((currentLine=d.readLine())!=null) {
		currentConvertedLine = convertLine(currentLine);
	  System.out.print(lineNumber+" parsed\r");		lineNumber++;
		if (currentConvertedLine!=null)
		    dos.writeBytes(currentConvertedLine + "\n");
	    }
	    dos.writeBytes(lastDate);
	}
	catch (EOFException e) {
	}
	catch (IOException e) { 
	    System.out.println("err");
	}

	try {
	    dis.close();
	    dos.flush();
	    dos.close();
	}
	catch (IOException e) { 
	    System.out.println("err");
	}
    }

    /**
     * Actionner : Convert a pirch line to his new mirc representation.
     * @@param line Line to convert.
     * @@return A new String representation of this line.
     */    
    private String convertLine(String line) {
	if (line.length()==0)
	    return null;
	if (line.startsWith("Session")) {
	    // Session Start (SERVER:#CHANNEL): Tue Dec 03 00:38:54 2002
	    // or
	    // Session Close (#CHANNEL): Tue Dec 03 00:45:18 2002
	    // must return
	    // Session Start: Fri Sep 07 19:22:08 2001
	    // or
	    // Session Close: Mon May 9 19:59:43 2001

	    StringTokenizer st = new StringTokenizer(line," ");
			st.nextToken(); // Session
			String sessionType = st.nextToken(); // Start or Stop
			String channelInfo = st.nextToken(); // (server:Channel) or Channel
			String result="Session "+sessionType+":"+line.substring(line.indexOf(channelInfo)+channelInfo.length());
			return result;
	}
	else
	    if (line.startsWith("<", 8)) // nick
		return line;
	    else
		return (new String( line.substring(0,7) + " *** " + line.substring(8))).replace('"','\'');
    }

    /**
     * Description : DESCRIPTION
     * @@param args Command line.
     * <I>Example</I> : java source.txt output.txt
     * <BR>
     */
    public static void main(String parameters[]) {
//	System.out.println(((VC_NAME.substring(7,VC_NAME.length()-1)).replace('_',' ')).replace('-','.'));
//	System.out.println((VC_DATE.substring(7,VC_DATE.length()-1)));

	IrcConvertor jIrcC = new IrcConvertor(parameters[0]);

	System.out.println("Date  : " + new Date().toString());
	jIrcC.convert(parameters[1]);
	System.out.println(lineNumber+" parsed");
	System.out.println("Date  : " + new Date().toString());
	//jIrcC.convert(parameters[1]);
	//	System.out.println("Date  : " + new Date().toString());

	System.exit(0);
    }
}
