<project name="SIPRP" basedir="." default="list">

	<!-- COMMON PROPERTIES -->
	<property name="common.dir" value="${basedir}/common" />
	<property name="common.src.dir" value="${common.dir}/src" />
	<property name="common.lib.dir" value="${common.dir}/lib" />
	<property name="common.lib.tools.dir" value="${basedir}/lib.tools" />

	<!-- TOMCAT MANAGER PROPERTIES -->
	<property name="url" value="http://localhost:8080/manager" />
	<property name="username" value="tomcat" />
	<property name="password" value="tomcat" />

	<!-- APP PROPERTIES -->
	<property name="app.name" value="SIPRP" />
	<property name="app.dir" value="${basedir}/SIPRPSoft" />
	<property name="app.src.dir" value="${app.dir}/src" />
	<property name="app.lib.dir" value="${app.dir}/lib" />
	<property name="app.images.dir" value="${app.dir}/images" />
	<property name="siprp.main" value="siprp.Main" />
	<property name="app.conf.dir" value="${app.dir}/conf" />
	<property name="app.dist.dir" value="${app.dir}/dist" />
	<property name="app.dist.lib.dir" value="${app.dist.dir}/lib" />
	<property name="app.build.dir" value="${app.dir}/build" />
	<property name="app.build.classes.dir" value="${app.build.dir}/classes" />
	<property name="app.main.class" value="siprp.Main" />
	<property name="app.jar.name" value="${app.name}.jar" />
	<property name="app.dependencies.chache.dir" value="${app.dir}/dependenciesCache" />

	<!-- WEB PROPERTIES -->
	<property name="web.name" value="siprpWeb" />
	<property name="web.dir" value="${basedir}/web" />
	<property name="web.src.dir" value="${web.dir}/src" />
	<property name="web.lib.dir" value="${web.dir}/lib" />
	<property name="web.build.dir" value="${web.dir}/build" />
	<property name="web.build.WEB-INF.dir" value="${web.build.dir}/WEB-INF" />
	<property name="web.build.classes.dir" value="${web.build.WEB-INF.dir}/classes" />
	<property name="web.build.contents.dir" value="${web.build.dir}/html" />
	<property name="web.dist.dir" value="${web.dir}/dist" />
	<property name="web.contents.dir" value="${web.dir}/contents" />
	<property name="web.warfile" value="${web.name}.war" />
	<property name="web.WEB-INF.dir" value="${web.dir}/WEB-INF" />
	<property name="web.xmlfile" value="web.xml" />
	<property name="web.dependencies.chache.dir" value="${web.dir}/dependenciesCache" />

	<property name="img.web.name" value="SIPRPImages" />
	<property name="img.web.dir" value="${basedir}/WebImages" />
	<property name="img.web.src.dir" value="${img.web.dir}/src" />
	<property name="img.web.lib.dir" value="${app.lib.dir}" />
	<property name="img.web.build.dir" value="${img.web.dir}/build" />
	<property name="img.web.build.WEB-INF.dir" value="${img.web.build.dir}/WEB-INF" />
	<property name="img.web.build.classes.dir" value="${img.web.build.WEB-INF.dir}/classes" />
	<property name="img.web.dist.dir" value="${img.web.dir}/dist" />
	<property name="img.web.warfile" value="${img.web.name}.war" />
	<property name="img.web.WEB-INF.dir" value="${img.web.dir}/WEB-INF" />
	<property name="img.web.xmlfile" value="web.xml" />
	<property name="img.web.dependencies.chache.dir" value="${img.web.dir}/dependenciesCache" />

	<!-- COMMON CLASSPATHS -->
	<path id="common.classpath">
		<fileset dir="${common.lib.dir}" />
	</path>

	<path id="jpox.enhancer.classpath">
		<fileset dir="${common.lib.dir}">
			<include name="evolute.jar" />
			<include name="log4j-1.2.8.jar" />
		</fileset>
		<fileset dir="${common.lib.tools.dir}">
			<include name="jpox-enhancer-1.0.4.jar" />
			<include name="bcel-5.1.jar" />
			<include name="jdo.jar" />
		</fileset>
	</path>

	<path id="catalina.path">
		<fileset dir="${common.lib.tools.dir}">
			<include name="catalina-ant.jar" />
		</fileset>
	</path>

	<!-- APP CLASSPATHS -->
	<path id="app.jar.classpath">
		<fileset dir="${common.lib.dir}" />
		<fileset dir="${app.lib.dir}" />
	</path>

	<path id="app.build.classpath">
		<path refid="app.jar.classpath" />
		<pathelement location="${app.build.classes.dir}" />
	</path>

	<path id="app.jpox.enhancer.classpath">
		<path refid="jpox.enhancer.classpath" />
		<pathelement location="${app.build.classes.dir}" />
	</path>

	<!-- WEB CLASSPATHS -->
	<path id="web.jar.classpath">
		<fileset dir="${common.lib.dir}" />
		<fileset dir="${web.lib.dir}" />
		<fileset dir="${common.lib.tools.dir}">
			<include name="servlet-api.jar" />
		</fileset>
	</path>

	<path id="img.web.build.classpath">
		<path refid="img.web.jar.classpath" />
		<pathelement location="${img.web.build.classes.dir}" />
	</path>

	<path id="img.web.jar.classpath">
		<fileset dir="${common.lib.dir}" />
		<fileset dir="${img.web.lib.dir}" />
		<fileset dir="${common.lib.tools.dir}">
			<include name="servlet-api.jar" />
		</fileset>
	</path>

	<path id="img.web.build.classpath">
		<path refid="img.web.jar.classpath" />
		<pathelement location="${img.web.build.classes.dir}" />
	</path>


	<!-- TOMCAT TASKS (from catalina-ant.jar) -->
	<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" classpathref="catalina.path" />
	<taskdef name="list" classname="org.apache.catalina.ant.ListTask" classpathref="catalina.path" />
	<taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask" classpathref="catalina.path" />
	<taskdef name="resources" classname="org.apache.catalina.ant.ResourcesTask" classpathref="catalina.path" />
	<taskdef name="roles" classname="org.apache.catalina.ant.RolesTask" classpathref="catalina.path" />
	<taskdef name="start" classname="org.apache.catalina.ant.StartTask" classpathref="catalina.path" />
	<taskdef name="stop" classname="org.apache.catalina.ant.StopTask" classpathref="catalina.path" />
	<taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask" classpathref="catalina.path" />

	<!-- COMMON TARGETS -->
	<target name="list">
		<echo message="***** SIPRP Ant targets ****" />
		<echo message="==   application targets   ==" />
		<echo message="    app-prepare" />
		<echo message="    app-build" />
		<echo message="    app-clean" />
		<echo message="    app-jpox" />
		<echo message="    app-run" />
		<echo message="    app-makeJar" />
		<echo message="== web application targets ==" />
		<echo message="    web-prepare" />
		<echo message="    web-build" />
		<echo message="    web-clean" />
		<echo message="    web-makeWar" />
		<echo message="    web-tomcat-deploy" />
		<echo message="    web-tomcat-reload" />
		<echo message="    web-tomcat-undeploy" />
		<echo message="== == == == == == == == == ==" />
		<echo message="usage example:" />
		<echo message="ant app-run" />
	</target>

	<!-- APP TARGETS -->
	<target name="app-prepare">
		<echo message="Copying non-java files to build directory" />
		<copy todir="${app.build.classes.dir}">
			<fileset dir="${app.src.dir}">
				<exclude name="**/*.java" />
				<exclude name="*.java" />
			</fileset>
		</copy>
		<copy todir="${app.build.classes.dir}">
			<fileset dir="${common.src.dir}">
				<exclude name="**/*.java" />
				<exclude name="*.java" />
			</fileset>
		</copy>
	</target>

	<target name="app-build" description="Compiles all application classes" depends="app-prepare">
		<mkdir dir="${app.build.classes.dir}" />
		<depend srcdir="${app.src.dir}" destdir="${app.build.classes.dir}" cache="${app.dependencies.chache.dir}">
			<include name="**/*.java" />
		</depend>
		<depend srcdir="${common.src.dir}" destdir="${app.build.classes.dir}" cache="${app.dependencies.chache.dir}">
			<include name="**/*.java" />
		</depend>
		<javac srcdir="${common.src.dir}" destdir="${app.build.classes.dir}" classpathref="app.build.classpath" debug="on" deprecation="on" fork="true" memoryMaximumSize="512M" source="1.5" target="1.5" />
		<javac srcdir="${app.src.dir}" destdir="${app.build.classes.dir}" classpathref="app.build.classpath" debug="on" deprecation="on" fork="true" memoryMaximumSize="512M" source="1.5" target="1.5" />
	</target>

	<target name="app-clean" description="Removes all generated files">
		<delete failonerror="false">
			<fileset dir="${app.build.dir}">
				<include name="**/*" />
			</fileset>
		</delete>
		<delete dir="${app.build.dir}" failonerror="false" />
		<delete dir="${app.dist.dir}" failonerror="false" />
	</target>

	<target name="app-run" description="Runs the application" depends="app-build">
		<java classname="${app.main.class}" classpathref="app.build.classpath" fork="true" dir="${app.build.classes.dir}" />
	</target>

	<target name="app-jpox" description="JPOX Enhancer" depends="app-build">
		<taskdef name="jpoxEnhancer" description="JPOX Enhancer" classpathref="app.jpox.enhancer.classpath" classname="org.jpox.enhancer.tools.EnhancerTask" />
		<jpoxEnhancer classpathref="app.jpox.enhancer.classpath" />
	</target>

	<target name="app-makeJar" description="Make application Jar" depends="app-clean, app-jpox">
		<pathconvert property="jar.classpath.converted" refid="app.jar.classpath" pathsep=" ">
			<map from="${app.dir}/" to="" />
			<map from="${common.lib.dir}/" to="lib/" />
			<map from="${basedir}/" to="" />
		</pathconvert>
		<mkdir dir="${app.dist.dir}" />
		<jar destfile="${app.dist.dir}/${app.jar.name}">
			<fileset dir="${app.build.classes.dir}">
				<include name="**/*" />
			</fileset>
			<manifest>
				<attribute name="Main-Class" value="${siprp.main}" />
				<attribute name="Class-Path" value="${jar.classpath.converted}" />
			</manifest>
		</jar>
		<copy todir="${app.dist.lib.dir}">
			<fileset dir="${app.lib.dir}">
				<include name="**/*" />
			</fileset>
			<fileset dir="${common.lib.dir}">
				<include name="**/*" />
			</fileset>
		</copy>
	</target>

	<!-- WEB TARGETS -->
	<target name="web-prepare">
		<mkdir dir="${web.build.classes.dir}" />
		<copy todir="${web.build.classes.dir}">
			<fileset dir="${web.src.dir}">
				<exclude name="**/*.java" />
				<exclude name="*.java" />
			</fileset>
		</copy>
		<mkdir dir="${web.build.contents.dir}" />
		<copy todir="${web.build.contents.dir}">
			<fileset dir="${web.contents.dir}" />
		</copy>
	</target>

	<target name="web-build" description="Compiles all application classes" depends="web-prepare">
		<depend srcdir="${web.src.dir}" destdir="${web.build.classes.dir}" cache="${web.dependencies.chache.dir}">
			<include name="**/*.java" />
		</depend>
		<javac srcdir="${web.src.dir}" destdir="${web.build.classes.dir}" classpathref="web.build.classpath" debug="on" deprecation="on" fork="true" memoryMaximumSize="512M" />
	</target>

	<target name="web-clean" description="Removes all generated files">
		<delete failonerror="false">
			<fileset dir="${web.build.dir}" />
		</delete>
		<delete dir="${web.build.dir}" failonerror="false" />
	</target>

	<target name="web-makeWar" depends="web-build">
		<mkdir dir="${web.dist.dir}" />
		<war destfile="${web.dist.dir}/${web.warfile}" webxml="${web.WEB-INF.dir}/${web.xmlfile}">
			<fileset dir="${web.build.dir}">
				<include name="html/**/*" />
			</fileset>
			<lib dir="${common.lib.dir}" />
			<lib dir="${web.lib.dir}" />
			<classes dir="${web.build.classes.dir}" />
		</war>
	</target>

	<target name="web-tomcat-deploy" description="Install web application" depends="web-makeWar">
		<deploy url="${url}" username="${username}" password="${password}" path="/${web.name}" war="${web.dist.dir}/${web.warfile}" />
	</target>

	<target name="web-tomcat-reload" description="Reload web application">
		<reload url="${url}" username="${username}" password="${password}" path="/${web.name}" />
	</target>

	<target name="web-tomcat-undeploy" description="Remove web application">
		<undeploy url="${url}" username="${username}" password="${password}" path="/${web.name}" />
	</target>

	
	
	<target name="img.web-prepare">
		<mkdir dir="${img.web.build.classes.dir}" />
		<copy todir="${img.web.build.classes.dir}">
			<fileset dir="${img.web.src.dir}">
				<exclude name="**/*.java" />
				<exclude name="*.java" />
			</fileset>
		</copy>
		<copy todir="${img.web.build.classes.dir}">
			<fileset dir="${common.src.dir}">
				<exclude name="**/*.java" />
				<exclude name="*.java" />
			</fileset>
		</copy>
	</target>

	<target name="img.web-build" description="Compiles all application classes" depends="img.web-prepare">
		<depend srcdir="${img.web.src.dir}" destdir="${img.web.build.classes.dir}" cache="${img.web.dependencies.chache.dir}">
			<include name="**/*.java" />
		</depend>
		<depend srcdir="${common.src.dir}" destdir="${img.web.build.classes.dir}" cache="${img.web.dependencies.chache.dir}">
			<include name="**/*.java" />
		</depend>
		<javac srcdir="${common.src.dir}" destdir="${img.web.build.classes.dir}" classpathref="img.web.build.classpath" debug="on" deprecation="on" fork="true" memoryMaximumSize="512M" />
		<javac srcdir="${img.web.src.dir}" destdir="${img.web.build.classes.dir}" classpathref="img.web.build.classpath" debug="on" deprecation="on" fork="true" memoryMaximumSize="512M" />
	</target>

	<target name="img.web-clean" description="Removes all generated files">
		<delete failonerror="false">
			<fileset dir="${img.web.build.dir}" />
		</delete>
		<delete dir="${img.web.build.dir}" failonerror="false" />
	</target>

	<target name="img.web-makeWar" depends="img.web-build">
		<mkdir dir="${img.web.dist.dir}" />
		<war destfile="${img.web.dist.dir}/${img.web.warfile}" webxml="${img.web.WEB-INF.dir}/${web.xmlfile}">
			<fileset dir="${img.web.build.dir}">
				<include name="html/**/*" />
			</fileset>
			<lib dir="${common.lib.dir}" />
			<lib dir="${img.web.lib.dir}" />
			<classes dir="${img.web.build.classes.dir}" />
		</war>
	</target>

	<target name="img.web-tomcat-deploy" description="Install web application" depends="img.web-makeWar">
		<deploy url="${url}" username="${username}" password="${password}" path="/${img.web.name}" war="${img.web.dist.dir}/${img.web.warfile}" />
	</target>

	<target name="img.web-tomcat-reload" description="Reload web application">
		<reload url="${url}" username="${username}" password="${password}" path="/${img.web.name}" />
	</target>

	<target name="img.web-tomcat-undeploy" description="Remove web application">
		<undeploy url="${url}" username="${username}" password="${password}" path="/${img.web.name}" />
	</target>

</project>
