<?xml version="1.0" encoding="UTF-8"?>
<project name="create_schemas" default="manage.schemas" basedir="../../">
	<property name="resources.dir" value="${basedir}/${relative.resources.dir}"></property>
	
	<property name="target.dir" value="${basedir}/target"></property>
	
	<!--
		This script triggers the running of the process for creating schemas
	-->	
	
	
	<target name="manage.schemas" depends="check, init, manage.all.schemas, manage.single.schemas">
		<echo>Ran create schemas</echo>
		
	</target>
	
	
	<target name="check">
		
		<echo>Perform check</echo>
		<condition property="all">
				<not>
					<isset property="single"/>
				</not>
		</condition>
		
		<available file="${datasourceloc}" property="external.datasource.dir"/>

	</target>

	
	<target name="init"
		depends="check.external.ds.dir, set.local.ds.dir">
		
		<mkdir dir="${target.dir}"/>

	</target>
	
	
	<target name="check.external.ds.dir"
		if="external.datasource.dir">
		
		<property name="ds.loc" value="${datasourceloc}"></property>

	</target>
	
	<target name="set.local.ds.dir"
		unless="external.datasource.dir">
	
		<property name="ds.loc" value="${resources.dir}/datasources/"></property>
		
	</target>
		
	<target name="manage.all.schemas" if="all">
		<echo>Creating all schemas from datasource dir ${ds.loc}</echo>
		<!--
		This will process each directory located under the refid location. 
		Its expects the following:
			- each directory represents one datasource.
			- connection.properties file must exist in the directory to be used 
		-->
 		<subant genericantfile="${resources.dir}/ddl/run_ddl.xml" inheritall="true"  >
 			<dirset dir="${ds.loc}" />
 			<property name="sql.ddl.dir" value="${resources.dir}/ddl"/>
			<property name="lib.dir" value="${basedir}/lib"/>

		</subant>
		
		<echo>Completed creating all schemas</echo>
	</target>


	<target name="manage.single.schemas" if="single">
		<echo>Creating single schema ${ds.loc}/${single}</echo>
		
		<available file="${ds.loc}/${single}" property="dir.exist"/>
		<fail unless="dir.exist" message="The datasource single was not found at ${ds.loc}/${single}" />


		<ant inheritAll="true" antfile="${resources.dir}/ddl/run_ddl.xml">
			<property name="basedir" value="${ds.loc}/${single}"/>
			<property name="sql.ddl.dir" value="${resources.dir}/ddl"/>
			<property name="lib.dir" value="${basedir}/lib"/>


		</ant>

		<echo>Completed creating single schema ${single}</echo>

	</target>
	
</project>	