<project name="evo-app-deploy-common" > 
		
	<!-- PROPERTIES -->
	<property name="properties.key.name" value="name" />
	<property name="properties.key.major" value="major" />
	<property name="properties.key.minor" value="minor" />
	<property name="properties.key.build" value="build" />
	
	<!-- SETUP -->
	<target name="prepare-deploy-location">
		<sshexec trust="yes" host="${app.deploy.server}" username="${deploy.user}" command="sh ${deploy.prepare.script} ${app.name}" password="${deploy.password}" />
	</target>
	
	<target name="run-jnlp-task" >
		<typedef classpathref="lib.tools.classpath" resource="net/jtools/classloadertask/antlib.xml">
			<classpath>
				<path refid="lib.tools.classpath"/>
			</classpath>
		</typedef>
		<classloader loader="project">
			<classpath>
				<fileset dir="${lib.tools.dir}" includes="*.jar"/>
			</classpath>
		</classloader>
		<taskdef name="generate-jnlp" classpathref="lib.tools.classpath" classname="com.evolute.ant.JNLPGeneratorTask" />
		<generate-jnlp propertiesDir="${properties.dir}" basedir="${app.dir}" />
	</target>
	
	<!-- DEPLOY -->
	
	<target name="load-properties" />
	
	<target name="pre-deploy" />
	
	<target name="app-deploy-version" if="deploy.version" >
		<antcall target="app-deploy-create-jar"/>
		<antcall target="pre-deploy" />
		<antcall target="run-jnlp-task" />
		<input defaultvalue="${basedir}" addproperty="deploy.jars">
			<handler classpathref="lib.tools.classpath" classname="com.evolute.ant.JarInputHandler"  />
		</input>
		<condition property="do.abort">
			<equals arg1="${deploy.jars}" arg2="null"/>
		</condition>
		<fail if="do.abort">Deploy cancelled</fail>
		<antcall target="app-deploy-version-jars"/>
	</target>
	
	<target name="app-version-increase-test">
		<property name="TESTDEPLOY" value="yes"/>
		<antcall target="app-version-increase-build"/>
	</target>
	
	<target name="app-version-increase-major">
		<propertyfile file="${app.version.file}" comment="${app.version.comment}" >
			<entry key="${properties.key.name}" value="${app.name}"/>
			<entry key="${properties.key.major}" type="int" operation="+" value="1"/>
			<entry key="${properties.key.minor}" type="int" operation="=" value="0"/>
			<entry key="${properties.key.build}" type="int" operation="=" value="0"/>
		</propertyfile>
		<antcall target="app-deploy-version"/>
	</target>
	
	<target name="app-version-increase-minor">
		<propertyfile file="${app.version.file}" comment="${app.version.comment}" >
			<entry key="${properties.key.name}" value="${app.name}"/>
			<entry key="${properties.key.major}" type="int" operation="+" value="0"/>
			<entry key="${properties.key.minor}" type="int" operation="+" value="1"/>
			<entry key="${properties.key.build}" type="int" operation="=" value="0"/>
		</propertyfile>
		<antcall target="app-deploy-version"/>
	</target>
	
	<target name="app-version-increase-build">
		<propertyfile file="${app.version.file}" comment="${app.version.comment}" >
			<entry key="${properties.key.name}" value="${app.name}"/>
			<entry key="${properties.key.major}" type="int" operation="+" value="0"/>
			<entry key="${properties.key.minor}" type="int" operation="+" value="0"/>
			<entry key="${properties.key.build}" type="int" operation="+" value="1"/>
		</propertyfile>
		<antcall target="app-deploy-version"/>
	</target>
	
	<target name="deploy-test" if="TESTDEPLOY">
		<sshexec trust="yes" host="${app.deploy.server}" username="${deploy.user}" command="sh ${app.deploy.test.script} ${app.name}" password="${deploy.password}" />
	</target>
	
	<target name="deploy-production" unless="TESTDEPLOY">
		<sshexec trust="yes" host="${app.deploy.server}" username="${deploy.user}" command="sh ${app.deploy.production.script} ${app.name}" password="${deploy.password}" />
	</target>
	
	<target name="app-deploy-version-jars-user-pass" if="deploy.password">
		<antcall target="prepare-deploy-location" />
		<echo message="Uploading files: ${deploy.jars}"/>
		<scp verbose="yes" todir="${deploy.user}@${app.deploy.server}:${app.deploy.path}" password="${deploy.password}" sftp="true" trust="true" >
			<fileset dir="${app.deploy.dist.dir}" includes="${deploy.jars}"/>
		</scp>
		<scp verbose="yes" todir="${deploy.user}@${app.deploy.server}:${app.deploy.jnlp.path}" password="${deploy.password}" sftp="true" trust="true" >
			<fileset dir="${jnlp.dir}" includes="**/*.jnlp"/>
		</scp>
		<antcall target="deploy-test"/>
		<antcall target="deploy-production"/>
	</target>
	
	<target name="app-deploy-version-jars-user" if="deploy.user">
		<input message="password:>" addproperty="deploy.password">
			<handler classpathref="lib.tools.classpath" classname="com.evolute.ant.PasswordInputHandler"  />
		</input>
		<antcall target="app-deploy-version-jars-user-pass"/>
	</target>
	
	<target name="app-deploy-version-jars" if="deploy.jars" >
		<input message="" addproperty="deploy.user">
			<handler classpathref="lib.tools.classpath" classname="com.evolute.ant.UserInputHandler"  />
		</input>
		<antcall target="app-deploy-version-jars-user" />
	</target>
	
	<target name="app-deploy-choose-version" if="deploy.version" >
		<antcall target="app-version-increase-${deploy.version}" />
	</target>
	
	<target name="app-deploy" description="Deploys application" depends="load-properties">
		<input defaultvalue="" addproperty="deploy.version">
			<handler classpathref="lib.tools.classpath" classname="com.evolute.ant.VersionInputHandler"  />
		</input>
		<antcall target="app-deploy-choose-version"/>
		<antcall target="app-create-software-evo-licensor"/>
	</target>
	
	<target name="app-create-software-evo-licensor" depends="load-properties">
		<property file="${app.version.file}" prefix="version" />
		<typedef classpathref="lib.tools.classpath" resource="net/jtools/classloadertask/antlib.xml">
			<classpath>
				<path refid="lib.tools.classpath"/>
			</classpath>
		</typedef>
		<classloader loader="project">
			<classpath>
				<fileset dir="${lib.tools.dir}" includes="*.jar"/>
				<fileset dir="${app.lib.dir}" includes="*.jar"/>
			</classpath>
		</classloader>
		<taskdef name="createTask" classpathref="lib.tools.classpath" classname="com.evolute.ant.CreateSoftwareAntTask" />

		<createTask projectVersion="${version.major}.${version.minor}.${version.build}" projectName="${app.name}" />
	</target>
</project>