<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * $Id$
 *
 * Copyright (c) 2008-2011 Andreas Heigl<andreas@heigl.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * This is the ant-buildfile
 *
 * @category  Org_Heigl
 * @package   Org_Heigl_Hyphenator
 * @author    Andreas Heigl <andreas@heigl.org>
 * @copyright 2008-2011 Andreas Heigl<andreas@heigl.org>
 * @license   http://www.opensource.org/licenses/mit-license.php MIT-License
 * @version   2.0.1
 * @since     24.11.2011
 */
-->
<project name="Org_Heigl_Hyphenator" default="build" basedir=".">

    <!-- This target deletes all temporary files from the directory -->
    <target name="clean">
        <delete includeemptydirs="true">
            <fileset dir="${basedir}/build">
                <include name="**/*" />
            </fileset>
        </delete>
    </target>

    <target name="build"
              depends="sniff, clean, parseDic, test, api, manual.rst, distribute" />

    <target name="test">
        <mkdir dir="${basedir}/build/logs"/>
        <mkdir dir="${basedir}/build/phpunit"/>
        <mkdir dir="${basedir}/build/phpunit/coverage"/>
        <delete includeemptydirs="true" verbose="false">
            <fileset dir="${basedir}/build/phpunit/coverage">
                <include name="**/*" />
            </fileset>
        </delete>
        <exec executable="phpunit" dir="${basedir}" failonerror="on"/>
        <exec executable="${basedir}/tools/removeBasePath.sh" dir="${basedir}/build/phpunit/coverage" failonerror="true">
            <arg line="${basedir}/src/"/>
        </exec>
    </target>

    <target name="api">
        <mkdir dir="${basedir}/build/api"/>
        <exec executable="phpdoc" dir="${basedir}" failonerror="on"/>
    </target>
    
    <target name="distribute">
        <buildnumber />
        <property file="build.number" />
        <mkdir dir="${basedir}/dist"/>
        <exec executable="${basedir}/tools/createPackage" dir="${basedir}/src" failonerror="true">
            <arg value="make"/>
        </exec>
        <exec executable="pear" dir="${basedir}/src">
            <arg value="package"/>
        </exec>
        <move todir="${basedir}/dist">
            <fileset dir="${basedir}/src">
                <include name="**/*.tgz"/>
            </fileset>
        </move>
    	<delete file="${basedir}/src/package.xml"/>
        <zip destfile="dist/Org_Heigl_Hyphenator-Build_${build.number}.zip">
            <zipfileset dir="build/api" prefix="documentation/api" />
            <zipfileset dir="build/doc" prefix="documentation/manual" />
            <zipfileset dir="build/phpunit/coverage" prefix="documentation/coverage" />
            <zipfileset dir="src/Org/Heigl/Hyphenator" prefix="library/Org/Heigl/Hyphenator"/>
            <fileset dir="." includes="CHANGELOG" />
            <fileset dir="." includes="LICENSE" />
            <fileset dir="." includes="README" />
        </zip>
        <antcall target="clean"/>
    </target>

    <target name="manual">
        <mkdir dir="${basedir}/build/doc"/>
        <delete includeemptydirs="true" verbose="true">
            <fileset dir="${basedir}/build/doc">
                <include name="**/*" />
            </fileset>
        </delete>
        <exec executable="xsltproc" dir="${basedir}">
            <arg line="--output build/doc/index.html doc/hyphenator.xsl doc/main.xml "/>
        </exec>
        <copy todir="${basedir}/build/doc/">
            <fileset dir="${basedir}/doc/inc/"/>
        </copy>
    </target>
	
	<target name="manual.rst">
		<mkdir dir="${basedir}/build/doc"/>
        <delete includeemptydirs="true" verbose="true">
            <fileset dir="${basedir}/build/doc">
                <include name="**/*" />
            </fileset>
        </delete>
		<exec executable="sphinx-build" dir="${basedir}/docs">
			<arg value="-b"/>
			<arg value="html"/>
			<arg value="."/>
			<arg value="${basedir}/build/doc"/>
		</exec>
	</target>
    
    <!--
    Check the code using phpcs using the heigl.org-Standard
    -->
    <target name="sniff">
        <exec executable="phpcs" dir="${basedir}/src" failonerror="true">
            <arg value="--standard=HeiglOrg"/>
            <arg value="-n"/>
            <arg value="-p"/>
        	<arg value="."/>
        </exec>
    </target>
	
    <!--
    Parse the hyphenation files and create the ini-files
    -->
    <target name="parseDic">
        <exec executable="${basedir}/tools/renderDicts" dir="${basedir}/tools/" failonerror="true"/>
        <exec executable="git" dir="${basedir}/src/Org/Heigl/Hyphenator/share/files/" failonerror="true">
            <arg line="diff --quiet --exit-code ."/>
        </exec>
    </target>
</project>
