<project default="main" basedir=".">
	<!-- import -->
	<import file="${basedir}/deploy-common/deploy-init.xml" />
	<import file="${basedir}/deploy-common/deploy-setup.xml" />
	<import file="${basedir}/deploy-common/plugin-methods.xml" />
	
	<!-- public static final -->
    <path id="ant.classpath">
        <fileset dir="${basedir}/lib/ant">
            <include name="*.jar" />
        </fileset>
    </path>
	<property name="esb.directory" value="${basedir}/esb" />
	
	<target name="main" depends="init, setup">
		<!-- deploy esb {optionally: data, web} -->
		<ant antfile="${basedir}/deploy-common/deploy-esb.xml" />
		<!-- deploy esb configuration -->
		<ant antfile="${basedir}/deploy-common/deploy-esb-configuration.xml" />
		<!-- optionally: deploy gfesuite -->
		<if>
			<equals arg1="${deploy.gfesuite}" arg2="true" />
			<then>
				<ant antfile="${basedir}/deploy-common/deploy-gfesuite.xml" />
			</then>
		</if>
		<!-- deploy edex plugins -->
		<!-- which feature do we want to deploy? -->
		<if>
			<isset property="wa.enabled" />
			<then>
				<!--
					Find the feature associated with
					Work Assignments.
				-->
				<var name="feature.to.find" 
					value="${wa.edex.feature}" />
			</then>
			<else>
				<!--
					Find the standard AWIPS II
					Baseline feature.
				-->
				<var name="feature.to.find"
					value="${edex.feature}" />
			</else>
		</if>
		
		<var name="feature" value="" />
		<!-- 
			find the feature file: we may deploy multiple features
			if multiple Work Assignment directories have been
			specified. 
		-->
 		<for list="${basedirectories}" param="directory"
 		delimiter=";">
 			<sequential>
 				<if>
 					<available file="@{directory}/${feature.to.find}" />
 					<then>
 						<var name="feature"
 							value="@{directory}/${feature.to.find}" />
 						<deploy.feature 
 							feature="${feature}" />
 					</then>
 				</if>
 			</sequential>
 		</for>
		
		<!-- optionally: deploy raytheon python -->
		<if>
			<equals arg1="${deploy.python}" arg2="true" />
			<then>
				<antcall target="deploy.python" />
			</then>
		</if>
		<!-- optionally: deploy site localization -->
		<if>
			<not>
				<equals arg1="${localization.sites}" arg2="" />
			</not>
			<then>
				<antcall target="deploy.localization" />
			</then>
		</if>

		<!-- 
			Search for WA-specific Non-Spring
			Web Applications to deploy.
		-->
		<if>
			<isset property="wa.enabled" />
			<then>
				<for list="${wa.to.deploy}" param="wa"
				delimiter="${path.separator}">
					<sequential>
						<if>
							<available file="@{wa}/edexOsgi/build.edex/wa-build.xml"
								type="file" />
							<then>
		        				<ant 
		        					antfile="@{wa}/edexOsgi/build.edex/wa-build.xml" 
		        					inheritall="true" inheritrefs="true"/>
							</then>
						</if>
					</sequential>
				</for>
		    </then>
		</if>
	</target>
	
	<!-- public -->
	<!-- Targets specific to the developer deployment. -->
	<macrodef name="deploy.feature">
		<attribute name="feature" />
		
		<sequential>
			<!-- prepare to run includegen -->
			<var name="includes.directory" value="${basedir}/tmp/includes" />
			<if>
				<available file="${includes.directory}" type="dir" />
				<then>
					<delete verbose="true" includeemptydirs="true">
						<fileset dir="${includes.directory}" 
							includes="*/**" />
					</delete>
				</then>
			</if>
			<mkdir dir="${includes.directory}" />
		
			<!-- run includegen -->
        	<echo message="Generating deployment list for feature: ${feature}" />
			<!--
				TODO: need to update the includegen ant task to recognize
				plugin architecture restrictions. Plugins that should only
				be deployed for a 64-bit system should not be deployed for
				a 32-bit system, etc.
			-->
        	<includegen providerfilter="${includegen.filter}" 
            	basedirectories="${basedirectories}" 
            	featurefile="${feature}" 
            	cotsout="${includes.directory}/cots.includes" 
            	plugsout="${includes.directory}/plugins.includes" 
            	coreout="${includes.directory}/core.includes" />
		
			<var name="destination.directory"
				value="${edex.root.directory}/lib/plugins" />
			<mkdir dir="${destination.directory}" />
			<processPlugins
				includes.file="${includes.directory}/plugins.includes"
				plugin.type="plugins"
				plugin.directories="${basedirectories}"
				destination.directory="${destination.directory}" />
			<processPlugins
				includes.file="${includes.directory}/core.includes"
				plugin.type="core"
				plugin.directories="${basedirectories}"
				destination.directory="${destination.directory}" />
		
			<var name="destination.directory"
				value="${edex.root.directory}/lib/dependencies" />
			<mkdir dir="${destination.directory}" />
			<processPlugins
				includes.file="${includes.directory}/cots.includes"
				plugin.type="cots"
				plugin.directories="${basedirectories}"
				destination.directory="${destination.directory}" />
		
			<!-- cleanup the temporary directories -->
			<if>
				<available file="${basedir}/tmp"
					type="dir" />
				<then>
					<delete includeemptydirs="true">
						<fileset dir="${basedir}"
							includes="tmp/**" />
					</delete>
				</then>
			</if>
		</sequential>
	</macrodef>
	
	<target name="deploy.python">
		<property name="site-package.directory" 
			value="${python.root.directory}/lib/${python.version.dir}/site-packages" />
		
		<echo message="Deploying the Raytheon python site-packages." />
		<deploy.python.site-package
			site.package="ufpy" />
		<deploy.python.site-package
			site.package="dynamicserialize" />
		<deploy.python.site-package
			site.package="pypies" />
	</target>
	
	<target name="deploy.localization">
	 	<for list="${localization.sites}" param="site"
	 	delimiter="${path.separator}">
	 		<sequential>
	 			<echo message="Deploying localization for site: @{site}" />
	 			
				<!-- determine which directory the localization
				     project should be in. -->
	 			<var name="localization.path" value="" />
				<if>
	 				<available file="${awips.baseline.directory}/localization.@{site}"
	 					type="dir" />
					<then>
						<var name="localization.path"
							value="${awips.baseline.directory}/localization.@{site}" />
					</then>
					<else>
						<var name="localization.path"
	 						value="${awips.baseline.directory}/${localization.directory}/localization.@{site}" />
					</else>
				</if>
	 			
	 			<!-- copy the localization files. -->
				<copy todir="${edex.root.directory}/data/utility"
				overwrite="true">
					<fileset dir="${localization.path}/utility"
						includes="*/**" />
	 			</copy>
	 		</sequential>
	 	</for>
	</target>
	
	<!-- private -->
	<macrodef name="deploy.python.site-package">
		<attribute name="site.package" />
		
		<sequential>
			<echo message="Deploying @{site.package} ..." />
			<if>
				<available file="${site-package.directory}/@{site.package}"
					type="dir" />
				<then>
					<delete dir="${site-package.directory}/@{site.package}" 
					includeemptydirs="true">
						<fileset dir="${site-package.directory}" 
							includes="@{site.package}/**" />
					</delete>
				</then>
			</if>
			
			<if>
				<equals arg1="@{site.package}" arg2="pypies" />
				<then>
					<!-- custom rule for pypies due to alternate directory layout -->
					<copy todir="${site-package.directory}">
						<fileset dir="${awips.baseline.directory}/pythonPackages/pypies"
							includes="@{site.package}/**" />
					</copy>
				</then>
				<else>
					<copy todir="${site-package.directory}">
						<fileset dir="${awips.baseline.directory}/pythonPackages"
							includes="@{site.package}/**" />
					</copy>
				</else>
			</if>
		</sequential>
	</macrodef>
	
	<!-- static -->
    <taskdef name="includegen" 
    	classname="com.raytheon.uf.anttasks.includesgen.GenerateIncludesFromFeature"
    	classpathref="ant.classpath" />	
	<taskdef resource="net/sf/antcontrib/antlib.xml" 
		classpath="${basedir}/lib/ant/ant-contrib-1.0b3.jar" />
</project>