package org.flexunit.ant.launcher.commands.player;

import java.io.File;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.types.FilterSet;
import org.apache.tools.ant.types.FilterSetCollection;
import org.apache.tools.ant.types.Commandline.Argument;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.types.resources.URLResource;
import org.apache.tools.ant.util.ResourceUtils;
import org.flexunit.ant.LoggingUtil;

public class AdlCommand extends DefaultPlayerCommand
{
   private final String ADT_JAR_PATH = "lib" + File.separatorChar + "adt.jar";
   private final String DESCRIPTOR_TEMPLATE = "flexUnitDescriptor.template";
   private final String DESCRIPTOR_FILE = "flexUnitDescriptor.xml";

   private File precompiledAppDescriptor;

   @Override
   public File getFileToExecute()
   {
	  if(getPrecompiledAppDescriptor() != null)
	  {
		  return new File(getPrecompiledAppDescriptor().getAbsolutePath());
	  }
      return new File(getSwf().getParentFile().getAbsolutePath() + File.separatorChar + DESCRIPTOR_FILE);
   }

   /**
    * Used to create the application descriptor used to invoke adl
    */
   private void createApplicationDescriptor()
   {
      try
      {
         //Template location in JAR
         URLResource template = new URLResource(getClass().getResource("/" + DESCRIPTOR_TEMPLATE));

         //Descriptor location, same location as SWF due to relative path required in descriptor
         File descriptor = new File(getSwf().getParentFile().getAbsolutePath() + File.separatorChar + DESCRIPTOR_FILE);

         //Create tokens to filter
         Double versionNumber = getVersion();
         FilterSet filters = new FilterSet();
         filters.addFilter("ADL_SWF", getSwf().getName());
         filters.addFilter("ADT_VERSION", Double.toString(versionNumber));
         if(versionNumber > 2.0) {
        	 filters.addFilter("VERSION_PROP", "versionNumber");
         } else {
        	 filters.addFilter("VERSION_PROP", "version");
         }

         //Copy descriptor template to SWF folder performing token replacement
         ResourceUtils.copyResource(
            template,
            new FileResource(descriptor),
            new FilterSetCollection(filters),
            null,
            true,
            false,
            null,
            null,
            getProject()
         );

         LoggingUtil.log("Created application descriptor at [" + descriptor.getAbsolutePath() + "]");
      }
      catch (Exception e)
      {
         throw new BuildException("Could not create application descriptor");
      }
   }

   private double getVersion()
   {
      String outputProperty = "AIR_VERSION";

      //Execute mxmlc to find SDK version number
      Java task = new Java();
      task.setFork(true);
      task.setFailonerror(true);
      task.setJar(new File(getProject().getProperty("FLEX_HOME") + File.separatorChar + ADT_JAR_PATH));
      task.setProject(getProject());
      task.setDir(getProject().getBaseDir());
      task.setOutputproperty(outputProperty);

      Argument versionArgument = task.createArg();
      versionArgument.setValue("-version");

      task.execute();

      //Parse version number and return as int
      String output = getProject().getProperty(outputProperty);
      int prefixIndex = output.indexOf("adt version \"");
      double version = Double.parseDouble(output.substring(prefixIndex + 13, prefixIndex + 16));

      LoggingUtil.log("Found AIR version: " + version);

      return version;
   }

   @Override
   public void prepare()
   {
      getCommandLine().setExecutable(generateExecutable());
      getCommandLine().addArguments(new String[]{getFileToExecute().getAbsolutePath()});

      if(getPrecompiledAppDescriptor() == null)
      {
    	  //Create Adl descriptor file
    	  createApplicationDescriptor();
      }
   }

   private String generateExecutable()
   {
      return getProject().getProperty("FLEX_HOME") + "/bin/" + getDefaults().getAdlCommand();
   }

   public File getPrecompiledAppDescriptor()
   {
	   return precompiledAppDescriptor;
   }

   public void setPrecompiledAppDescriptor(File precompiledAppDescriptor)
   {
	   this.precompiledAppDescriptor = precompiledAppDescriptor;
   }

}
@@ -3333,22 +3333,19 @@
 );%0D%0A
-%0D%0A
       
-//Pars
+doubl
 e ve
@@ -3354,99 +3354,430 @@
 ion 
-number and return as int%0D%0A      String output = getProject().getProperty(outputProperty);
+= parseAdtVersionNumber( getProject().getProperty(outputProperty) );%0D%0A      LoggingUtil.log(%22Found AIR version: %22 + version);%0D%0A      return version;%0D%0A   %7D%0D%0A%0D%0A%0D%0A   private double parseAdtVersionNumber( String versionString )%0D%0A   %7B%0D%0A%09  double version;%0D%0A%0D%0A      //AIR 2.6 and greater only returns the version number.%0D%0A      if( versionString.startsWith(%22adt%22) )%0D%0A      %7B%0D%0A          //Parse version number and return as int
 %0D%0A
+%09
     
@@ -3796,22 +3796,29 @@
 Index = 
-output
+versionString
 .indexOf
@@ -3834,37 +3834,31 @@
 sion %5C%22%22);%0D%0A
+%09
       
-double 
 version = Do
@@ -3874,22 +3874,29 @@
 eDouble(
-output
+versionString
 .substri
@@ -3949,65 +3949,106 @@
     
-LoggingUtil.log(%22Found AIR version: %22 + version);%0D%0A%0D%0A    
+%7Delse%0D%0A      %7B%0D%0A    %09  version = Double.parseDouble(versionString.substring(0, 3) );%0D%0A      %7D%0D%0A%0D%0A%09
   re

Version Check Fix from horixon