<?xml version="1.0" encoding="UTF-8"?>

<!-- $This file is distributed under the terms of the license in LICENSE$ -->

<!-- ====================================================================== 
     Build script for the ORNG Shindig webapp. 
     
     This can be used on its own, or invoked from the "orng" target of the main build file.
     ====================================================================== -->

<project name="ORNG-shindig" default="describe">

	<!-- ================================= 
          target: describe              
         ================================= -->
	<target name="describe" description="--> Describe the targets (this is the default).">
		<echo>
all      - Runs "clean", then "deploy".
clean    - Delete all artifacts so the next build will be from scratch.
deploy   - Configure the application and deploy directly into the Tomcat webapps directory.
	    </echo>
	</target>

	<!-- ================================= 
          target: all              
         ================================= -->
	<target name="all" depends="clean, deploy" description="--> Run 'clean', then 'deploy'" />

	<!-- - - - - - - - - - - - - - - - - - 
          target: properties                      
         - - - - - - - - - - - - - - - - - -->
	<target name="properties">
		<property name="build.properties.file" location="../webapp/config/build.properties" />

		<fail message="You must create a &quot;${build.properties.file}&quot; file.">
			<condition>
				<not>
					<available file="${build.properties.file}" />
				</not>
			</condition>
		</fail>
		
		<property file="${build.properties.file}" />

		<fail unless="tomcat.home" message="${build.properties.file} must contain a value for tomcat.home" />
		<fail unless="vitro.home" message="${build.properties.file} must contain a value for vitro.home" />
		<fail unless="webapp.name" message="${build.properties.file} must contain a value for webapp.name" />

		<property name="runtime.properties.file" location="${vitro.home}/runtime.properties" />

		<fail message="The runtime properties file &quot;${runtime.properties.file}&quot; does not exist."> 
			<condition>
				<not>
					<available file="${runtime.properties.file}" />
				</not>
			</condition>
		</fail>
		
		<property file="${runtime.properties.file}" />

		<fail unless="VitroConnection.DataSource.url" message="${runtime.properties.file} must contain a value for VitroConnection.DataSource.url" />
		<fail unless="VitroConnection.DataSource.username" message="${runtime.properties.file} must contain a value for VitroConnection.DataSource.username" />
		<fail unless="VitroConnection.DataSource.password" message="${runtime.properties.file} must contain a value for VitroConnection.DataSource.password" />
		<fail unless="VitroConnection.DataSource.driver" message="${runtime.properties.file} must contain a value for VitroConnection.DataSource.driver" />
		<fail unless="OpenSocial.shindigURL" message="${runtime.properties.file} must contain a value for OpenSocial.shindigURL" />
		<fail unless="OpenSocial.tokenService" message="${runtime.properties.file} must contain a value for OpenSocial.tokenService" />
		<fail unless="OpenSocial.tokenKeyFile" message="${runtime.properties.file} must contain a value for OpenSocial.tokenKeyFile" />

		<fail message="The Vitro home directory &quot;${vitro.home}&quot; does not exist.">
			<condition>
				<not>
					<available file="${vitro.home}" />
				</not>
			</condition>
		</fail>

		<fail message="The Vitro home directory &quot;${vitro.home}&quot; is not writable.">
			<condition>
				<not>
					<isfileselected file="${vitro.home}">
						<writable />
					</isfileselected>
				</not>
			</condition>
		</fail>

		<fail message="The Shindig token key file  &quot;${OpenSocial.tokenKeyFile}&quot; does not exist.">
			<condition>
				<not>
					<available file="${OpenSocial.tokenKeyFile}" />
				</not>
			</condition>
		</fail>

		<!-- build directories -->
		<property name="build.dir" location="./build" />
		<property name="build.shindig.dir" location="${build.dir}/shindig" />

		<!-- deploy directories -->
		<property name="shindig.home.dir" location="${vitro.home}/shindig" />
		<property name="shindig.config.dir" location="${shindig.home.dir}/conf" />
		<property name="tomcat.webapps.dir" location="${tomcat.home}/webapps" />

		<!-- the Shindig WAR -->
		<property name="shindig.war.original.file" location="./shindigorng.war" />
		<property name="shindig.war.deployed.file" location="${tomcat.webapps.dir}/shindigorng.war" />
		<property name="shindig.war.deployed.dir" location="${tomcat.webapps.dir}/shindigorng" />

		<!-- Shindig properties file -->
		<property name="shindig.properties.template.file" location="${basedir}/shindigorng.properties.template" />
		<property name="shindig.properties.modified.file" location="${build.shindig.dir}/shindigorng.properties" />
		<property name="shindig.properties.deployed.file" location="${shindig.config.dir}/shindigorng.properties" />
		
		<!-- sample-gadgets webapp -->
		<property name="sample.webapp.original.dir" location="./sample-gadgets" />
		<property name="sample.webapp.deployed.dir" location="${tomcat.webapps.dir}/sample-gadgets" />
		

	</target>

	<!-- ================================= 
          target: clean              
         ================================= -->
	<target name="clean" depends="properties" description="--> Delete all artifacts">
		<delete includeemptydirs="true" failonerror="false">
			<fileset dir="${build.shindig.dir}" />
			<fileset file="${shindig.war.deployed.file}" />
			<fileset dir="${shindig.war.deployed.dir}" />
			<fileset dir="${sample.webapp.deployed.dir}" />
		</delete>
	</target>

	<!-- - - - - - - - - - - - - - - - - - 
          target: prepare                      
         - - - - - - - - - - - - - - - - - -->
	<target name="prepare" depends="properties">
		<mkdir dir="${build.dir}" />
		<mkdir dir="${build.shindig.dir}" />

		<mkdir dir="${shindig.home.dir}" />
		<mkdir dir="${shindig.config.dir}" />
	</target>

	<!-- - - - - - - - - - - - - - - - - - 
          target: modifyPropertiesFile                      
         - - - - - - - - - - - - - - - - - -->
	<target name="modifyPropertiesFile" depends="properties, prepare">
		<copy file="${shindig.properties.template.file}" toFile="${shindig.properties.modified.file}">
			<filterset>
				<filter token="TOKEN_KEY_FILE" value="${OpenSocial.tokenKeyFile}" />
				<filter token="DATA_SOURCE_URL" value="${VitroConnection.DataSource.url}" />
				<filter token="DATA_SOURCE_USERNAME" value="${VitroConnection.DataSource.username}" />
				<filter token="DATA_SOURCE_PASSWORD" value="${VitroConnection.DataSource.password}" />
				<filter token="DATA_SOURCE_DRIVER" value="${VitroConnection.DataSource.driver}" />
				<filter token="WEBAPP_NAME" value="${webapp.name}" />
			</filterset>
		</copy>
	</target>

	<!-- ================================= 
          target: deploy              
         ================================= -->
	<target name="deploy" depends="modifyPropertiesFile" description="--> Deploy the application directly into the Tomcat webapps directory.">
		<copy file="${shindig.properties.modified.file}" tofile="${shindig.properties.deployed.file}" />
		<copy file="${shindig.war.original.file}" tofile="${shindig.war.deployed.file}" overwrite="true" />
		<copy todir="${sample.webapp.deployed.dir}" overwrite="true" >
			<fileset dir = "${sample.webapp.original.dir}" />
		</copy>
	</target>

</project>