<?xml version="1.0"?>

<project name="sub" basedir="." default="all">

	<!--================================================
         Set the properties related to the source tree
        ================================================-->
	<property file="WebContent/WEB-INF/signjar.properties" />
	<property name="basename" value="sub" />
	<property name="rootpackage" value="com.softnet" />
	<property name="src.dir.client" value="../${basename}-client/src" />
	<property name="src.dir.server" value="src" />
	<property name="lib.dir.client" value="../${basename}-client/lib" />
	<property name="lib.dir.server" value="lib" />
	<property name="lib.dir.webapps" value="WebContent/WEB-INF/lib" />
	<property name="build.dir" value="build" />
	<property name="build.classes" value="${build.dir}/classes" />
	<property name="build.classes.client" value="${build.dir}/classes/client" />
	<property name="build.classes.server" value="${build.dir}/classes/server" />
	<property name="build.jar" value="${build.dir}/jar" />
	<property name="build.jar.dev" value="${build.dir}/jar/dev" />
	<property name="build.jar.client" value="${build.dir}/jar/client" />
	<property name="build.jar.server" value="${build.dir}/jar/server" />
	<property name="dist" value="dist" />
	<property name="launcherClass" value="${rootpackage}.${basename}Application" />

	<path id="client.class.path">
		<fileset dir="${lib.dir.client}">
			<include name="**/*.jar" />
			<include name="**/*.zip" />
		</fileset>
	</path>

	<path id="server.class.path">
		<fileset dir="${lib.dir.webapps}">
			<include name="**/*.jar" />
			<include name="**/*.zip" />
		</fileset>
	</path>

	<path id="development.class.path">
		<fileset dir="${lib.dir.server}">
			<include name="**/*.jar" />
			<include name="**/*.zip" />
		</fileset>
	</path>

	<path id="class.path">
		<path refid="client.class.path" />
		<path refid="server.class.path" />
	</path>

	<target name="init" description="initial tasks">
		<tstamp />
		<mkdir dir="${build.dir}" />
	</target>

	<target name="all" depends="war" description="builds the extension and the sample app" />

	<target name="clean" description="deletes build and dist folders">
		<delete dir="${build.dir}" />
		<delete dir="${dist}" />
	</target>

	<target name="compile-client" depends="clean, init" description="compiles client classes">
		<mkdir dir="${build.classes.client}" />

		<javac destdir="${build.classes.client}" debug="on" deprecation="on">
			<classpath refid="client.class.path" />
			<src path="${src.dir.client}" />
		</javac>
	</target>

	<target name="compile-server" depends="clean, init" description="compiles sample classes">
		<mkdir dir="${build.classes.server}" />

		<javac destdir="${build.classes.server}" debug="on" deprecation="on">
			<classpath refid="server.class.path" />
			<classpath refid="development.class.path" />
			<src path="${src.dir.server}" />
		</javac>
	</target>

	<target name="devrun" depends="jar" description="runs application in DevelopmentRunner">
		<java classname="${launcherClass}" fork="yes">
			<classpath>
				<fileset dir="${build.jar.client}" />
				<fileset dir="${build.jar.server}" />
				<path refid="client.class.path" />
				<path refid="server.class.path" />
				<path refid="development.class.path" />
			</classpath>
		</java>
	</target>

	<target name="jar" depends="compile-client, compile-server" description="builds sample jar files">
		<mkdir dir="${build.jar}" />
		<mkdir dir="${build.jar.dev}" />
		<mkdir dir="${build.jar.client}" />
		<mkdir dir="${build.jar.server}" />

		<jar jarfile="${build.jar.client}/${basename}-client.jar">
			<fileset dir="${build.classes.client}" />
			<fileset dir="${src.dir.client}">
				<exclude name="**/*.java" />
			</fileset>
		</jar>

		<jar jarfile="${build.jar.server}/${basename}-server.jar">
			<fileset dir="${build.classes.server}" />
			<fileset dir="${src.dir.server}">
				<exclude name="**/*.java" />
			</fileset>

		</jar>
	</target>

	<target name="war" depends="jar" description="creates war file of sample application">

		<!-- collect the client libs -->
		<mkdir dir="${dist}/client" />
		<copy todir="${dist}/client" flatten="true">
			<fileset dir="${build.jar.client}" />
		</copy>

		<!-- sign the client jars if necessary -->
		<antcall target="sign.jars" />

		<!-- collect the server libs -->
		<mkdir dir="${dist}/server" />
		<copy todir="${dist}/server" flatten="true">
			<fileset dir="${build.jar.server}" />
		</copy>

		<!-- collect the web content -->
		<mkdir dir="${dist}/web" />
		<copy todir="${dist}/web">
			<fileset dir="WebContent" />
		</copy>

		<war warfile="${dist}/${basename}.war" webxml="${dist}/web/WEB-INF/web.xml">
			<manifest>
				<attribute name="${basename}-Version" value="${basename.version}" />
			</manifest>
			<fileset file="${dist}/web/**">
				<exclude name="web.xml" />
			</fileset>
			<lib dir="${dist}/client" />
			<lib dir="${dist}/server" />
		</war>

		<delete dir="${dist}/client" />
		<delete dir="${dist}/server" />
	</target>

	<target name="sign.jars" if="signjar.keystore" description="sign client jar files if required" depends="readPass">
		
		<!-- if keypass is not define use storepass -->
		<property name="signjar.keypass" value="${signjar.storepass}" />
		<signjar alias="${signjar.alias}" keystore="WebContent/WEB-INF/${signjar.keystore}" storepass="${signjar.storepass}" keypass="${signjar.keypass}" lazy="false">
			<fileset dir="${dist}/client" includes="**/*.jar" />
		</signjar>
	</target>
	
	<target name="readPass" unless="signjar.storepass">
		<input message="Please enter the keystore's password:" addproperty="signjar.storepass" />
		<input message="Please enter the key's password:" addproperty="signjar.keypass" defaultvalue="${signjar.storepass}" />
	</target>
</project>