<?xml version="1.0"?>
<project name="SabreDAV" default="buildzip" basedir=".">

    <!-- Any default properties -->
    <property file="build.properties" />

    <!-- Where to write api documentation -->
    <property name="sabredav.apidocspath" value="docs/api" />

    <target name="buildzip" depends="init, test, clean">
        <mkdir dir="build" />
        <echo>Running composer</echo>
        <exec command="composer create-project --no-dev sabre/dav build/SabreDAV ${sabredav.version}" checkreturn="false" passthru="1" />
        <zip destfile="build/SabreDAV-${sabredav.version}.zip" basedir="build/SabreDAV" prefix="SabreDAV/" />
    </target>

    <target name="uploadzip" depends="buildzip">
        <echo>Uploading to Google Code</echo>
        <propertyprompt propertyName="googlecode.username" promptText="Enter your googlecode username" useExistingValue="true" />
        <propertyprompt propertyName="googlecode.password" promptText="Enter your googlecode password" useExistingValue="true" />
        <exec command="bin/googlecode_upload.py -s 'SabreDAV ${sabredav.version}' -p sabredav --labels=${sabredav.ucstability} -u '${googlecode.username}' -w '${googlecode.password}' build/SabreDAV-${sabredav.version}.zip" checkreturn="true" />
    </target>

    <target name="clean" depends="init">
        <echo msg="Removing build files (cleaning up distribution)" />
        <delete dir="docs/api" />
        <delete dir="build" />
    </target>

    <target name="markrelease" depends="init,clean,test">
        <echo>Creating Git release tag</echo>
        <exec command="git tag ${sabredav.version}" checkreturn="false" passthru="1" />
    </target>

    <target name="test">
        <phpunit haltonfailure="1" haltonerror="1" bootstrap="tests/bootstrap.php" haltonskipped="1" printsummary="1">
          <batchtest>
            <fileset dir="tests">
              <include name="**/*.php"/>
            </fileset>
          </batchtest>
        </phpunit>
    </target>

    <target name="apidocs" depends="init">

        <echo>Creating api documentation using PHP documentor</echo>
        <echo>Writing to ${sabredav.apidocspath}</echo>
        <exec command="phpdoc parse -t ${sabredav.apidocspath} -d lib/" passthru="1" />
        <exec command="bin/phpdocmd ${sabredav.apidocspath}/structure.xml ${sabredav.apidocspath} --lt %c" passthru="1" />
        <!--<delete file="${sabredav.apidocspath}/structure.xml" />-->

    </target>

    <target name="init">

        <!-- This sets SabreDAV version information -->
        <adhoc-task name="sabredav-version"><![CDATA[

            class SabreDAV_VersionTask extends Task {

                public function main() {

                    include_once 'lib/Sabre/DAV/Version.php';
                    $this->getProject()->setNewProperty('sabredav.version',\Sabre\DAV\Version::VERSION);
                    $this->getProject()->setNewProperty('sabredav.stability',\Sabre\DAV\Version::STABILITY);
                    $this->getProject()->setNewProperty('sabredav.ucstability',ucwords(Sabre\DAV\Version::STABILITY));

                }

            }

        ]]></adhoc-task>
        <sabredav-version />
        <echo>SabreDAV version ${sabredav.version}</echo>

    </target>

</project>
