<?xml version="1.0"?>

<project name="Flux CMS" default="dist" basedir=".">
   
   <property name="hasTidy" value="auto"/>
    <target name="prepare" depends="prereq">
        <mkdir dir="${BxRootDir}/tmp/"/>
        <copy includeemptydirs="true" todir="${BxRootDir}/data/">
            <fileset dir="${BxRootDir}/install/dist/data/">
                <exclude name="**/.svn/"/>
            </fileset>
        </copy>

        <mkdir dir="${BxRootDir}/files/"/>
        <copy includeemptydirs="true" todir="${BxRootDir}/files/">
            <fileset dir="${BxRootDir}/install/dist/files/">
                <exclude name="**/.svn/"/>
            </fileset>
        </copy>

        <mkdir dir="${BxRootDir}/dynimages/"/>
        <mkdir dir="${BxRootDir}/lang/"/>
        <copy includeemptydirs="true" todir="${BxRootDir}/lang/">
            <fileset dir="${BxRootDir}/install/dist/lang/">
                <exclude name="**/.svn/"/>
            </fileset>
        </copy>

    </target>

    <target name="copyConfigTemplates" depends="prepare, installTables">
        <echo message="Copying Config Templates..."/>
        
        <copy file="${BxRootDir}/install/dist/conf/config.xml" tofile="${BxRootDir}/conf/config.xml">
            <filterchain>
                <replacetokens begintoken="##" endtoken="##">
                    <token key="dbtype" value="${database.type}"/>
                    <token key="dbuser" value="${database.user}"/>
                    <token key="dbpass" value="${database.password}"/>
                    <token key="dbdatabase" value="${database.name}"/>
                    <token key="dbhost" value="${database.host}"/>
                    <token key="dbprefix" value="${database.prefix}"/>
                    <token key="DIR" value="${dir.sub}"/>
                </replacetokens>
            </filterchain>
        </copy>
        <copy file="${BxRootDir}/install/dist/conf/htaccess" tofile="${BxRootDir}/conf/.htaccess">
        </copy>
         <copy file="${BxRootDir}/install/dist/conf/htaccess" tofile="${BxRootDir}/tmp/.htaccess">
        </copy>
       
         <copy includeemptydirs="true" todir="${BxRootDir}/conf/">
            <fileset dir="${BxRootDir}/install/dist/conf/">
                <exclude name="**/.svn/"/>
                 <exclude name="config.xml"/>
            </fileset>
        </copy>
        <php expression="(file_exists('${BxRootDir}/.htaccess') &amp;&amp; preg_match('#\nAddHandler application\/x\-httpd\-php5#',file_get_contents('${BxRootDir}/.htaccess')) ) ? 'AddHandler application/x-httpd-php5' : '#AddHandler foo application/x-httpd-php5'" returnProperty="replacePhp5Instruction"/>
    
        <copy file="${BxRootDir}/install/dist/htaccess" tofile="${BxRootDir}/.htaccess" overwrite="true">
            <filterchain>
                <replacetokens begintoken="##" endtoken="##">
                    <token key="DIR" value="${dir.sub.htaccess}"/>
                </replacetokens>
                <replaceregexp>
                    <regexp pattern="(\n)php_" replace="\1${replacePhpInHtaccess}" ignoreCase="true"/>
                </replaceregexp>
                <replaceregexp>
                    <regexp pattern="(\n)#AddHandler application\/x\-httpd\-php5" replace="\1${replacePhp5Instruction}" ignoreCase="true"/>
                </replaceregexp>
            </filterchain>
        </copy>

    </target>

    <target name="dist" depends="copyConfigTemplates">
        
         <echo message="Flux CMS installed."/>
         <echo><![CDATA[Go to the <a href="../admin/">admin</a> or the <a href="../">live page</a> now and have fun]]></echo>
    </target>
    <target name="installTables" depends="setupPrefixedTable, installUser">
    
     <mdb2 src="${BxRootDir}/tmp/bxcms.prefixed.sql" url="${database.type}://${database.user}:${database.password}@${database.host}/${database.name}"></mdb2>
     <!-- 
     for 2.0
     
     <mdb2  url="${database.type}://${database.user}:${database.password}@${database.host}/${database.name}">
     INSERT INTO ${database.prefix}live__users ( user_login, user_pass) VALUES ('${cms.user}',md5('${cms.password}'));
     </mdb2>
     -->
     
     <mdb2  url="${database.type}://${database.user}:${database.password}@${database.host}/${database.name}">
     INSERT INTO ${database.prefix}users ( user_login, user_pass) VALUES ('${cms.user}',md5('${cms.password}'));
     </mdb2>
     <delete file="bxcms.prefixed.sql" dir="${BxRootDir}/tmp/" />
    </target>
    
    <target name="setupPrefixedTable">
        <copy file="${BxRootDir}/div/db/bxcms.sql" tofile="${BxRootDir}/tmp/bxcms.prefixed.sql" overwrite="true">
            <filterchain>
                    <replacetokens begintoken="##" endtoken="##">
                        <!--<token key="bxcms_" value="${database.prefix}live__"/>-->
                        <token key="bxcms_" value="${database.prefix}"/>
                    </replacetokens>
                </filterchain>
        </copy>
    </target>

    <target name="installDB" if="databaseRoot.User" unless="DbHasUTF8" description="Create the Database">
        <mdb2 onerror="continue" url="${database.type}://${databaseRoot.User}:${databaseRoot.Password}@${database.host}/"> CREATE DATABASE ${database.name} </mdb2>

    </target>
    
    <target name="installDBUTF8" if="databaseRoot.User" unless="DbHasNoUTF8"  description="Create the Database">
        <mdb2 onerror="continue" url="${database.type}://${databaseRoot.User}:${databaseRoot.Password}@${database.host}/"> CREATE DATABASE ${database.name} DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;</mdb2>

    </target>
  

    <target name="installUser" depends="installDB, installDBUTF8" if="databaseRoot.User" description="Create the DB user">
        <mdb2  url="${database.type}://${databaseRoot.User}:${databaseRoot.Password}@${database.host}/${database.name}">GRANT ALTER, CREATE, INDEX, INSERT, LOCK TABLES, SELECT, UPDATE, DROP, DELETE ON ${database.name}. * TO '${database.user}'@${database.host} IDENTIFIED BY '${database.password}';</mdb2>
    </target>
    
    
    
    <target name="prereq">
        <echo message="Check if prerequisites are met"/>
        <phingcall target="tidyCheck"/>
    </target>

    <target name="tidyCheck">
        <php expression="function_exists('tidy_clean_repair') ? 'true' : 'false'" returnProperty="hasTidy"/>
        <mkdir dir="${BxRootDir}/sitemap/"/>
        <xslt style="${BxRootDir}/install/sitemap_tidy.xsl" file="${BxRootDir}/install/dist/sitemap/sitemap.xml" tofile="${BxRootDir}/sitemap/sitemap.xml">
            <param name="tidy" expression="${hasTidy}"/>

        </xslt>

    </target>
   
  

</project>
