<?xml version="1.0" encoding="utf-8"?>

<!--
  ~ The Antville Project
  ~ http://code.google.com/p/antville
  ~
  ~ Copyright 2001-2014 by the Workers of Antville.
  ~
  ~ Copyright 2001–2007 Robert Gaggl, Hannes Wallnöfer, Tobi Schäfer,
  ~ Matthias & Michael Platzer, Christoph Lincke.
  ~
  ~ Licensed under the Apache License, Version 2.0 (the ``License'');
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~   http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an ``AS IS'' BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<project name="antville" default="dist" basedir=".">
  <target name="help" depends="usage"/>
  <target name="usage">
    <echo message="Antville Build Instructions"/>
    <echo message="==========================="/>
    <echo message="Available targets are:"/>
    <echo message="  clean     Clean up build directory"/>
    <echo message="  click     Generate the AntClick packages"/>
    <echo message="  client    Build client-side scripts and styles"/>
    <echo message="  dist      Generate the Antville and AntClick packages (default)"/>
    <echo message="  docs      Generate the API documentation"/>
    <echo message="  help      Show these build instructions"/>
    <echo message="  libs      Update Java libraries"/>
    <echo message="  messages  Generate JavaScript message files"/>
    <echo message="  patch     Apply patch file to Antville installation"/>
    <echo message="  pot       Extract gettext call strings into POT file"/>
    <echo message="  ville     Generate the Antville packages"/>
    <echo message="  +claustra Scaffolds a new claustra, ready for implementation"/>
    <echo message="  +patch    Scaffolds a new patch, ready for implementation"/>
  </target>

  <target name="dist" depends="antville, antclick"/>
  <target name="all" depends="dist"/>
  <target name="build" depends="dist"/>

  <target name="init">
    <tstamp>
      <format property="date" pattern="d MMM yyyy" locale="en"/>
    </tstamp>
    <tstamp>
      <format property="file-date" pattern="yyyyMMdd" locale="en"/>
    </tstamp>

    <property name="name" value="antville"/>
    <property name="year" value="2001-${year}"/>
    <property name="version" value="1.6"/>
    <property name="package.name" value="${name}-${version}"/>

    <property name="antville.src" value="https://github.com/antville/antville.git"/>
    <property name="helma.src" value="https://github.com/antville/helma.git"/>
    <property name="jala.src" value="https://github.com/antville/helma-jala.git"/>

    <property name="uikit.dir" location="./node_modules/uikit/dist"/>

    <property name="build.dir" location="${basedir}/build"/>
    <property file="${build.dir}/build.properties"/>

    <property name="helma.dir" location="/opt/helma"/>
    <property name="extra.dir" location="${build.dir}/extra"/>
    <property name="dist.dir" location="${build.dir}/dist"/>
    <property name="work.dir" location="${build.dir}/work"/>
    <property name="source.dir" location="${work.dir}/src"/>
    <property name="export.dir" location="${work.dir}/export"/>
    <property name="modules.dir" location="${export.dir}/helma/modules"/>
    <property name="antville.dir" location="${export.dir}/antville"/>

    <property name="docs.title" value="Antville&amp;#32;${version}&amp;#32;API&amp;#32;Reference"/>
    <property name="docs.infiles" location="${basedir}/code"/>
    <property name="docs.outfiles" location="${basedir}/docs"/>

    <property name="i18n.scan" value="claustra code compat extra"/>
    <property name="i18n.destination" location="${basedir}/i18n"/>
    <property name="i18n.poDirectory" location="${i18n.destination}"/>
    <property name="i18n.template" location="${i18n.destination}/antville.pot"/>
    <property name="i18n.namespace" value=""/>
  </target>

  <target name="export" depends="init">
    <mkdir dir="${source.dir}"/>
    <exec dir="${source.dir}" executable="git">
      <arg line="clone ${antville.src}"/>
    </exec>
    <exec dir="${source.dir}/antville" executable="git">
      <arg line="checkout-index -f -a --prefix=${antville.dir}/"/>
    </exec>
    <delete includeemptydirs="true">
      <fileset dir="${antville.dir}" includes="build.xml build/** docs/** i18n/*.po*"/>
    </delete>
    <exec dir="${source.dir}/antville" executable="git" outputproperty="hash">
      <arg line="rev-parse --short HEAD"/>
    </exec>
    <replace file="${antville.dir}/code/Root/Root.js" value="${version}">
      <replacetoken><![CDATA[<v>0</v>]]></replacetoken>
    </replace>
    <replace file="${antville.dir}/code/Root/Root.js" value="${hash}">
      <replacetoken><![CDATA[<h>0</h>]]></replacetoken>
    </replace>
    <replace file="${antville.dir}/code/Root/Root.js" value="${date}">
      <replacetoken><![CDATA[<d/>]]></replacetoken>
    </replace>
  </target>

  <target name="ville" depends="antville"/>
  <target name="click" depends="antclick"/>

  <target name="antville" depends="export">
    <antcall target="make-tar">
      <param name="filename" value="${package.name}"/>
      <param name="path" value="${antville.dir}"/>
    </antcall>
    <antcall target="make-zip">
      <param name="filename" value="${package.name}"/>
      <param name="path" value="${antville.dir}"/>
    </antcall>
  </target>

  <target name="antclick" depends="export">
    <!-- Export the Helma and Jala repositories to a temporary directory -->
    <exec dir="${source.dir}" executable="git">
      <arg line="clone ${helma.src}"/>
    </exec>
    <exec dir="${source.dir}" executable="git">
      <arg line="clone ${jala.src}"/>
    </exec>

    <!-- Checkout each repository index to the export directory -->
    <mkdir dir="${export.dir}"/>
    <exec dir="${source.dir}/helma" executable="git">
      <arg line="checkout-index -f -a --prefix=${export.dir}/helma/"/>
    </exec>
    <exec dir="${source.dir}/helma-jala" executable="git">
      <arg line="checkout-index -f -a --prefix=${export.dir}/helma/modules/jala/"/>
    </exec>

    <!-- Copy exported files to final destinations -->
    <copy todir="${work.dir}">
      <fileset dir="${export.dir}/helma" includes="lib/** licenses/**"/>
    </copy>
    <copy todir="${work.dir}/apps/antville">
      <fileset dir="${antville.dir}"/>
    </copy>
    <copy todir="${work.dir}/modules">
      <fileset dir="${modules.dir}" includes="core/** helma/** jala/**"/>
    </copy>

    <!-- Copy extra files to final destinations -->
    <copy file="${export.dir}/helma/license.txt" tofile="${work.dir}/licenses/helma.txt"/>
    <copy todir="${work.dir}">
       <fileset dir="${extra.dir}"
          includes="start.bat apps.properties server.properties launcher.jar"/>
    </copy>
    <exec dir="${extra.dir}" executable="cp">
       <arg line="-p start.sh ${work.dir}"/>
    </exec>

    <copy file="${extra.dir}/helma.jar" todir="${work.dir}/lib"/>
    <copy file="${extra.dir}/h2-1.3.176.jar" todir="${work.dir}/lib/ext"/>
    <copy file="${extra.dir}/h2.txt" todir="${work.dir}/licenses"/>

    <copy todir="${build.dir}/work/db">
       <fileset dir="${extra.dir}" includes="antville.h2.db"/>
    </copy>
    <copy todir="${work.dir}/licenses">
      <fileset dir="${antville.dir}/legal" includes="**"/>
    </copy>
    <copy file="${modules.dir}/jala/licenses/jala.txt" todir="${work.dir}/licenses"/>

    <!-- Delete unnecessary files -->
    <delete>
      <fileset dir="${work.dir}/modules/helma" includes="*.jar Search.js Ssh.js Chart.js"/>
    </delete>
    <delete includeemptydirs="true">
      <fileset dir="${work.dir}/modules/jala"
          includes="build.properties docs/** lib/** licenses/** tests/** util/**"/>
    </delete>
    <delete dir="${work.dir}/apps/antville/legal"/>
    <delete dir="${source.dir}"/>
    <delete dir="${export.dir}"/>

    <!-- Create the packages -->
    <antcall target="make-zip">
       <param name="filename" value="antclick-${version}"/>
       <param name="path" value="${work.dir}"/>
    </antcall>
    <antcall target="make-tar">
       <param name="filename" value="antclick-${version}"/>
       <param name="path" value="${work.dir}"/>
    </antcall>
  </target>

  <target name="client" depends="init">
    <echo message="Building client-side files"/>
    <exec executable="yarn">
      <arg line="build"/>
    </exec>
  </target>

  <target name="libs" depends="init">
    <exec executable="${helma.dir}/gradlew">
      <arg line="-b build/build.gradle"/>
    </exec>
  </target>

  <target name="docs" depends="init">
    <exec dir="${build.dir}/jsdoc/toolkit" executable="${build.dir}/jsdoc/jsrunwrapper.sh">
      <arg line='-D="title:${docs.title}" -q -r=2 -p -t=../templates/codeview
				-d=${docs.outfiles} ${docs.infiles}'/>
    </exec>
  </target>

  <target name="pot" depends="init">
    <java dir="${helma.dir}" classpath="${helma.dir}/launcher.jar"
        classname="helma.main.launcher.Commandline" fork="true">
      <!-- Root.extractMessages is currently located in Global/i18n.js -->
      <arg value="antville.extractMessages"/>
      <arg value="${build.dir}/MessageParser.js"/>
      <arg value="${i18n.scan}"/>
      <arg value="${i18n.template}"/>
    </java>
  </target>

  <target name="messages" depends="init">
    <java dir="${helma.dir}" classpath="${helma.dir}/lib/rhino-1.7.9.jar"
        classname="org.mozilla.javascript.tools.shell.Main">
      <arg value="${build.dir}/PoParser.js"/>
      <arg value="${i18n.poDirectory}"/>
      <arg value="${i18n.destination}"/>
      <arg value="${i18n.namespace}"/>
    </java>
  </target>

  <target name="make-tar" depends="init">
    <mkdir dir="${dist.dir}"/>
    <fixcrlf srcdir="${path}" eol="lf" eof="remove" includes="**/*.txt,
    		**/*.properties, **/*.hac, **/*.js, **/*.skin, **/.xml, **/.sh"/>
    <tar tarfile="${dist.dir}/${filename}.tar" basedir="${path}" excludes="**">
      <tarfileset prefix="${filename}" dir="${path}" mode="755">
        <include name="**/*.sh"/>
      </tarfileset>
      <tarfileset prefix="${filename}" dir="${path}">
        <include name="**"/>
        <exclude name="**/*.sh"/>
      </tarfileset>
    </tar>
    <bzip2 zipfile="${dist.dir}/${filename}.tbz" src="${dist.dir}/${filename}.tar"/>
    <delete file="${dist.dir}/${filename}.tar"/>
  </target>

  <target name="make-zip" depends="init">
    <mkdir dir="${dist.dir}"/>
    <fixcrlf srcdir="${path}" eol="crlf"
        includes="**/*.txt, **/*.properties,	**/*.hac, **/*.js, **/*.skin, **/*.xml, **/.bat"/>
    <zip zipfile="${dist.dir}/${filename}.zip">
      <zipfileset dir="${path}" prefix="${filename}" includes="**"/>
    </zip>
  </target>

  <target name="patch" depends="init">
    <input message="Please enter the patch ID: " addproperty="patch.id"/>
    <loadfile property="patch" srcFile="${basedir}/extra/updater/patch-${patch.id}.js"/>
    <echo message="${patch}"/>
    <input message="Apply the displayed patch? " validargs="y,n" addproperty="patch.confirm"/>
    <condition property="patch.abort">
      <equals arg1="${patch.confirm}" arg2="n" casesensitive="false" trim="true"/>
    </condition>
    <fail if="patch.abort">Build aborted by user.</fail>
    <java dir="${helma.dir}" classpath="${helma.dir}/launcher.jar"
        classname="helma.main.launcher.Commandline" fork="true">
      <arg value="antville.patch"/>
      <arg value="${patch}"/>
    </java>
  </target>

  <target name="+patch" depends="init">
    <property name="patch.dir" value="extra/updater"/>
    <echo file="${patch.dir}/patch-${file-date}.js"><!--
      -->// Apply with enabled updater repository using `ant patch -Dpatch.id=${file-date}`${line.separator}<!--
      -->var sql = new Sql();
    </echo>
  </target>

  <target name="+claustra" depends="init">
    <input message="Please enter the name of the new claustra: " addproperty="claustra.name"/>
    <script language="javascript"><![CDATA[
      name = project.getProperty('claustra.name');
      firstLetter = name.substr(0, 1).toUpperCase();
      title = firstLetter + name.substr(1);
      project.setProperty('claustra.title', title);
    ]]></script>
    <property name="claustra.dir" value="claustra/${claustra.name}/${claustra.title}"/>
    <mkdir dir="${claustra.dir}"/>
    <echo file="${claustra.dir}/${claustra.title}.properties" append="true">#sites = collection(Site)</echo>
    <echo file="${claustra.dir}/${claustra.title}.js" append="true"><!--
        -->${claustra.title}.prototype.main_action = function () {${line.separator}<!--
        -->  //res.debug(this.sites.count());${line.separator}<!--
        -->};${line.separator}</echo>
    <mkdir dir="${claustra.dir}/../Claustra"/>
    <echo file="${claustra.dir}/../Claustra/Claustra.properties" append="true"><!--
        -->${claustra.name} = mountpoint(${claustra.title})${line.separator}</echo>
  </target>

  <target name="clean" depends="init">
    <delete dir="${work.dir}"/>
    <delete dir="${dist.dir}"/>
  </target>

  <target name="file-exists" unless="file.exists">
    <available property="file.exists" file="${file}"/>
  </target>

  <target name="debug" depends="init">
    <!-- For debugging and testing purposes -->
  </target>
 </project>
