<project name="selenium" default="generate-sources">
    <target name="init">
		<property environment="env" />
		<property name="env.PERL5LIB" value="" />
		<property name="perl.executable" value="perl" />
		<presetdef name="perl">
			<exec executable="${perl.executable}" failonerror="true">
				<env key="PERL5LIB" value="${env.PERL5LIB}" />
			</exec>
		</presetdef>
		<!-- Cygwin make won't work with the auto-generated Makefile -->
		<condition property="make.executable" value="nmake">
			<os family="windows" />
		</condition>
		<property name="make.executable" value="make" />
		<presetdef name="make">
			<exec executable="${make.executable}" failonerror="true">
				<env key="PERL5LIB" value="${env.PERL5LIB}" />
			</exec>
		</presetdef>
		<!-- <perl>
			<arg value="-e" />
			<arg value="print qq(@INC);" />
		</perl>-->
	</target>
	<target name="generate-sources" depends="init">
    	<perl>
    	    <arg value="util/create_www_selenium.pl" />
    	</perl>
    </target>
    <target name="doc" depends="init, generate-sources">
    	<mkdir dir="doc"/>
    	<property name="outfile.www.selenium" location="doc/WWW-Selenium.html" />
    	<perl>
			<arg value="mypod2html.pl"/>
			<arg value="--infile=lib/WWW/Selenium.pm" />
			<arg value="--outfile=${outfile.www.selenium}" />
		</perl>
		<property name="outfile.test.www.selenium" location="doc/Test-WWW-Selenium.html" />
    	<perl>
			<arg value="mypod2html.pl"/>
			<arg value="--infile=lib/Test/WWW/Selenium.pm" />
			<arg value="--outfile=${outfile.test.www.selenium}" />
		</perl>
		<echo file="doc/index.html"><![CDATA[
<html><head><title>Selenium RC PerlDoc</title></head>
<body>
<h1>Selenium RC PerlDoc</h1>
<p><a href="WWW-Selenium.html">WWW::Selenium</a></p>
<p><a href="Test-WWW-Selenium.html">Test::WWW::Selenium</a></p>
</body></html>
]]>
</echo>
    </target>
    <target name="all" depends="generate-sources, test, doc"/>
    <target name="make" depends="init, generate-sources">
    	<perl>
    		<arg value="Makefile.PL" />
    	</perl>
    	<make />
    </target>
    
    <target name="pre-test">
        <condition property="skiptests-headless">
            <or>
                <isset property="skiptests" />
                <not><istrue value="${headless}" /></not>
            </or>
        </condition>
        <condition property="skiptests-nonheadless">
            <or>
                <istrue value="${headless}" />
                <isset property="skiptests" />
            </or>
        </condition>
    </target>
    
    <target name="test-nonheadless" depends="init, make, pre-test" unless="skiptests-nonheadless">
    	<echo>${selenium-server}</echo>
        <java jar="${selenium-server}" fork="true" spawn="true"/>
        <sleep seconds="20"/>
        <make failonerror="false" resultproperty="result">
        	<arg value="test" />
        </make>
        <get taskname="selenium-shutdown" src="http://localhost:4444/selenium-server/driver/?cmd=shutDown" dest="target/shutdown-result.txt" ignoreerrors="true" />
        <sleep seconds="20"/>
        <condition property="build.failed">
        	<not><equals arg1="0" arg2="${result}" /></not>
        </condition>
        <fail if="build.failed" message="perl tests failed!" />
    </target>
    
    <target name="test-headless" depends="init, make, pre-test" unless="skiptests-headless">
    	<echo>${selenium-server}</echo>
        <java jar="${selenium-server}" fork="true" spawn="true"/>
        <sleep seconds="20"/>
        <property name="lib.dir" location="lib" />
        <perl failonerror="false" resultproperty="result">
            <arg value="-MExtUtils::Command::MM" />
            <arg value="-e" />
            <arg value="test_harness(0, 'inc', 'blib/lib', 'blib/arch')" />
            <arg file="t/test_i18n.t" />
        </perl>
        <get taskname="selenium-shutdown" src="http://localhost:4444/selenium-server/driver/?cmd=shutDown" dest="target/shutdown-result.txt" ignoreerrors="true" />
        <sleep seconds="20"/>
        <condition property="build.failed">
        	<not><equals arg1="0" arg2="${result}" /></not>
        </condition>
        <fail if="build.failed" message="perl tests failed!" />
    </target>
    
    <target name="test" depends="test-headless, test-nonheadless" />
    
    <target name="dist" depends="generate-sources, doc">
        <mkdir dir="target" />
        <property name="project.version" value="UNKNOWNVERSION" />
        <property name="perl.release" value="selenium-perl-client-driver-${project.version}"/>
        <zip zipfile="target/${perl.release}.zip">
            <zipfileset dir="." prefix="${perl.release}">
                <include name="Makefile.PL"/>
                <include name="Changes"/>
                <include name="todo.txt"/>
                <include name="inc/**"/>
                <include name="lib/**"/>
                <include name="t/**"/>
                <exclude name="mypod2html.pl" />
                <include name="doc/**" />
            </zipfileset>
        </zip>
        <zip zipfile="target/${perl.release}-doc.zip">
            <zipfileset dir="doc" prefix="perl" />
        </zip>
    </target>
    
    <target name="clean">
    	<delete file="lib/WWW/Selenium.pm" />    	
    	<mkdir dir="doc" />
    	<delete dir="doc" />
    	<mkdir dir="blib" />
    	<delete dir="blib" />
    	<mkdir dir="target" />
    	<delete dir="target" />
    </target>
</project>
