<!--
  ~ Copyright 2005-2011 WSO2, Inc. (http://wso2.com)
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~ http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<project default="setup" basedir=".." xmlns:ac="antlib:net.sf.antcontrib">

    <property name="carbon.home" value="${basedir}"/>

    <property name="lib" value="${carbon.home}/repository/lib"/>
    <property name="tempdir" value="${carbon.home}/../tmp/setup"/>
    <property name="output.dir" value="${carbon.home}/../tmp/wso2"/>



    <target name="setup">
        <mkdir dir="${lib}"/>

        <copy todir="${lib}" overwrite="false">
            <fileset dir="${carbon.home}/../components/plugins/">
                <include name="org.wso2.carbon.ndatasource*.jar"/>
                <include name="org.wso2.carbon.securevault*.jar"/>
                <include name="org.wso2.carbon.queuing*.jar"/>
                <include name="jdbc-pool*.jar"/>
                <include name="axis2*.jar"/>
                <include name="axiom*.jar"/>
                <include name="woden*.jar"/>
                <include name="wsdl4j*.jar"/>
                <include name="xerces*.jar"/>
                <include name="xalan*.jar"/>
                <include name="xml-apis*.jar"/>
                <include name="XmlSchema*.jar"/>
                <include name="neethi*.jar"/>
                <include name="backport-util-concurrent*.jar"/>
                <include name="commons-httpclient*.jar"/>
                <include name="commons-codec*.jar"/>
                <include name="commons-fileupload*.jar"/>
                <include name="httpcore*.jar"/>
                <include name="org.wso2.carbon.core*.jar"/>
                <include name="org.wso2.carbon.utils*.jar"/>
                <include name="jibx*.jar"/>
                <include name="axis2-jibx*.jar"/>
                <include name="rampart-policy*.jar"/>
                <include name="rampart-core*.jar"/>
                <include name="wss4j*.jar"/>
                <include name="bcel*.jar"/>
                <include name="hibernate*.jar"/>
                <include name="derby*.jar"/>
                <include name="abdera*.jar"/>
                <include name="org.wso2.carbon.registry.core*.jar"/>
                <include name="org.wso2.carbon.registry.ws*.jar"/><!-- used by the g-reg ws client sample-->
                <include name="org.wso2.carbon.registry.admin.api*.jar"/><!-- used for g-reg JMX API-->
                <include name="org.wso2.carbon.authenticator.proxy*.jar"/> <!-- used by the g-reg ws client sample-->
                <include name="org.wso2.carbon.authenticator.stub*.jar"/> <!-- used by the g-reg ws client sample-->
                <include name="org.wso2.carbon.user.core*.jar"/>
                <include name="opensaml*.jar"/>
                <include name="xmlbeans*.jar"/>
                <include name="geronimo-*.jar"/>
                <include name="ehcache*.jar"/> <!-- The following 4 jars required in using Embedded Registry. -->
                <include name="commons-pool*.jar"/>
                <include name="commons-dbcp*.jar"/>
                <include name="commons-io*.jar"/>
                <include name="compass*.jar"/>
                <include name="org.wso2.carbon.rm*.jar"/>
                <include name="rampart-trust*.jar"/>
                <include name="org.wso2.carbon.security.mgt*.jar"/>
                <include name="org.wso2.carbon.addressing*.jar"/>
                <include name="org.wso2.carbon.xfer*.jar"/>
                <include name="org.wso2.carbon.mex*.jar"/>
                <include name="savan-core*.jar"/>
                <include name="xkms*.jar"/>
                <include name="derby*.jar"/> <!-- This is required by WSAS DS Sample. Please don't remove -->
                <include name="org.wso2.carbon.discovery.core*.jar"/>
                <include name="org.osgi.core*.jar"/><!-- used by the g-reg jcr api-->
                <include name="javax.jcr*.jar"/><!-- used by the g-reg ws jcr api-->
                <include name="org.wso2.carbon.registry.jcr*.jar"/><!-- used by the g-reg ws jcr api-->
                <include name="org.wso2.carbon.dataservices.core*.jar"/> <!-- Required by DSS for custom validators etc.. -->
                <include name="org.wso2.carbon.governance.api*.jar"/>
                <include name="org.wso2.carbon.registry.extensions*.jar"/>
                <include name="jettison*.jar"/><!-- used by the JSON sample client in appserver -->
                <include name="org.wso2.carbon.user.api*.jar"/>
                <include name="org.wso2.carbon.registry.api*.jar"/>
                <include name="org.wso2.carbon.base*.jar"/>
                <include name="org.wso2.carbon.logging*.jar"/>
                <include name="org.wso2.securevault*.jar"/> <!-- used by the G-reg jcr sample-->
                <include name="org.wso2.pwprovider*.jar"/>
                <include name="org.wso2.carbon.rule.core*.jar"/>
                <include name="cxf-bundle*.jar"/><!-- used by the JAX-RS and JAX-WS sample cliets in appserver -->
                <include name="spring.framework*.jar"/><!-- used by the JAX-RS and JAX-WS sample clients in appserver -->
                <include name="javax.ws.rs.jsr311-api*.jar"/><!-- used by the JAX-RS sample client in appserver -->
            </fileset>
        </copy>

        <!-- Copy policy xml files-->
        <mkdir dir="${tempdir}"/>
        <unzip dest="${tempdir}">
            <fileset dir="${carbon.home}/../components/plugins/">
                <include name="org.wso2.carbon.security.mgt*.jar"/>
            </fileset>
        </unzip>
        <delete dir="${tempdir}"/>
        <!-- End Copy policy xml files-->

        <unzip dest="${lib}">
            <fileset dir="${carbon.home}/../components/plugins/">
                <!--<include name="org.wso2.carbon.stax.*.jar"/>-->
                <include name="org.wso2.carbon.utils*.jar"/>
                <include name="h2_*.jar"/>
            </fileset>
        </unzip>

        <copy todir="${lib}" overwrite="false">
            <fileset dir="${carbon.home}/../lib">
                <include name="commons-*.jar"/>
                <include name="backport-util-concurrent-*.jar"/>
            </fileset>
        </copy>

        <move todir="${lib}">
            <fileset dir="${lib}"/>
            <mapper>
                <mapper type="regexp" from="(.*).wso2v(.*)" to="\1-wso2v\2" />
                <mapper type="regexp" from="(.*).SNAPSHOT(.*)" to="\1-SNAPSHOT\2" />
            </mapper>
        </move>

        <delete dir="${lib}/META-INF"/>
        <delete dir="${lib}/org"/>
        <delete dir="${lib}/com"/>
        <delete dir="${lib}/javax"/>
    </target>

    <target name="createWorker" description="Removes front end components from the server runtime">
        <input message="You are about to delete all the front-end components from the server runtime. Do you really want to proceed?" addproperty="deleteJars" validargs="y,n" defaultvalue="n" />

        <condition property="isApproved">
            <and>
                <isset property="deleteJars" />
                <equals arg1="${deleteJars}" arg2="y" />
            </and>
        </condition>

        <antcall target="deleteUI" />
    </target>


    <!--remove the Frontend related jars from the plugins directory-->
    <target name="deleteUI" if="isApproved">
        <echo>Removing UI related jars located in : ${carbon.home}/repository/components/plugins</echo>
        <delete includeEmptyDirs="true">
            <fileset dir="${carbon.home}/../components/plugins/">
                <include name="org.wso2.carbon.*ui_*" />
                <include name="org.wso2.carbon.*stub_*" />
                <include name="org.wso2.stratos.*ui_*" />
                <include name="org.wso2.stratos.*stub_*" />
                <include name="org.jaggeryjs.*ui_*" />
                <include name="org.jaggeryjs.*stub_*" />
                <include name="org.wso2.carbon.ui.menu.*" />
                <include name="org.wso2.*styles_*" />
                <include name="org.wso2.carbon.authenticator.proxy_*" />
                <exclude name="org.wso2.carbon.cloud.gateway.agent.stub*"/>
            </fileset>
        </delete>
    </target>
    <!--create language bundles in dropins folder-->
    <taskdef uri="antlib:net.sf.antcontrib"
             resource="net/sf/antcontrib/antlib.xml"
             classpath="${carbon.home}/lib/ant-contrib-1.0b3.jar"/>

    <property name="resources.directory" value="${carbon.home}/resources"/>
    <property name="dropins.directory"  value="${carbon.home}/dropins"/>
    <property name="temp.dir"  value="../temp"/>
    <property name="last.dir" value="i18n"/>

    <target name="localize" description="creating fragment bundles">

        <ac:if>
            <available file="${resources.directory}" type="dir" />
            <ac:then>
                <echo message="Directory exists" />


                <ac:for param="file">
                    <path>
                        <dirset dir="${resources.directory}" includes="*"/>
                    </path>
                    <sequential>
                        <ac:var name="basename" unset="true"/>
                        <basename file="@{file}" property="basename"/>
                        <echo message="step1 = ${basename}"/>


                        <ac:var name="fragmentHost.name" unset="true" />
                        <ac:var name="fragmentHost.version" unset="true" />

                        <ac:propertyregex property="fragmentHost.name" input="${basename}" regexp="(.*)_(.*)" select="\1" />
                        <ac:propertyregex property="fragmentHost.version" input="${basename}" regexp="(.*)_(.*)" select="\2" />


                        <ac:var name="ExportPackage" unset="true" />
                        <ac:var name="FragmentHost" unset="true" />
                        <ac:var name="FragmentHostVersion" unset="true" />
                        <ac:var name="BundleSymbolicName" unset="true" />
                        <ac:var name="src.dir" unset="true" />

                        <property name="ExportPackage" value="${fragmentHost.name}"/>
                        <property name="FragmentHost" value="${fragmentHost.name}"/>
                        <property name="FragmentHostVersion" value="${fragmentHost.version}"/>
                        <property name="BundleSymbolicName" value="${fragmentHost.name}.languageBundle_${fragmentHost.version}"/>
                        <property name="src.dir" value="${resources.directory}/${basename}"/>


                        <mkdir dir="${dropins.directory}"/>
                        <mkdir dir="${temp.dir}"/>
                        <ac:propertyregex property="propB" input="${ExportPackage}" regexp="\." replace="/" global="true" override="true" />
                        <echo message="step2 = ${propB}"/>

                        <ac:var name="dest.dir" unset="true" />
                        <property name="dest.dir" value="${temp.dir}\${ExportPackage}\${propB}\${last.dir}"/>
                        <mkdir dir="${dest.dir}"/>
                        <echo message="step3 = ${src.dir}"/>
                        <copy todir="${dest.dir}">
                            <fileset dir="${src.dir}"/>
                        </copy>
                        <echo message="step4"/>

                        <jar destfile="${dropins.directory}/${BundleSymbolicName}.jar" basedir="${temp.dir}\${ExportPackage}">
                            <manifest>
                                <attribute name="Manifest-Version" value="1.0"/>
                                <attribute name="Build-Jdk" value="1.6.0_22"/>
                                <attribute name="Bundle-Description" value="Localized Bundle"/>
                                <attribute name="Bundle-Name" value="i18n Localized Bundle"/>
                                <attribute name="Bundle-SymbolicName" value="${BundleSymbolicName}"/>
                                <attribute name="Bundle-Vendor" value="Apache Software Foundation"/>
                                <attribute name="Bundle-Version" value="${FragmentHostVersion}"/>
                                <attribute name="Carbon-Component" value="UIBundle"/>
                                <attribute name="Export-Package" value="${ExportPackage}"/>
                                <attribute name="Fragment-Host" value="${FragmentHost}"/>
                            </manifest>
                        </jar>

                        <echo message="step5"/>

                    </sequential>
                </ac:for>
                <delete dir="${temp.dir}"/>

            </ac:then>
            <ac:else>
                <fail message="Resource directory does not Exists at CARBON_HOME.Please refer the section 8 of the README.txt file for more details."/>

            </ac:else>
        </ac:if>
    </target>
</project>
