<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.atlassian.bonfire</groupId>
        <artifactId>bonfire-browser-extensions</artifactId>
        <version>2.9.7.0032</version>
    </parent>

    <artifactId>bonfire-xpi</artifactId>
    <name>Capture for JIRA - Browser Extensions - Firefox</name>
    <packaging>pom</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>unpack-keystore</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.atlassian.keystore</groupId>
                                    <artifactId>atlassian-keystore</artifactId>
                                    <version>${atlassian.keystore.version}</version>
                                    <outputDirectory>${project.build.directory}/atlassian-keystore</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                    <execution>
                        <id>unpack-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/unpack</outputDirectory>
                            <overWriteReleases>true</overWriteReleases>
                            <overWriteSnapshots>true</overWriteSnapshots>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <configuration>
                            <target>
                                <taskdef resource="net/sf/antcontrib/antcontrib.properties"
                                         classpathref="maven.plugin.classpath"/>

                                <condition property="isWindows">
                                    <os family="windows"/>
                                </condition>

                                <if>
                                    <equals arg1="${isWindows}" arg2="true"/>
                                    <then>
                                        <echo>
                                            =
                                            Not building Extension since you ARE running on Windows
                                            ==================================
                                        </echo>
                                    </then>
                                    <else>

                                        <property name="firefox.server.url" value="${server.base.url}/firefox"/>

                                        <echo>Building the release version pointing back to
                                            ${firefox.server.url}
                                        </echo>

                                        <copy todir="${project.build.directory}/xpi/chrome">
                                            <fileset dir="${project.build.directory}/unpack/chrome">
                                                <!--No scripts!-->
                                                <exclude name="**/*.sh"/>
                                                <!-- no certificates -->
                                                <exclude name="**/*.pfx"/>
                                                <exclude name="**/*.pem"/>

                                                <include name="content/aui/**/*.*"/>
                                                <include name="content/*.js"/>
                                                <include name="content/*.xul"/>
                                                <include name="content/*.xml"/>
                                                <include name="content/*.html"/>
                                                <include name="content/*.css"/>
                                                <include name="content/annotations/*"/>
                                                <include name="content/css/*"/>
                                                <include name="content/css/aui/*"/>
                                                <include name="content/css/lib/*"/>
                                                <include name="content/lib/*"/>
                                                <include name="content/help/*"/>
                                                <include name="content/i18n/*"/>
                                                <include name="content/test/*"/>
                                                <include name="content/firefox/*"/>
                                                <include name="content/firefox/modules/*"/>
                                                <include name="skin/*.css"/>
                                                <include name="skin/*.png"/>
                                                <include name="skin/*.svg"/>
                                                <include name="skin/*.gif"/>
                                                <include name="locale/**/*.dtd"/>
                                                <include name="locale/**/*.properties"/>
                                            </fileset>
                                        </copy>

                                        <replace file="${project.build.directory}/unpack/install.rdf"
                                                 token="@@build.version@@" value="${build.version}"/>
                                        <replace file="${project.build.directory}/unpack/package.json"
                                                 token="@@build.version@@" value="${build.version}"/>
                                        <replace file="${project.build.directory}/unpack/install.rdf"
                                                 token="@@build.version@@" value="${project.version}"/>
                                        <replace file="${project.build.directory}/unpack/install.rdf"
                                                 token="@@server.base.url@@"
                                                 value="${firefox.server.url}"/>


                                        <copy todir="${project.build.directory}/xpi">
                                            <fileset dir="${project.build.directory}/unpack">
                                                <include name="install.rdf"/>
                                                <include name="chrome.manifest"/>
                                                <include name="bootstrap.js"/>
                                                <include name="package.json"/>
                                                <include name="lib/main.js"/>
                                            </fileset>
                                        </copy>

                                        <!--If we have not PFX password then we can sign the XPI.  This is good enough for day to day dev-->
                                        <if>
                                            <equals arg1="${ci.pfx.password}" arg2=""/>
                                            <then>

                                                <property name="xpi.file.name"
                                                          value="${project.build.directory}/atlassian-bonfire-unsigned-${project.version}.xpi"/>

                                                <echo message="The ci.pfx.password is NOT set.  Building a unsigned XPI as ${xpi.file.name}"/>
                                                <zip destfile="${xpi.file.name}"
                                                     basedir="${project.build.directory}/xpi"/>

                                            </then>
                                            <else>
                                                <property name="xpi.file.name"
                                                          value="${project.build.directory}/atlassian-bonfire-${project.version}.xpi"/>
                                                <property name="atlassian.privkey.certificate"
                                                          value="${project.build.directory}/unpack/certificates/atlassian-privkey-certificate.pem"/>
                                                <echo message="The ci.pfx.password is set.  Building a signed XPI as ${xpi.file.name}"/>

                                                <echo>
                                                    Signing XPI ${xpi.file.name}
                                                    via Python using key and certificates from ${atlassian.privkey.certificate}
                                                </echo>

                                                <exec executable="python" failonerror="true">
                                                    <arg value="${project.build.directory}/../../../tools/signerxpi/signerxpi.py"/>
                                                    <arg value="${project.build.directory}/xpi"/>
                                                    <arg value="${atlassian.privkey.certificate}"/>
                                                    <arg value="${ci.pfx.password}"/>
                                                    <arg value="${xpi.file.name}"/>
                                                </exec>

                                            </else>
                                        </if>


                                        <if>
                                            <equals arg1="${ci.dev.channel}" arg2=""/>
                                            <then>
                                                <echo>
                                                    Signing an update manifest ${project.build.directory}/ff_update.rdf
                                                    via Python
                                                </echo>

                                                <exec executable="python" failonerror="true">
                                                    <arg value="${project.build.directory}/../../../tools/rdfsigner/rdfsigner.py"/>
                                                    <arg value="-k"/>
                                                    <arg value="${project.build.directory}/unpack/certificates/bonfire.pem"/>
                                                    <arg value="-o"/>
                                                    <arg value="${project.build.directory}/ff_update.rdf"/>

                                                    <arg value="${xpi.file.name}"/>
                                                    <!--TODO: temporarily passing redundant argument-->
                                                    <arg value="${xpi.file.name}"/>
                                                    <arg value="${firefox.server.url}/atlassian-bonfire-extension-${project.version}.xpi"/>
                                                </exec>
                                            </then>
                                            <else>

                                                <echo>
                                                    Generating an update manifest
                                                    ${project.build.directory}/ff_update.rdf
                                                    without signing it
                                                </echo>

                                                <replace file="${project.build.directory}/unpack/ff_update.rdf"
                                                         token="@@build.version@@" value="${build.version}"/>
                                                <replace file="${project.build.directory}/unpack/ff_update.rdf"
                                                         token="@@project.version@@" value="${project.version}"/>
                                                <replace file="${project.build.directory}/unpack/ff_update.rdf"
                                                         token="@@server.base.url@@"
                                                         value="${firefox.server.url}"/>

                                                <copy todir="${project.build.directory}">
                                                    <fileset dir="${project.build.directory}/unpack">
                                                        <include name="ff_update.rdf"/>
                                                    </fileset>
                                                </copy>
                                            </else>
                                        </if>

                                        <attachartifact file="${xpi.file.name}" type="xpi"/>


                                        <echo>
                                            =
                                            Capture for JIRA Artifacts Produced for build.number ${build.version}
                                            ==================================

                                            ${xpi.file.name}
                                            ${project.build.directory}/ff_update.rdf
                                        </echo>

                                    </else>
                                </if>

                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>ant-contrib</groupId>
                        <artifactId>ant-contrib</artifactId>
                        <version>20020829</version>
                    </dependency>
                </dependencies>
            </plugin>

        </plugins>
    </build>

    <profiles>
        <profile>
            <!-- Skip generation of ff_update.rdf as this file is not deployed anyways, but requires the specific python version and modules -->
            <id>freezer-release-profile</id>
            <properties>
                <maven.antrun.skip>true</maven.antrun.skip>
            </properties>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>com.atlassian.bonfire</groupId>
            <artifactId>bonfire-extension-core</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
    </dependencies>

</project>
