<project name="Workbench" default="build" basedir=".">
    <!-- KK: WARNING: The embedded webserver bundle may not be generated correctly with WOLips versions
            earlier than 4118 -->
    <!-- KK: This build script is based on standard WOLips script that comes with WOApplication template
            Changes and additions are commented. Use diff tool to compare this to standard WOLips build.xml if u like -->

    <!-- main targets  -->
    <!-- add optional targets to depends list if needed -->
    <!-- KK: additional depends targets added: record.build.info, ws.split.embed -->
    <target name="build" depends="setProps,init.build,build.woapp,ws.split.embed,ssdd,war"/>

  <target name="compileAndBuild" depends="setProps,init.build,compile,build.woapp,ws.split.embed,ssdd,war" />

    <target name="install" depends="setProps,init.install,build.woapp,ws.split.embed,ssdd,war"/>

    <target name="clean" depends="setProps">
          <delete dir="dist"/>
    </target>

    <!-- property determination  -->
    <target name="setProps">
        <property file="${user.home}${file.separator}build.properties"/>
        <property file="build.properties"/>
        <property file="${user.home}${file.separator}Library${file.separator}wobuild.properties"/>
        <condition property="wo.properties.check.failed">
            <not>
                <and>
                      <isset property="wo.wosystemroot"/>
                      <isset property="wo.wolocalroot"/>
                </and>
            </not>
          </condition>
          <fail message="Could not find ${user.home}${file.separator}Library${file.separator}wobuild.properties." if="wo.properties.check.failed"/>
        <property name="install.dir" value="${wo.wolocalroot}/Library/WebObjects/Applications"/>

        <!-- KK: the next property supports webserver split install bundle installation location -->
        <property name="wsinstall.dir" value="${wo.wolocalroot}/Library/WebServer/Documents"/>

        <!-- KK: Defaults property 'build.app.name' to be 'project.name' if not already defined explicitly -->
        <condition property="build.app.name" value="${build.app.name}" else="${project.name}">
            <and>
                <isset property="build.app.name"/>
            </and>
        </condition>

        <!-- KK: This next line logs all ant properties to console - useful for troubleshooting build failures -->
        <echoproperties />
    </target>

    <!-- basic initializations  -->
    <target name="init.install">
         <tstamp/>
        <property name="dest.dir" value="${install.dir}"/>
        <!-- KK: Sets the webserver document root as destination -->
        <property name="wsdest.dir" value="${wsinstall.dir}"/>
    </target>

    <target name="init.build">
         <tstamp/>
        <property name="dest.dir" value="dist"/>
        <!-- KK: Since the bundle names are the same for app and ws split bundle, put ws bundle in sub-dir -->
        <property name="wsdest.dir" value="dist/ws_root"/>
    </target>

    <!-- This copies webserver resources from app.woa's embedded frameworks into split install bundle -->
    <target name="ws.split.embed"
            depends="setProps,build.woapp"
            description="Adds embedded framework webserver resources to the split install bundle by copying from deployment build bundle">
        <copy todir="${wsdest.dir}/WebObjects/${build.app.name}.woa"
                verbose="true">
            <fileset dir="${dest.dir}/${build.app.name}.woa/Contents/Library">
                <include name="Frameworks/*.framework/WebServerResources/**"/>
            </fileset>
        </copy>
        <!-- create a tar archive of the webserver bundle -->
        <tar destfile="${wsdest.dir}/WebObjects/${build.app.name}-webserver-bundle.tar.gz"
            compression="gzip"
            basedir="${wsdest.dir}/WebObjects"
            longfile="gnu"
            includes="${build.app.name}.woa/**">
        </tar>
    </target>

    <!-- woproject tasks -->
    <target name="build.woapp" depends="setProps,init.build,compile">

        <taskdef name="woapplication" classname="org.objectstyle.woproject.ant.WOApplication">
        </taskdef>

        <!-- add webXML="true" in build.properties file to generate a web.xml file -->
        <!-- KK: added wsDestDir and frameworksBaseURL properties here -->
        <!-- using build.app.name instead of project name -->
        <!-- KK: About frameworksBaseURL: This is not documented, but basically it adds this info as a
            launch argument inside the application executable script resulting in ALL webserver
            resource URLs pointing to our embedded webserver resource frameworks.
            Consequently, we MUST embed ALL frameworks including Apple's frameworks, not
            just the 3rd party ones in wo.localroot if we are to use this feature -->
        <woapplication name="${build.app.name}"  stdFrameworks="false"
                                             destDir="${dest.dir}"
                                             customInfoPListContent="${customInfoPListContent}"
                                             principalClass="${principalClass}"
                                             webXML="${webXML}"
                                             webXML_CustomContent="${webXML_CustomContent}"
                                             wsDestDir="${wsdest.dir}"
                                             frameworksBaseURL="/WebObjects/${build.app.name}.woa/Frameworks">
               <classes dir="${classes.dir}">
                  <patternset>
                      <includesfile name="woproject/classes.include.patternset"/>
                    <excludesfile name="woproject/classes.exclude.patternset"/>
                </patternset>
               </classes>
               <wsresources dir=".">
                  <patternset>
                      <includesfile name="woproject/wsresources.include.patternset"/>
                    <excludesfile name="woproject/wsresources.exclude.patternset"/>
                </patternset>
            </wsresources>
               <resources dir=".">
                  <patternset>
                      <includesfile name="woproject/resources.include.patternset"/>
                    <excludesfile name="woproject/resources.exclude.patternset"/>
                </patternset>
               </resources>
               <!-- KK: Changed embed to true -->
               <frameworks root="${wo.wolocalroot}" embed="true">
                  <patternset>
                      <includesfile name="woproject/ant.frameworks.wo.wolocalroot"/>
                </patternset>
               </frameworks>
               <!-- KK: Changed embed to true -->
            <frameworks root="${wo.wosystemroot}" embed="true">
                  <patternset>
                      <includesfile name="woproject/ant.frameworks.wo.wosystemroot"/>
                </patternset>
               </frameworks>
               <!-- KK: Changed embed to true -->
            <frameworks root="${user.home}" embed="true">
                  <patternset>
                      <includesfile name="woproject/ant.frameworks.user.home"/>
                </patternset>
               </frameworks>
            <otherclasspath root="${wo.wosystemroot}">
                   <patternset>
                      <includesfile name="woproject/ant.classpaths.wo.wosystemroot"/>
                </patternset>
            </otherclasspath>
            <lib dir="Libraries">
                <include name="*.jar"/>
                <exclude name="**/*.woa/**"/>
            </lib>
           </woapplication>

        <!-- create a tar archive of the application bundle -->
        <tar destfile="${dest.dir}/${build.app.name}-app-bundle.tar.gz"
            compression="gzip"
            basedir="${dest.dir}"
            longfile="gnu"
            includes="${build.app.name}.woa/**">
        </tar>
    </target>

    <!-- optional targets -->

    <!-- war target-->
    <!-- To use this target remove the if="${never}" statement and create the LICENSE in your project
         and create the web.xml file with the woapplication task.-->
    <target name="war" if="${never}"  depends="ssdd">
        <war destfile="${dest.dir}/${project.name}.war" webxml="${dest.dir}/${project.name}/WEB-INF/web.xml" basedir="${dest.dir}/${project.name}">
            <lib dir="${wo.wosystemroot}/Library/Frameworks/JavaWOJSPServlet.framework/WebServerResources/Java/">
                <include name="JavaWOJSPServlet_client.jar"/>
            </lib>
        </war>
        <copy todir="/tmp/${project.name}.dst/Library/WebObjects/Applications/">
            <fileset dir="${dest.dir}">
                <include name="${project.name}.woa/**"/>
            </fileset>
        </copy>
    </target>

    <!-- ssdd target-->
    <!-- To use this target
        1)  remove the if="${never}" statement
        2)  change embed=true  in the woapp target above
        3)  create the LICENSE in your project directory
        4)  add JavaWOJSPServlet in your WOFrameworks build path
        5)  Edit the build.properties file
        6)     set webXML = true
    -->
    <target name="ssdd" if="${never}" depends="build.woapp">
        <mkdir dir="${dest.dir}/${project.name}/WEB-INF/classes"/>
        <mkdir dir="${dest.dir}/${project.name}/WEB-INF/tlds"/>
        <copy todir="${dest.dir}/${project.name}/WEB-INF/">
            <fileset dir="${dest.dir}/${project.name}.woa/Contents/">
                <include name="web.xml"/>
            </fileset>
        </copy>
        <copy todir="${dest.dir}/${project.name}/WEB-INF/">
            <fileset dir=".">
                <include name="LICENSE"/>
            </fileset>
        </copy>
        <copy todir="${dest.dir}/${project.name}/WEB-INF/">
            <fileset dir="${dest.dir}">
                <include name="${project.name}.woa/**"/>
            </fileset>
        </copy>
        <!-- copy the frameworks to the WEBINFROOT/Library directory -->
        <copy todir="${dest.dir}/${project.name}/WEB-INF/">
            <fileset dir="${dest.dir}/${project.name}.woa/Contents">
                <include name="Library/**"/>
            </fileset>
        </copy>
        <copy todir="${dest.dir}/${project.name}/WEB-INF/">
            <fileset dir="${dest.dir}/${project.name}.woa">
                <include name="Resources/**"/>
            </fileset>
        </copy>
        <copy todir="${dest.dir}/${project.name}/WEB-INF/lib/">
            <fileset dir="${dest.dir}/${project.name}/WEB-INF">
                <include name="**/Resources/**/*.jar"/>
            </fileset>
             <mapper type="flatten"/>
        </copy>

        <!--  Get the necessary Frameworks from the webobjects system root instead of the project wrapper -->
        <copy todir="${dest.dir}/${project.name}/WEB-INF/lib"
            file="${wo.wosystemroot}/Library/Frameworks/JavaWOJSPServlet.framework/WebServerResources/Java/JavaWOJSPServlet_client.jar" />

        <copy todir="${dest.dir}/${project.name}/WEB-INF/tlds">
            <fileset dir="${wo.wosystemroot}/Library/Frameworks/JavaWOJSPServlet.framework/Resources/">
                <include name="WOtaglib_1_0.tld"/>
            </fileset>
        </copy>
        <!-- the WebObject Extensions -->
        <copy todir="${dest.dir}/${project.name}/WEB-INF/lib">
            <fileset dir="/Library/WebObjects/Extensions/">
                <include name="*.jar"/>
                <exclude name="servlet.jar"/>
            </fileset>
             <mapper type="flatten"/>
        </copy>


        <!-- fix the Macos*ClassPath.txt files  -->

         <replaceregexp file="${dest.dir}/${project.name}/WEB-INF/${project.name}.woa/Contents/MacOS/MacOSClassPath.txt"
            match="APPROOT/Resources/Java/${project.name.lowercase}.jar"
            replace="APPROOT/${project.name}.woa/Contents/Resources/Java/${project.name.lowercase}.jar" byline="true" />

        <replaceregexp file="${dest.dir}/${project.name}/WEB-INF/${project.name}.woa/Contents/MacOS/MacOSXServerClassPath.txt"
            match="APPROOT/Resources/Java/${project.name.lowercase}.jar"
            replace="APPROOT/${project.name}.woa/Contents/Resources/Java/${project.name.lowercase}.jar" byline="true" />

        <!-- fix the web.xml file:  the app itself needs project.name/Contents -->

        <replaceregexp file="${dest.dir}/${project.name}/WEB-INF/web.xml" match="WEBINFROOT/Resources/Java/${project.name.lowercase}.jar"
            replace="WEBINFROOT/${project.name}.woa/Contents/Resources/Java/${project.name.lowercase}.jar" byline="true" />

        <!-- fix the web.xml file to remove the extra Frameworks/ directory level for the frameworks -->

        <replaceregexp file="${dest.dir}/${project.name}/WEB-INF/web.xml" match="WEBINFROOT/Frameworks//"
            replace="WEBINFROOT/" byline="true" />

     </target>

    <!-- copy strings files -->
    <target name="copy.strings">
            <copy todir="${dest.dir}/${project.name}.woa/Contents/Resources">
                   <fileset dir="." casesensitive="yes">
                <include name="*.strings"/>
                    <include name="**/*.strings"/>
                </fileset>
                 <mapper type="flatten"/>
             </copy>
     </target>

  <target name = "compile" depends = "setProps,init.build" >
    <mkdir dir = "bin"/>
    <javac srcdir = "Sources" destdir = "bin">
      <classpath>
        <fileset dir="${wo.dir.user.home.library.frameworks}" includesfile = "woproject/ant.frameworks.user.home">
          <include name = "**/*.jar"/>
        </fileset>
        <fileset dir="${wo.wolocalroot}" includesfile = "woproject/ant.frameworks.wo.wolocalroot">
          <include name = "**/*.jar"/>
        </fileset>
        <fileset dir="${wo.wosystemroot}" includesfile = "woproject/ant.frameworks.wo.wosystemroot">
          <include name = "**/*.jar"/>
        </fileset>
      </classpath>
    </javac>
  </target>
</project>
