<?xml version="1.0"?>
<project name="Derby install"  default=""  >

	<taskdef name="waitForDerby" classname="org.eclipse.aperi.install.WaitForDerby" />
	<taskdef name="stopDerby" classname="org.eclipse.aperi.install.ShutdownDerby" />
	<taskdef name="startDerby" classname="org.eclipse.aperi.install.StartDerby" />
	<taskdef name="dbInstall" classname="org.eclipse.aperi.install.DatabaseInstall" />
	<taskdef name="installCannedReports" classname="org.eclipse.aperi.install.InstallCannedReports" />
			
	<!-- Configure Derby -->
	<target name="configureDerby" depends="">
		
		<if>
			<equals arg1="${osfamily}" arg2="windows"/>
			<then>
				<property name="extension" value="bat" />
			</then>
			<else>
				<property name="extension" value="sh" />
			</else>
		</if>
		
		<!-- If the schema exists then skip this step -->
		<available file="${basedir}/../db/Aperi" type="dir" property="db.exists" />
		<if>
			<equals arg1="${db.exists}" arg2="true" />
			<then>
				<echo message="The schema already exists: ${basedir}/../db/Aperi" level="info" />
			</then>
			<else>
				<antcall target="configureDerbyFiles" >
					<param name="extension" value="${extension}" />
				</antcall>
				<antcall target="configureDerbyLoadSchema" >
					<param name="extension" value="${extension}" />
				</antcall>
			</else>
		</if>

	</target>

	<!--  Create the configuration files and launch scripts  -->
	<target name="configureDerbyFiles" depends="">

		<echo message="Updating the properties file: ${basedir}/../db/aperi-ij.${extension}" level="info" />
		<copy tofile="${basedir}/../db/aperi-ij.${extension}" file="${basedir}/templates/db/aperi-ij.${extension}" overwrite="true" />
		<replace file="${basedir}/../db/aperi-ij.${extension}" token="@java_home@" value="${java.home}" />
		<replace file="${basedir}/../db/aperi-ij.${extension}" token="@base_dir@" value="${basedir}" />
		<replace file="${basedir}/../db/aperi-ij.${extension}" token="@jdbc_driver@" value="${db.driver}" />
		<replace file="${basedir}/../db/aperi-ij.${extension}" token="@jdbc_url@" value="jdbc:${db.type}://${db.hostname}:${db.port}/${db.schema}" />
		<replace file="${basedir}/../db/aperi-ij.${extension}" token="@user@" value="${db.username}" />
		<replace file="${basedir}/../db/aperi-ij.${extension}" token="@password@" value="${db.password.encrypted}" />
		<chmod perm="ugo+x" file="${basedir}/../db/aperi-ij.${extension}" />

		<echo message="Updating the properties file: ${basedir}/../db/derby.${extension}" level="info" />
		<copy tofile="${basedir}/../db/derby.${extension}" file="${basedir}/templates/db/derby.${extension}"  overwrite="true" />
		<replace file="${basedir}/../db/derby.${extension}" token="@java_home@" value="${java.home}" />
		<replace file="${basedir}/../db/derby.${extension}" token="@base_dir@" value="${basedir}" />
		<replace file="${basedir}/../db/derby.${extension}" token="@max_heap@" value="${db.derby.heap}" />
		<replace file="${basedir}/../db/derby.${extension}" token="@hostname@" value="${db.hostname}" />
		<replace file="${basedir}/../db/derby.${extension}" token="@port@" value="${db.port}" />
		<chmod perm="ugo+x" file="${basedir}/../db/derby.${extension}" />
		
		<echo message="Updating the properties file: ${basedir}/../db/derbystop.${extension}" level="info" />
		<copy tofile="${basedir}/../db/derbystop.${extension}" file="${basedir}/templates/db/derbystop.${extension}" overwrite="true" />
		<replace file="${basedir}/../db/derbystop.${extension}" token="@java_home@" value="${java.home}" />
		<replace file="${basedir}/../db/derbystop.${extension}" token="@base_dir@" value="${basedir}" />
		<replace file="${basedir}/../db/derbystop.${extension}" token="@hostname@" value="${db.hostname}" />
		<replace file="${basedir}/../db/derbystop.${extension}" token="@port@" value="${db.port}" />
		<chmod perm="ugo+x" file="${basedir}/../db/derbystop.${extension}" />
	</target>				

	<!-- Load the schema -->
	<target name="configureDerbyLoadSchema" depends="" >
		
		<echo message="Loading the schema" level="info" />
		<echo message="Starting Derby" level="info" />
		<startDerby hostname="${db.hostname}" port="${db.port}" />
		
		<echo message="Waiting for Derby to start" level="info" />
		<waitForDerby hostname="${db.hostname}" port="${db.port}" timeout="60" property="derbyStarted" />
		<fail unless="derbyStarted" message="Derby failed to start" />
		<dbInstall dbType="derby" driverName="${db.driver}" hostname="${db.hostname}" port="${db.port}" username="${db.username}"
			password="${db.password.encrypted}" schemaPath="${basedir}/../db/schema" />
		<installCannedReports reportRepository="${server.reporting.dir}"
			cannedReportsArchive="${basedir}/../reporting/configuration/canned-reports.zip" dbDriver="${db.driver}"
			jdbcUrl="jdbc:${db.type}://${db.hostname}:${db.port}/${db.schema}" username="${db.username}" password="${db.password.encrypted}" />
		
		<stopDerby hostname="${db.hostname}" port="${db.port}" />
		
        <echo message="The output above should indicate that the schema was created" level="info" />
        <echo message="successfully on your system. If there was an error," level="info" />
        <echo message="Aperi will not function correctly. Consult the installation" level="info" />
        <echo message="documentation for more information." level="info" />
		
	</target>
</project>