<?xml version="1.0" encoding="UTF-8"?>
<project name="install_ekp_cfr" default="install" basedir=".">

<!-- Username and password for the account to execute database scripts -->
<property name="db.username" value="" />
<property name="db.password" value="" />

<!-- Web application directory of EKP -->
<property environment="env"/> 
    <fail message="Please set the TOMCAT_HOME environment variable before running this script.">
        <condition>
            <not>
                <isset property="env.TOMCAT_HOME" />
            </not>
        </condition>
    </fail>
<property name="webapps.ekp"  location="${env.TOMCAT_HOME}/webapps/ekp"/>

<fail message="Please set the database username and password in 'db.username' and 'db.password' of build.xml before running this script.">
    <condition>
        <or>
            <equals
                arg1="${db.username}"
                arg2=""
            />
            <equals
                arg1="${db.password}"
                arg2=""
            />
        </or>
    </condition>
</fail> 

<path id="project.class.path">
    <fileset dir="${webapps.ekp}/WEB-INF/lib">
        <include name="**/*.jar"/>
    </fileset>
</path>

<target name="install">
    <!-- Take a backup of existing schema files. -->
    <mkdir dir="${webapps.ekp}/WEB-INF/nschema.backup"/>

    <move todir="${webapps.ekp}/WEB-INF/nschema.backup" failonerror="false">
        <fileset dir="${webapps.ekp}/WEB-INF/nschema"/>
    </move>

    <!-- Extract files to web application directory -->
    <unzip src="install-ekp-cfr.zip" dest="${webapps.ekp}" />

    <loadproperties srcFile="${webapps.ekp}/WEB-INF/conf/ekp.properties" />
    <condition property="is.mssql">
        <equals 
            trim="true"
            arg1="${default.dbType}"
            arg2="mssql"
        />
    </condition>
    <condition property="is.oracle">
        <equals
            trim="true"
            arg1="${default.dbType}"
            arg2="oracle"
        />
    </condition>
    <condition property="is.mysql">
        <equals
            trim="true"
            arg1="${default.dbType}"
            arg2="mysql"
        />
    </condition>

    <!-- Upgrade database -->
    <antcall target="mssql"/>
    <antcall target="oracle"/>
    <antcall target="mysql"/>
</target>

<target name="mssql" if="is.mssql">
    <sql
        driver="${default.driverName}"
        url="${default.connectURL}"
        userid="${db.username}"
        password="${db.password}"
        src="${webapps.ekp}/WEB-INF/nschema/mssql/mssql-audit.sql"
        classpathref="project.class.path"
        delimiter="GO"
        autocommit="true"
        onerror="continue"
    />
</target>

<target name="oracle" if="is.oracle">
    <sql
        driver="${default.driverName}"
        url="${default.connectURL}"
        userid="${db.username}"
        password="${db.password}"
        src="${webapps.ekp}/WEB-INF/nschema/oracle/oracle-audit.sql"
        classpathref="project.class.path"
        delimiter="/"
        autocommit="true"
        onerror="continue"
    />
</target>

<target name="mysql" if="is.mysql">
    <replaceregexp file="${webapps.ekp}/WEB-INF/nschema/mysql/mysql-audit.sql" match="delimiter \/" replace="" />
    <sql
        driver="${default.driverName}"
        url="${default.connectURL}"
        userid="${db.username}"
        password="${db.password}"
        src="${webapps.ekp}/WEB-INF/nschema/mysql/mysql-audit.sql"
        classpathref="project.class.path"
        delimiter="/"
        autocommit="true"
        onerror="continue"
    />
</target>

</project>
