<?xml version="1.0" encoding="UTF-8"?>
<project name="Woocommerce zipMoney Payments" default="build" basedir=".">
    <property file="./build.properties" />

    <property name="tmp-dir" value="/tmp/"/>
    <property name="wp.base_dir" value="${tmp-dir}/wordpress" />
    <property name="wp.lib_dir" value="${tmp-dir}/wordpress-tests-lib" />
    <property name="extension.base_dir" value="${project.basedir}" />
    <property name="vendor-dir" value="${project.basedir}/vendor/"/>
    <property name="global.vendor.dir" value="~/.composer/vendor/"/>

    <property name="bin.composer"   value="composer --working-dir=${project.basedir}"/>
    <property name="bin.wp"         value="${vendor-dir}bin/wp" />
    <property name="bin.wc-setup"   value="${vendor-dir}webgig/wc-setup/bin/wc-setup" />
    <property name="bin.phpunit"  value="${global.vendor.dir}bin/phpunit" />
    <property environment="env"/>


    <property name="db.name.safe" refid="db.name">
        <filterchain>
            <replaceregexp>
                <regexp pattern="[^0-9a-zA-Z$_]" replace="_" ignoreCase="true"/>
            </replaceregexp>
        </filterchain>
    </property>

    <target name="install" depends="install-wordpress,install-woocommerce" description="Install Wordpress and Woocommerce" />

    <target name="test" depends="test-phpunit" description="Run available tests" />

    <target name="validate-build-properties" description="Ensure all require properties are defined">
        <if>
            <not><isset property="wp.version"/></not>
            <then><fail>wp.version is not specified</fail></then>
        </if>
        <if>
            <not><isset property="wp.base_url"/></not>
            <then><fail>wp.base_url is not specified</fail></then>
        </if>  
        <if>
            <not><isset property="wp.multisite"/></not>
            <then><fail>wp.multisite is not specified</fail></then>
        </if> 
        <if>
            <not><isset property="wc.version"/></not>
            <then><fail>wc.version is not specified</fail></then>
        </if>
        <if>
            <not><isset property="db.user"/></not>
            <then><fail>db.user is not specified</fail></then>
        </if>
        <if>
            <not><isset property="db.name"/></not>
            <then><fail>db.name is not specified</fail></then>
        </if>
        <if>
            <not><isset property="db.pass"/></not>
            <then><fail>db.pass is not specified</fail></then>
        </if>  
    </target>


    <target name="install-deps" description="Install all required dependencies">
        <exec command="${bin.composer} install" passthru="true" checkreturn="true" />
    </target>

    <target name="clean"  description="Remove Wordpress directory and Wordpress database">
       
        <exec command="rm -rf ./${packagename}-v*.zip" />   
        <exec command="mysqladmin -f drop ${db.name} -u${db.user} -p${db.pass}" />
        
        <delete dir="${wp.base_dir}" />
        <delete dir="${wp.lib_dir}" />       
    </target>

    <target name="install-wordpress"  description="Install Wordpress and Woocomerce  in the ${tmp-dir} directory">
        <if>
            <not><available type="dir" file="${wp.base_dir}" /></not>
            <then>
                <exec command="${bin.wc-setup} ${db.name.safe} ${db.user} ${db.pass} ${db.host} ${wp.version} --allow-root" passthru="true" checkreturn="true" />                        
                <exec command="php ${wp.lib_dir}/includes/install.php ${wp.lib_dir}/wp-tests-config.php {$wp.multisite}" passthru="true" checkreturn="true" />                        
            </then>
            <else>
                <echo message="Wordpress is already installed in ${tmp-dir}" />
            </else>
        </if>
    </target>
    
    <target name="install-woocommerce"  description="Install Wordpress and Woocomerce  in the ${tmp-dir} directory">                
        <exec command="${bin.wp} plugin install woocommerce --version='${wc.version}' --path='${wp.base_dir}' --activate --allow-root" passthru="true" checkreturn="true" />                        
    </target>
  
    <target name="test-phpunit" depends="install-deps,install">
        <exec command="${bin.phpunit}" passthru="true" checkreturn="true" />
    </target>

    <target name="create_package">
        <exec command="composer install --no-dev" passthru="true" checkreturn="true" />
        <exec command="zip -r ${env.packagename}-v${env.branch}.zip ./assets/* ./includes/* ./vendor/autoload.php ./vendor/composer/*.php ./vendor/zipmoney/**/* ./*.php ./README.txt ./LICENSE.txt" passthru="true" checkreturn="true" />
    </target>

    <target name="publish_to_s3">
        <exec command="aws s3 cp ${env.packagename}-v${env.branch}.zip s3://zm-software-assets/Plugins-Libraries/Plugins/Woocommerce/${env.packagename}-v${env.branch}.zip  --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers" passthru="true" checkreturn="true" />
    </target> 

    <target name="add_to_svn">
        <exec command="cp -a ./assets/ ./includes/ ./vendor/ ./*.php ./README.txt ./LICENSE.txt ~/zipmoney-woocommerce-addon/trunk/" />
        <exec command="cd ~/zipmoney-woocommerce-addon/  &amp;&amp;  svn stat" />
        <exec command="cd ~/zipmoney-woocommerce-addon/  &amp;&amp;  svn ci -m '${env.branch}'" />
        <exec command="cd ~/zipmoney-woocommerce-addon/  &amp;&amp;  svn cp trunk tags/${env.branch} " />
        <exec command="cd ~/zipmoney-woocommerce-addon/  &amp;&amp;  svn ci -m 'Tagging version version ${env.branch}' " />    
    </target>

    <target name="package" description="Package module into a wordpress plugin package">
        <propertyregex property="tagname" subject="${env.branch}" pattern="^(\*|\d+(\.\d+){0,2}(\.\*)?)$"  match="$1" casesensitive="false" defaultvalue="false"/>                
        <echo msg="${tagname}" />
        <if>
            <ispropertytrue property="tagname" />
            <then>
                <echo msg="Is a tag" />
                <phingcall target="create_package"/>
            </then>
            <else>
                <echo msg="Is not a tag, so not creating a package" />
            </else>
        </if>
    </target>


    <target name="deploy_to_s3" description="Deploy to s3">
        <propertyregex property="tagname" subject="${env.branch}" pattern="^(\*|\d+(\.\d+){0,2}(\.\*)?)$"  match="$1" casesensitive="false" defaultvalue="false"/>                
        <echo msg="${tagname}" />
        <if>
            <ispropertytrue property="tagname" />
            <then>
                <echo msg="Is a tag" />
                <phingcall target="publish_to_s3"/>
            </then>
            <else>
                <echo msg="Is not a tag, so not deploying to s3" />
            </else>
        </if>
    </target>

    <target name="deploy_to_plugins_directory" description="Deplopy to Wordpress plugins directory">
        <propertyregex property="tagname" subject="${env.branch}" pattern="^(\*|\d+(\.\d+){0,2}(\.\*)?)$"  match="$1" casesensitive="false" defaultvalue="false"/>                
        <echo msg="${tagname}" />
        <if>
            <ispropertytrue property="tagname" />
            <then>
                <echo msg="Is a tag" />
                <phingcall target="add_to_svn"/>
            </then>
            <else>
                <echo msg="Is not a tag, so not deploying to wordpress plugins directory" />
            </else>
        </if>
    </target>
</project>