package org.mwc.debrief.dis.runner;

import java.io.File;
import java.io.IOException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.mwc.debrief.dis.diagnostics.CustomEspduSender;
import org.mwc.debrief.dis.ui.preferences.DebriefDISSimulatorPrefs;

public class SimulationRunner
{

  final private DebriefDISSimulatorPrefs _prefs;
  private Process _process;
  private Job _simJob;

  public SimulationRunner(DebriefDISSimulatorPrefs simPrefs)
  {
    _prefs = simPrefs;
  }

  public void run()
  {
    final File exe = new File(_prefs.getExePath());
    final File input = new File(_prefs.getInputFile());
    final File parent = input.getParentFile();

    if (!exe.exists())
    {
      System.err.println("Executable not found");
      
      // FOR TESTING - FIRE OUR GENERATOR
       _simJob = new Job("Run simulation")
      {
        @Override
        protected IStatus run(IProgressMonitor monitor)
        {
          CustomEspduSender.main(new String[]{"2000", "2"});
          return Status.OK_STATUS;
        }

        @Override
        protected void canceling()
        {
          CustomEspduSender.terminate();
        }
      };
      _simJob.setUser(false);
      _simJob.schedule();

    }
    else if (!input.exists())
    {
      System.err.println("Input file not found");
    }
    else if (!parent.isDirectory())
    {
      System.err.println("Unable to find parent directory");
    }
    else
    {
      final String[] exeCmd = new String[]
      {_prefs.getExePath(), _prefs.getInputFile()};
//      SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
//      final String processName = input.getName() + " " + sdf.format(new Date());
      // fire up the processs
      try
      {
        _process = Runtime.getRuntime().exec(exeCmd, null, parent);
      }
      catch (IOException e1)
      {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
    }
  }

//  private MessageConsole findConsole(String name)
//  {
//    ConsolePlugin plugin = ConsolePlugin.getDefault();
//    IConsoleManager conMan = plugin.getConsoleManager();
//    IConsole[] existing = conMan.getConsoles();
//    for (int i = 0; i < existing.length; i++)
//    {
//      if (name.equals(existing[i].getName()))
//      {
//        return (MessageConsole) existing[i];
//      }
//    }
//    // no console found -> create new one
//    MessageConsole newConsole =
//        new MessageConsole(name, CorePlugin
//            .getImageDescriptor("icons/16/Calculator.png"));
//    conMan.addConsoles(new IConsole[]
//    {newConsole});
//    return newConsole;
//  }

  public void stop()
  {
    if (_process != null)
    {
      _process.destroy();
      _process = null;
    }
    
    // TESTING
    if(_simJob != null)
    {
      _simJob.cancel();
      _simJob = null;
    }
  }

}

@@ -625,18 +625,35 @@
 oid run(
+final String args
 )%0A
-
   %7B%0A    
@@ -887,15 +887,9 @@
 %22);%0A
-      
 %0A
+
     
@@ -926,17 +926,16 @@
 NERATOR%0A
- 
       _s
@@ -968,16 +968,16 @@
 ation%22)%0A
+
       %7B%0A
@@ -1052,32 +1052,76 @@
 itor)%0A        %7B%0A
+          String%5B%5D cArgs = args.split(%22 %22);%0A
           Custom
@@ -1141,33 +1141,13 @@
 ain(
-new String%5B%5D%7B%222000%22, %222%22%7D
+cArgs
 );%0A 
@@ -1692,156 +1692,8 @@
 )%7D;%0A
-//      SimpleDateFormat sdf = new SimpleDateFormat(%22hh:mm:ss%22);%0A//      final String processName = input.getName() + %22 %22 + sdf.format(new Date());%0A
     
@@ -1945,691 +1945,8 @@
  %7D%0A%0A
-//  private MessageConsole findConsole(String name)%0A//  %7B%0A//    ConsolePlugin plugin = ConsolePlugin.getDefault();%0A//    IConsoleManager conMan = plugin.getConsoleManager();%0A//    IConsole%5B%5D existing = conMan.getConsoles();%0A//    for (int i = 0; i %3C existing.length; i++)%0A//    %7B%0A//      if (name.equals(existing%5Bi%5D.getName()))%0A//      %7B%0A//        return (MessageConsole) existing%5Bi%5D;%0A//      %7D%0A//    %7D%0A//    // no console found -%3E create new one%0A//    MessageConsole newConsole =%0A//        new MessageConsole(name, CorePlugin%0A//            .getImageDescriptor(%22icons/16/Calculator.png%22));%0A//    conMan.addConsoles(new IConsole%5B%5D%0A//    %7BnewConsole%7D);%0A//    return newConsole;%0A//  %7D%0A%0A
   pu
@@ -2057,13 +2057,9 @@
   %7D%0A
-    
 %0A
+
     
@@ -2069,22 +2069,23 @@
 TESTING%0A
-
     if
+ 
 (_simJob

fix killing the processs. Support command line args