<?xml version="1.0" encoding="utf-8"?>
<project  xmlns:ivy="antlib:org.apache.ivy.ant" name="marc4j" default="jar" basedir=".">

    <target name="init">
        <tstamp />
       <!-- <record name="build.log" loglevel="verbose" append="no" /> -->
    </target>

    <property file="build.properties" />

    <tstamp>
        <format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss Z" timezone="GMT" />
    </tstamp>

        <target name="prepare" depends="init,setup-ivy" unless="prepared">
        <mkdir dir="${build.dir}" />
        <mkdir dir="${build.classes.dir}"/>
        <mkdir dir="${build.test-classes.dir}"/>
        <mkdir dir="${build.generated-src.dir}"/>
        <mkdir dir="${lib.dir}"/>
        <ivy:retrieve />
  <!--      <property file="${user.home}/gnupg.properties"/>
        <taskdef resource="org/apache/commons/openpgp/ant/antlib.xml">
            <classpath>
                <pathelement location="${user.home}/.m2/repository/org/bouncycastle/bcprov-jdk12/130/bcprov-jdk12-130.jar"/>
                <pathelement location="${user.home}/.m2/repository/org/apache/commons/commons-openpgp/1.0-SNAPSHOT/commons-openpgp-1.0-SNAPSHOT.jar"/>
                <pathelement location="${user.home}/.m2/repository/org/bouncycastle/bcpg-jdk12/130/bcpg-jdk12-130.jar"/>
            </classpath>
        </taskdef><condition property="gpg.dir" value="${user.home}/AppData/Roaming/gnupg"><os family="windows"/></condition><property name="gpg.dir" location="${user.home}/.gnupg"/>         -->
        <property name="prepared" value="true"/>
    </target>

    <path id="classpath">
        <fileset dir="${lib.dir}">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <path id="test.classpath">
        <pathelement location="${build.classes.dir}" />
        <pathelement location="${build.test-classes.dir}" />
        <pathelement location="test/resources"/>
        <fileset dir="${lib.dir}">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <target name="prepare-codetablegen" depends="prepare">
        <mkdir dir="${build.generated-src.dir}/org/marc4j/converter/impl" />
        <dependset>
            <srcfileset  dir="${src.dir}/org/marc4j/converter/impl/"/>
            <targetfileset dir="${build.generated-src.dir}/org/marc4j/converter/impl/" includes="*.java"/>
        </dependset>
        <available file="${build.generated-src.dir}/org/marc4j/converter/impl/CodeTableGenerated.java" property="already.ctged"/>
    </target>

    <target name="codetablegen" depends="prepare-codetablegen" unless="already.ctged">
        <java fork="true" classname="org.marc4j.converter.impl.CodeTableGenerator" errorproperty="CodeTableGenerated.error" output="${build.generated-src.dir}/org/marc4j/converter/impl/CodeTableGenerated.java" >
            <classpath location="${build.classes.dir}" />
            <arg value="MARC8"/>
        </java>
        <java fork="true" classname="org.marc4j.converter.impl.ReverseCodeTableGenerator" errorproperty="ReverseCodeTableGenerated.error" output="${build.generated-src.dir}/org/marc4j/converter/impl/ReverseCodeTableGenerated.java" >
            <classpath location="${build.classes.dir}" />
            <arg value="MARC8"/>
        </java>
        <java fork="true" classname="org.marc4j.converter.impl.CodeTableGenerator" errorProperty="UnimarcCodeTableGenerated.error" output="${build.generated-src.dir}/org/marc4j/converter/impl/UnimarcCodeTableGenerated.java" >
            <classpath location="${build.classes.dir}" />
            <arg value="UNIMARC"/>
        </java>
        <java fork="true" classname="org.marc4j.converter.impl.ReverseCodeTableGenerator" errorProperty="UnimarcReverseCodeTableGenerated.error" output="${build.generated-src.dir}/org/marc4j/converter/impl/UnimarcReverseCodeTableGenerated.java" >
            <classpath location="${build.classes.dir}" />
            <arg value="UNIMARC"/>
        </java>
       <!-- <concat >
            <fileset dir="${build.generated-src.dir}/org/marc4j/converter/impl" includes="Code*.java" />
            <filterchain>
                <headfilter lines="15"/>
            </filterchain>
        </concat>
        <concat >
            <fileset dir="${build.generated-src.dir}/org/marc4j/converter/impl" includes="Rev*.java" />
            <filterchain>
                <headfilter lines="15"/>
            </filterchain>
        </concat>  -->
    </target>

    <target name="compile-codegen" depends="codetablegen">
        <javac encoding="UTF-8" srcdir="${build.generated-src.dir}" destdir="${build.classes.dir}" includes="**/*Generated.java" debug="false">
            <classpath refid="classpath" />
        </javac>

    </target>
    <target name="compile-all" depends="compile-base,compile-codegen"/>

    <target name="compile-base" depends="prepare">
        <javac encoding="UTF-8" srcdir="${src.dir}" destdir="${build.classes.dir}" includes="**/*.java" excludes="**/*Generated.java" debug="true" debuglevel="source,lines,vars">
            <classpath refid="classpath" />
        </javac>
        <copy todir="${build.classes.dir}">
            <fileset dir="${src.dir}">
                <include name="**/*.properties" />
                <include name="**/*.xml" />
                <exclude name="**/tests/*" />
                <exclude name="**/tests" />
            </fileset>
        </copy>
    </target>

    <target name="compile-tests" depends="compile-all">
        <javac encoding="UTF-8" srcdir="${test.src.dir}" destdir="${build.test-classes.dir}" includes="**/*.java" excludes="**/*Generated.java" debug="true" debuglevel="source,lines,vars">
            <classpath refid="test.classpath" />
        </javac>
    </target>

    <target name="test" depends="compile-all,compile-tests">
        <junit fork="yes" forkmode="perBatch" haltonfailure="true">
            <batchtest>
                <fileset dir="${test.src.dir}">
                    <exclude name="org/marc4j/test/utils/**"/>
                </fileset>
            </batchtest>
            <formatter type="brief" usefile="false" />
            <classpath refid="test.classpath" />
        </junit>
    </target>

    <target name="jar" depends="compile-all,version">
        <jar jarfile="${build.dir}/${jar.name}">
            <manifest>
                <attribute name="Main-Class" value="org.marc4j.util.UtilDriver"/>
                <attribute name="Version" value="${version}"/>
                <attribute name="Built-Date" value="${TODAY}"/>
                <section name="org/marc4j">
                  <attribute name="Specification-Title" value="marc4j"/>
                  <attribute name="Specification-Version" value="${specversion}"/>
                  <attribute name="Specification-Vendor" value="org.marc4j"/>

                  <attribute name="Implementation-Title" value="marc4j"/>
                  <attribute name="Implementation-Version" value="${version}"/>
                  <attribute name="Implementation-Vendor" value="Team marc4j"/>
                </section>
            </manifest>
            <fileset dir="${build.classes.dir}">
                <include name="**/*.class" />
                <exclude name="**/CodeTableGenerator.class" />
                <exclude name="**/ReverseCodeTableGenerator.class" />
                <exclude name="**/samples/**/*.*" />
                <exclude name="**/solrmarc/**/*.class" />
                <exclude name="**/test/**/*.*" />
                <include name="**/*.properties" />
                <exclude name="**/*.xml" />
            </fileset>
        </jar>
    </target>

    <target name="javadoc" depends="prepare" unless="javadoc.already.run" >
        <mkdir dir="${apidoc.dir}" />
        <javadoc encoding="UTF-8" charset="UTF-8" docencoding="UTF-8" destdir="${apidoc.dir}" author="true" version="true" overview="overview.html" use="true" windowtitle="MARC4J API" doctitle="MARC4J API" bottom="Copyright &#169; 2002-2006 Bas Peters. All Rights Reserved.">
            <packageset dir="${src.dir}" defaultexcludes="yes">
                <exclude name="org/marc4j/samples" />
                <exclude name="org/marc4j/test" />
            </packageset>
            <classpath refid="classpath" />
            <link offline="true" href="http://java.sun.com/products/jdk/1.6/docs/api/" packagelistLoc="." />
        </javadoc>
        <property name="javadoc.already.run" value="true" />
    </target>

    <target name="clean" depends="init">
        <delete includeEmptyDirs="true">
            <fileset dir="${dist.dir}" includes="**/*" defaultexcludes="no"/>
        </delete>
        <delete dir="${build.dir}" />
    </target>
    <target name="distclean" depends="clean">
        <delete dir="${lib.dir}"/>
        <delete dir="${ivy.jar.dir}"/>

    </target>
    <target name="dist" depends="jar,javadoc" unless="dist.already.run" >
        <property name="prefix" value="${project.name}-${version}" />
        <property name="tmp.dir" value="${dist.dir}/tmp" />
        <property name="tmp.arc.dir" value="${tmp.dir}/${prefix}" />
        <property name="dist.arc" value="${dist.dir}/${prefix}" />
        <mkdir dir="${tmp.arc.dir}" />
        <copy file="${build.dir}/${jar.name}" tofile="${dist.dir}/${jar.name}" overwrite="true" />
        <copy file="${basedir}/pom_template.xml" tofile="${dist.dir}/${project.name}-${version}.pom" overwrite="true" >
            <filterchain>
                <replacetokens>
                    <token key="VERSION" value="${version}"/>
                </replacetokens>
            </filterchain>
        </copy>
        <copy todir="${tmp.arc.dir}">
            <fileset dir=".">
                <include name="LICENSE.*" />
                <include name="build.*" />
                <exclude name="*.log" />
                <include name="*.jar" />
                <include name="*.html" />
            </fileset>
        </copy>
        <copy todir="${tmp.arc.dir}" file="${build.dir}/${jar.name}"/>

        <copy todir="${tmp.arc.dir}/${src.dir}">
            <fileset dir="${src.dir}" >
                <exclude name="**/CodeTableGenerated.java" />
            </fileset>
        </copy>

        <copy todir="${tmp.arc.dir}/${test.src.dir}">
            <fileset dir="${test.src.dir}" >
            </fileset>
        </copy>

        <copy todir="${tmp.arc.dir}/${lib.dir}">
            <fileset dir="${lib.dir}">
                <exclude name="junit*.jar" />
            </fileset>
        </copy>
        <copy todir="${tmp.arc.dir}/apidoc">
            <fileset dir="${apidoc.dir}" />
        </copy>
        <tar tarfile="${dist.arc}.tar" basedir="${tmp.arc.dir}" />
        <gzip zipfile="${dist.arc}.tar.gz" src="${dist.arc}.tar" />
        <delete file="${dist.arc}.tar" />
        <delete file="${dist.arc}.zip" />
        <zip compress="true" basedir="${tmp.dir}" zipfile="${dist.arc}.zip" />
        <delete dir="${tmp.dir}" />
        <jar jarfile="${dist.dir}/${jar.javadoc.name}">
            <manifest>
                <section name="org/marc4j">
                  <attribute name="Specification-Title" value="marc4j"/>
                  <attribute name="Specification-Version" value="${specversion}"/>
                  <attribute name="Specification-Vendor" value="org.marc4j"/>

                  <attribute name="Implementation-Title" value="marc4j"/>
                  <attribute name="Implementation-Version" value="${version}"/>
                  <attribute name="Implementation-Vendor" value="Team marc4j"/>
                </section>
            </manifest>
            <fileset dir="${apidoc.dir}">
                <include name="**" />
            </fileset>
        </jar>
        <jar jarfile="${dist.dir}/${jar.sources.name}" >
            <manifest>
                <section name="org/marc4j">
                  <attribute name="Specification-Title" value="marc4j"/>
                  <attribute name="Specification-Version" value="${specversion}"/>
                  <attribute name="Specification-Vendor" value="org.marc4j"/>

                  <attribute name="Implementation-Title" value="marc4j"/>
                  <attribute name="Implementation-Version" value="${version}"/>
                  <attribute name="Implementation-Vendor" value="Team marc4j"/>
                </section>
            </manifest>
            <fileset dir="${src.dir}">
                <include name="**/*.java" />
                <exclude name ="**/samples/**" />
            </fileset>
            <fileset dir="${build.generated-src.dir}">
                <include name="**/*.java" />
            </fileset>
        </jar>
        <property name="dist.already.run" value="true" />
    </target>

    <target name="sign" depends="dist" if="gpg.exe" unless="is.snapshot" >
        <delete dir="${dist.dir}" includes="*.asc" />
        <input message="Enter Pass Phrase for Signing" addproperty="passphrase" >
