<?xml version="1.0"?>

<project name="Installer" default="install">
	<property file="./build.properties" />

	<target name="install" description="Install the current version of the application" depends="-core_config_exists,database_check">
		<clearcache />
		<dbinstall application="${application}" />
		<clearcache />
	</target>

	<target name="upgrade" description="Upgrade the database schema to the latest version" depends="-core_config_exists,database_check">
		<clearcache />
		<readversion application="${application}" from="db" property="prev_version" />
		<filescleanup application="${application}" after="${prev_version}" />
		<dbupdate application="${application}" />
		<clearcache />
	</target>

	<target name="write_config" description="Create config file" depends="sanity_check">
		<if>
			<or>
				<not><isset property="install.data_dir"/></not>
				<not><isset property="install.db.type"/></not>
				<not><isset property="install.db.host"/></not>
				<not><isset property="install.db.name"/></not>
				<not><isset property="install.db.user"/></not>
				<not><isset property="install.db.pass"/></not>
			</or>
			<then>
				<fail msg="Some installation properties are not defined, can't create config file" />
			</then>
		</if>
		<if>
			<!--suppress PhingDomInspection -->
			<available file="${core_config_file}" type="file" />
			<then>
				<fail msg="Config file already exists" />
			</then>
		</if>

		<php function="addslashes" returnProperty="install.data_dir_escaped">
			<param value="${install.data_dir}"/>
		</php>
		<php function="addslashes" returnProperty="install.local_data_dir_escaped">
			<param value="${install.local_data_dir}"/>
		</php>
		<copy file="web/intranet/common/config.php-dist" tofile="${core_config_file}">
			<filterchain>
				<replacetokens begintoken="@@" endtoken="@@">
					<token key="db_type" value="${install.db.type}" />
					<token key="db_name" value="${install.db.name}" />
					<token key="db_host" value="${install.db.host}" />
					<token key="db_user" value="${install.db.user}" />
					<token key="db_password" value="${install.db.pass}" />

					<token key="data_dir" value="${install.data_dir_escaped}" />
					<token key="local_data_dir" value="${install.local_data_dir_escaped}" />

					<token key="system_name" value="${install.system_name}" />
					<token key="system_email" value="${install.system_email}" />
				</replacetokens>
			</filterchain>
		</copy>
	</target>

	<target name="prompt_make_config" depends="sanity_check">
		<promptconfig then="write_config" /> <!-- calls 'write_config' task -->
	</target>


	<target name="-core_config_exists" depends="sanity_check" hidden="true">
		<if>
			<not>
				<!--suppress PhingDomInspection -->
				<available file="${core_config_file}" type="file" />
			</not>
			<then>
				<fail msg="No config file exists" />
			</then>
		</if>
	</target>

	<target name="database_check" depends="-core_config_exists">
		<if>
			<not><isset property="force" /></not>
			<then>
				<databasecheck />
			</then>
			<else>
				<echo>Skipping database checks because -Dforce was given</echo>
			</else>
		</if>
		
	</target>

	<target name="sanity_check" hidden="true">
		<if>
			<not><isset property="application" /></not>
			<then>
				<fail msg="Application name is not given" />
			</then>
		</if>

		<if>
			<not><isset property="core_config_file" /></not>
			<then>
				<fail msg="Property 'core_config_file' is not defined" />
			</then>
		</if>

	</target>

	<taskdef name="promptconfig" classname="web.intranet.setup.classes.Phing.PromptConfigTask" />
	<taskdef name="databasecheck" classname="web.intranet.setup.classes.Phing.DatabaseCheckTask" />
	<taskdef name="readversion" classname="web.intranet.setup.classes.Phing.ReadVersionTask" />
	<taskdef name="filescleanup" classname="web.intranet.setup.classes.Phing.DoCleanupTask" />
	<taskdef name="dbinstall" classname="web.intranet.setup.classes.Phing.DbInstallTask" />
	<taskdef name="dbupdate" classname="web.intranet.setup.classes.Phing.DbUpdateTask" />
	<taskdef name="clearcache" classname="web.intranet.setup.classes.Phing.ClearCacheTask" />
</project>
