<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.metaeffekt.core</groupId>
        <artifactId>ae-core-plugin-management-pom</artifactId>
        <version>0.42.0</version>
        <relativePath />
    </parent>

    <groupId>org.metaeffekt.dita</groupId>
    <artifactId>ae-dita-toolkit</artifactId>
    <version>0.29.0</version>
    <packaging>pom</packaging>

    <properties>
        <dita.ot.version>3.2.1</dita.ot.version>
        <dita.name.string>dita-ot-${dita.ot.version}</dita.name.string>
        <dita.archive>${project.build.directory}/${dita.name.string}.zip</dita.archive>
        <dita.customized.archive>${project.build.directory}/ae-dita-toolkit-${project.version}-bin.zip</dita.customized.archive>
        <dita.root.folder>${project.build.directory}/${dita.name.string}</dita.root.folder>


        <ant.version>1.10.1</ant.version>

        <ae.dita.version>0.29.0</ae.dita.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.metaeffekt.dita</groupId>
            <artifactId>ae-standard-pdf</artifactId>
            <version>${ae.dita.version}</version>
            <type>zip</type>
            <classifier>dita-plugin</classifier>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--
                Get the binaries required for the customization.
            -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies-to-target</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <excludeTransitive>true</excludeTransitive>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <!-- DITA-OT is explicitly not modelled as dependency. This way it does
                             not need to be published (even not as POM only) in the public maven repository. -->
                        <id>copy-dita-ot-to-target</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.dita-ot</groupId>
                                    <artifactId>dita-ot</artifactId>
                                    <version>${dita.ot.version}</version>
                                    <type>zip</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- perform the actual customization -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>customization</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <echo message="Unzipping the downloaded DITA OT binary." />
                                <unzip src="${dita.archive}" dest="${project.build.directory}" />
                                <echo message="Removing not required content." />
                                <delete includeEmptyDirs="true">
                                    <fileset dir="${dita.root.folder}/doc"/>
                                    <fileset dir="${dita.root.folder}/docsrc"/>
                                    <fileset dir="${dita.root.folder}/plugins/com.sophos.tocjs" />

                                    <fileset dir="${dita.root.folder}/plugins/org.dita.eclipsehelp" />
                                    <fileset dir="${dita.root.folder}/plugins/org.dita.htmlhelp" />
                                    <fileset dir="${dita.root.folder}/plugins/org.dita.pdf2.axf" />
                                    <fileset dir="${dita.root.folder}/plugins/org.dita.pdf2.xep" />
                                    <fileset dir="${dita.root.folder}/plugins/org.dita.troff" />
                                </delete>
                                <copy overwrite="true" toDir="${dita.root.folder}">
                                    <fileset dir="${project.basedir}/src/main/resources" includes="**/*" />
                                </copy>
                                <echo message="Correcting line breaks for Linux binaries." />
                                <fixcrlf srcDir="${dita.root.folder}/bin"
                                     eol="lf" eof="remove"
                                     includes="ant,dita" />
                                <chmod file="${dita.root.folder}/bin/dita" perm="+x" />
                                <chmod file="${dita.root.folder}/bin/ant" perm="+x" />
                                <echo message="Install the configured PDF DITA OT Plugin: ae-standard-pdf-${ae.dita.version}-dita-plugin.zip" />
                                <property name="dita.dir" value="${dita.root.folder}" />
                                <![CDATA[NOTE: experimenting with RTF output
                                <exec executable="${dita.root.folder}/bin/dita" osfamily="unix">
                                    <arg value="--install" />
                                    <arg value="https://github.com/dita-ot/org.dita.wordrtf/archive/2.3.zip" />
                                </exec>
                                <exec executable="cmd" osfamily="windows">
                                    <arg value="${dita.root.folder}/bin/dita.bat" />
                                    <arg value="--install" />
                                    <arg value="https://github.com/dita-ot/org.dita.wordrtf/archive/2.3.zip" />
                                </exec>]]>
                                <exec executable="${dita.root.folder}/bin/dita" osfamily="unix">
                                    <arg value="--install" />
                                    <arg value="file:///${project.build.directory}/ae-standard-pdf-${ae.dita.version}-dita-plugin.zip" />
                                </exec>
                                <exec executable="cmd" osfamily="windows">
                                    <arg value="${dita.root.folder}/bin/dita.bat" />
                                    <arg value="--install" />
                                    <arg value="file:///${project.build.directory}/ae-standard-pdf-${ae.dita.version}-dita-plugin.zip" />
                                </exec>

                                <echo message="Removing not required content (after plugin installation)." />
                                <delete includeEmptyDirs="true">
                                    <!-- remove deprecated startcmd files -->
                                    <fileset dir="${dita.root.folder}" includes="startcmd.*"/>
                                </delete>

                                <echo message="Creating customized archive." />
                                <move file="${project.build.directory}/${dita.name.string}" tofile="${project.build.directory}/ae-${dita.name.string}-${project.version}" />

                                <zip destfile="${dita.customized.archive}"
                                     basedir="${project.build.directory}"
                                     includes="ae-${dita.name.string}-${project.version}/**" />
                            </target>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.ant</groupId>
                        <artifactId>ant</artifactId>
                        <version>${ant.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>com.google.guava</groupId>
                        <artifactId>guava</artifactId>
                        <version>19.0</version>
                    </dependency>
                    <dependency>
                        <groupId>net.sf.saxon</groupId>
                        <artifactId>Saxon-HE</artifactId>
                        <version>LATEST</version>
                    </dependency>
                    <dependency>
                        <groupId>commons-io</groupId>
                        <artifactId>commons-io</artifactId>
                        <version>2.5</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>install-customized-artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attach-artifact</goal>
                        </goals>
                        <configuration>
                            <artifacts>
                                <artifact>
                                    <file>${dita.customized.archive}</file>
                                    <type>zip</type>
                                    <classifier>bin</classifier>
                                </artifact>
                            </artifacts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>
