<?xml version="1.0" encoding="UTF-8"?>
<project name="ctc" default="main" >
	
	
	<taskdef resource="net/sf/antcontrib/antlib.xml">
	  <classpath>
	  	<pathelement path="${maven.runtime.classpath}" />
	  </classpath>
	</taskdef>
	
	<target name="main" depends="init, is.config.file, set.default.config.file, set.config.file" 
		if="" unless="" description="create all property files">
		<echo>PROJECT DIRECTORY: ${proj_dir}</echo>
		
		<antcall target="run.all.test" inheritall="true" />
		<antcall target="run.single.test" inheritall="true" />
		
		<available file="${ERROR_FILE}" property="error.flagged"/>

		<fail if="error.flagged" message="One or more ctc tests failed" />
	
	</target>

	<target name="init" depends="set.os, set.win, set.linux" >
		
		
		<property name="ERROR_FILE" value="${root_output}/ERROR_FOUND.txt" />

		<available file="${scenario.dir}" type="dir" property="scenario.dir.exist"/>
		<fail unless="scenario.dir.exist" message="Scenario directory ${scenario.dir} does not exist or was not specified." />

		<available file="${queryset.artifacts.dir}" type="dir" property="queryset.dir.exist"/>
		<fail unless="queryset.dir.exist" message="QuerySet directory ${queryset.artifacts.dir} does not exist or was not specified." />
		
		<condition property="single">
			<available file="${scenario.dir}/${scenario.file}"  />
		</condition>
		
		<delete file="${ERROR_FILE}"/>

	</target>
	
	<target name="set.win"
		if="WinOS">
		

		<pathconvert targetos="unix" property="proj_dir" >
		   		 <map from="\" to="/"/>
		   		<path location="${proj.dir}"/>
			 </pathconvert>
		
		<property name="root_output" value="${output.dir}" />

	</target>
	
	<target name="set.linux"
		if="UnixOS">
		<property name="root_output" value="${output.dir}" />
		<property name="proj_dir" value="${proj.dir}" />

	</target>
	
	<target name="is.config.file">
		
		<available file="${config.file}" type="file" property="config.exist"/>

		<condition property="set.config.default">
			<not>
				<isset property="config.exist"/>
			</not>
		</condition>
	
	</target>
	
	<target name="set.default.config.file"
		if="set.config.default">
		<property name="use.config.file" value="${proj_dir}/resources/ctc_tests/ctc-test.properties" />
		<echo>DEFAULT CONFIG: ${use.config.file}</echo>
	
	</target>
	
	<target name="set.config.file"
		unless="set.config.default" >
		<property name="use.config.file" value="${config.file}" />
		<echo>USE CONFIG: ${use.config.file}</echo>
	
	</target>

    <target name="run.all.test"
    		unless="single" >
    	<echo>Executing ALL Scenarios</echo>
    	
    	<for param="file">
    	  <path>
    	    <fileset dir="${scenario.dir}" includes="*.properties"/>
    	  </path>
    	  <sequential>
    	  	<antcall target="exec.scenario" inheritall="true" >
   				<param name="scenario.file" value="@{file}" />
    	  		<param name="scenario.dir" value="@{file}" />
			</antcall>
	  	
    	  	<!--
    		<antcall target="exec.scenario" inheritall="true" >
    			<param name="scenario.file" value="@{file}" />
			</antcall>
	  	-->
    	  </sequential>
    	</for>
    	
    </target>
	
    <target name="run.single.test"
    		if="single" >
    	<echo>Executing Scenario ${test.scenario}</echo>
		<property name="include.what" value="${scenario.dir}/${scenario.file}" />

        <iterate target="run.scenario"/>
    </target>
	
	<target name="exec.scenario" depends="exec.win, exec.linux" >
		
	  	 <antcall target="are.there.errors" inheritall="true" />

	</target>
	
	<target name="exec.win"
		if="WinOS">
		<echo>Scenario property file ${scenario.file}</echo>
		<echo>Config property file ${use.config.file}</echo>
		
			<pathconvert targetos="unix" property="scenario_file" >
			   		 <map from="\" to="/"/>
			   		<path location="${scenario.file}"/>
				 </pathconvert>
			
			<pathconvert targetos="unix" property="queryset_artifacts_dir" >
			   		 <map from="\" to="/"/>
			   		<path location="${queryset.artifacts.dir}"/>
				 </pathconvert>
			
			<pathconvert targetos="unix" property="vdb_artifacts_dir" >
			   		 <map from="\" to="/"/>
			   		<path location="${vdb.artifacts.dir}"/>
				 </pathconvert>
				 
			<pathconvert targetos="unix" property="root.output" >
			   		 <map from="\" to="/"/>
			   		<path location="${root_output}"/>
				 </pathconvert>		 
			
			
			  <java classname="org.teiid.test.client.TestClient" fork="true" >
			  	<classpath>
					<pathelement path="${maven.runtime.classpath}" />
			        <fileset dir="${proj_dir}/lib">
			          <include name="**/*.jar"/>
			        </fileset>
			    </classpath>
			    <jvmarg value="-Xmx1024m" />
				<!--
				<jvmarg value="-Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y" />
				-->
			  	<jvmarg value="-Dconfig=${use.config.file}" />
			  	<jvmarg value="-Dscenariofile=${scenario_file}" />
			  	<jvmarg value="-Dqueryset.artifacts.dir=${queryset_artifacts_dir}" />
		       	<jvmarg value="-Dquery.scenario.classname=${query.scenario.classname}" />
				<jvmarg value="-Dusedatasources=${usedatasources}" />
				<jvmarg value="-Ddatasourceloc=${datasourceloc}" />
				<jvmarg value="-Dserver.host.name=${server.host.name}" />   
				<jvmarg value="-Dproj.dir=${proj_dir}" /> 
			  	<jvmarg value="-Doutput.dir=${root.output}" />

			</java>

	</target>
	
	<target name="exec.linux"
		if="UnixOS">
		<echo>Scenario property file ${scenario.file}</echo>
		<echo>Config property file ${use.config.file}</echo>
		<echo>Query Artifacts ${queryset.artifacts.dir}</echo>
		<echo>Classpath ${proj_dir}/lib</echo>

