<?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>

    <parent>
        <groupId>com.atlassian.bitbucket.server</groupId>
        <artifactId>bitbucket-mesh-parent</artifactId>
        <version>7.17.4-BSERV-13141</version>
    </parent>

    <artifactId>bitbucket-mesh-rpc</artifactId>

    <name>Bitbucket Mesh - RPC</name>

    <dependencies>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-protobuf</artifactId>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-stub</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>proto-sources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>src/main/assembly/protos.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-proto-source-root</id>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${project.basedir}/src/main/proto</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <configuration>
                    <protocArtifact>
                        com.google.protobuf:protoc:${protobuf.libversion}:exe:${os.detected.classifier}
                    </protocArtifact>
                </configuration>
                <executions>
                    <execution>
                        <id>protoc-compile</id>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>grpc-compile</id>
                        <goals>
                            <goal>compile-custom</goal>
                        </goals>
                        <configuration>
                            <!-- The compile execution has already attached these, so if we attach them here
                                 too we end up with all the *.proto files copied twice -->
                            <attachProtoSources>false</attachProtoSources>
                            <pluginId>grpc-java</pluginId>
                            <pluginArtifact>
                                io.grpc:protoc-gen-grpc-java:${grpc.libversion}:exe:${os.detected.classifier}
                            </pluginArtifact>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.6.2</version>
            </extension>
        </extensions>
    </build>

    <profiles>
        <!-- DO NOT USE THIS PROFILE UNDER ANY OS BUT WINDOWS.
             - The git packages contain Windows binaries and will not run on Linux or MacOS

             This profile exists to support git version matrix testing on BEAC, but it may also be used by developers
             who run Windows. -->
        <profile>
            <id>git-windows</id>
            <activation>
                <property>
                    <name>gfw.version</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-protobuf-executables</id>
                                <goals>
                                    <goal>copy</goal>
                                </goals>
                                <phase>initialize</phase>
                                <configuration>
                                    <artifactItems>
                                        <artifactItem>
                                            <groupId>com.google.protobuf</groupId>
                                            <artifactId>protoc</artifactId>
                                            <version>${protobuf.libversion}</version>
                                            <type>exe</type>
                                            <classifier>${os.detected.classifier}</classifier>
                                        </artifactItem>
                                        <artifactItem>
                                            <groupId>io.grpc</groupId>
                                            <artifactId>protoc-gen-grpc-java</artifactId>
                                            <version>${grpc.libversion}</version>
                                            <type>exe</type>
                                            <classifier>${os.detected.classifier}</classifier>
                                        </artifactItem>
                                    </artifactItems>
                                    <outputDirectory>${project.build.directory}/protoc-plugins</outputDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <!-- Since plexus-utils 3.1.0 (which is a transitive dependency of maven-dependency-plugin),
                         files are copied using Files.copy, also copying any file attributes. On some Windows
                         machines, this means copying the .exe without execute permissions. The following code
                         adds the executable permission again for the owner of the file. -->
                    <plugin>
                        <groupId>org.codehaus.gmavenplus</groupId>
                        <artifactId>gmavenplus-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>protobuf-set-executable</id>
                                <goals>
                                    <goal>execute</goal>
                                </goals>
                                <phase>initialize</phase>
                                <configuration>
                                    <scripts>
                                        <script><![CDATA[
                                            import java.nio.file.Files
                                            import java.nio.file.Paths
                                            import java.nio.file.attribute.AclEntry
                                            import java.nio.file.attribute.AclFileAttributeView

                                            import static java.nio.file.attribute.AclEntryPermission.EXECUTE
                                            import static java.nio.file.attribute.AclEntryPermission.READ_DATA
                                            import static java.nio.file.attribute.AclEntryType.ALLOW

                                            new File($/${project.build.directory}/$ + File.separator + 'protoc-plugins').eachFileRecurse { file ->
                                                def view = Files.getFileAttributeView(file.toPath(), AclFileAttributeView)
                                                def acl = view.getAcl()
                                                if (acl.find { view.getOwner() == it.principal() && it.permissions().contains(EXECUTE) } == null) {
                                                    println 'File ' + file.toPath() + ' was missing EXECUTE permission, trying to add...'
                                                    def entry = AclEntry.newBuilder()
                                                            .setType(ALLOW)
                                                            .setPrincipal(view.getOwner())
                                                            .setPermissions(EXECUTE, READ_DATA)
                                                            .build()
                                                    acl.add(entry)
                                                    view.setAcl(acl)
                                                }
                                            }
                                            ]]></script>
                                    </scripts>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>
