<?xml version="1.0" encoding="UTF-8"?>
<project default="dist" name="PLM">
	
    <!-- load ChangeLog and set property plm.minor.version accordingly -->
    <loadfile property="plm.minor.version" srcFile="ChangeLog">
        <filterchain>
            <linecontainsregexp>
              <regexp pattern="[0-9]+:.*"/>
            </linecontainsregexp>          
          <headfilter lines="1"/>
          <striplinebreaks/>
          <tokenfilter>
	    <replaceregex pattern=":.*" replace=""/>
	  </tokenfilter>
        </filterchain>
    </loadfile>
    
    <loadfile property="plm.major.version" srcFile="lib/resources/plm.configuration.properties">
        <filterchain>
            <linecontainsregexp>
              <regexp pattern="plm.major.version=.*"/>
            </linecontainsregexp>          
          <headfilter lines="1"/>
          <striplinebreaks/>
          <tokenfilter>
	    <replacestring from="plm.major.version=" to=""/>
	  </tokenfilter>
        </filterchain>
    </loadfile>
    
    <property name="base.dir" value="."/>
    <property name="dist.dir" value="${base.dir}/dist"/>
    <property name="lib.dir" value="${base.dir}/lib"/>
    <property name="src.dir" value="${base.dir}/src"/>
    <property name="site.dir" value="${base.dir}/site"/>
    <property name="classes.dir" value="${site.dir}/bin"/>

    <property file="${base.dir}/keystore.properties"/>

    <condition property="is_windows">
       <os family="windows"/>
    </condition>

    <path id="project.class.path">
            <pathelement path="${classes.dir}"/>    
            <fileset dir="${lib.dir}">
                    <include name="**/*.jar"/>                      
            </fileset>      
    </path>

    <target name="clean" description="clean project">
        <delete dir="${site.dir}/lib"/>
        <delete dir="${site.dir}/po"/>
        <delete dir="${site.dir}/bin"/>
    </target>

    <taskdef resource="scala/tools/ant/antlib.xml">
      <classpath>
        <pathelement location="${lib.dir}/scala/scala-compiler-2.11.12.jar" />
        <pathelement location="${lib.dir}/scala/scala-library-2.11.12.jar" />
        <pathelement location="${lib.dir}/scala/scala-reflect-2.11.12.jar" />
      </classpath>
    </taskdef>
	
    <target name="compile" description="compile all source files">
        <mkdir dir="${classes.dir}"/>
        <scalac classpathref="project.class.path" destdir="${classes.dir}" srcdir="${src.dir}" encoding="UTF-8"/>
        <javac classpathref="project.class.path" destdir="${classes.dir}" srcdir="${src.dir}" includeantruntime="false" fork="true" encoding="UTF-8"/>
    </target>    
    
    <target name="source">
        <delete dir="${dist.dir}/plm-${plm.major.version}"/>
	<mkdir  dir="${dist.dir}/plm-${plm.major.version}"/>
	<copy todir="${dist.dir}/plm-${plm.major.version}">
            <fileset dir="${base.dir}" includes="src/**/*.py   src/**/*.scala src/**/*.c src/**/*.java src/**/*.js"/>
            <fileset dir="${base.dir}" includes="src/**/*.html src/**/*.css src/**/*.png src/**/*.svg src/**/*.map"/>
            <fileset dir="${base.dir}" includes="img/**/*.png  img/**/*.svg"/>
            <fileset dir="${base.dir}" includes="l10n/**/*.po  l10n/**/*.pot l10n/**/README"/>
            <fileset dir="${base.dir}" includes="lib/doc/*.html lib/resources/**"/>
            <fileset dir="${base.dir}" includes="ChangeLog COPYING *.pl build.xml LICENSE-GPL-3 po4a.conf README.md TODO.org" />
	</copy>
        <tar destfile="${dist.dir}/plm.${plm.major.version}-${plm.minor.version}.tar">
            <fileset dir="${dist.dir}/" includes="plm-${plm.major.version}/**"/>
	</tar>
	<bzip2  src="${dist.dir}/plm.${plm.major.version}-${plm.minor.version}.tar" destfile="${dist.dir}/plm.${plm.major.version}-${plm.minor.version}.tar.bz2"/>
	<delete file="${dist.dir}/plm.${plm.major.version}-${plm.minor.version}.tar" />
        <delete dir="${dist.dir}/plm-${plm.major.version}"/>
	
        <jar destfile="${dist.dir}/plm-src.${plm.major.version}-${plm.minor.version}.jar" filesetmanifest="mergewithoutmain">
            <manifest>
                <attribute name="Built-By" value="M. Quinson, G. Oster and others" />
            </manifest>
            <fileset dir="${base.dir}" includes="src/**/*.py   src/**/*.scala src/**/*.c src/**/*.java src/**/*.js"/>
            <fileset dir="${base.dir}" includes="src/**/*.html src/**/*.css src/**/*.png src/**/*.svg src/**/*.map"/>
            <fileset dir="${base.dir}" includes="img/**/*.png  img/**/*.svg"/>
            <fileset dir="${base.dir}" includes="l10n/**/*.po  l10n/**/*.pot l10n/**/README"/>
            <fileset dir="${base.dir}" includes="lib/doc/*.html lib/resources/**"/>
            <fileset dir="${base.dir}" includes="ChangeLog COPYING *.pl build.xml LICENSE-GPL-3 po4a.conf README.md TODO.org" />
	</jar>
    </target>    
    
    <target name="dist" depends="clean, compile, source, i18n-generate-jar, updateversion" description="build a standalone application jar file">
        <mkdir dir="${dist.dir}"/>
        <jar destfile="${dist.dir}/plm-${plm.major.version}-${plm.minor.version}.jar" filesetmanifest="mergewithoutmain">
            <manifest>
                <attribute name="Built-By" value="M. Quinson, G. Oster and others" />
                <attribute name="Main-Class" value="plm.core.ui.ProgrammersLearningMachine" />
                <attribute name="Class-Path" value="." />
            </manifest>
            <fileset dir="${classes.dir}" excludes="**/.gitignore **/*.java **/*.css **/*.html"/>
            <fileset dir="${src.dir}" includes="**/*.py **/*.c **/*.scala **/*.java **/*.html **/*.css **/*.png **/*.map" excludes="**/.gitignore"/>
            <fileset dir="${base.dir}" includes="img/*/*.png img/*.png" excludes="*"/>
            <fileset dir="${base.dir}" includes="ChangeLog" />
            <fileset dir="${lib.dir}" excludes="**/*.jar *.pl l10n/missions/* l10n/engine/*"/>
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/RSyntaxTextArea-2.5.4.jar" />			
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/miglayout-4.2.jar" />
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/tools-openjdk8.jar" />
                        
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/scala/scala-compiler-2.11.12.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/scala/scala-library-2.11.12.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/scala/scala-reflect-2.11.12.jar"/>

            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/jython/antlr3-runtime-3.2.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/jython/asm-7.2.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/jruby-1.5.6-5.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/jnr-constants-0.9.9.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/jnr-ffi-2.1.7.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/jnr-posix-3.0.45.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/jython/jython-2.7.2~rc1.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/guava-19.0.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/jline2-2.14.6.jar"/>

            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/httpclient-4.3.4.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/httpcore-4.3.2.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/httpmime-4.3.4.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/commons-logging-1.1.1.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/gettext-commons-0.9.6.jar"/>
            
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/json_simple-1.1.jar" />
            
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/plm-messages.jar" />
            
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/gson-2.2.4.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/org.eclipse.egit.github.core-2.1.5.jar"/>
	    <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/org.eclipse.jgit-3.2.0.201312181205-r.jar" />
	    <zipfileset excludes="META-INF/*.SF" src="${lib.dir}/jsch-0.1.50.jar" />
        </jar>	
    </target>
    
    <target name="dist-web" depends="compile, updateversion" description="build webstart application jar files">
        <mkdir dir="${site.dir}/lib"/>
        <jar destfile="${site.dir}/lib/plm-webstart.jar" filesetmanifest="mergewithoutmain">
            <manifest>
                <attribute name="Built-By" value="M. Quinson and G. Oster" />
                <attribute name="Main-Class" value="plm.ui.ProgrammersLearningMachine" />
                <attribute name="Class-Path" value="." />
            </manifest>
            <fileset dir="${classes.dir}" excludes="**/.gitignore **/*.java **/*.css **/*.html"/>
            <fileset dir="${src.dir}" includes="**/*.java **/*.py **/*.html **/*.css **/*.map **/*.png" excludes="**/.gitignore"/> 
            <fileset dir="${base.dir}" includes="img/*/*.png img/*.png" excludes="*"/>
            <fileset dir="${lib.dir}" excludes="*.jar"/>
        </jar>
        <copy todir="${site.dir}/lib">
            <fileset dir="${lib.dir}">
                <include name="*.jar"/>
            </fileset>
        </copy>
        <signjar alias="${keystore.alias}" keystore="${keystore.file}" storepass="${keystore.passwd}" lazy="true">
          <path>
            <fileset dir="${site.dir}/lib" includes="**/*.jar" />
          </path>
        </signjar>
    </target>
    
    <target name="javadoc" description="Create Javadocs">
        <mkdir dir="${site.dir}/javadoc/"/>
        <javadoc 
            destdir="${site.dir}/javadoc/">
            <fileset dir="${src.dir}/plm" includes="**/*.java" />
            <fileset dir="${src.dir}/lessons" includes="**/*.java" />
        </javadoc>
    </target>

    <target name="updateversion" description="Update the version number in the properties file">
      <propertyfile file="${lib.dir}/resources/plm.configuration.properties">
        <entry key="plm.minor.version" value="${plm.minor.version}" operation="="/>
      </propertyfile>
    </target>

    <property name="gettexttasks.jar" value="lib/gettext-ant-tasks-0.9.3.jar"/>
    <target name="i18n-init" description="Loads the Ant gettext tasks">
      <taskdef name="gettext-extract" classname="org.xnap.commons.ant.gettext.GettextExtractKeysTask" classpath="${gettexttasks.jar}"/>
      <taskdef name="gettext-merge"   classname="org.xnap.commons.ant.gettext.GettextMergeKeysTask"   classpath="${gettexttasks.jar}"/>
      <taskdef name="gettext-generate-default" classname="org.xnap.commons.ant.gettext.GenerateDefaultBundleTask" classpath="${gettexttasks.jar}"/>
      <taskdef name="gettext-dist"    classname="org.xnap.commons.ant.gettext.GettextDistTask"        classpath="${gettexttasks.jar}"/>
    </target>
    <target name="i18n-extract" description="Extracts message keys from the source code" depends="i18n-init">
      <gettext-extract keysFile="plm.pot" poDirectory="l10n/engine">
        <fileset dir="${src.dir}" includes="**/*.java"/>
      </gettext-extract>
    </target>
    <target name="i18n-update" description="Merges newly extracted messages into existing po files" depends="i18n-extract">
      <gettext-merge keysFile="plm.pot" poDirectory="l10n/engine"/>
    </target>
    <target name="i18n-check"
            unless="is_windows"
            description="Checks that the extracted messages are correct" 
	    depends="i18n-update">
      <!-- single quote sign is used to escape the format strings in MessageFormat;
           '' has to be used to get a single quote displayed. 
	   That's unfortunate given the amount of ' we have in french and english but anyway.
	   In addition, the strings without {0} are not passed to MessageFormat by i18n.tr().
	   See: https://code.google.com/p/gettext-commons/issues/detail?id=55 

           Rq: check that a word does not appear in translations:
           msggrep -T -e  theworld  - -sort-by-file  l10n/missions/fr.po
	   -->
      <exec executable="msggrep">
           <arg value="-K"/>
	   <arg value="-e"/>
	   <arg value="[^']'[^'].*{"/>
	   <arg value="--sort-by-file"/>
	   <arg value="l10n/engine/fr.po"/>
	   <redirector output="errors-msgid.txt" alwayslog="true"/>           
      </exec>
      <exec executable="msggrep">
           <arg value="-T"/>
	   <arg value="-e"/>
	   <arg value="[^']'[^'].*{"/>
	   <arg value="--sort-by-file"/>
	   <arg value="l10n/engine/fr.po"/>
	   <redirector output="errors-msgstr.txt" alwayslog="true"/>           
      </exec>
      <exec executable="./check_pseudoMD.pl" />
      <exec executable="./checkDave.pl" />
    </target>
    
    <target name="i18n-generate-jar" description="Generates Java ResourceBundles and jars them up" depends="i18n-update, i18n-check">
      <mkdir dir="${site.dir}/po"/>
      <gettext-dist targetBundle="org.plm.i18n.Messages" poDirectory="l10n/engine" outputDirectory="${site.dir}/po" />
      <jar destfile="lib/plm-messages.jar" basedir="${site.dir}/po" includes="org/**"/>
      <delete dir="${site.dir}/po"/>
    </target>

    <target name="test-exos" depends="compile" description="tests compilation of provided solutions of all exos">
	  <junit haltonfailure="true" haltonerror="true" printsummary="withOutAndErr" fork="yes">		
		<classpath refid="project.class.path"/>
		<formatter type="plain"/>
        <test name="plm.test.integration.ExoTest"/>		
	  </junit>
    </target>

    <target name="test-all" depends="compile" description="launch all tests">
	  <junit haltonfailure="true" haltonerror="true" printsummary="withOutAndErr" fork="yes">		
		<classpath refid="project.class.path"/>
		<formatter type="plain"/>
        <test name="plm.test.AllTests"/>		
	  </junit>
    </target>
	
    <target name="test-unit" depends="compile" description="launch all unit tests">
	  <junit printsummary="withOutAndErr" fork="yes">		
		<classpath refid="project.class.path"/>
		<formatter type="plain"/>
        <test name="plm.test.UnitTests"/>		
	  </junit>
    </target>
	
    <target name="test-integration" depends="compile" description="launch all integration tests">
	  <junit haltonfailure="true" haltonerror="true" printsummary="withOutAndErr" fork="yes">		
		<classpath refid="project.class.path"/>
		<formatter type="plain"/>
        <test name="plm.test.IntegrationTests"/>		
	  </junit>
    </target>
	
