<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You 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 default="all">

    <target name="clean">
        <delete dir="build"/>
        <delete dir="tmp"/>
    </target>

    <target name="get-forrest-root">
        <property environment="env"/>
        <echo message="${env.FORREST_HOME}"/>
        <property name="forrest-root" value="${env.FORREST_HOME}"/>
    </target>
    
    <target name="calculate-forrest-condition" depends="get-forrest-root">
        <available file="${forrest-root}" property="forrestAvailable"/>
    </target>

    <target name="forrest-check" depends="calculate-forrest-condition" unless="forrestAvailable">
        <echo message="Site cannot be built without Apache Forrest installed"/>
    </target>
    
    <target name="calculate-dejavu-condition">
        <available file="fonts/dejavu-fonts-ttf-2.37.zip" property="dejavuAvailable"/>
    </target>

    <target name="dejavu-check" depends="download-dejavu-fonts,calculate-dejavu-condition" unless="dejavuAvailable">
        <echo message="Dejavu fonts cannot be built without dejavu-fonts-ttf-2.37.zip"/>
    </target>

    <target name="calculate-ipa-condition">
        <available file="fonts/IPAGTTC00303.zip" property="ipaAvailable"/>
    </target>

    <target name="ipa-check" depends="download-ipa-fonts,calculate-ipa-condition" unless="ipaAvailable">
        <echo message="IPA fonts cannot be built without IPAGTTC00303.zip"/>
    </target>

    <target name="setup-local-forrest" depends="forrest-check" if="forrestAvailable">
        <mkdir dir="tmp/forrest"/>
        <copy todir="tmp/forrest">
            <fileset dir="${forrest-root}"/>
        </copy>
        <chmod dir="tmp/forrest/bin" perm="ugo+rx" includes="forrest"/>
        <chmod dir="tmp/forrest/tools/ant/bin" perm="ugo+rx" includes="ant"/>
        <!-- Replace the fop jar with a more up-to-date version -->
        <delete file="tmp/forrest/plugins/org.apache.forrest.plugin.output.pdf/lib/fop-0.95.jar"/>
        <delete file="tmp/forrest/build/plugins/lib/fop-0.95.jar"/>
        <copy todir="tmp/forrest/plugins/org.apache.forrest.plugin.output.pdf/lib">
            <fileset dir="../lib">
                <include name="fop*.jar"/>
            </fileset>
        </copy>
        <!-- Same with the XML Graphics jar -->
        <delete file="tmp/forrest/lib/core/xmlgraphics-commons-1.3.1.jar"/>
        <copy todir="tmp/forrest/lib/core">
            <fileset dir="../lib">
                <include name="xmlgraphics-commons*.jar"/>
            </fileset>
        </copy>
        <!-- Replace the file tmp/forrest/plugins/org.apache.forrest.plugin.output.pdf/output.xmap with our own patched version -->
        <delete file="tmp/forrest/plugins/org.apache.forrest.plugin.output.pdf/output.xmap"/>
        <copy todir="tmp/forrest/plugins/org.apache.forrest.plugin.output.pdf">
            <fileset dir="pdf">
                <include name="output.xmap"/>
                <include name="config.xml"/>
            </fileset>
        </copy>
        <dirname file="build.xml" property="project-path"/>
        <replace file="tmp/forrest/plugins/org.apache.forrest.plugin.output.pdf/output.xmap" token="{root}" value="${project-path}"/>
        <replace file="tmp/forrest/plugins/org.apache.forrest.plugin.output.pdf/config.xml" token="{root}" value="${project-path}"/>
        <delete file="tmp/forrest/plugins/org.apache.forrest.plugin.output.pdf/resources/stylesheets/document-to-fo.xsl"/>
        <copy todir="tmp/forrest/plugins/org.apache.forrest.plugin.output.pdf/resources/stylesheets">
            <fileset dir="pdf">
                <include name="document-to-fo.xsl"/>
            </fileset>
        </copy>
        <delete file="tmp/forrest/plugins/org.apache.forrest.plugin.output.pdf/resources/stylesheets/helper-footerinfo.xsl"/>
        <copy todir="tmp/forrest/plugins/org.apache.forrest.plugin.output.pdf/resources/stylesheets">
            <fileset dir="pdf">
                <include name="helper-footerinfo.xsl"/>
            </fileset>
        </copy>
    </target>

    <target name="build-dejavu-fonts" depends="dejavu-check" if="dejavuAvailable">
        <mkdir dir="tmp/font/dejavu-fonts"/>
        <unzip src="fonts/dejavu-fonts-ttf-2.37.zip" dest="tmp/font/dejavu-fonts"/>
        <java classname="org.apache.fop.fonts.apps.TTFReader" fork="true">
            <classpath>
                <fileset dir="../lib">
                    <include name="fop*.jar"/>
                    <include name="xmlgraphics-commons*.jar"/>
                    <include name="avalon*.jar"/>
                    <include name="commons-io*.jar"/>
                    <include name="commons-logging*.jar"/>
                </fileset>
            </classpath>
            <arg value="tmp/font/dejavu-fonts/dejavu-fonts-ttf-2.37/ttf/DejaVuSans.ttf"/>
            <arg value="tmp/font/DejaVuSans.xml"/>
        </java>
        <java classname="org.apache.fop.fonts.apps.TTFReader" fork="true">
            <classpath>
                <fileset dir="../lib">
                    <include name="fop*.jar"/>
                    <include name="xmlgraphics-commons*.jar"/>
                    <include name="avalon*.jar"/>
                    <include name="commons-io*.jar"/>
                    <include name="commons-logging*.jar"/>
                </fileset>
            </classpath>
            <arg value="tmp/font/dejavu-fonts/dejavu-fonts-ttf-2.37/ttf/DejaVuSans-Oblique.ttf"/>
            <arg value="tmp/font/DejaVuSans-Oblique.xml"/>
        </java>
        <java classname="org.apache.fop.fonts.apps.TTFReader" fork="true">
            <classpath>
                <fileset dir="../lib">
                    <include name="fop*.jar"/>
                    <include name="xmlgraphics-commons*.jar"/>
                    <include name="avalon*.jar"/>
                    <include name="commons-io*.jar"/>
                    <include name="commons-logging*.jar"/>
                </fileset>
            </classpath>
            <arg value="tmp/font/dejavu-fonts/dejavu-fonts-ttf-2.37/ttf/DejaVuSans-Bold.ttf"/>
            <arg value="tmp/font/DejaVuSans-Bold.xml"/>
        </java>
        <java classname="org.apache.fop.fonts.apps.TTFReader" fork="true">
            <classpath>
                <fileset dir="../lib">
                    <include name="fop*.jar"/>
                    <include name="xmlgraphics-commons*.jar"/>
                    <include name="avalon*.jar"/>
                    <include name="commons-io*.jar"/>
                    <include name="commons-logging*.jar"/>
                </fileset>
            </classpath>
            <arg value="tmp/font/dejavu-fonts/dejavu-fonts-ttf-2.37/ttf/DejaVuSerif.ttf"/>
            <arg value="tmp/font/DejaVuSerif.xml"/>
        </java>
        <java classname="org.apache.fop.fonts.apps.TTFReader" fork="true">
            <classpath>
                <fileset dir="../lib">
                    <include name="fop*.jar"/>
                    <include name="xmlgraphics-commons*.jar"/>
                    <include name="avalon*.jar"/>
                    <include name="commons-io*.jar"/>
                    <include name="commons-logging*.jar"/>
                </fileset>
            </classpath>
            <arg value="tmp/font/dejavu-fonts/dejavu-fonts-ttf-2.37/ttf/DejaVuSerif-Italic.ttf"/>
            <arg value="tmp/font/DejaVuSerif-Italic.xml"/>
        </java>
        <java classname="org.apache.fop.fonts.apps.TTFReader" fork="true">
            <classpath>
                <fileset dir="../lib">
                    <include name="fop*.jar"/>
                    <include name="xmlgraphics-commons*.jar"/>
                    <include name="avalon*.jar"/>
                    <include name="commons-io*.jar"/>
                    <include name="commons-logging*.jar"/>
                </fileset>
            </classpath>
            <arg value="tmp/font/dejavu-fonts/dejavu-fonts-ttf-2.37/ttf/DejaVuSerif-Bold.ttf"/>
            <arg value="tmp/font/DejaVuSerif-Bold.xml"/>
        </java>
        <java classname="org.apache.fop.fonts.apps.TTFReader" fork="true">
            <classpath>
                <fileset dir="../lib">
                    <include name="fop*.jar"/>
                    <include name="xmlgraphics-commons*.jar"/>
                    <include name="avalon*.jar"/>
                    <include name="commons-io*.jar"/>
                    <include name="commons-logging*.jar"/>
                </fileset>
            </classpath>
            <arg value="tmp/font/dejavu-fonts/dejavu-fonts-ttf-2.37/ttf/DejaVuSansMono.ttf"/>
            <arg value="tmp/font/DejaVuSansMono.xml"/>
        </java>

    </target>
    
    <target name="build-ipa-fonts" depends="ipa-check" if="ipaAvailable">
        <mkdir dir="tmp/font/ipa-fonts"/>
        <unzip src="fonts/IPAGTTC00303.zip" dest="tmp/font/ipa-fonts"/>
        <java classname="org.apache.fop.fonts.apps.TTFReader" fork="true">
            <classpath>
                <fileset dir="../lib">
                    <include name="fop*.jar"/>
                    <include name="xmlgraphics-commons*.jar"/>
                    <include name="avalon*.jar"/>
                    <include name="commons-io*.jar"/>
                    <include name="commons-logging*.jar"/>
                </fileset>
            </classpath>
            <arg value="-ttcname"/>
            <arg value="IPAPGothic"/>
            <arg value="tmp/font/ipa-fonts/IPAGTTC00303/ipag.ttc"/>
            <arg value="tmp/font/ipapgothic.xml"/>
        </java>
        <java classname="org.apache.fop.fonts.apps.TTFReader" fork="true">
            <classpath>
                <fileset dir="../lib">
                    <include name="fop*.jar"/>
                    <include name="xmlgraphics-commons*.jar"/>
                    <include name="avalon*.jar"/>
                    <include name="commons-io*.jar"/>
                    <include name="commons-logging*.jar"/>
                </fileset>
            </classpath>
            <arg value="-ttcname"/>
            <arg value="IPAGothic"/>
            <arg value="tmp/font/ipa-fonts/IPAGTTC00303/ipag.ttc"/>
            <arg value="tmp/font/ipagothic.xml"/>
        </java>

    </target>
    
    <target name="dependencies-check" depends="ipa-check,dejavu-check,forrest-check">
        <condition property="dependenciesAvailable">
          <and>
              <isset property="ipaAvailable"/>
              <isset property="dejavuAvailable"/>
              <isset property="forrestAvailable"/>
          </and>
        </condition>
    </target>
    
    <target name="dist" depends="setup-local-forrest,build-ipa-fonts,build-dejavu-fonts,dependencies-check" if="dependenciesAvailable">
        <dirname file="tmp/forrest/LICENSE.txt" property="forrest-dir"/>
        <echo message="${forrest-dir}"/>
        <exec executable="cmd.exe" dir="." searchpath="true" failonerror="true" osfamily="windows">
            <arg line="/c .\tmp\forrest\bin\forrest.bat site"/>
            <env key="JAVA_TOOL_OPTIONS" value="-Dfile.encoding=UTF8"/>
            <env key="FORREST_HOME" value="${forrest-dir}"/>
        </exec>
        <exec executable="./tmp/forrest/bin/forrest" dir="." searchpath="true" failonerror="true" osfamily="unix">
            <arg line="site"/>
            <env key="JAVA_TOOL_OPTIONS" value="-Dfile.encoding=UTF8"/>
            <env key="FORREST_HOME" value="${forrest-dir}"/>
        </exec>
    </target>
    
    <target name="calculate-dejavu-condition-before">
        <available file="fonts/dejavu-fonts-ttf-2.37.zip" property="dejavuAvailableBefore"/>
    </target>

    <target name="download-dejavu-fonts" depends="calculate-dejavu-condition-before" unless="dejavuAvailableBefore">
      <mkdir dir="fonts"/>
      <get src="https://github.com/dejavu-fonts/dejavu-fonts/releases/download/version_2_37/dejavu-fonts-ttf-2.37.zip" dest="fonts"/>
    </target>
    
    <target name="calculate-ipa-condition-before">
        <available file="fonts/IPAGTTC00303.zip" property="ipaAvailableBefore"/>
    </target>

    <target name="download-ipa-fonts" depends="calculate-ipa-condition-before" unless="ipaAvailableBefore">
      <mkdir dir="fonts"/>
      <!-- http://ossipedia.ipa.go.jp/ipafont/download.html?ruleagreement=%E5%90%8C%E6%84%8F%E3%81%99%E3%82%8B%2FAccept -->
      <!-- get src="http://ossipedia.ipa.go.jp/ipafont/IPAGTTC00303.php" dest="fonts/IPAGTTC00303.zip"/ -->
      <!-- get src="http://info.openlab.ipa.go.jp/ipafont/fontdata/IPAGTTC00303.zip" dest="fonts"/ -->
      <!-- get src="http://ossipedia.ipa.go.jp/ipafont/ipafont/IPAGTTC00303.zip" dest="fonts"/ -->
      <!-- get src="http://jaist.dl.sourceforge.jp/ipafonts/51867/IPAGTTC00303.zip" dest="fonts"/ -->
	  <get src="https://ftp.iij.ad.jp/pub/osdn.jp/ipafonts/51867/IPAGTTC00303.zip" dest="fonts" />
    </target>
    
    <target name="download-dependencies" depends="download-dejavu-fonts,download-ipa-fonts">
    </target>

    <target name="download-cleanup">
        <delete dir="fonts"/>
    </target>

    <target name="all" depends="dist"/>
    
</project>
