<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>
        <artifactId>pi4j-parent</artifactId>
        <groupId>com.pi4j</groupId>
        <version>2.6.1</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>pi4j-distribution</artifactId>
    <name>Pi4J :: DISTRIBUTION :: Pi4J Distribution Packages</name>
    <description>Pi4J Distribution Packages</description>
    <packaging>deb</packaging>

    <!-- PROJECT DEPENDENCIES -->
    <dependencies>

        <!-- include Pi4J Core -->
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-core</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-core</artifactId>
            <version>${project.version}</version>
            <classifier>sources</classifier>
        </dependency>
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-core</artifactId>
            <version>${project.version}</version>
            <classifier>javadoc</classifier>
        </dependency>

        <!-- include Pi4J Plugins (Platforms and I/O Providers) -->
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-plugin-raspberrypi</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-plugin-pigpio</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-plugin-linuxfs</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-plugin-gpiod</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!-- include Pi4J Plugins (Platforms and I/O Providers) -->
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-library-pigpio</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-library-linuxfs</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-library-gpiod</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!--
            include Pi4J native libraries
        -->
        <!-- pigpio -->
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-library-pigpio</artifactId>
            <version>${project.version}</version>
            <classifier>armhf</classifier>
            <type>so</type>
        </dependency>
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-library-pigpio</artifactId>
            <version>${project.version}</version>
            <classifier>aarch64</classifier>
            <type>so</type>
        </dependency>

        <!-- linuxfs -->
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-library-linuxfs</artifactId>
            <version>${project.version}</version>
            <classifier>armhf</classifier>
            <type>so</type>
        </dependency>
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-library-linuxfs</artifactId>
            <version>${project.version}</version>
            <classifier>aarch64</classifier>
            <type>so</type>
        </dependency>

        <!-- gpiod -->
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-library-gpiod</artifactId>
            <version>${project.version}</version>
            <classifier>armhf</classifier>
            <type>so</type>
        </dependency>
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-library-gpiod</artifactId>
            <version>${project.version}</version>
            <classifier>aarch64</classifier>
            <type>so</type>
        </dependency>

    </dependencies>

    <properties>
        <timestamp>${maven.build.timestamp}</timestamp>
        <maven.build.timestamp.format>yyyy-MM-dd hh:mm:ss</maven.build.timestamp.format>
    </properties>

    <!-- BUILD INSTRUCTIONS -->
    <build>
        <extensions>
            <!-- Add support for the "deb" packaging -->
            <extension>
                <groupId>org.vafer</groupId>
                <artifactId>jdeb</artifactId>
            </extension>
        </extensions>

        <plugins>

            <!-- ACQUIRE DEPENDENCIES -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>

                    <!-- DOWNLOAD LICENSE, README & NOTICE ARTIFACTS -->
                    <execution>
                        <id>copy-license</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.pi4j</groupId>
                                    <artifactId>pi4j-parent</artifactId>
                                    <version>${project.version}</version>
                                    <classifier>license</classifier>
                                    <type>txt</type>
                                    <overWrite>true</overWrite>
                                    <destFileName>LICENSE.txt</destFileName>
                                    <outputDirectory>${project.build.directory}/distro-contents</outputDirectory>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>com.pi4j</groupId>
                                    <artifactId>pi4j-parent</artifactId>
                                    <version>${project.version}</version>
                                    <classifier>notice</classifier>
                                    <type>txt</type>
                                    <overWrite>true</overWrite>
                                    <destFileName>NOTICE.txt</destFileName>
                                    <outputDirectory>${project.build.directory}/distro-contents</outputDirectory>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>com.pi4j</groupId>
                                    <artifactId>pi4j-parent</artifactId>
                                    <version>${project.version}</version>
                                    <classifier>readme</classifier>
                                    <type>md</type>
                                    <overWrite>true</overWrite>
                                    <destFileName>README.md</destFileName>
                                    <outputDirectory>${project.build.directory}/distro-contents</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>true</overWriteSnapshots>
                        </configuration>
                    </execution>

                    <!-- DOWNLOAD LIBRARY ARTIFACTS -->
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/distro-contents/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>true</overWriteSnapshots>
                            <excludeTransitive>true</excludeTransitive>
                            <stripVersion>true</stripVersion>

                            <!-- exclude dependencies not intended for distribution  -->
                            <excludeGroupIds>com.fazecast,org.junit.jupiter</excludeGroupIds>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>com.google.code.maven-replacer-plugin</groupId>
                <artifactId>replacer</artifactId>
                <version>1.5.3</version>
                <executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>replace</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <file>${basedir}/src/scripts/pi4j</file>
                    <outputFile>${project.build.directory}/distro-contents/bin/pi4j</outputFile>
                    <replacements>
                        <replacement>
                            <token>%PROJECT_VERSION%</token>
                            <value>${project.version}</value>
                        </replacement>
                        <replacement>
                            <token>%PROJECT_TIMESTAMP%</token>
                            <value>${timestamp}</value>
                        </replacement>
                    </replacements>
                </configuration>
            </plugin>

            <!-- CREATE DEBIAN (.DEB) PACKAGE -->
            <plugin>
                <artifactId>jdeb</artifactId>
                <groupId>org.vafer</groupId>
                <configuration>
                    <attach>true</attach>
                    <deb>${project.build.directory}/pi4j-${project.version}.deb</deb>
                    <installDir>/opt/pi4j</installDir>

                    <signPackage>${pi4j.gpg.sign}</signPackage>
                    <signMethod>dpkg-sig</signMethod>
                    <signRole>builder</signRole>

                    <!-- NOTE:
                        The "{pi4j.gpg.key}" property must be configured in your maven settings.xml file.
                        The "{pi4j.gpg.passphrase}" property must be configured in your maven settings.xml file.
                        (This is intended only for Pi4J public deployment)
                    -->
                    <key>${pi4j.gpg.key}</key>
                    <passphrase>${pi4j.gpg.passphrase}</passphrase>

                    <dataSet>
                        <!--                        <data>-->
                        <!--                            <src>${project.build.directory}/distro-contents/LICENSE.txt</src>-->
                        <!--                            <dst>LICENSE</dst>-->
                        <!--                            <type>file</type>-->
                        <!--                            <dst>/opt/pi4j/LICENSE</dst>-->
                        <!--                        </data>-->
                        <!--                        <data>-->
                        <!--                            <src>${project.build.directory}/distro-contents/NOTICE.txt</src>-->
                        <!--                            <type>file</type>-->
                        <!--                            <dst>/opt/pi4j/NOTICE</dst>-->
                        <!--                        </data>-->
                        <!--                        <data>-->
                        <!--                            <src>${project.build.directory}/distro-contents/README.md</src>-->
                        <!--                            <type>file</type>-->
                        <!--                            <dst>/opt/pi4j/README</dst>-->
                        <!--                        </data>-->

                        <!-- include all meta files (README, LICENSE, etc) -->
                        <data>
                            <src>${project.build.directory}/distro-contents</src>
                            <type>directory</type>
                            <mapper>
                                <type>perm</type>
                                <prefix>/opt/pi4j</prefix>
                            </mapper>
                        </data>

                        <!-- include all project JARs -->
                        <data>
                            <src>${project.build.directory}/distro-contents/lib</src>
                            <type>directory</type>
                            <mapper>
                                <type>perm</type>
                                <prefix>/opt/pi4j/lib</prefix>
                            </mapper>
                        </data>

                        <!-- include `pi4j` bash shell script -->
                        <data>
                            <src>${project.build.directory}/distro-contents/bin/pi4j</src>
                            <type>file</type>
                            <dst>/opt/pi4j/bin/pi4j</dst>
                            <mapper>
                                <type>perm</type>
                                <filemode>755</filemode>
                            </mapper>
                        </data>
                    </dataSet>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>

                    <!-- ENSURE ALL LINE ENDINGS ARE <LF> ONLY FOR TEXT BASED FILES -->
                    <execution>
                        <id>create-target-distro-directories</id>
                        <phase>initialize</phase>
                        <configuration>
                            <target>
                                <mkdir dir="${project.build.directory}/distro-contents/bin"/>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>

                    <!-- ENSURE ALL LINE ENDINGS ARE <LF> ONLY FOR TEXT BASED FILES -->
                    <execution>
                        <id>ant-fix-line-endings</id>
                        <phase>prepare-package</phase>
                        <configuration>
                            <target>
                                <fixcrlf srcdir="${project.build.directory}/distro-contents"
                                         eol="unix"
                                         excludes="**/*.jar"/>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>

                    <!-- OPTIONALLY DEPLOY THE FINAL .DEB TO THE RASPBERRY PI -->
                    <!-- copy the compiled JAR file to the Raspberry Pi platform platform -->
                    <execution>
                        <id>transfer-packaged-pi4j-deb</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 -->
                                        <sshexec host="${pi4j.dev.host}" port="${pi4j.dev.port}"
                                                 username="${pi4j.dev.user}"
                                                 password="${pi4j.dev.password}" trust="true" failonerror="false"
                                                 verbose="true" command="mkdir --parents ${pi4j.dev.directory}"/>

                                        <!-- copy the DEB file to the Raspberry Pi -->
                                        <scp
                                            file="${project.build.directory}/pi4j-${project.version}.deb"
                                            remoteTofile="${pi4j.dev.user}:${pi4j.dev.password}@${pi4j.dev.host}:${pi4j.dev.directory}/pi4j.deb"
                                            port="${pi4j.dev.port}" trust="true" verbose="true" failonerror="true">
                                        </scp>
                                    </then>
                                </if>
                            </target>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.ant</groupId>
                        <artifactId>ant-jsch</artifactId>
                        <version>${ant-jsch.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>com.jcraft</groupId>
                        <artifactId>jsch</artifactId>
                        <version>${jsch.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>ant-contrib</groupId>
                        <artifactId>ant-contrib</artifactId>
                        <version>${ant-contrib.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

            <!-- CREATE .ZIP PACKAGE OF Pi4J DISTRIBUTION -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <tarLongFileMode>posix</tarLongFileMode>
                    <finalName>pi4j-${project.version}</finalName>
                    <descriptors>
                        <descriptor>src/assembly/distribution.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-distribution-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
    </profiles>
</project>
