<project name="build installers" default="build" basedir=".">
    <!--<property file="${basedir}/template.properties"/>-->
    
    <target name="build" depends="init,prepare-sources,prepare-zip,-build,post-build,run-build"/>

    <target name="-init-props">
        <available file="${suite.location}/nbproject/project.properties" property="do.load.ant.props"/>    
        <available file="${suite.location}/pom.xml" property="do.load.maven.props"/>    
    </target>

    <target name="-init-ant-props" if="do.load.ant.props" depends="-init-props">
        <property file="${suite.location}/nbproject/platform.properties" prefix="suite.platform."/>    
        <condition property="branding.token" value="${suite.platform.branding.token}">
            <isset property="suite.platform.branding.token"/>
        </condition>
        <property file="${suite.location}/nbproject/project.properties" prefix="suite.props."/>    
    </target>
    <target name="-init-maven-props" if="do.load.maven.props" depends="-init-props">
        <!-- There is no need to read any property from pom.xml - the properties are solved by maven plug-in. -->
        <!-- xmlproperty file="${suite.location}/pom.xml" prefix="pom.props"/>
        <property name="suite.props.app.name"  value="${pom.props.project.properties.brandingToken}"/>
        <property name="suite.props.app.title" value="${pom.props.project.artifactId}"/ -->
    </target>
    
    <target name="init" depends="-clean,-init-ant-props,-init-maven-props">
        <property name="installer.build.dir" value="${suite.location}/build/installer"/>
        <property name="suite.dist.directory" value="${suite.location}/dist"/>
        <property name="suite.dist.zip"       value="${suite.dist.directory}/${suite.props.app.name}.zip"/>
        <property name="installers.file.prefix" value="${suite.props.app.name}"/>
        <delete dir="${installer.build.dir}" includeemptydirs="yes" failonerror="false"/>

        <mkdir dir="${installer.build.dir}"/>
    </target>

    
    <target name="-clean">
    </target>

    <target name="post-build">
    </target>
    
    <target name="-build">
    </target>

    <target name="run-build">
        <ant dir="${installer.build.dir}" inheritAll="false"/>
    </target>

    <target name="-init-zip">
        <condition property="do.build.zip">
            <available file="${suite.dist.zip}"/>
        </condition>
    </target>
    <target name="-build-zip" depends="-init-zip" unless="do.build.zip">
        <antcall target="-build-zip-ant"/>
        <antcall target="-build-zip-maven"/>
    </target>

    <target name="-build-zip-ant" depends="-init-zip"  if="do.load.ant.props">
        <subant buildpath="${suite.location}" inheritAll="false" target="build-zip"/>
    </target>

    <target name="-build-zip-maven" depends="-init-zip" if="do.load.maven.props">
        <condition property="mvn.executable" value="mvn.bat" else="mvn">
            <os family="windows"/>
        </condition>

        <exec executable="${mvn.executable}" dir="${suite.location}">
           <arg value="install"/>
           <env key="JAVA_HOME" value="${java.home}"/>
        </exec>
    </target>

    <target name="prepare-zip" depends="-init-zip,-build-zip"/>

    <target name="prepare-sources">
        <copy todir="${installer.build.dir}">
           <fileset dir="${nbi.stub.location}"/>
        </copy>
        <copy todir="${installer.build.dir}/.common">
           <fileset dir="${nbi.stub.common.location}"/>
        </copy>

        <property name="product-uid"  value="${suite.nbi.product.uid}"/>

        <condition property="icon.path" value="${nbi.icon.file}" else="${installer.build.dir}/ext/components/products/helloworld/data/icon48.png">
             <and>
                 <isset property="nbi.icon.file"/>
                 <available file="${nbi.icon.file}"/>
             </and>
        </condition>

        <basename property="icon.file.name" file="${icon.path}"/>
        <condition property="icon.extension" value=".gif">
             <or>
                 <matches string="${icon.file.name}" pattern="^.*\.gif$"/>
                 <matches string="${icon.file.name}" pattern="^.*\.GIF$"/>
             </or>
        </condition>
        <condition property="icon.extension" value=".png">
             <or>
                 <matches string="${icon.file.name}" pattern="^.*\.png$"/>
                 <matches string="${icon.file.name}" pattern="^.*\.PNG$"/>
             </or>
        </condition>

        <property name="icon.name" value="${suite.props.app.name}${icon.extension}"/>

        <copy file="${icon.path}" tofile="${installer.build.dir}/ext/components/products/helloworld/src/org/mycompany/${icon.name}"/>



        <replace file="${installer.build.dir}/build.properties" encoding="utf-8">
            <replacefilter token="{nbi.ant.tasks.jar}"             value="${nbi.ant.tasks.jar}"/>
            <replacefilter token="{nbi.registries.management.jar}" value="${nbi.registries.management.jar}"/>
            <replacefilter token="{nbi.engine.jar}"                value="${nbi.engine.jar}"/>
            <replacefilter token="{product-platforms}"             value="${generate.installer.for.platforms}"/>
            <replacefilter token="{generator-jdk-location-forward-slashes}"         value="${generator-jdk-location-forward-slashes}"/>
            <replacefilter token="{generated-installers-location-forward-slashes}"  value="${suite.dist.directory}"/>
            <replacefilter token="{pack200.enabled}"               value="${pack200.enabled}"/>
            <replacefilter token="{generated-installers-prefix}"   value="${installers.file.prefix}"/>
            <replacefilter token="{product-uid}"                   value="${product-uid}"/>
        </replace>

        <replace dir="${installer.build.dir}/ext" encoding="utf-8">
            <replacefilter token="{product-name}" value="${suite.props.app.title}"/>
            <replacefilter token="{product-simple-name}" value="${suite.props.app.name}"/>
            <replacefilter token="{product-description}" value=""/>
            <replacefilter token="{product-uid}"         value="${product-uid}"/>
            <replacefilter token="{product-icon-name}"   value="${icon.name}"/>
        </replace>

        <replace dir="${installer.build.dir}/ext/infra/build/products" encoding="utf-8">
            <replacefilter token="{product.platforms}"        value="${generate.installer.for.platforms}"/>
            <replacefilter token="{product-data-path}"        value="${suite.dist.zip}"/>
            <replacefilter token="{product-data-sub-dir}"     value="${suite.props.app.name}"/>
            <replacefilter token="{product-uid}"              value="${product-uid}"/>
            <replacefilter token="{product-install-directory-name}"         value="${suite.props.app.name}"/>
            <replacefilter token="{product-install-directory-name-windows}" value="${suite.props.app.name}"/>
            <replacefilter token="{product-install-directory-name-macosx}"  value="${suite.props.app.name}"/>
        </replace>       
        <property name="dir.dir" value="${installer.build.dir}/tmpdirfornac"/>
        <mkdir dir="${dir.dir}"/>

        <native2ascii encoding="utf-8" src="${installer.build.dir}" dest="${dir.dir}" includes="build.properties ext/**/*.properties"/>

        <copy todir="${installer.build.dir}" overwrite="true" includeEmptyDirs="true">
             <fileset dir="${dir.dir}"/>
        </copy> 
        <condition property="license.defined">
             <and>
                 <isset property="nbi.license.file"/>
                 <available file="${nbi.license.file}"/>
             </and>
        </condition> 
        <antcall target="-prepare-license"/>

        <condition property="icon.defined">
             <and>
                 <isset property="nbi.icon.file"/>
                 <available file="${nbi.icon.file}"/>
             </and>
        </condition> 
        <antcall target="-prepare-icon"/>

        <condition property="dock.icon.defined">
             <and>
                 <isset property="nbi.dock.icon.file"/>
                 <available file="${nbi.dock.icon.file}"/>
             </and>
        </condition> 
        <antcall target="-prepare-dock-icon"/>

        <delete dir="${dir.dir}" includeEmptyDirs="true"/>
    </target>
    
    <target name="-prepare-license" if="license.defined">
        <copy file="${nbi.license.file}" tofile="${installer.build.dir}/ext/components/products/helloworld/src/org/mycompany/license.txt" overwrite="true"/>
    </target>
    <target name="-prepare-icon" if="icon.defined">
        <copy file="${nbi.icon.file}" tofile="${installer.build.dir}/ext/engine/src/org/mycompany/installer/wizard/wizard-icon.png" overwrite="true"/>
    </target>

    <target name="-prepare-dock-icon" if="dock.icon.defined">
        <mkdir dir="${installer.build.dir}/ext/engine/src/org/netbeans/installer/utils/system/launchers/impl"/>
        <copy file="${nbi.dock.icon.file}" tofile="${installer.build.dir}/ext/engine/src/org/netbeans/installer/utils/system/launchers/impl/dockicon.icns"/>
        <copy file="${nbi.dock.icon.file}" tofile="${installer.build.dir}/ext/components/products/helloworld/src/org/mycompany/${suite.props.app.name}.icns"/>
    </target>

    <target name="replace">
        <property file="${token.file}"/>
        <replaceregexp file="${file.to.replace}" flags="mg">
            <regexp pattern="${regexp.token}"/>
            <substitution expression="${regexp.replacement}"/>
        </replaceregexp>
    </target>
</project>
