<?xml version="1.0" encoding="UTF-8"?>
<!--

  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

      https://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 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.apache.training</groupId>
        <artifactId>training-tools</artifactId>
        <version>1.3.0</version>
    </parent>

    <artifactId>content-parent-pom</artifactId>
    <version>1.3.0</version>
    <packaging>pom</packaging>

    <name>Training: Tools: Content Master Pom</name>
    <description>
        Parent pom containing all the configuration for building presentations
        with the training template.
    </description>

    <properties>
        <project.main.contentfile>index</project.main.contentfile>
        <!-- Put your custom theme files in src/main/theme and reference the name (without ".css") here. -->
        <project.main.theme>apache</project.main.theme>
        <project.slides.directory>${project.build.directory}/generated-slides</project.slides.directory>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <java.version>1.8</java.version>

        <asciidoctorj.version>3.0.0</asciidoctorj.version>
        <asciidoctorj-revealjs.version>5.1.0</asciidoctorj-revealjs.version>
        <asciidoctorj.diagram.version>2.3.1</asciidoctorj.diagram.version>

        <revealjs.version>5.1.0</revealjs.version>

        <asciidoctor-maven-plugin.version>3.0.0</asciidoctor-maven-plugin.version>
        <maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version>
        <download-maven-plugin.version>1.9.0</download-maven-plugin.version>
        <groovy-maven-plugin.version>2.1.1</groovy-maven-plugin.version>
        <jetty-maven-plugin.version>11.0.23</jetty-maven-plugin.version>
        <maven-resources-plugin.version>3.3.1</maven-resources-plugin.version>
        <maven-war-plugin.version>3.4.0</maven-war-plugin.version>
        <maven-dependency-plugin.version>3.8.0</maven-dependency-plugin.version>
    </properties>

    <dependencies>
        <!-- Just to ensure this is built first as we need this to exist in order to unpack it -->
        <dependency>
            <groupId>org.apache.training</groupId>
            <artifactId>content-parent-resources</artifactId>
            <version>1.3.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.asciidoctor</groupId>
            <artifactId>asciidoctorj-revealjs</artifactId>
            <version>${asciidoctorj-revealjs.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Check if all source files have the required apache license headers -->
            <plugin>
                <groupId>org.apache.rat</groupId>
                <artifactId>apache-rat-plugin</artifactId>
                <version>0.16.1</version>
                <executions>
                    <execution>
                        <id>license-check</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <consoleOutput>true</consoleOutput>
                    <excludes>
                        <!-- Maven related files -->
                        <exclude>**/target/**</exclude>

                        <!-- Eclipse related files -->
                        <exclude>**/.project</exclude>
                        <exclude>**/.settings/**</exclude>
                        <exclude>**/.classpath</exclude>

                        <!-- IntelliJ related files -->
                        <exclude>**/.idea/**</exclude>
                        <exclude>**/*.iml</exclude>

                        <!-- Asciidoctor generated files -->
                        <exclude>**/.asciidoctor/**</exclude>

                        <!-- JSON doesn't like comments -->
                        <exclude>**/*.json</exclude>
                        <!-- Mermaid does't like comments -->
                        <exclude>**/*.mmd</exclude>

                        <!-- Output of the profiler maven extension -->
                        <exclude>**/.profiler/**</exclude>

                        <exclude>licenses/**</exclude>
                        <exclude>README.md</exclude>

                        <!-- Exclude the Docker local repo -->
                        <exclude>out/**</exclude>
                    </excludes>
                </configuration>
            </plugin>

            <!-- Download all the tools needed to generate the output -->
            <plugin>
                <groupId>com.googlecode.maven-download-plugin</groupId>
                <artifactId>download-maven-plugin</artifactId>
                <version>${download-maven-plugin.version}</version>
                <executions>
                    <!-- Get the reveal js sources -->
                    <execution>
                        <id>install-revealjs</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>wget</goal>
                        </goals>
                        <configuration>
                            <url>https://codeload.github.com/hakimel/reveal.js/zip/refs/tags/${revealjs.version}</url>
                            <unpack>true</unpack>
                            <outputFileName>reveal.js-${revealjs.version}.zip</outputFileName>
                            <outputDirectory>${project.slides.directory}</outputDirectory>
                        </configuration>
                    </execution>
                    <!-- Download the C3 and D3 libraries -->
                    <execution>
                        <id>download-c3-css</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>wget</goal>
                        </goals>
                        <configuration>
                            <url>https://cdnjs.cloudflare.com/ajax/libs/c3/0.6.12/c3.min.css</url>
                            <outputDirectory>${project.slides.directory}/css</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>download-c3-js</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>wget</goal>
                        </goals>
                        <configuration>
                            <url>https://cdnjs.cloudflare.com/ajax/libs/c3/0.6.12/c3.min.js</url>
                            <outputDirectory>${project.slides.directory}/js</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>download-d3-js</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>wget</goal>
                        </goals>
                        <configuration>
                            <url>https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js</url>
                            <outputDirectory>${project.slides.directory}/js</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!--
                Make sure the filtered resources are output to the slides directory and
                theme content is output to the themes directory of reveal js.
            -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>${maven-resources-plugin.version}</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                            <resources>
                                <!-- Copy the normal resources into the root of the slides directory -->
                                <resource>
                                    <directory>src/main/resources</directory>
                                    <targetPath>${project.slides.directory}</targetPath>
                                </resource>
                                <!-- Copy our css-theme to the reveal.js theme directory -->
                                <resource>
                                    <directory>src/main/theme</directory>
                                    <filtering>true</filtering>
                                    <targetPath>${project.slides.directory}/reveal.js-${revealjs.version}/dist/theme</targetPath>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Get some static Apache resources shared throughout all presentations -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>${maven-dependency-plugin.version}</version>
                <executions>
                    <execution>
                        <id>copy-asciidoctor-revealjs-remplates</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.asciidoctor</groupId>
                                    <artifactId>asciidoctorj-revealjs</artifactId>
                                    <type>jar</type>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${project.build.directory}/asciidoctorj-revealjs-${asciidoctorj-revealjs.version}</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                    <!-- Update the document.html.slim with an updated one as currently the docinfo part seems to not work -->
                    <execution>
                        <id>copy-asciidoctor-apache-training-resources</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.apache.training</groupId>
                                    <artifactId>content-parent-resources</artifactId>
                                    <type>jar</type>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${project.build.directory}/asciidoctorj-revealjs-${asciidoctorj-revealjs.version}/gems/asciidoctor-revealjs-${asciidoctorj-revealjs.version}/templates</outputDirectory>
                                    <includes>*.slim</includes>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-training-presentation-themes</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.apache.training</groupId>
                                    <artifactId>content-parent-resources</artifactId>
                                    <type>jar</type>
                                    <outputDirectory>${project.slides.directory}/reveal.js-${revealjs.version}/css/theme
                                    </outputDirectory>
                                    <includes>*.css</includes>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-training-presentation-resources</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.apache.training</groupId>
                                    <artifactId>content-parent-resources</artifactId>
                                    <type>jar</type>
                                    <outputDirectory>${project.slides.directory}/images</outputDirectory>
                                    <excludes>*.slim,*.css,META-INF/**</excludes>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Configure the asciidoctor generation -->
            <plugin>
                <groupId>org.asciidoctor</groupId>
                <artifactId>asciidoctor-maven-plugin</artifactId>
                <version>${asciidoctor-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>generate-slides</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>process-asciidoc</goal>
                        </goals>
                        <configuration>
                            <backend>revealjs</backend>
                            <requires>
                                <require>asciidoctor-revealjs</require>
                                <require>asciidoctor-diagram</require>
                            </requires>
                            <!-- Configure where are all the asciidoc source files located -->
                            <sourceDirectory>src/main/asciidoc</sourceDirectory>
                            <!-- Configure the target director to where will the output be generated -->
                            <outputDirectory>${project.slides.directory}</outputDirectory>
                            <templateDirs>
                                <templateDir>${project.build.directory}/asciidoctorj-revealjs-${asciidoctorj-revealjs.version}/gems/asciidoctor-revealjs-${asciidoctorj-revealjs.version}/templates</templateDir>
                            </templateDirs>
                            <attributes>
                                <revealjsdir>reveal.js-${revealjs.version}</revealjsdir>
                                <!-- All image resources will be located in this directory -->
                                <imagesdir>images</imagesdir>
                                <docinfodir>${project.basedir}/src/main/asciidoc</docinfodir>
                                <!--
                                    Use our custom theme, which is defined by src/main/theme/apache.css
                                    and copied inside the reveal.js installation by the resources plugin a few lines
                                    up inside this pom.
                                -->
                                <revealjs_theme>${project.main.theme}</revealjs_theme>
                                <!-- Some basic settings -->
                                <revealjs_transition>linear</revealjs_transition>
                                <project-version>${project.version}</project-version>
                                <source-highlighter>highlightjs</source-highlighter>
                                <!--highlightjs-theme>https://raw.githubusercontent.com/isagalaev/highlight.js/master/src/styles/arduino-light.css</highlightjs-theme-->
                                <erd>${basedir}/libs/third-party/erd/.stack-work/dist/x86_64-osx/Cabal-2.2.0.1/build/erd/erd</erd>
                                <!--convert>${basedir}/libs/third-party/ImageMagick-7.0.8/bin/convert</convert>
                                <identify>${basedir}/libs/third-party/ImageMagick-7.0.8/bin/identify</identify-->
                                <mermaid>${basedir}/libs/third-party/node_modules/.bin/mmdc</mermaid>
                                <phantomjs>${basedir}/libs/third-party/phantomjs-2.1.1-macosx/bin/phantomjs</phantomjs>
                                <svgbob>${basedir}/libs/third-party/svgbob/bin/svgbob</svgbob>
                                <vg2svg>${basedir}/libs/third-party/node_modules/.bin/vg2svg</vg2svg>
                            </attributes>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.asciidoctor</groupId>
                        <artifactId>asciidoctorj</artifactId>
                        <version>${asciidoctorj.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.asciidoctor</groupId>
                        <artifactId>asciidoctorj-revealjs</artifactId>
                        <version>${asciidoctorj-revealjs.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.asciidoctor</groupId>
                        <artifactId>asciidoctorj-diagram</artifactId>
                        <version>${asciidoctorj.diagram.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

            <!--
                As it's the "revealjs" output, asciidoctor will name the file with an ending "revealjs".
                Here we rename that file back to "html".
            -->
            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>groovy-maven-plugin</artifactId>
                <version>${groovy-maven-plugin.version}</version>
                <executions>
                    <!-- Set some dynamic variables which are useful for the site generation -->
                    <execution>
                        <id>rename-revealjs-files</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <source>
                                import static groovy.io.FileType.FILES

                                println "\nRenaming output files:"
                                def baseDirectory = project.model.pomFile.parent
                                def slideOutputDirectory = new File(baseDirectory, "target/generated-slides")
                                if (slideOutputDirectory.exists()) {
                                    slideOutputDirectory.eachFileRecurse(FILES) {
                                        if (it.name.endsWith('.revealjs')) {
                                            def oldFileName = it.name
                                            def newFileName = oldFileName.substring(0, oldFileName.lastIndexOf(".")) + ".html"
                                            it.renameTo(new File(it.parent, newFileName))
                                            println "Renaming " + oldFileName + " to " + newFileName
                                        }
                                    }
                                    println ""
                                }
                            </source>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- If the packaging is se to "war", pack the slides into a war file -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>${maven-war-plugin.version}</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <warSourceDirectory>${project.slides.directory}</warSourceDirectory>
                </configuration>
            </plugin>
        </plugins>

        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${maven-compiler-plugin.version}</version>
                    <configuration>
                        <release>8</release>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.eclipse.jetty</groupId>
                    <artifactId>jetty-maven-plugin</artifactId>
                    <version>${jetty-maven-plugin.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>
