<?xml version="1.0" encoding="UTF-8"?>
<project name="SF-COMMON-TEST" xmlns:hlm="http://www.nokia.com/helium">

    <target name="sf-test-smoketest" depends="sf-prebuild,sf-build-smoketestpkg,sf-send-testpkg,sf-zip-logs,sf-publish" />
    <target name="sf-test-bc-check" depends="sf-prebuild,sf-bc-check,sf-zip-logs,sf-publish" />

    <target name="sf-find-ATS-worker-root">
      <exec executable="perl" dir="${sf.common.config.dir}/tools/ats/devices" failonerror="false" outputproperty="sf.spec.test.workerroot">
        <arg value="-pe"/>
        <arg value="&quot;m/TEMPDIR=(\S+)/;print $1;s/.*\n//g&quot;"/>
        <arg value="${env.COMPUTERNAME}\EMULATOR_GENERIC_${env.COMPUTERNAME}.properties" /> 
      </exec>
      <echo message="ATS Worker Root = ${sf.spec.test.workerroot}"/> 
    </target>
    
    <target name ="sf-make-junction" depends="sf-find-ATS-worker-root">        
        <mkdir dir="${sf.spec.test.workerroot}/${sf.spec.test.epocroot}"/>
        <exec executable="perl" dir="${sf.common.config.dir}/tools/ats" failonerror="false" output="${build.log.dir}/ATS_${build.id}_make_junction.log">
        <arg value="make_junction.pl"/>
        <arg value="--link=${sf.spec.test.workerroot}/${sf.spec.test.epocroot}/epoc32"/>
        <arg value="--target=${build.drive}/epoc32"/>
        <arg value="--force"/>
      </exec>
    </target>

    <target name ="sf-delete-junction" depends="sf-find-ATS-worker-root">
        <exec executable="junction.exe" dir="${sf.spec.test.workerroot}/${sf.spec.test.epocroot}" failonerror="false" output="${build.log.dir}/ATS_${build.id}_delete_junction.log">
        <arg value="-d"/>
        <arg value="epoc32"/>
      </exec>
    </target>  