<!--  This requires Java7 to work, but I'd prefer to support for Java6
      a bit longer if possible. Disable this for now, sorry Franck.
      
      
      
    <property name="mac.dir" value="${lib.dir}/mac-resources"/>
    <property name="fr.lproj.dir" value="${mac.dir}/fr.lproj"/>
    <property name="mac.app.name" value="JavaLearningMachine"/>
    <property name="mac.bundle.dir" value="${dist.dir}/${mac.app.name}.app"/>

    <taskdef name="bundleapp"
             classname="com.oracle.appbundler.AppBundlerTask"   
             classpath="${lib.dir}/appbundler-1.0.jar" />

    <target name="build-Mac.app-bundle">
        <bundleapp outputdirectory="dist"
            name="${mac.app.name}"
            displayname="Java Learning Machine"
            identifier="plm.core.ui.JavaLearningMachine"
            mainclassname="plm.core.ui.JavaLearningMachine"
			icon="${mac.dir}/PLM.icns">
            <classpath file="dist/plm-${plm.minor.version}.jar" />
        </bundleapp>
		<copy todir="${mac.bundle.dir}/Contents/Resources/fr.lproj">
			<fileset dir="${fr.lproj.dir}"/>
		</copy>
    </target>
	
	
	<target name="Mac.app" depends="dist, build-Mac.app-bundle">
	</target>
-->	
</project>
