<project default="build" basedir=".">
	<target name="build" depends="init.build,build.project,package.project" />

	<target name="install" depends="init.install,build.project" />

	<target name="clean" depends="init.properties">
		<delete dir="bin" />
		<delete dir="dist" />
	</target>

	<!-- property determination  -->
	<target name="init.properties">
		<property file="build.properties" />

		<property name="wolips.properties" value="${user.home}${file.separator}Library${file.separator}Application Support${file.separator}WOLips${file.separator}wolips.properties" />
		<property file="${wolips.properties}" />
		<condition property="wo.properties.check.failed">
			<not>
				<and>
					<isset property="wo.system.frameworks" />
					<isset property="wo.local.frameworks" />
				</and>
			</not>
		</condition>
		<fail message="The properties 'wo.system.frameworks' and 'wo.local.frameworks' must be set. Check that your ${wolips.properties} is correct." if="wo.properties.check.failed" />

		<property name="wo.install.root" value="${wo.local.frameworks}" />
		<property name="build.app.name" value="${project.name}" />
		<property name="build.app.name.lowercase" value="${project.name.lowercase}" />
		<property name="build.root" value="${wo.user.root}" />
		
		<condition property="isFramework">
			<equals arg1="${project.type}" arg2="framework" casesensitive="no" trim="yes" />
		</condition>
		<condition property="isApplication">
			<not><isset property="isFramework" /></not>
		</condition>
	</target>

	<!-- basic initializations  -->
	<target name="init.install" depends="init.install.framework,init.install.application" />
	
	<target name="init.install.framework" if="isFramework" depends="init.properties">
		<tstamp />
		<property name="dest.dir" value="${wo.install.root}" />
	</target>

	<target name="init.install.application" if="isApplication" depends="init.properties">
		<tstamp />
		<property name="dest.dir" value="${wo.apps.root}" />
	</target>

	<target name="init.woproject" depends="init.properties">
		<path id="woproject.lib.path">
			<fileset dir="${build.root}" includes="lib/woproject.jar" />
		</path>
		<taskdef name="wocompile" classname="org.objectstyle.woproject.ant.WOCompile" classpathref="woproject.lib.path" />
		<taskdef name="woframework" classname="org.objectstyle.woproject.ant.WOFramework" classpathref="woproject.lib.path" />
		<taskdef name="woapplication" classname="org.objectstyle.woproject.ant.WOApplication" classpathref="woproject.lib.path" />
		<taskdef name="wounit" classname="org.objectstyle.woproject.ant.WOUnit" classpathref="woproject.lib.path" />
		<typedef name="wopath" classname="org.objectstyle.woproject.ant.WOPath" classpathref="woproject.lib.path" />
	</target>
	
	<target name="init.build" depends="init.properties,init.woproject">
		<tstamp />
		<property name="dest.dir" value="dist" />
	</target>

	<target name="package.project" depends="package.framework, package.application" />
	
	<target name="package.framework" if="isFramework">
		<tar destfile="${dest.dir}/${project.name}.tar.gz" compression="gzip" longfile="gnu">
			<tarfileset dir="${dest.dir}">
				<include name="${project.name}.framework/**" />
			</tarfileset>
		</tar>
	</target>

	<target name="package.application" if="isApplication" unless="servletDeployment">
		<tar destfile="${dest.dir}/${build.app.name}-Application.tar.gz" compression="gzip" longfile="gnu">
			<tarfileset dir="${dest.dir}" mode="750">
				<include name="${build.app.name}.woa/${build.app.name}" />
			</tarfileset>
			<tarfileset dir="${dest.dir}">
				<exclude name="${build.app.name}.woa/${build.app.name}" />
				<include name="${build.app.name}.woa/**" />
			</tarfileset>
		</tar>
		<tar destfile="${dest.dir}/${build.app.name}-WebServerResources.tar.gz" compression="gzip" basedir="${wsdest.dir}/WebObjects" longfile="gnu" includes="${build.app.name}.woa/**">
		</tar>
	</target>

	<!-- woproject tasks -->
	<target name="build.project" depends="build.framework,build.application" />

	<target name="build.application" if="isApplication" depends="init.build,build.woapp,javaclient,ssdd,war,split" />

	<target name="build.framework" if="isFramework" depends="compile">
		<woframework name="${project.name}" destDir="${dest.dir}" customInfoPListContent="${customInfoPListContent}" principalClass="${principalClass}" eoAdaptorClassName="${eoAdaptorClassName}" javaVersion="${javaVersion}" cfbundleversion="${cfBundleVersion}" cfbundleshortversion="${cfBundleShortVersion}" cfbundleID="${cfBundleID}">
			<classes dir="${classes.dir}">
				<includesfile name="woproject/classes.include.patternset" />
				<excludesfile name="woproject/classes.exclude.patternset" />
			</classes>

			<wsresources dir=".">
				<includesfile name="woproject/wsresources.include.patternset" />
				<excludesfile name="woproject/wsresources.exclude.patternset" />
			</wsresources>

			<resources dir=".">
				<includesfile name="woproject/resources.include.patternset" />
				<excludesfile name="woproject/resources.exclude.patternset" />
			</resources>

			<lib dir="Libraries">
				<include name="*.jar" />
			</lib>
		</woframework>
	</target>

	<target name="compile" depends="init.build">
		<mkdir dir="bin" />
		<wocompile srcdir="Sources" destdir="bin" source="1.5" target="1.5" debug="true">
			<frameworks root="ProjectLocal" embed="false" eclipse="true" />
			<frameworks root="External" embed="false" eclipse="true" />
			<frameworks root="Local" embed="false" eclipse="true" />
			<frameworks root="User" embed="false" eclipse="true" />
			<frameworks root="System" embed="false" eclipse="true" />
			<frameworks root="Network" embed="false" eclipse="true" />
			<classpath>
				<fileset dir="Libraries">
					<include name="*.jar" />
				</fileset>
				<fileset dir="${wo.extensions}">
					<include name="*.jar" />
				</fileset>
			</classpath>
		</wocompile>
	</target>

	<target name="install.application" if="isApplication" depends="init.install.application,build.woapp,javaclient,ssdd,war,split,package.application" />

	<!-- This copies webserver resources from app.woa's embedded frameworks into split install bundle -->
	<target name="split" if="isApplication" description="Adds embedded framework webserver resources to the split install bundle by copying from deployment build bundle" unless="servletDeployment">
		<property name="wsdest.dir" value="${dest.dir}/Split" />
		<mkdir dir="${wsdest.dir}" />
		<copy todir="${wsdest.dir}/WebObjects/${build.app.name}.woa" verbose="true">
			<fileset dir="${dest.dir}/${build.app.name}.woa/Contents">
				<include name="Frameworks/*.framework/WebServerResources/**" />
			</fileset>
			<fileset dir="${dest.dir}/${build.app.name}.woa">
				<include name="Contents/WebServerResources/**" />
			</fileset>
		</copy>
	</target>



	<!-- woproject tasks -->
	<target name="build.woapp" if="isApplication" depends="compile">
	  <woapplication name="${build.app.name}" frameworksBaseURL="/WebObjects/${build.app.name}.woa/Frameworks" destDir="${dest.dir}" customInfoPListContent="${customInfoPListContent}" principalClass="${principalClass}" webXML="${webXML}" webXML_CustomContent="${webXML_CustomContent}" servletAdaptor="er.extensions.jspservlet.ERXServletAdaptor">
			<classes dir="${classes.dir}">
				<includesfile name="woproject/classes.include.patternset" />
				<excludesfile name="woproject/classes.exclude.patternset" />
			</classes>

			<wsresources dir=".">
				<includesfile name="woproject/wsresources.include.patternset" />
				<excludesfile name="woproject/wsresources.exclude.patternset" />
			</wsresources>

			<resources dir=".">
				<includesfile name="woproject/resources.include.patternset" />
				<excludesfile name="woproject/resources.exclude.patternset" />
			</resources>

			<frameworks root="ProjectLocal" embed="${embed.ProjectLocal}" eclipse="true" />
			<frameworks root="External" embed="${embed.External}" eclipse="true" />
			<frameworks root="Local" embed="${embed.Local}" eclipse="true" />
			<frameworks root="User" embed="${embed.User}" eclipse="true" />
			<frameworks root="System" embed="${embed.System}" eclipse="true" />
			<frameworks root="Network" embed="${embed.Network}" eclipse="true" />

			<lib dir="Libraries">
				<include name="*.jar" />
			</lib>

			<otherclasspath dir="${wo.extensions}">
				<include name="*.jar" />
			</otherclasspath>
		</woapplication>
	</target>

	<target name="javaclient" if="javaClient" depends="build.woapp">
		<mkdir dir="${dest.dir}/${build.app.name}.woa/Contents/WebServerResources/Java" />
		<jar basedir="${classes.dir}" includes="**/client/**/*.class,**/common/**/*.class" jarfile="${dest.dir}/${build.app.name}.woa/Contents/WebServerResources/Java/${build.app.name}.jar" />

		<!-- project user.d2wmodel file (D2JC only) -->
		<copy file="user.d2wmodel" tofile="${dest.dir}/${build.app.name}.woa/Contents/Resources/user.d2wmodel" />
	</target>

	<target name="war" if="servletDeployment" depends="ssdd">
		<war destfile="${dest.dir}/${build.app.name}.war" webxml="${dest.dir}/${build.app.name}/WEB-INF/web.xml">
			<fileset dir="${dest.dir}/${build.app.name}">
				<include name="**" />
			</fileset>
		</war>
	</target>

	<!-- To use this target
        1) create the LICENSE in your project directory
        2) add JavaWOJSPServlet in your WOFrameworks build path
        3) set servlet deployment to true in WOLips Properties
    -->
	<target name="ssdd" if="servletDeployment" depends="build.woapp">
		<mkdir dir="${dest.dir}/${build.app.name}/WEB-INF/classes" />
		<mkdir dir="${dest.dir}/${build.app.name}/WEB-INF/tlds" />
		<copy todir="${dest.dir}/${build.app.name}/WEB-INF/">
			<fileset dir="${dest.dir}/${build.app.name}.woa/Contents/">
				<include name="web.xml" />
			</fileset>
		</copy>
		<copy todir="${dest.dir}/${build.app.name}/WEB-INF/">
			<fileset dir="..">
				<include name="LICENSE" />
			</fileset>
		</copy>
		<copy todir="${dest.dir}/${build.app.name}/WEB-INF/">
			<fileset dir="${dest.dir}">
				<include name="${build.app.name}.woa/**" />
			</fileset>
		</copy>
		<!-- copy the frameworks to the WEBINFROOT/Library directory -->
		<copy todir="${dest.dir}/${build.app.name}/WEB-INF/">
			<fileset dir="${dest.dir}/${build.app.name}.woa/Contents">
				<include name="Library/**" />
			</fileset>
		</copy>
		<copy todir="${dest.dir}/${build.app.name}/WEB-INF/">
			<fileset dir="${dest.dir}/${build.app.name}.woa">
				<include name="Resources/**" />
			</fileset>
		</copy>
		<copy todir="${dest.dir}/${build.app.name}/WEB-INF/lib/">
			<fileset dir="${dest.dir}/${build.app.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}/${build.app.name}/WEB-INF/lib" file="${wo.system.frameworks}/JavaWOJSPServlet.framework/WebServerResources/Java/JavaWOJSPServlet_client.jar" />

		<copy todir="${dest.dir}/${build.app.name}/WEB-INF/tlds">
			<fileset dir="${wo.system.frameworks}/JavaWOJSPServlet.framework/Resources/">
				<include name="WOtaglib_1_0.tld" />
			</fileset>
		</copy>

		<!-- the WebObject Extensions -->
		<copy todir="${dest.dir}/${build.app.name}/WEB-INF/lib">
			<fileset dir="${wo.extensions}">
				<include name="*.jar" />
				<exclude name="servlet.jar" />
			</fileset>
			<mapper type="flatten" />
		</copy>

		<!-- the Java Client Client-Side Classes      -->
		<copy todir="${dest.dir}/${build.app.name}/WEB-INF/${build.app.name}.woa/Contents/WebServerResources/Java">
			<fileset dir="${wo.local.frameworks}">
				<include name="**/WebServerResources/Java/*.jar" />
			</fileset>
			<mapper type="flatten" />
		</copy>

		<!-- fix the Macos*ClassPath.txt files  -->
		<replaceregexp file="${dest.dir}/${build.app.name}/WEB-INF/${build.app.name}.woa/Contents/MacOS/MacOSClassPath.txt" match="APPROOT/Resources/Java/${build.app.name.lowercase}.jar" replace="APPROOT/${build.app.name}.woa/Contents/Resources/Java/${build.app.name.lowercase}.jar" byline="true" />
		<replaceregexp file="${dest.dir}/${build.app.name}/WEB-INF/${build.app.name}.woa/Contents/MacOS/MacOSXServerClassPath.txt" match="APPROOT/Resources/Java/${build.app.name.lowercase}.jar" replace="APPROOT/${build.app.name}.woa/Contents/Resources/Java/${build.app.name.lowercase}.jar" byline="true" />

		<!-- fix the web.xml file:  the app itself needs build.app.name/Contents -->
		<replaceregexp file="${dest.dir}/${build.app.name}/WEB-INF/web.xml" match="WEBINFROOT/Resources/Java/${build.app.name.lowercase}.jar" replace="WEBINFROOT/${build.app.name}.woa/Contents/Resources/Java/${build.app.name.lowercase}.jar" byline="true" />

		<!-- fix the web.xml file to remove the extra Frameworks/ directory level for the frameworks -->
		<replaceregexp file="${dest.dir}/${build.app.name}/WEB-INF/web.xml" match="WEBINFROOT/Frameworks//" replace="WEBINFROOT/" byline="true" />
	</target>

</project>