<!--            <handler classname="org.apache.tools.ant.input.SecureInputHandler" /> -->
        </input>
        <!--  - -batch -a - -passphrase "ENTERED_PASSPHRASE" - -detach-sign $file -->
        <exec executable="${gpg.exe}" dir="${dist.dir}" searchpath="true" >
            <arg value="--batch" />
            <arg value="-a" />
            <arg value="--passphrase" />
            <arg value="${passphrase}" />
            <arg value="--detach-sign" />
            <arg value="${jar.name}" />
        </exec>
        <exec executable="${gpg.exe}" dir="${dist.dir}" searchpath="true" >
            <arg value="--batch" />
            <arg value="-a" />
            <arg value="--passphrase" />
            <arg value="${passphrase}" />
            <arg value="--detach-sign" />
            <arg value="${jar.sources.name}" />
        </exec>
        <exec executable="${gpg.exe}" dir="${dist.dir}" searchpath="true" >
            <arg value="--batch" />
            <arg value="-a" />
            <arg value="--passphrase" />
            <arg value="${passphrase}" />
            <arg value="--detach-sign" />
            <arg value="${jar.javadoc.name}" />
        </exec>
        <exec executable="${gpg.exe}" dir="${dist.dir}" searchpath="true" >
            <arg value="--batch" />
            <arg value="-a" />
            <arg value="--passphrase" />
            <arg value="${passphrase}" />
            <arg value="--detach-sign" />
            <arg value="${project.name}-${version}.pom" />
        </exec>
        <jar jarfile="${dist.dir}/bundle-${version}.jar" >
            <fileset dir="${dist.dir}">
                <include name="marc4j*.jar" />
                <include name="marc4j*.pom" />
                <include name="marc4j*.asc" />
            </fileset>
        </jar>
    </target>

    <property name="ivy.install.version" value="2.5.0" />
    <property name="ivy.jar.dir" value="${basedir}/ivy" />
    <property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar" />

    <target name="check-download" >
        <available file="${ivy.jar.file}" property="skip.download"/>
    </target>

    <target name="download-ivy" depends="check-download" unless="skip.download">
        <mkdir dir="${ivy.jar.dir}"/>

        <echo message="installing ivy..."/>
        <get src="https://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar" dest="${ivy.jar.file}" usetimestamp="true"/>
    </target>

    <target name="setup-ivy" depends="download-ivy" description="--> setup ivy" unless="prepared">
        <path id="ivy.lib.path">
            <fileset dir="${ivy.jar.dir}" includes="*.jar"/>
        </path>
        <taskdef resource="org/apache/ivy/ant/antlib.xml" uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/>
    </target>


    <target name="-version1" unless="version" >
        <mkdir dir="${dist.dir}/tmp" />
        <get src="https://api.github.com/repos/marc4j/marc4j/releases/latest" dest="${dist.dir}/tmp/git.release.txt" />
        <loadfile srcfile="${dist.dir}/tmp/git.release.txt" property="versionstr" >
            <filterchain>
                <tokenfilter>
                    <replacestring from="," to=",${line.separator}" />
                </tokenfilter>
                <linecontains>
                   <contains value="tag_name"/>
                </linecontains>
                <tokenfilter>
                     <replaceregex pattern=".*&quot;tag_name&quot;:[ ]*&quot;([0-9.]*).*" replace="\1" />
                </tokenfilter>
                <striplinebreaks/>
            </filterchain>
        </loadfile>
        <loadresource property="specversion">
          <propertyresource name="versionstr"/>
          <filterchain>
            <tokenfilter>
              <filetokenizer/>
              <replaceregex pattern="[.][0-9]*$" replace=""/>
            </tokenfilter>
          </filterchain>
        </loadresource>
        <echo message="specversion = ${specversion}" />
        <echo message="versionstr = ${versionstr}" />
    </target>

    <target name="-version2" unless="version" >
        <get src="https://api.github.com/repos/marc4j/marc4j/compare/${versionstr}...master" dest="${dist.dir}/tmp/git.commits.txt" />
        <loadfile srcfile="${dist.dir}/tmp/git.commits.txt" property="versionsuffix" >
            <filterchain>
                <tokenfilter>
                    <replacestring from="," to=", ${line.separator}" />
                </tokenfilter>
                <linecontains>
                   <contains value="total_commits"/>
                </linecontains>
                <striplinebreaks/>
                <tokenfilter>
                    <replaceregex pattern=".*&quot;total_commits&quot;:[ ]*([1-9][0-9]*).*" replace="-\1-SNAPSHOT" />
                    <replaceregex pattern=".*&quot;total_commits&quot;:[ ]*(0).*" replace="" />
                </tokenfilter>
            </filterchain>
        </loadfile>
        <echo message="versionsuffix = ${versionsuffix}" />
        <property name="versionsuffix" value="" />
        <echo message="versionsuffix = ${versionsuffix}" />
     <!--    <delete dir="${dist.dir}/tmp" /> -->
    </target>

    <available file=".git" type="dir" property="git.present"/>

    <target name="git.revision" description="Store git revision in ${repository.version}" if="git.present" unless="version">
        <property environment="env"/>
        <exec executable="git" outputproperty="git.revision" failifexecutionfails="false" errorproperty="git.not.found">
            <arg value="describe"/>
            <arg value="--tags"/>
            <arg value="--dirty=-SNAPSHOT"/>
            <arg value="--always"/>
        </exec>
        <exec executable="git" outputproperty="git.major.revision" failifexecutionfails="false" errorproperty="git.not.found">
            <arg value="describe"/>
            <arg value="--tags"/>
            <arg value="--abbrev=0"/>
        </exec>
        <condition property="version" value="${git.revision}">
            <and>
                <isset property="git.revision"/>
                <length string="${git.revision}" trim="yes" length="0" when="greater"/>
            </and>
        </condition>
        <condition property="versionstr" value="${git.major.revision}">
            <and>
                <isset property="git.major.revision"/>
                <length string="${git.major.revision}" trim="yes" length="0" when="greater"/>
            </and>
        </condition>
    </target>

    <target name="version" depends="git.revision,-version1,-version2" description="Get the version number to use from github" >
        <echo message="git.revision = ${git.revision}" />

        <property name="version" value="${versionstr}${versionsuffix}" />
        <echo message="version = ${version}" />
        <condition property="is.snapshot">
            <contains string="${version}" substring="SNAPSHOT" />
        </condition>
        <echo message="is.snapshot = ${is.snapshot}" />
        <property name="jar.name" value="${project.name}-${version}.jar" />
        <property name="jar.sources.name" value="${project.name}-${version}-sources.jar" />
        <property name="jar.javadoc.name" value="${project.name}-${version}-javadoc.jar" />
        <property name="test.jar.name" value="${project.name}_test-${version}.jar" />
    </target>


</project>