<!-- static and dynamic BC Test target -->
    <target name="sf-bc-check">
        <if><isset property="env.PDT_HOME"/>
        <then>
            <!-- create BC dir -->
            <mkdir dir="${build.log.dir}/BC"/>
            <delete file="${build.log.dir}/BC/bc.config" quiet="true"/>
            <delete file="${build.log.dir}/BC/BBCResults.xsl" quiet="true"/>
            <delete file="${build.log.dir}/BC/libraries.txt" quiet="true"/>
            <delete file="${build.log.dir}/BC/libraries_report.xml" quiet="true"/>
            <delete file="${build.log.dir}/BC/headers.txt" quiet="true"/>
            <delete file="${build.log.dir}/BC/headers_report.xml" quiet="true"/>

            <exec executable="cmd" output="${build.log.dir}/BC/bc.config" append="true">
                <arg value="/c"/>
                <arg value="echo"/>
                <arg value="BASELINE_NAME=${sf.spec.bccheck.baseline.name}"/>
            </exec>
            <exec executable="cmd" output="${build.log.dir}/BC/bc.config" append="true">
                <arg value="/c"/>
                <arg value="echo"/>
                <arg value="BASELINE_SDK_DIR=${sf.spec.bccheck.baseline.skd.dir}"/>
            </exec>
            <exec executable="cmd" output="${build.log.dir}/BC/bc.config" append="true">
                <arg value="/c"/>
                <arg value="echo"/>
                <arg value="BASELINE_SDK_S60_VERSION=${sf.spec.bccheck.baseline.s60.version}"/>
            </exec>
            <exec executable="cmd" output="${build.log.dir}/BC/bc.config" append="true">
                <arg value="/c"/>
                <arg value="echo"/>
                <arg value="CURRENT_NAME=${build.id}"/>
            </exec>
            <exec executable="cmd" output="${build.log.dir}/BC/bc.config" append="true">
                <arg value="/c"/>
                <arg value="echo"/>
                <arg value="CURRENT_SDK_DIR=${build.drive}"/>
            </exec>
            <exec executable="cmd" output="${build.log.dir}/BC/bc.config" append="true">
                <arg value="/c"/>
                <arg value="echo"/>
                <arg value="CURRENT_SDK_S60_VERSION=${sf.spec.bccheck.current.s60.version}"/>
            </exec>

            <copy file="${sf.spec.pdt.root}/fts/compatibilityanalyser/reports/BBCResults.xsl" tofile="${build.log.dir}/BC/BBCResults.xsl" failonerror="false" verbose="true"/>

            <if><istrue value="${sf.spec.bccheck.enable.la}"/>
                <then>
                    <exec executable="python" dir="${sf.spec.pdt.root}/fts/compatibilityanalyser" output="${build.log.dir}/BC/libraries.txt">
                        <arg value="CheckBC.py"/>
                        <arg value="${build.log.dir}/BC/bc.config"/>
                        <arg value="-la"/>
                        <arg value="-f"/>
                        <arg value="${sf.spec.bccheck.reportid}"/>
                    </exec>
                    <copy file="${sf.spec.pdt.root}/fts/compatibilityanalyser/reports/Libraries_CompatibilityReport_${sf.spec.bccheck.reportid}.xml" tofile="${build.log.dir}/BC/libraries_report.xml" failonerror="false" verbose="true"/>
                </then>
            </if>

            <if><istrue value="${sf.spec.bccheck.enable.ha}"/>
                <then>
                    <exec executable="python" dir="${sf.spec.pdt.root}/fts/compatibilityanalyser" output="${build.log.dir}/BC/headers.txt">
                        <arg value="CheckBC.py"/>
                        <arg value="${build.log.dir}/BC/bc.config"/>
                        <arg value="-ha"/>
                        <arg value="-f"/>
                        <arg value="${sf.spec.bccheck.reportid}"/>
                    </exec>
                    <copy file="${sf.spec.pdt.root}/fts/compatibilityanalyser/reports/Headers_CompatibilityReport_${sf.spec.bccheck.reportid}.xml" tofile="${build.log.dir}/BC/headers_report.xml" failonerror="false" verbose="true"/>
                </then>
            </if>

            <if><istrue value="${sf.spec.bccheck.enable.dynamic}"/>
                <then>
                    <for param="file">
						<path>
							<fileset dir="${sf.spec.test.package.location}/../bctest" includes="*.zip"/>
						</path>
						<sequential>
							<propertyregex override="yes" property="program"  input="@{file}" regexp="([^\\/\/]*)$" select="\1"/>  						
                            <echo message="INFO Updating bctest package with test info"/>
					        <echo message="************ Test drop package name:  ${program} " />
                            <exec executable="perl" dir="${sf.common.config.dir}/tools/ats/" failonerror="false" append="true" output="${build.log.dir}/ATS_${build.id}_dynamicbc.log">
                              <arg value="ats_specialise_test_drop.pl"/>
                              <arg value="--test-drop-name=${env.COMPUTERNAME}-bctest-${build.id}_${program}"/> 
                              <arg value="--host-name=${env.COMPUTERNAME}.ad-sfpd.intra"/>
                              <arg value="--src=${sf.spec.test.package.location}/../bctest/${program}"/>
                              <arg value="--dest=${sf.spec.test.package.location}\${env.COMPUTERNAME}-bctest-${build.id}_${program}" />
                              <arg value="--publish=${publish.dir}\ats_reports\${program}"/>
                            </exec>
                            <!-- Now ready to send to ATS -->  
					      <echo message="Sending Test drop :  ${program} ............" />									
					<antcall target="sf-send-testpkg" inheritAll="false">
                        <param name="sf.spec.test.package.name" value="${env.COMPUTERNAME}-bctest-${build.id}_${program}"/>
                    </antcall>					
							
						</sequential>
  
					</for>
				</then>
			</if>
		</then>
        <else>
            <echo message="WARNING: PDT_HOME not set, I dont know where the BC tools are installed so cannot continue"/>
        </else>
		</if>	
    </target>
    
	<!-- Preparing smoketest package (zip) for sending to ATS -->
	
	<target name="sf-build-smoketestpkg" depends="sf-getenv-tools">
        <echo message="INFO Building smoketest"/>
		
		<!-- Have to replace qt.conf with the one in the smoketest package -->
		<copy file="${build.drive}/epoc32/tools/qt/qt.conf" tofile="${build.drive}/epoc32/tools/qt/qt.conf.temp" overwrite="true" />
		<copy file="${build.drive}/smoketest/qt/qt.conf.smoketest" tofile="${build.drive}/epoc32/tools/qt/qt.conf" overwrite="true" />
		
         <fmpp sourceFile="${qt.qmake.ant.template}" outputFile="${temp.build.dir}/run-qmake-test.ant.xml">
            <data expandProperties="yes">
                data: xml(${build.drive}/smoketest/package_definition.xml)
                ant: antProperties()

            </data>
        </fmpp>
		<hlm:assertFileExists xmlns:hlm="http://www.nokia.com/helium" file="${temp.build.dir}/run-qmake-test.ant.xml"/>

		<!-- Running the qmake steps -->
        <ant antfile="${temp.build.dir}/run-qmake-test.ant.xml" dir="${build.drive}/" output="${compile.log.dir}/${build.id}_test.qmake.log"/>
		
        <exec executable="cmd" dir="${build.drive}/smoketest" failonerror="false" append="true" output="${build.log.dir}/ATS_${build.id}_smoketest.log">
            <arg value="/c"/>
            <arg value="sbs -c winscw_udeb -s package_definition.xml"/>
        </exec>
		
        <exec executable="cmd" dir="${build.drive}/smoketest" failonerror="false" append="true" output="${build.log.dir}/ATS_${build.id}_smoketest.log">
            <arg value="/c"/>
            <arg value="sbs -c winscw_udeb.test -s package_definition.xml"/>
        </exec>
		
		<!-- Be a good boy and put the file back where it was -->
		<copy file="${build.drive}/epoc32/tools/qt/qt.conf" tofile="${build.drive}/epoc32/tools/qt/qt.conf.smoketest" overwrite="true" />
		<copy file="${build.drive}/epoc32/tools/qt/qt.conf.temp" tofile="${build.drive}/epoc32/tools/qt/qt.conf" overwrite="true" />

        <echo message="INFO Creating smoketest testpackage"/>
        <exec executable="perl" dir="${build.drive}/smoketest/group" failonerror="false" append="true" output="${build.log.dir}/ATS_${build.id}_smoketest.log">
            <arg value="smoketest.pl"/>
			<arg value="--target=${sf.spec.test.target}"/>
			<arg value="--ats-version=${sf.spec.ats.version}"/>
        </exec>
        <copy file="${build.drive}/smoketest/group/smoketest.zip" todir="${sf.spec.test.package.location}"  failonerror="false"/>
        
        <echo message="INFO Updating smoketest package with test info"/>

        <exec executable="perl" dir="${sf.common.config.dir}/tools/ats/" failonerror="false" append="true" output="${build.log.dir}/ATS_${build.id}_smoketest.log">
          <arg value="ats_specialise_test_drop.pl"/>
          <arg value="--test-drop-name=Smoketest-${build.id}"/> 
          <arg value="--host-name=${env.COMPUTERNAME}.ad-sfpd.intra"/>
          <arg value="--src=${sf.spec.test.package.location}\smoketest.zip"/>
          <arg value="--dest=${sf.spec.test.package.location}\${sf.spec.test.package.name}"/>	
          <arg value="--build-id=http://${sf.spec.publish.diamonds.server}${diamonds.build.id}"/>	
          <arg value="--publish=${publish.dir}\ats_reports"/>
          <arg value="--bld-drive=${build.drive}"/>
          <arg value="--test-target=${sf.spec.test.target}"/>
          <arg value="--image-path=${sf.spec.test.imagepath}"/>
          <arg value="--ats-version=${sf.spec.ats.version}"/>
        </exec>

        <!-- Now ready to send to ATS -->
    </target>   

	<!-- Sending testdrop to ATS -->
	
    <target name="sf-send-testpkg" depends="sf-start-ats,sf-find-ATS-worker-root,sf-make-junction">
        <if><equals arg1="${sf.spec.test.target}" arg2="syborg"/>
          <then>
            <delete file="${sf.spec.test.workerroot}/syborg.dtb" quiet="true"/>
            <copy file="${build.drive}/epoc32/rom/syborg/syborg.dtb" todir="${sf.spec.test.workerroot}" failonerror="false"/>
          </then>
        </if>
        <mkdir dir="${sf.spec.test.workerroot}/${sf.spec.test.package.droppath}"/>
        <echo message="Sending test package ${sf.spec.test.package.name} to ${sf.spec.test.host.name} path ${sf.spec.test.host.droppath}"/>
        <exec executable="perl" dir="${sf.common.config.dir}/tools/ats" failonerror="false" output="${build.log.dir}/ATS_${build.id}_testdrop.log">
            <arg value="ats_testdrop.pl"/>
            <arg value="--host=${sf.spec.test.host.name}"/>
            <arg value="--username=${sf.spec.test.host.username}"/>
            <arg value="--password=${sf.spec.test.host.password}"/>
            <arg value="--local-test-pkg=${sf.spec.test.package.location}\${sf.spec.test.package.name}"/>
            <arg value="--local-drop-path=${sf.spec.test.workerroot}\${sf.spec.test.package.droppath}"/>
            <arg value="--host-drop-path=${sf.spec.test.host.droppath}"/>
            <arg value="--ats-version=${sf.spec.ats.version}"/>
            <arg value="--ats-worker-path=${sf.spec.ats_worker.drive}"/>
        </exec>
    </target>
    
    <target name="sf-start-ats">
        <if><equals arg1="${sf.spec.ats.version}" arg2="ats3"/>
            <then>
                <if><available file="${ats.drive}/start.cmd"/>  
                    <then>
                        <forget>
                            <exec executable="cmd" dir="${ats.drive}" failonerror="true">
                                <arg value="/c"/>
                                <arg value="start"/>
                                <arg value='"ATS3 STAF"'/>
                                <arg value="/D"/>
                                <arg value="."/>
                                <arg value="/MAX"/>
                                <arg value="run.cmd"/>
                            </exec>
                        </forget>
                        <echo message="Waiting for 10 seconds for the ATS Worker to start......."/>
                        <sleep seconds="10"/>
                    </then>
                </if>
            </then>
        </if>
        <if><equals arg1="${sf.spec.ats.version}" arg2="ats4"/>
            <then>
                <forget>
                    <exec executable="cmd" dir="${ats.drive}" failonerror="true">
                        <arg value="/c"/>
                        <arg value="start"/>
                        <arg value='"ATS4 Agent"'/>
                        <arg value="/D"/>
                        <arg value="."/>
                        <arg value="/MAX"/>
                        <arg value="startAgent.cmd"/>
                    </exec>
                </forget>
                <echo message="Waiting for 10 seconds for the ATS4 Agent(s) to start......."/>
                <sleep seconds="10"/>
                <forget>
                    <exec executable="cmd" dir="${ats.drive}" failonerror="true">
                        <arg value="/c"/>
                        <arg value="start"/>
                        <arg value='"ATS4 Engine"'/>
                        <arg value="/D"/>
                        <arg value="."/>
                        <arg value="/MAX"/>
                        <arg value="startEngine.cmd"/>
                    </exec>
                </forget>
                <echo message="Waiting for 10 seconds for the ATS4 Engine to start......."/>
                <sleep seconds="10"/>
            </then>
        </if>
    </target> 
</project>
