<?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">
    <parent>
        <artifactId>atlassian-amps-parent</artifactId>
        <groupId>com.atlassian.amps</groupId>
        <version>4.2.2</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>sdk-installer</artifactId>
    <packaging>pom</packaging>
    <name>SDK Installer</name>

    <profiles>
        <profile>
            <id>installer-win</id>
            <build>
                <finalName>atlassian-plugin-sdk</finalName>

                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>unpack-for-windows</id>
                                <goals>
                                    <goal>unpack</goal>
                                </goals>
                                <phase>generate-resources</phase>
                                <configuration>
                                    <artifactItems>
                                        <artifactItem>
                                            <groupId>${groupId}</groupId>
                                            <artifactId>atlassian-plugin-sdk</artifactId>
                                            <version>${version}</version>
                                            <type>zip</type>
                                            <overWrite>true</overWrite>
                                            <outputDirectory>${project.build.directory}/win-unzip</outputDirectory>
                                        </artifactItem>
                                    </artifactItems>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.1</version>
                        <executions>
                            <execution>
                                <id>installbuilder-windows</id>
                                <phase>compile</phase>
                                <configuration>
                                    <executable>${installbuilder.builder}</executable>
                                    <outputFile>${project.build.directory}/installbuilder-out.txt</outputFile>
                                    <arguments>
                                        <argument>build</argument>
                                        <argument>amps-installbuilder.xml</argument>
                                        <argument>windows</argument>
                                        <argument>--license</argument>
                                        <argument>${project.basedir}/installbuilder-license.xml</argument>
                                        <argument>--setvars</argument>
                                        <argument>project.version=${version}</argument>
                                        <argument>project.outputDirectory=${project.build.directory}</argument>
                                        <argument>sdkUnzipDir=${project.build.directory}/win-unzip/atlassian-plugin-sdk-${project.version}/*</argument>
                                    </arguments>
                                </configuration>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>build-helper-maven-plugin</artifactId>
                        <version>1.7</version>
                        <executions>
                            <execution>
                                <id>parse-win-version</id>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>parse-version</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>attach-win-installer</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>attach-artifact</goal>
                                </goals>
                                <configuration>
                                    <artifacts>
                                        <artifact>
                                            <file>${project.build.directory}/atlassian-plugin-sdk-${project.version}-windows.exe</file>
                                            <type>exe</type>
                                        </artifact>
                                    </artifacts>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>installer-linux</id>
            <build>
                <finalName>atlassian-plugin-sdk</finalName>

                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>build-helper-maven-plugin</artifactId>
                        <version>1.7</version>
                        <executions>
                            <execution>
                                <id>parse-deb-version</id>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>parse-version</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>timestamp-property</id>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>timestamp-property</goal>
                                </goals>
                                <configuration>
                                    <name>snap.ts</name>
                                    <pattern>yyyyMMddHHmmss</pattern>
                                </configuration>
                            </execution>
                            <execution>
                                <id>deb-version</id>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>regex-property</goal>
                                </goals>
                                <configuration>
                                    <name>deb.version</name>
                                    <value>${project.version}</value>
                                    <regex>-SNAPSHOT</regex>
                                    <replacement>~${snap.ts}</replacement>
                                    <failIfNoMatch>false</failIfNoMatch>
                                </configuration>
                            </execution>
                        
                            <execution>
                                <id>rpm-snapshot-version</id>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>regex-property</goal>
                                </goals>
                                <configuration>
                                    <name>rpm.ts.version</name>
                                    <value>${project.version}</value>
                                    <regex>-SNAPSHOT</regex>
                                    <replacement>_${snap.ts}_SNAPSHOT</replacement>
                                    <failIfNoMatch>false</failIfNoMatch>
                                </configuration>
                            </execution>
                            <execution>
                                <id>rpm-version</id>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>regex-property</goal>
                                </goals>
                                <configuration>
                                    <name>rpm.version</name>
                                    <value>${rpm.ts.version}</value>
                                    <regex>[-]</regex>
                                    <replacement>_</replacement>
                                    <failIfNoMatch>false</failIfNoMatch>
                                </configuration>
                            </execution>
                            <execution>
                                <id>attach-linux-installers</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>attach-artifact</goal>
                                </goals>
                                <configuration>
                                    <artifacts>
                                        <artifact>
                                            <file>${project.build.directory}/atlassian-plugin-sdk-${deb.version}.deb</file>
                                            <type>deb</type>
                                        </artifact>
                                        <artifact>
                                            <file>${project.build.directory}/atlassian-plugin-sdk-${rpm.version}.noarch.rpm</file>
                                            <type>rpm</type>
                                        </artifact>
                                    </artifacts>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>unpack-for-deb</id>
                                <goals>
                                    <goal>unpack</goal>
                                </goals>
                                <phase>generate-resources</phase>
                                <configuration>
                                    <artifactItems>
                                        <artifactItem>
                                            <groupId>${groupId}</groupId>
                                            <artifactId>atlassian-plugin-sdk</artifactId>
                                            <version>${version}</version>
                                            <type>tar.gz</type>
                                            <overWrite>true</overWrite>
                                            <outputDirectory>${project.build.directory}/deb-work/usr/share/</outputDirectory>
                                        </artifactItem>
                                    </artifactItems>
                                </configuration>
                            </execution>
                            <execution>
                                <id>unpack-for-rpm</id>
                                <goals>
                                    <goal>unpack</goal>
                                </goals>
                                <phase>generate-resources</phase>
                                <configuration>
                                    <artifactItems>
                                        <artifactItem>
                                            <groupId>${groupId}</groupId>
                                            <artifactId>atlassian-plugin-sdk</artifactId>
                                            <version>${version}</version>
                                            <type>tar.gz</type>
                                            <overWrite>true</overWrite>
                                            <outputDirectory>${project.build.directory}/rpm-work/unzip/usr/share/</outputDirectory>
                                        </artifactItem>
                                    </artifactItems>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-deb</id>
                                <phase>prepare-package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                                <configuration>
                                    <finalName>deb-work</finalName>
                                    <outputDirectory>${project.build.directory}</outputDirectory>
                                    <appendAssemblyId>false</appendAssemblyId>
                                    <attach>false</attach>
                                    <descriptors>
                                        <descriptor>src/main/assembly/assembly-deb.xml</descriptor>
                                    </descriptors>
                                </configuration>
                            </execution>
                            <execution>
                                <id>copy-rpm</id>
                                <phase>prepare-package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                                <configuration>
                                    <finalName>rpm-work</finalName>
                                    <outputDirectory>${project.build.directory}</outputDirectory>
                                    <appendAssemblyId>false</appendAssemblyId>
                                    <attach>false</attach>
                                    <descriptors>
                                        <descriptor>src/main/assembly/assembly-rpm.xml</descriptor>
                                    </descriptors>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.1</version>
                        <executions>
                            <execution>
                                <id>native-deb</id>
                                <phase>package</phase>
                                <configuration>
                                    <executable>${basedir}/deb-builder.sh</executable>
                                    <outputFile>${project.build.directory}/dpkg-out.txt</outputFile>
                                    <arguments>
                                        <argument>${project.version}</argument>
                                        <argument>${deb.version}</argument>
                                    </arguments>
                                </configuration>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>native-rpm</id>
                                <phase>package</phase>
                                <configuration>
                                    <executable>${basedir}/rpm-builder.sh</executable>
                                    <outputFile>${project.build.directory}/rpm-out.txt</outputFile>
                                    <arguments>
                                        <argument>${project.version}</argument>
                                        <argument>${rpm.version}</argument>
                                    </arguments>
                                </configuration>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.6</version>
                        <dependencies>
                            <dependency>
                                <groupId>org.apache.ant</groupId>
                                <artifactId>ant-jsch</artifactId>
                                <version>1.8.2</version>
                            </dependency>
                            <dependency>
                                <groupId>jsch</groupId>
                                <artifactId>jsch</artifactId>
                                <version>0.1.44</version>
                            </dependency>
                        </dependencies>
                        <executions>
                            <execution>
                                <id>upload-to-linux-repos</id>
                                <phase>deploy</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <target unless="no.installer.deploy">
                                        <scp todir="${sdk.scp.user}@${sdk.scp.host}:/var/packages/incoming" port="22" trust="true" verbose="true" keyfile="${sdk.scp.key}">
                                            <fileset dir="${build.directory}" casesensitive="yes">
                                                <include name="**/*.deb" />
                                                <include name="**/*.rpm" />
                                            </fileset>
                                        </scp>
                                        <sshexec host="${sdk.scp.host}" username="${sdk.scp.user}" keyfile="${sdk.scp.key}" command="chgrp developer-relations /var/packages/incoming/*" />
                                        <sshexec host="${sdk.scp.host}" username="${sdk.scp.user}" keyfile="${sdk.scp.key}" command="chmod 774 /var/packages/incoming/*" />
                                    </target>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>installer-osx</id>
            <build>
                <finalName>atlassian-plugin-sdk</finalName>

                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>unpack-for-osx</id>
                                <goals>
                                    <goal>unpack</goal>
                                </goals>
                                <phase>generate-resources</phase>
                                <configuration>
                                    <artifactItems>
                                        <artifactItem>
                                            <groupId>${groupId}</groupId>
                                            <artifactId>atlassian-plugin-sdk</artifactId>
                                            <version>${version}</version>
                                            <type>tar.gz</type>
                                            <overWrite>true</overWrite>
                                            <outputDirectory>${project.build.directory}/osx-unzip</outputDirectory>
                                        </artifactItem>
                                    </artifactItems>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.1</version>
                        <executions>
                            <execution>
                                <id>native-osx</id>
                                <phase>package</phase>
                                <configuration>
                                    <executable>${basedir}/osx-builder.sh</executable>
                                    <outputFile>${project.build.directory}/osx-out.txt</outputFile>
                                    <arguments>
                                        <argument>${project.version}</argument>
                                    </arguments>
                                </configuration>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-osx</id>
                                <phase>prepare-package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                                <configuration>
                                    <finalName>osx-work</finalName>
                                    <outputDirectory>${project.build.directory}</outputDirectory>
                                    <appendAssemblyId>false</appendAssemblyId>
                                    <attach>false</attach>
                                    <descriptors>
                                        <descriptor>src/main/assembly/assembly-osx.xml</descriptor>
                                    </descriptors>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>build-helper-maven-plugin</artifactId>
                        <version>1.7</version>
                        <executions>
                            <execution>
                                <id>attach-osx-installer</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>attach-artifact</goal>
                                </goals>
                                <configuration>
                                    <artifacts>
                                        <artifact>
                                            <file>${project.build.directory}/atlassian-plugin-sdk-${project.version}.pkg</file>
                                            <type>pkg</type>
                                        </artifact>
                                    </artifacts>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    <properties>
        <installbuilder.builder>/tools/installbuilder/bin/builder</installbuilder.builder>
        <buildhelper.version>1.7</buildhelper.version>
        <sdk.scp.user>${user.name}</sdk.scp.user>
        <sdk.scp.host>plugin-sdk-repo.private.atlassian.com</sdk.scp.host>
        <sdk.scp.key>${user.home}/.ssh/id_rsa</sdk.scp.key>
    </properties>
</project>
