import java.io.*;
import java.util.*;

/**
 * This program goes through a file, pulls out each string
 * then recursively pulls out the substrings in each string
 *
 * @author Micheal Cottingham
 * @version 11/19/08
 */

public class Recursifier
{
	// Scanner: keyboard for input by user
	private static Scanner keyboard = new Scanner(System.in);

	// Scanner: file name for reading the file
	private static Scanner fileName;

	// File: for accessing the file by scanner
	private static File file;

	// Items to write to the file
	private static ArrayList<String> items = new ArrayList<String>();

   public static int MyCount; // adams added
	/**
	 * The main method
	 *
	 * @param String The arguments for the command line
	 */
	public static void main(String args[])
	{
		String read;
		String wriFile;

		// Welcome the user
		System.out.println("Welcome to Recursifier!");
		System.out.println("This program will recurse through a string " +
			"and give the substrings in that string. Strings are in a file you specify.");
		System.out.println();

		// Ask the user for an input filename
		System.out.print("Please enter the input filename: ");
		read = keyboard.next();

		// Ask the user for an output filename
		System.out.print("Please enter the output filename: ");
		wriFile = keyboard.next();

		System.out.println();

		// Pass this in to the read file
		// so that we can read the file
		if(!readFile(read))
		{
			System.out.println("File " + read + " not found.");
		}

		if(!writeFile(wriFile))
		{
			System.out.println("Could not write file: " + wriFile);
		}

		System.out.println();
		System.out.println("The input file was: " + read);
		System.out.println("The output file was: " + wriFile);
		System.out.println();
		System.out.println("This program ended normally");
	}

	/**
	 * This method reads the file, picks up the line
	 * and passes the line (word) to the first
	 * recursive method so that it'll call  the
	 * second recursive method for indirect and
	 * direct recursion
	 *
	 * @param String This is the name of the file to
	 *		read
	 * @return This returns a true or false depending
	 * 	on whether or not the file could be read
	 */
	public static boolean readFile(String fname)
	{
		boolean found;
		found = true;

		String line;

		// Read the file
		file = new File(fname);

		try
		{
			fileName = new Scanner(file);
		}

		catch(FileNotFoundException fnfe)
		{
			found = false;
		}

		// If we have the file, continue
		if(found)
		{
			// Read each line in the file
			while(fileName.hasNext())
			{
				line = fileName.nextLine();

				// Pass each each string in to the correct
				// method

				// All string permutations
				recurseString(line, 0);

				items.add("&&&&&&&&&&&&&&&\n");
				System.out.println("&&&&&&&&&&&&&&&");
			}
		}

		return found;
	}

	/**
	 * This method takes the result of running a counter
	 * on substrings, getting the result of the second
	 * recurseString for an indirect recursion
	 *
	 * @param String This is the string we want the substrings
	 *		of
	 * @param int This is the counter for the substring method
	 */
	public static void recurseString(String name, int count)
	{
	   int temp;
	//  MyCount++; System.out.println (" count 2 : \t" + MyCount); // adams added
		// Base case
		if(name.equals(""))
		{
			System.out.println("");
			items.add("\n");
		}

		else
		{
			// Chop off the character
			// before recursing through the remaining
			// substrings
			temp = recurseString(name);
			System.out.println (" temp in 2 param is " + temp);
			name = name.substring(count, temp);
         System.out.println ("name in 2 param is " + name);
			recurseString(name, count++);
		}
	}

	/**
	 * This method provides direct recursion to handle
	 * the bulk of the substrings
	 *
	 * @param String This is the string we want the sub
	 * 	strings of
	 * @return int This returns the length of the string
	 * 	in question, for the first recurseString to use
	 * 	for its substring
	 */
	public static int recurseString(String name)
	{  
	   int temp;
	   MyCount++; System.out.println (" count 1 : \t" + MyCount); // adams added

		// Base case
		if(name.equals(""))
		{
			return 0;
		}

		else
		{
			System.out.println(name);
			items.add(name + "\n");

			// Chop off the character
			name = name.substring(1, name.length());

			temp = recurseString(name);
			System.out.println (" temp in 1 param is: " + temp);
			return name.length();
		}
	}

	/**
	 * This method writes to the file the user wants
	 *
	 * @param String The name of the file to be written
	 * @return This returns a true or false, depdending
	 *		whether or not the file can be written to
	 *
	 */
	public static boolean writeFile(String fname)
	{
		FileWriter fwriter;
		PrintWriter write;
		boolean okay;
		okay = true;

		try
		{
			fwriter = new FileWriter(fname);
			write = new PrintWriter(fwriter);

			// Write to the file
			for(int i = 0; i < items.size(); i++)
			{
				write.print(items.get(i));
			}
			write.close();
		}

		catch(IOException ioe)
		{
			okay = false;
		}

		return okay;
	}
}