<?xml version="1.0" encoding="UTF-8"?>
<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>

    <!-- MAVEN PARENT POM -->
    <parent>
        <artifactId>pi4j-library</artifactId>
        <groupId>com.pi4j</groupId>
        <version>2.3.0</version>
        <relativePath>../pi4j-library/pom.xml</relativePath>
    </parent>

    <!-- MAVEN ARTIFACT INFORMATION -->
    <artifactId>pi4j-library-linuxfs</artifactId>
    <name>Pi4J :: LIBRARY  :: JNI Wrapper for LinuxFS Library</name>
    <description>Pi4J wrapper for the LinuxFS library</description>
    <packaging>jar</packaging>

    <!-- PROJECT DEPENDENCIES -->
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>${slf4j.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!-- STANDARD BUILD INSTRUCTIONS -->
    <build>

        <!-- RESOURCES TO INCLUDE IN FINAL JAR PACKAGE -->
        <resources>
            <resource>
                <directory>${project.build.directory}</directory>
                <filtering>false</filtering>
                <includes>
                    <include>LICENSE.txt</include>
                    <include>NOTICE.txt</include>
                    <include>README.md</include>
                    <include>lib/armhf/pi4j-linuxfs/libpi4j-linuxfs.so</include> <!-- include the native 32-bit JNI library as a resource -->
                    <include>lib/aarch64/pi4j-linuxfs/libpi4j-linuxfs.so</include> <!-- include the native 64-bit JNI library as a resource -->
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
            </resource>
        </resources>

        <!-- BUILD PLUGINS -->
        <plugins>

            <!-- ENFORCE THAT THE NATIVE JNI SO FILES EXISTS TO BE INCLUDED IN FINAL JAR           -->
            <!-- (The "lippi4j-linuxfs.so" JNI library is embedded as a resource in the JAR file.) -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <executions>
                    <execution>
                        <id>enforce-native-jni-file--exist</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <phase>prepare-package</phase>
                        <configuration>
                            <rules>
                                <requireFilesExist>
                                    <files>
                                        <!-- include 32-bit (ARMHF) Pi4J LinuxFS JNI Library  -->
                                        <file>${project.build.outputDirectory}/lib/armhf/pi4j-linuxfs/libpi4j-linuxfs.so</file>
                                        <!-- include 64-bit (AARCH64) Pi4J LinuxFS JNI Library  -->
                                        <file>${project.build.outputDirectory}/lib/aarch64/pi4j-linuxfs/libpi4j-linuxfs.so</file>
                                    </files>
                                </requireFilesExist>
                            </rules>
                            <fail>true</fail>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <!-- OPTIONALLY DEPLOY THE FINAL JAR (AND ANY OTHER RUNTIME DEPENDENCIES) TO THE RASPBERRY PI -->
                    <!-- (copy the compiled JAR file to the Raspberry Pi platform platform vis SSH/SCP)           -->
                    <execution>
                        <id>transfer-compiled-pi4j-jar</id>
                        <phase>install</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <taskdef resource="net/sf/antcontrib/antcontrib.properties"
                                         classpathref="maven.plugin.classpath" />
                                <if>
                                    <equals arg1="${pi4j.dev.transfer}" arg2="true" />
                                    <then>
                                        <!-- ensure the target directory exists on the Raspberry Pi via SSH -->
                                        <sshexec host="${pi4j.dev.host}" port="${pi4j.dev.port}" username="${pi4j.dev.user}"
                                                 password="${pi4j.dev.password}" trust="true" failonerror="false"
                                                 verbose="false" command="mkdir --parents ${pi4j.dev.directory}" />

                                        <!-- copy the JAR file to the Raspberry Pi vs SCP -->
                                        <scp file="${project.build.directory}/${project.build.finalName}.jar"
                                             todir="${pi4j.dev.user}:${pi4j.dev.password}@${pi4j.dev.host}:${pi4j.dev.directory}"
                                             port="${pi4j.dev.port}" trust="true" verbose="false" failonerror="true">
                                        </scp>
                                    </then>
                                </if>
                            </target>
                        </configuration>
                    </execution>

                    <!-- ATTACH NATIVE LIBRARY ARTIFACT -->
                    <!-- attach the compiled JNI native library ("libpi4j-linuxfs.so") as an artifact -->
                    <execution>
                        <id>pi4j-attach-native</id>
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target name="pi4j-attach-native-library" >
                                <taskdef  resource="net/sf/antcontrib/antcontrib.properties"
                                          classpathref="maven.plugin.classpath"/>
                                <!-- attach 32-bit (ARMHF) Pi4J LinuxFS JNI Library  -->
                                <attachartifact file="${project.build.directory}/lib/armhf/pi4j-linuxfs/libpi4j-linuxfs.so" classifier="armhf" type="so"/>
                                <!-- attach 64-bit (AARCH64) Pi4J LinuxFS JNI Library  -->
                                <attachartifact file="${project.build.directory}/lib/aarch64/pi4j-linuxfs/libpi4j-linuxfs.so" classifier="aarch64" type="so"/>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- DOWNLOAD AND COPY ANY RUNTIME DEPENDENCIES TO THE TARGET DIRECTORY -->
            <!-- (if using the development transfer option, these will be copied    -->
            <!--  over to the hardware platform via ssh. (mvn install -Ptransfer) ) -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeScope>runtime</includeScope>
                            <excludeGroupIds>com.pi4j</excludeGroupIds>
                            <outputDirectory>${project.build.directory}/dependencies</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <!-- CUSTOM BUILD PROFILES -->
    <profiles>

        <!-- DEFAULT BUILD PROFILE -->
        <!-- This profile is used when building the JAR only and not building the native sources.    -->
        <!-- This profile will automatically download the required JNI SO library file required      -->
        <!-- to be included/embedded as a resource in the final packaged JAR file from a Maven Repo. -->
        <!-- Alternatively, you can use the "native" profile to build the native JNI source instead. -->
        <profile>
            <id>default</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <!-- RETRIEVE THE JNI NATIVE LIBRARY (SO FILE) FROM MAVEN REPO -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>download-jni-dependency</id>
                                <phase>validate</phase>
                                <goals>
                                    <goal>copy</goal>
                                </goals>
                                <configuration>
                                    <artifactItems>
                                        <!-- LIBPI4J-LinuxFS for the Raspberry-Pi 32-bit (ARMHF) platform  -->
                                        <!-- (download the JNI native library to a staging directory -->
                                        <!--  so it can be included in the final packaged jar)       -->
                                        <artifactItem>
                                            <groupId>${project.groupId}</groupId>
                                            <artifactId>${project.artifactId}</artifactId>
                                            <version>${project.version}</version>
                                            <classifier>armhf</classifier>
                                            <type>so</type>
                                            <overWrite>true</overWrite>
                                            <outputDirectory>${project.build.directory}/lib/armhf/pi4j-linuxfs</outputDirectory>
                                            <destFileName>libpi4j-linuxfs.so</destFileName>
                                        </artifactItem>

                                        <!-- LIBPI4J-LinuxFS for the Raspberry-Pi 64-bit (AARCH64) platform -->
                                        <!-- (download the JNI native library to a staging directory -->
                                        <!--  so it can be included in the final packaged jar)       -->
                                        <artifactItem>
                                            <groupId>${project.groupId}</groupId>
                                            <artifactId>${project.artifactId}</artifactId>
                                            <version>${project.version}</version>
                                            <type>so</type>
                                            <classifier>aarch64</classifier>
                                            <overWrite>true</overWrite>
                                            <outputDirectory>${project.build.directory}/lib/aarch64/pi4j-linuxfs</outputDirectory>
                                            <destFileName>libpi4j-linuxfs.so</destFileName>
                                        </artifactItem>
                                    </artifactItems>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <!-- NATIVE BUILD PROFILE -->
        <!-- This profile is optionally used when needing to re-compile the "libpi4j-LinuxFS.so".   -->
        <!-- JNI native library from sources and included in the final packaged JAR file.          -->
        <!-- This profile must be run when producing a "release" build or performing a deployment. -->
        <!-- Please see the <pi4j.native.compiler> property in the master parent POM for further   -->
        <!-- details on compiler options used to compile these native sources.                     -->
        <profile>
            <id>native</id>
            <activation>
                <property>
                    <name>release-build</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <!-- ensure that a target directory has been created and copy the JNI native sources -->
                            <execution>
                                <id>pi4j-prepare-native</id>
                                <phase>generate-resources</phase>
                                <configuration>
                                    <target>
                                        <mkdir dir="target" />
                                        <mkdir dir="target/classes" />
                                        <mkdir dir="target/build" />
                                        <mkdir dir="target/build/native" />
                                        <copy todir="target/build/native">
                                            <fileset dir="src/main/native"/>
                                        </copy>
                                        <chmod dir="target/build/native" perm="ugo+rx" includes="*.sh"/>
                                    </target>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>

                            <!-- TODO :: Reconfigure the build script to execute the Docker commands directly
                                         instead of bash script (to provide build support for Windows) -->
                            <!-- build the native JNI library (and any dependencies needed)   -->
                            <!-- copy the compiled JNI SO files to the "lib" staging directory -->
                            <execution>
                                <id>pi4j-build-native</id>
                                <phase>generate-resources</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <target>
                                        <taskdef resource="net/sf/antcontrib/antcontrib.properties"
                                                 classpathref="maven.plugin.classpath" />
                                        <if>
                                            <!-- check for DOCKER-COMPILER setting in the 'pi4j.native.compiler' property -->
                                            <equals arg1="${pi4j.native.compiler.effective}" arg2="DOCKER-COMPILER" />
                                            <then>
                                                <!-- perform native build using Pi4J Docker Builder Image for native cross-compiling -->
                                                <exec dir="${project.build.directory}/build/native" executable="/bin/bash" failonerror="true">
                                                    <arg value="build-docker.sh"/>
                                                </exec>
                                            </then>
                                            <else>
                                                <if>
                                                    <!-- check for CROSS-COMPILER setting in the 'pi4j.native.compiler' property -->
                                                    <equals arg1="${pi4j.native.compiler.effective}" arg2="CROSS-COMPILER" />
                                                    <then>
                                                        <!-- perform native build using cross-compiler toolchains directly on host -->
                                                        <exec dir="${project.build.directory}/build/native" executable="/bin/bash" failonerror="true">
                                                            <arg value="build.sh"/>
                                                        </exec>
                                                    </then>
                                                    <else>
                                                        <fail message="NATIVE BUILD FAILED/ABORTED; Missing or invalid 'pi4j.native.compiler' property. [${pi4j.native.compiler}]; Expected 'CROSS-COMPILER|DOCKER-COMPILER'"/>
                                                    </else>
                                                </if>
                                            </else>
                                        </if>

                                        <!-- copy compiled 32-bit (ARMHF) Pi4J LinuxFS JNI Library  -->
                                        <copy failonerror="true" includeEmptyDirs="false"
                                              file="${project.build.directory}/build/native/lib/armhf/pi4j-linuxfs/libpi4j-linuxfs.so"
                                              todir="${project.build.directory}/lib/armhf/pi4j-linuxfs"/>

                                        <!-- copy compiled 64-bit (AARCH64) Pi4J LinuxFS JNI Library  -->
                                        <copy failonerror="true" includeEmptyDirs="false"
                                              file="${project.build.directory}/build/native/lib/aarch64/pi4j-linuxfs/libpi4j-linuxfs.so"
                                              todir="${project.build.directory}/lib/aarch64/pi4j-linuxfs"/>
                                    </target>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>
