package org.fugerit.java.tool;

import java.util.Iterator;
import java.util.Properties;

import org.fugerit.java.core.cli.ArgUtils;
import org.fugerit.java.core.lang.helpers.ClassHelper;
import org.fugerit.java.core.util.PropsIO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>The launcher for all the tools.</p>
 * 
 * @author Fugerit
 *
 */
public class Launcher {

	protected static final Logger logger = LoggerFactory.getLogger(Launcher.class);
	
	/**
	 * Argument for the tool handler to use
	 */
	public static final String ARG_TOOL = "tool";

	/**
	 * Argument for the the verbose version
	 */
	public static final String ARG_VERBOSE = "verbose";
	
	/**
	 * Argumento for the help
	 */
	public static final String ARG_HELP = "help";
	
	private final static Properties HANDLER_LIST = loadSafe();
	private static Properties loadSafe() {
		Properties props = new Properties();
		try {
			props = PropsIO.loadFromClassLoader( "tool/config/handler-list.properties" );
		} catch (Exception e) {
			logger.error( "init error", e );
		}
		return props;
	}
	
	private static int handle( Properties params ) throws Exception {
		int exit = ToolHandlerHelper.EXIT_OK;
		String toolName = params.getProperty( ARG_TOOL );
		String help = params.getProperty( ARG_HELP );
		String verbose = params.getProperty( ARG_VERBOSE );
		if ( verbose != null ) {
			logger.info( "Setting to verbose output..." );
		}
		ToolHandler handler = null;
		if ( toolName != null ) {
			String toolType = HANDLER_LIST.getProperty( toolName );
			handler = (ToolHandler)ClassHelper.newInstance( toolType );
		}
		if ( toolName == null || help != null ) {
			printHelp();
			if ( handler instanceof ToolHandlerHelper ) {
				logger.info( "Handler help : \n"+((ToolHandlerHelper)handler).getHelp() );		
			}
		} else {
			exit = handler.handle( params );
		}
		return exit;
	}
	
	private static void printHelp() {
		logger.info( "fj-tool launcher v 0.0.1 [2017-05-04] quickstart : " );
		logger.info( "		--tool tool name [run the named tool]" );
		logger.info( "		--help [print this help]" );
		logger.info( "		--verbose [verbose output]" );
		logger.info( "	tool valid options : " );
		Iterator<Object> toolIt = HANDLER_LIST.keySet().iterator();
		while ( toolIt.hasNext() ) {
			logger.info( "		"+toolIt.next() );	
		}
	}
	
	public static void main( String[] args ) {
		try {
			Properties params = ArgUtils.getArgs( args );
			int exit = handle( params );
			logger.info( "EXIT -> "+exit );
			System.exit( exit );
		} catch ( Exception e ) {
			logger.error( "Errore during fj-tool launcher", e );
			printHelp();
		}
	}
	
}

@@ -23,24 +23,65 @@
 va.tool;%0D%0A%0D%0A
+import java.io.StringBufferInputStream;%0D%0A
 import java.
@@ -2607,16 +2607,64 @@
 xit );%0D%0A
+%09%09%09if ( exit != ToolHandlerHelper.EXIT_OK ) %7B%0D%0A%09
 %09%09%09Syste
@@ -2674,24 +2674,31 @@
 xit( exit );
+%09%0D%0A%09%09%09%7D
 %0D%0A%09%09%7D catch 

Exit Handler