// MosesGenerators.java
//
//    Created: August 2005
//
// This file is a component of the BioMoby project.
// Copyright Martin Senger (martin.senger@gmail.com).
//

import org.biomoby.client.CmdLineHelper;
import org.biomoby.service.generator.DataTypesGenerator;
import org.biomoby.service.generator.ServicesGenerator;
import org.biomoby.service.generator.Generator;
import org.biomoby.shared.Central;
import org.biomoby.shared.CentralCached;

import org.tulsoft.tools.BaseCmdLine;
import java.util.Properties;

/**
 * A command-line client that can invoke Moses code generators. The
 * generators (either for service instances or for data types) use
 * information stored in a Biomoby registry to produce Java source
 * code. The code is then used by service providers to implement their
 * Biomoby services. <p>
 *
 * Start it with <tt>-h</tt> option:
 * <pre>
 * build/run/run-generator -h
 * </pre>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: MosesGenerators.java,v 1.5 2008/03/03 11:34:16 senger Exp $
 */
public class MosesGenerators
    extends CmdLineHelper {

    /*************************************************************************
     *
     *************************************************************************/
    static boolean notEmpty (String value) {
	return ( value != null && ! "".equals (value.trim()) );
    }

    /*************************************************************************
     *
     *  An entry point...
     *
     *************************************************************************/
    public static void main (String [] args) {
	try {

	    BaseCmdLine cmd = getCmdLine (args, MosesGenerators.class);
	    CentralCached worker = getCachableRegistryWorker (cmd);

	    boolean generateDataTypes = cmd.hasOption ("-dt");
	    boolean generateServices = cmd.hasOption ("-s");
	    boolean nogenerate = cmd.hasOption ("-n");
	    boolean nographs = cmd.hasOption ("-ng");
	    String filter = cmd.getParam ("-filter");
	    String outdir = cmd.getParam ("-outdir");
	    String dotLocation = cmd.getParam ("-dot");

	    String service = cmd.getParam ("-service");
	    String authority = cmd.getParam ("-auth");
	    if (service != null || authority != null)
		generateServices = true;

	    if (generateDataTypes) {
		if (notEmpty (filter))
		    qmsgln ("Using filter: '" + filter + "'");

		if (nogenerate)
		    msg ("It WOULD generate for the following data types:\n");
		else
		    qmsgln ("Generating data types" +
			    (outdir == null ? "..." : " into '" + outdir + "'... "));
		DataTypesGenerator dtg = new DataTypesGenerator (worker);
		Properties props = new Properties();
		if (filter != null) props.put (Generator.GPROP_FILTER, filter);
		if (outdir != null) props.put (Generator.GPROP_OUTDIR, outdir);
		if (nogenerate) props.put (Generator.GPROP_NOGEN, "true");
		if (verbose) props.put (Generator.GPROP_VERBOSE, "true");
		if (nographs) props.put (Generator.GPROP_NOGRAPHS, "true");
		if (notEmpty (dotLocation))
		    props.put (Generator.GPROP_DOTLOCATION, dotLocation);
		dtg.generate (props);
	    }

	    if (generateServices) {
		if (notEmpty (authority))
		    qmsgln ("Using filter for authorities: '" + authority + "'");
		if (notEmpty (service))
		    qmsgln ("Using filter for services: '" + service + "'");

		if (nogenerate)
		    msgln ("It WOULD generate for the following services:\n");
		else
		    qmsgln ("Generating services" +
			    (outdir == null ? "..." : " into '" + outdir + "'... "));
		ServicesGenerator sg = new ServicesGenerator (worker);
		Properties props = new Properties();
		if (service != null) props.put (Generator.GPROP_SERVICE, service);
		if (authority != null) props.put (Generator.GPROP_AUTH, authority);
		if (outdir != null) props.put (Generator.GPROP_OUTDIR, outdir);
		if (nogenerate) props.put (Generator.GPROP_NOGEN, "true");
		if (verbose) props.put (Generator.GPROP_VERBOSE, "true");
		if (nographs) props.put (Generator.GPROP_NOGRAPHS, "true");
		if (notEmpty (dotLocation))
		    props.put (Generator.GPROP_DOTLOCATION, dotLocation);
		sg.generate (props);
	    }

	} catch (Throwable e) {
	    processErrorAndExit (e);
	}
    }
}
