package com.bennavetta.util.tycho;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.jar.Manifest;

import org.apache.logging.log4j.core.config.plugins.PluginManager;
import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.ExampleMode;
import org.kohsuke.args4j.Option;

import com.bennavetta.util.tycho.cli.XmlWrapDescriptorParser;
import com.bennavetta.util.tycho.impl.DefaultWrapperGenerator;

public class Main
{	
	static {
		PluginManager.addPackage("com.bennavetta.util.tycho.cli.logging"); // a bit of a hack, but none of the other methods seen to be working
	}
	
	public static final String LOGGING_PROP = "tycho.gen.logging.level";
	
	@Option(name="--log", aliases="-l", required=false, metaVar="<level>", usage="Log level to use. One of: off, fatal, error, warn, info, debug, trace, all (case-insensitive)")
	private String logLevel;
	
	@Option(name="-v", aliases="--version", required=false, usage="Display version information")
	private boolean version;
	
	@Argument(required=true, metaVar="config-file", usage="The XML configuration describing the dependencies to wrap", multiValued=false)
	private File configFile;
	
	private Manifest manifest; // store it so we don't have to parse it each time
	
	private Manifest getManifest() throws IOException
	{
		if(manifest == null)
		{
			//try(InputStream in = getClass().getResourceAsStream("/META-INF/MANIFEST.MF"))
			//{
			//	manifest = new Manifest();
			//	manifest.read(in);
			//}
			URL resource = Main.class.getResource(Main.class.getSimpleName() + ".class");
			URLConnection conn = resource.openConnection();
			if(conn instanceof JarURLConnection)
			{
				manifest = ((JarURLConnection) conn).getManifest();
			}
			else
			{
				try(InputStream in = getClass().getResourceAsStream("/META-INF/MANIFEST.MF"))
				{
					manifest = new Manifest();
					manifest.read(in);
				}
			}
		}
		return manifest;
	}
	
	private int run(String[] args) throws Exception
	{
		CmdLineParser parser = new CmdLineParser(this);
		try
		{
			parser.parseArgument(args);
			
			if(logLevel != null)
			{
				System.setProperty(LOGGING_PROP, logLevel.toUpperCase());
			}
			
			if(version)
			{
				System.out.println("Tycho-Gen version " + getManifest().getMainAttributes().getValue("Implementation-Version"));
			}
			else
			{
				SAXBuilder builder = new SAXBuilder();
				Document doc = builder.build(configFile);
				WrapRequest request = new XmlWrapDescriptorParser().createRequest(doc);
				
				WrapperGenerator generator = new DefaultWrapperGenerator();
				generator.generate(request);
			}
			
			return 0;
		}
		catch(CmdLineException e)
		{	
			String invocation = getManifest().getMainAttributes().getValue("Dist-Jar");
			// tell the user what was wrong with the arguments
			System.err.println(e.getMessage());
			
			// show usage information
			System.err.println("Usage: java -jar " + invocation + " [options] config-file");
			parser.printUsage(System.err);
			
			// show an example
			System.err.println();
			System.err.println("    Example: java -jar " + invocation + parser.printExample(ExampleMode.REQUIRED) + " myconfig.xml");
			return 1;
		}
	}
	
	public static void main(String[] args)
	{
		try
		{
			int rc = new Main().run(args);
			System.exit(rc);
		}
		catch(Exception e)
		{
			//System.err.println("Error: " + e.getLocalizedMessage());
			e.printStackTrace();
			System.exit(1);
		}
	}
}

@@ -50,16 +50,49 @@
 o.File;%0A
+import java.io.FileOutputStream;%0A
 import j
@@ -131,32 +131,90 @@
 io.InputStream;%0A
+import java.io.OutputStreamWriter;%0Aimport java.io.Writer;%0A
 import java.net.
@@ -378,24 +378,116 @@
 ginManager;%0A
+import org.apache.maven.model.Model;%0Aimport org.apache.maven.model.io.xpp3.MavenXpp3Writer;%0A
 import org.j
@@ -855,16 +855,56 @@
 nerator;
+%0Aimport com.google.common.base.Charsets;
 %0A%0Apublic
@@ -3051,16 +3051,56 @@
 quest);%0A
+%09%09%09%09%0A%09%09%09%09writePom(request.getParent());%0A
 %09%09%09%7D%0A%09%09%09
@@ -3667,24 +3667,339 @@
 1;%0A%09%09%7D%0A%09%7D%0A%09%0A
+%09private void writePom(Model pom) throws IOException%0A%09%7B%0A%09%09MavenXpp3Writer writer = new MavenXpp3Writer();%0A%09%09Writer out = null;%0A%09%09try%0A%09%09%7B%0A%09%09%09out = new OutputStreamWriter(new FileOutputStream(pom.getPomFile()), Charsets.UTF_8);%0A%09%09%09writer.write(out, pom);%0A%09%09%7D%0A%09%09finally%0A%09%09%7B%0A%09%09%09if(out != null)%0A%09%09%09%09out.close();%0A%09%09%7D%0A%09%7D%0A%0A
 %09public stat

write modified parent pom to disk