<project name="documentation" basedir="." default="optimize-pdf">

    <target name="optimize-pdf" depends="check-gs" if="havegs">
        <property name="generated.pdf.directory" value="${project.build.directory}/generated/${infinispan.base.version}/pdf"/>
        <property name="optimized.pdf.directory" value="${project.build.directory}/generated/${infinispan.base.version}/optimized"/>
        <mkdir dir="${optimized.pdf.directory}" />
        <apply executable="gs" failonerror="false" dest="${optimized.pdf.directory}" verbose="true">
            <arg value="-sDEVICE=pdfwrite"/>
            <arg value="-dCompatibilityLevel=1.4"/>
            <arg value="-dPDFSETTINGS=/ebook"/>
            <arg value="-dNOPAUSE"/>
            <arg value="-dQUIET"/>
            <arg value="-dBATCH"/>
            <targetfile prefix="-sOutputFile="/>
            <fileset dir="${generated.pdf.directory}" includes="*.pdf" />
            <flattenmapper />
        </apply>
        <move todir="${generated.pdf.directory}">
            <fileset dir="${optimized.pdf.directory}">
                <include name="*.pdf" />
            </fileset>
        </move>
    </target>

    <target name="check-gs">
        <exec executable="gs" failonerror="false" failifexecutionfails="false" outputproperty="gsversion" resultproperty="gsresult">
            <arg value="--version" />
        </exec>
        <check-version version="${gsversion}" major="9" minor="10" property="havegs"/>
    </target>

    <scriptdef name="check-version" language="groovy" >
        <attribute name="version" />
        <attribute name="major" />
        <attribute name="minor" />
        <attribute name="property" />
        <![CDATA[
            version = attributes.get("version");
            checkMajor = attributes.get("major").toInteger();
            checkMinor = attributes.get("minor").toInteger();
            if (version.indexOf('.') > 0) {
                parts = version.tokenize(".");
                major = parts[0].toInteger();
                minor = parts[1].toInteger();
                if (major > checkMajor || (major == checkMajor && minor >= checkMinor)) {
                    project.setProperty(attributes.get("property"), "true");
                    self.log("Version " + version);
                } else {
                    self.log("Version " + version + " does not satisfy the minimum requirement of " + checkMajor + "." + checkMinor);
                }
            }
        ]]>

    </scriptdef>
</project>
