<project name="create-db" default="create" basedir=".">
	<property file="build.properties" />
	<path id="lib.path">
		<fileset dir="../lib">
			<include name="**/*.jar" />
		</fileset>
	</path>
	<taskdef resource="net/sf/antcontrib/antlib.xml">
		<classpath>
			<fileset dir="../lib">
				<include name="**/*.jar" />
			</fileset>
		</classpath>
	</taskdef>

	<target name="create" depends="init,create-database,load-functions,create-schema,load-data,load-others">
	</target>
	<target name="init">
		<echo message="============================================================" />
		<echo message="     PostgreSQL database create tool for Adempiere ERP" />
		<echo message="	  	Adempiere Licese is GNU GPL License" />
		<echo message="============================================================" />
		<echo message="" file="log/psql.log" append="false" />
		<echo append="false" file="log/build.log" message="#Build log:${line.separator}${line.separator}" />
	</target>
	<target name="create-database">
		<ant antfile="build-ddl.xml" target="createDb" />
	</target>
	<target name="create-schema">
		<echo message="----- Creating tables -----" />
		<ant antfile="build-ddl.xml" target="writeSchemaSqlToFile" />
		<echo file="log/psql.log" append="true" message="${line.separator}#Create Tables:${line.separator}${line.separator}" />
		<antcall target="load">
			<param name="file.name" value="${basedir}/adempiere-schema.sql" />
		</antcall>
	</target>
	<target name="load-data">
		<echo message="----- Loading Adempiere Seed data -----" />
		<ant antfile="build-ddl.xml" target="writeDataToDb" />
	</target>

        	<!-- ==================================================== -->
	<!-- Init                                                 -->
	<!-- ==================================================== -->
	<target name="setupInit" >
		<!-- Environment variables -->
		<condition property="isWindows">
			<os family="windows" />
		</condition>
	</target>

	<target name="load-functions">
		<echo file="log/psql.log" append="true" message="${line.separator}#Create Functions [1st Pass]:${line.separator}${line.separator}" />
		<foreach param="file.name" target="load">
			<path>
				<fileset dir="functions">
					<include name="**" />
				</fileset>
			</path>
		</foreach>

		<echo file="log/psql.log" append="true" message="${line.separator}#Create Functions [2nd Pass]:${line.separator}${line.separator}" />
		<foreach param="file.name" target="load">
			<path>
				<fileset dir="functions">
					<include name="**" />
				</fileset>
			</path>
		</foreach>
	</target>

	<target name="load-others">
		<echo message="----- Loading other database objects -----" />
		<echo file="log/psql.log" append="true" message="${line.separator}#Create Views [1st Pass]:${line.separator}${line.separator}" />
		<foreach param="file.name" target="load">
			<path>
				<fileset dir="views">
					<include name="**" />
				</fileset>
			</path>
		</foreach>

		<echo file="log/psql.log" append="true" message="${line.separator}#Create Views [2nd Pass]:${line.separator}${line.separator}" />
		<foreach param="file.name" target="load">
			<path>
				<fileset dir="views">
					<include name="**" />
				</fileset>
			</path>
		</foreach>

		<echo file="log/psql.log" append="true" message="${line.separator}#Create Sequences:${line.separator}${line.separator}" />
		<foreach param="file.name" target="load">
			<path>
				<fileset dir="../sequences">
					<include name="**" />
				</fileset>
			</path>
		</foreach>

		<ant antfile="build-ddl.xml" target="writeFkSqlToFile" />
		<echo file="log/psql.log" append="true" message="${line.separator}#Create Foreign Keys:${line.separator}${line.separator}" />
		<antcall target="load">
			<param name="file.name" value="${basedir}/adempiere-fk.sql" />
		</antcall>
		<antcall target="load">
			<param name="file.name" value="${basedir}/install_procedures.sql" />
		</antcall>
	</target>

	<target name="load">
		<echo message="Loading file ${file.name}" />
		<exec dir="${postgresql.home}/bin" executable="${postgresql.home}/bin/psql" resultproperty="psql.result" output="log/psql.log" append="true">
			<arg line="-d ${postgresql.database} -q" />
			<arg line="-U ${postgresql.user}" />
			<arg line="-f ${file.name}" />
			<env key="PGPASSWORD" value="${postgresql.password}" />
		</exec>
		<echo message="File ${file.name} status ${psql.result}" />
		<echo append="true" file="log/build.log">File ${file.name} status ${psql.result}</echo>
		<echo append="true" file="log/build.log" message="${line.separator}" />
	</target>

</project>