<?xml version="1.0"?>

<project name="Claromentis" default="info">
	<property name="build_files_path" value="web/intranet/setup" />
	<property name="core_config_file" value="web/intranet/common/config.php" />

	<if>
		<not><isset property="app"/></not>
		<then>
			<property name="app" value="core" />
		</then>
	</if>
	<property name="application" value="${app}" />


	<target name="info" description="Show help">
		<echo>

Install framework or a module:
  phing install
  phing -Dapp=innovate install

Upgrade framework or a module (run latest db updates):
  phing upgrade
  phing -Dapp=innovate upgrade

Run database permission checks:
  phing check
  phing check -debug

For developers' options help, type
  phing devhelp
		</echo>
	</target>

	<target name="devhelp" description="Show developers' help">
		<echo>

Prepare releasing next version of the framework or a module:
  phing -Dapp=core release_minor
  phing release_major
  phing -Dapp=innovate release_minor

All release_* tasks are for developers. They group the database updates and create a new database schema file.
Review files after this and commit all changes if everything is fine.
		</echo>
	</target>

	<target name="install" description="Install the application">
		<exec command="${phing.dir}/clc app:install ${application} --recover" passthru="true" checkreturn="true"/>
	</target>

	<target name="upgrade" description="Upgrade the database schema to the latest version">
		<exec command="${phing.dir}/clc app:upgrade ${application}" passthru="true" checkreturn="true"/>
	</target>

	<target name="uninstall" description="Uninstall a module">
		<if>
			<equals arg1="${application}" arg2="core"/>
			<then><fail message="Please specify application to uninstall. Can't uninstall core" /></then>
		</if>
		<exec command="${phing.dir}/clc app:uninstall ${application}" passthru="true" checkreturn="true"/>
	</target>

	<target name="release_minor" description="Build DB schema for new MINOR version release">
		<phing buildfile="${build_files_path}/phing_release.xml" target="release_minor" haltonfailure="true" />
	</target>

	<target name="release_major" description="Build DB schema for new MAJOR version release">
		<phing buildfile="${build_files_path}/phing_release.xml" target="release_major" haltonfailure="true" />
	</target>

	<target name="load_core_config" depends="sanity_check">
		<loadcoreconfig file="${core_config_file}" />
	</target>

	<target name="create_core_config" depends="sanity_check">
		<if>
			<not>
				<!--suppress PhingDomInspection -->
				<available file="${core_config_file}" />
			</not>
			<then>
				<phing buildfile="${build_files_path}/phing_install.xml" target="prompt_make_config" haltonfailure="true" />
			</then>
		</if>
	</target>

	<target name="check" depends="create_core_config">
		<phing buildfile="${build_files_path}/phing_install.xml" target="database_check" haltonfailure="true" />
	</target>

	<target name="sanity_check">
		<if>
			<and>
				<not><equals arg1="${application}" arg2="core"/></not>
				<not><available file="${core_config_file}" property="core_config_exists" /></not>
			</and>
			<then>
				<fail msg="Core config file doesn't exist, cannot install modules" />
			</then>
		</if>

		<if>
			<and>
				<not><equals arg1="${application}" arg2="core"/></not>
				<not><available file="web/intranet/${application}" type="dir" property="module_dir_exists" /></not>
				<not><available file="web/custom/${application}" type="dir" property="module_dir_exists" /></not>
			</and>
			<then>
				<fail msg="Unknown application '${application}'" />
			</then>
		</if>

		<if>
			<and>
				<not><equals arg1="${application}" arg2="core"/></not>
				<not><available file="web/intranet/${application}/_init" type="dir" property="init_dir_exists" /></not>
				<not><available file="web/custom/${application}/_init" type="dir" property="init_dir_exists" /></not>
			</and>
			<then>
				<fail msg="Application '${application}' doesn't have _init folder" />
			</then>
		</if>
	</target>

	<taskdef name="loadcoreconfig" classname="web.intranet.setup.classes.Phing.LoadCoreConfigTask" />
</project>
