<?xml version="1.0" encoding="UTF-8"?>
<project name="Zabbix" default="build" basedir=".">
	<target name="init-common">
		<!-- Delete build directories from the previous run -->
		<delete dir="build/logs" failonerror="off"/>
		<!-- Create the different build directories -->
		<mkdir dir="build/logs"/>
	</target>

	<target name="create-configs" depends="init-common">
		<property name="url" value="${URL}"/>
		<property name="dbtype" value="${DBTYPE}"/>
		<property name="dbhost" value="${DBHOST}"/>
		<property name="dbname" value="${DBNAME}"/>
		<property name="dbuser" value="${DBUSER}"/>
		<property name="dbpassword" value="${DBPASSWORD}"/>

		<!-- Create bootstrap.php -->
		<copy overwrite="true" file="frontends/php/tests/bootstrap.php.template" tofile="frontends/php/tests/bootstrap.php"/>
		<replace file="frontends/php/tests/bootstrap.php" token="{URL}" value="${url}"/>
		<replace file="frontends/php/tests/bootstrap.php" token="{BASEDIR}" value="${basedir}"/>

		<!-- Create PHP config file -->
		<copy overwrite="true" file="tests/templates/zabbix.conf.php" tofile="frontends/php/conf/zabbix.conf.php"/>
		<replace file="frontends/php/conf/zabbix.conf.php" token="{DBTYPE}" value="${dbtype}"/>
		<replace file="frontends/php/conf/zabbix.conf.php" token="{DBHOST}" value="${dbhost}"/>
		<replace file="frontends/php/conf/zabbix.conf.php" token="{DBNAME}" value="${dbname}"/>
		<replace file="frontends/php/conf/zabbix.conf.php" token="{DBUSER}" value="${dbuser}"/>
		<replace file="frontends/php/conf/zabbix.conf.php" token="{DBPASSWORD}" value="${dbpassword}"/>
	</target>

	<target name="make-dbschema">
		<!-- Create database schema -->
		<exec executable="./bootstrap.sh" failonerror="on"></exec>
		<exec executable="./configure" failonerror="on"></exec>
		<exec executable="make" failonerror="on">
			<arg line="-s"/>
			<arg line="dbschema"/>
		</exec>
	</target>

	<target name="init-postgresql" depends="create-configs,make-dbschema">
		<!-- Create PostgreSQL database -->
		<exec executable="psql" failonerror="on">
			<env key="PGPASSWORD" value="${dbpassword}"/>
			<arg line="-h '${dbhost}'"/>
			<arg line="-U '${dbuser}'"/>
			<arg line="-1"/>
			<arg line="--set ON_ERROR_STOP=1"/>
			<arg line="-c 'drop database if exists ${dbname}'"/>
		</exec>
		<exec executable="psql" failonerror="on">
			<env key="PGPASSWORD" value="${dbpassword}"/>
			<arg line="-h '${dbhost}'"/>
			<arg line="-U '${dbuser}'"/>
			<arg line="-1"/>
			<arg line="--set ON_ERROR_STOP=1"/>
			<arg line="-c 'create database ${dbname}'"/>
		</exec>
		<exec executable="psql" input="database/postgresql/schema.sql" failonerror="on">
			<env key="PGPASSWORD" value="${dbpassword}"/>
			<arg line="-h '${dbhost}'"/>
			<arg line="-U '${dbuser}'"/>
			<arg line="-1"/>
			<arg line="--set ON_ERROR_STOP=1"/>
			<arg line="${dbname}"/>
		</exec>
		<exec executable="psql" input="database/postgresql/images.sql" failonerror="on">
			<env key="PGPASSWORD" value="${dbpassword}"/>
			<arg line="-h '${dbhost}'"/>
			<arg line="-U '${dbuser}'"/>
			<arg line="-1"/>
			<arg line="--set ON_ERROR_STOP=1"/>
			<arg line="${dbname}"/>
		</exec>
		<exec executable="psql" input="database/postgresql/data.sql" failonerror="on">
			<env key="PGPASSWORD" value="${dbpassword}"/>
			<arg line="-h '${dbhost}'"/>
			<arg line="-U '${dbuser}'"/>
			<arg line="-1"/>
			<arg line="--set ON_ERROR_STOP=1"/>
			<arg line="${dbname}"/>
		</exec>
		<exec executable="psql" input="tests/data/data_test.sql" failonerror="on">
			<env key="PGPASSWORD" value="${dbpassword}"/>
			<arg line="-h '${dbhost}'"/>
			<arg line="-U '${dbuser}'"/>
			<arg line="-1"/>
			<arg line="--set ON_ERROR_STOP=1"/>
			<arg line="${dbname}"/>
		</exec>
	</target>

	<target name="init-mysql" depends="create-configs,make-dbschema">
		<!-- Create MySQL database -->
		<exec executable="mysql" failonerror="on">
			<arg line="--host=${dbhost}"/>
			<arg line="--user=${dbuser}"/>
			<arg line="--password=${dbpassword}"/>
			<arg line="--execute='drop database if exists ${dbname}'"/>
		</exec>
		<exec executable="mysql" failonerror="on">
			<arg line="--host=${dbhost}"/>
			<arg line="--user=${dbuser}"/>
			<arg line="--password=${dbpassword}"/>
			<arg line="--execute='create database ${dbname} character set utf8 collate utf8_bin'"/>
		</exec>
		<exec executable="mysql" input="database/mysql/schema.sql" failonerror="on">
			<arg line="--host=${dbhost}"/>
			<arg line="--user=${dbuser}"/>
			<arg line="--password=${dbpassword}"/>
			<arg line="${dbname}"/>
		</exec>
		<exec executable="mysql" input="database/mysql/images.sql" failonerror="on">
			<arg line="--host=${dbhost}"/>
			<arg line="--user=${dbuser}"/>
			<arg line="--password=${dbpassword}"/>
			<arg line="${dbname}"/>
		</exec>
		<exec executable="mysql" input="database/mysql/data.sql" failonerror="on">
			<arg line="--host=${dbhost}"/>
			<arg line="--user=${dbuser}"/>
			<arg line="--password=${dbpassword}"/>
			<arg line="${dbname}"/>
		</exec>
		<exec executable="mysql" input="tests/data/data_test.sql" failonerror="on">
			<arg line="--host=${dbhost}"/>
			<arg line="--user=${dbuser}"/>
			<arg line="--password=${dbpassword}"/>
			<arg line="${dbname}"/>
		</exec>
	</target>

	<target name="test-phpunit" depends="init-common">
		<exec executable="phpunit" dir="frontends/php/tests/unit" failonerror="off">
			<arg line="--log-junit=${basedir}/build/logs/phpunit.xml"/>
			<arg line="include/"/>
		</exec>
	</target>

	<target name="test-frontend">
		<exec executable="phpunit" dir="frontends/php/tests" failonerror="off">
			<arg line="--log-junit=${basedir}/build/logs/phpunit.xml --bootstrap=bootstrap.php frontend.php"/>
		</exec>
	</target>

	<target name="build-phpunit" depends="test-phpunit"/>
	<target name="build-mysql" depends="init-mysql,test-frontend"/>
	<target name="build-postgresql" depends="init-postgresql,test-frontend"/>

	<!-- Default target -->
	<target name="build" depends="build-phpunit,build-mysql,build-postgresql"/>
</project>