<!--
<pathelement path="${maven.runtime.classpath}" />
-->
          <java classname="org.teiid.test.client.TestClient" fork="true" >
          	<classpath>
        		
                <fileset dir="${proj_dir}/lib">
                  <include name="**/*.jar"/>
                </fileset>
            </classpath>
            <jvmarg value="-Xmx1024m" />
			<!--
			<jvmarg value="-Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y" />
			-->
           	<jvmarg value="-Dconfig=${use.config.file}" />
          	<jvmarg value="-Dscenariofile=${scenario.file}" />
          	<jvmarg value="-Dqueryset.artifacts.dir=${queryset.artifacts.dir}" />
        	<jvmarg value="-Dquery.scenario.classname=${query.scenario.classname}" />
			<jvmarg value="-Dusedatasources=${usedatasources}" />
			<jvmarg value="-Ddatasourceloc=${datasourceloc}" />   
			<jvmarg value="-Dserver.host.name=${server.host.name}" />   
          	<jvmarg value="-Dproj.dir=${proj_dir}" /> 
			<jvmarg value="-Doutput.dir=${root_output}" /> 
          	
      	  </java>
		
		<!--
		 <jvmarg value="-Dmetamatrix.sockets=true" />
        	<jvmarg value="-Xdebug" />
            <jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,address=8000,suspend=y" />
		
		-->

	
	</target>
	
	<target name="are.there.errors" >
		<!-- load the scenario file to get the queryset thats being run -->
		<loadproperties srcFile="${scenario.file}" />
		
		
		<!-- get the scenario name based on the scenario file, strip the suffix  -->
		<basename property="scenario.name" file="${scenario.file}"
		          suffix=".properties"/>

		<echo>Are there any files ${root_output}/${queryset.dir}/output/${scenario.name}</echo>
		
	      <path id="output.dir.files">
	                  <fileset dir="${root_output}/${queryset.dir}/output/${scenario.name}">
	                  		<include name="*.txt"/>
	                  </fileset>
	        </path>
		
		<condition property="has.output">
			<resourcecount refid="output.dir.files" when="greater" count="0" />
		</condition>
		<echo>Is there output ${has.output}</echo>

	      <path id="error.files">
	                  <fileset dir="${root_output}/${queryset.dir}/output/${scenario.name}">
	                  		<include name="ERROR*"/>
	                  </fileset>
            		<fileset dir="${root_output}/${queryset.dir}/output/${scenario.name}">
            			<include name="*.err"/>
            		</fileset>
	        </path>
		
		<condition property="error.files.exist">
			<resourcecount refid="error.files" when="greater" count="0" />

		</condition>
		
		<echo>Do Error files exists ${error.files.exist}</echo>
		
		<condition property="error.exist">
			<not>
				<isset property="has.output"/>
			</not>
		</condition>
		<condition property="error.exist">
				<isset property="error.files.exist"/>
		</condition>

		<echo>Is this considered an error ${error.exist}</echo>
		
		<antcall target="touch.error.file" inheritall="true" />

	</target>

	<target name="touch.error.file"
		if="error.exist">
			<echo>Flag Error - Query Set: ${queryset.dir} Scenario: ${scenario.name} </echo>

		<concat destfile="${ERROR_FILE}" append="true" fixlastline="yes">Query Set: ${queryset.dir} Scenario: ${scenario.name} </concat>

	
	</target>
	
	<target name="set.os" >
			<condition property="isUnix">
				<and>
					<os family="unix"/>
					<not>
						<os name="Linux"/>
					</not>
					<not>
						<os name="HP-UX"/>
					</not>
				</and>
			</condition>
			<condition property="isLinux">
				<os name="Linux"/>
			</condition>
			<condition property="isHP-UX">
				<os name="HP-UX"/>
			</condition>
			<condition property="isWindows">
				<os family="windows"/>
			</condition>

			<condition property="UnixOS">
				<os family="unix"/>
			</condition>
			<condition property="WinOS">
				<os family="windows"/>
			</condition>
		</target>

	

</project>		