/*
* Scrabaid
*
* Revisions:
*
* 18/05/2001 lc - v1.0 release
*
* ----------------------------------------------------------------------------
*
* Scrabaid version 1.0, Copyright 2001 Linus Chang
* Scrabaid comes with ABSOLUTELY NO WARRANTY; for details, view the LICENCE
* file included with this distribution. This is free software, and you are
* welcome to redistribute it under certain conditions; view the LICENCE
* file for details.
*/

import java.io.*;
import java.util.*;
import java.text.DecimalFormat;

class TableGenerator {
	static final int DEFAULT_PENDING_SIZE = 3500000;
	public static void main(String []args) {
		if (args.length != 1 && args.length != 2) {
			System.out.println("Usage: java TableGenerator <upper wordlength boundary> <optional: pending size>");
			System.out.println("Note: pending size defaults to 3500000, which works fine for a 150 MB JVM.");
			System.out.println("      Try a smaller size for smaller JVMs - eg. 2000000 for a 100MB JVM.");
			System.exit(1);
		}

		int upperWordLimit = 0;
		try {
			upperWordLimit=Integer.parseInt(args[0]);
		}
		catch (Exception e){
			System.out.println("Unable to parse upper word limit. Make sure it's an integer, 15 or less.");
			System.exit(1);
		}
		if (upperWordLimit < 2 || upperWordLimit > 15) {
			System.out.println("Illegal upper word limit - must be between 2 and 15.");
			System.exit(1);
		}
		int pendingSize = DEFAULT_PENDING_SIZE;
		try {
			pendingSize = Integer.parseInt(args[1]);
		}
		catch (Exception e) {}
		if (pendingSize < 0) {
			System.out.println("Illegal pending size. Must be positive.");
			System.exit(1);
		}

		try {
			File curdir = new File(".");
			File patdir = new File(curdir, "patterns");
			File [] subpatdir = new File[16];
			if (!patdir.isDirectory()) {
				if (!patdir.mkdir()) {
					System.err.println("Unable to create patterns directory");
					System.exit(1);
				}
			}
			DecimalFormat decimalFormat = new DecimalFormat("00");

			WordList wordList = WordList.getInstance();
			Word[][] words = wordList.getWords();
			for (int wordLength = 02; wordLength <= upperWordLimit; wordLength++) {
				PatternCollection patternCollection = new PatternCollection(patdir,decimalFormat.format(wordLength),false);

				for (short counter = 0; counter < words[wordLength].length; counter++) {
					long time1 = System.currentTimeMillis();
					System.out.print("Word " + wordLength + "/" + counter + "\t" + words[wordLength][counter] + " ");
					Pattern[] patterns = words[wordLength][counter].getPatterns();

					patternCollection.addPatterns(patterns, counter);
					System.out.println(System.currentTimeMillis() - time1);
					if (patternCollection.getNumPending() > pendingSize) {
						long writetime1 = System.currentTimeMillis();
						patternCollection.write();
						System.out.println("Writing to disk took " + (System.currentTimeMillis() - writetime1) + " milliseconds.");
					}
				}
				patternCollection.write();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}



