/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.forrest.cli;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.forrest.core.Controller;
import org.apache.forrest.core.IController;
import org.apache.forrest.core.document.AbstractOutputDocument;
import org.apache.forrest.core.exception.ProcessingException;

/**
 * A command line interface for Forrest.
 * 
 */
public class CLI {
	private static final Log log = LogFactory.getLog(CLI.class);

	private static Set<String> processedUris = new HashSet<String>();

	private static Set<String> unProcessedUris = new HashSet<String>();

	private static IController controller;

	/**
	 * @param args
	 */
	public static void main(final String[] args) {
		System.out.println("Forrest 2 Command Line Interface");
		System.out.println("================================");

		if (args[0].equals("--help")) {
			displayHelp();
			return;
		}

		try {
			controller = new Controller();
			System.out.println("\n Processing request for " + args[0]);
			unProcessedUris.add(args[0]);
			while (unProcessedUris.size() > 0) {
				processURIs(unProcessedUris);
			}
		} catch (final Exception e) {
			e.printStackTrace();
			log.error(e);
			System.exit(1);
		}
	}

	/**
	 * Processes a URI to get the response document. Any local links found in
	 * the document are added to the list of documents to be processed.
	 * 
	 * @param uri
	 * @param controller
	 * @throws MalformedURLException
	 * @throws ProcessingException
	 * @throws IOException
	 * @throws URISyntaxException
	 */
	private static void processURIs(final Set<String> uris)
			throws MalformedURLException, ProcessingException, IOException,
			URISyntaxException {
		AbstractOutputDocument doc;
		HashSet<String> processingUris = new HashSet<String>(uris);
		unProcessedUris = new HashSet<String>();
		for (String strUri : processingUris) {
			URI uri = new URI(strUri);
			if (!(processedUris.contains(strUri))) {
				log.debug("Processing: " + strUri);
				doc = controller.getOutputDocument(uri);
				unProcessedUris.addAll(doc.getLocalDocumentLinks());
				outputDocument(doc, uri);
				processedUris.add(strUri);
			}
		}
	}

	/**
	 * Output the document.
	 * @param doc
	 * @param uri
	 * @throws IOException
	 */
	private static void outputDocument(AbstractOutputDocument doc, URI uri) throws IOException {
		System.out.println("\n Resulting document for request " + uri
				+ " is:\n");
		System.out.println(doc.getContentAsString());
		
		File outFile = new File("build\\" + doc.getPath());
		FileWriter writer = new FileWriter(outFile);
		writer.write(doc.getContentAsString());
		writer.close();
	}

	/**
	 * Outputs a short description on how to use the CLI.
	 */
	private static void displayHelp() {
		System.out.println("Help");
		System.out.println("====");
		System.out.println("CLI [URI]");
		System.out.println("URI is the request URI that you wish to process");
		System.out
				.println("The default behaviour is to process the request and output the results to the System.out");
	}

}

@@ -3487,16 +3487,108 @@
 ());%0A%09%09%0A
+%09%09String path = getContentBuildPath();%0A%09%09File outPath = new File(path);%0A%09%09outPath.mkdirs();%0A
 %09%09File o
@@ -3609,17 +3609,12 @@
 ile(
-%22build%5C%5C%22
+path
  + d
@@ -3735,16 +3735,188 @@
 lose();%0A
+%09%09log.debug(%22Written document to %22 + path + doc.getPath());%0A%09%7D%0A%0A%09private static String getContentBuildPath() %7B%0A%09%09return System.getProperty(%22user.dir%22) + %22/build/content/%22;%0A
 %09%7D%0A%0A%09/**

Make build directories if they don't exist. Change build directory to a more specific one