<?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>org.rhq</groupId>
        <artifactId>rhq-parent</artifactId>
        <version>4.11.0</version>
        <relativePath>../../../../pom.xml</relativePath>
    </parent>

    <artifactId>embeddedagent</artifactId>

    <name>RHQ Embedded Agent Subsystem</name>

    <properties>
        <!-- this is used to replace the variable in module.xml -->
        <moduleName>${project.groupId}.${project.artifactId}</moduleName>
        <moduleDir>org/rhq/${project.artifactId}</moduleDir>
    </properties>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <!-- this doesn't need to be in the jar, just in our module .zip -->
                    <exclude>module/main/module.xml</exclude>
                </excludes>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-agent</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.rhq</groupId>
                                    <artifactId>rhq-enterprise-agent</artifactId>
                                    <type>zip</type>
                                    <outputDirectory>${project.build.directory}</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>rename-native-libs-and-reconfigure</id>
                        <phase>process-resources</phase>
                        <configuration>
                            <target>
                                <echo>Must move native libraries so JBoss Modules can find them</echo>
                                <property name="agent.lib" location="${project.build.directory}/rhq-agent/lib"/>
                                <property name="module.lib" location="${project.build.directory}/module-lib"/>
                                <!-- Notice that the agent's "augeas" native libs are not copied.
                                     I think its only used for some plugins we won't be using in embedded mode anyway. -->
                                <!-- TODO these should probably be "move", I don't think we need dup copies in agent lib anymore -->
                                <copy tofile="${module.lib}/linux-i686/libsigar.so"       file="${agent.lib}/libsigar-x86-linux.so"       preservelastmodified="true"/>
                                <copy tofile="${module.lib}/linux-x86_64/libsigar.so"     file="${agent.lib}/libsigar-amd64-linux.so"     preservelastmodified="true"/>
                                <copy tofile="${module.lib}/linux-ia64/libsigar.so"       file="${agent.lib}/libsigar-ia64-linux.so"      preservelastmodified="true"/>
                                <copy tofile="${module.lib}/linux-ppc64/libsigar.so"      file="${agent.lib}/libsigar-ppc64-linux.so"     preservelastmodified="true"/>
                                <copy tofile="${module.lib}/linux-ppc/libsigar.so"        file="${agent.lib}/libsigar-ppc-linux.so"       preservelastmodified="true"/>
                                <copy tofile="${module.lib}/linux-s390x/libsigar.so"      file="${agent.lib}/libsigar-s390x-linux.so"     preservelastmodified="true"/>
                                <copy tofile="${module.lib}/win-i686/sigar.dll"           file="${agent.lib}/sigar-x86-winnt.dll"         preservelastmodified="true"/>
                                <copy tofile="${module.lib}/win-x86_64/sigar.dll"         file="${agent.lib}/sigar-amd64-winnt.dll"       preservelastmodified="true"/>
                                <copy tofile="${module.lib}/hpux-ia64w/libsigar.sl"       file="${agent.lib}/libsigar-ia64-hpux-11.sl"    preservelastmodified="true"/>
                                <copy tofile="${module.lib}/hpux-parisc/libsigar.sl"      file="${agent.lib}/libsigar-pa-hpux-11.sl"      preservelastmodified="true"/>
                                <copy tofile="${module.lib}/aix-ppc/libsigar.so"          file="${agent.lib}/libsigar-ppc-aix-5.so"       preservelastmodified="true"/>
                                <copy tofile="${module.lib}/aix-ppc64/libsigar.so"        file="${agent.lib}/libsigar-ppc64-aix-5.so"     preservelastmodified="true"/>
                                <copy tofile="${module.lib}/solaris-i686/libsigar.so"     file="${agent.lib}/libsigar-x86-solaris.so"     preservelastmodified="true"/>
                                <copy tofile="${module.lib}/solaris-x86_64/libsigar.so"   file="${agent.lib}/libsigar-amd64-solaris.so"   preservelastmodified="true"/>
                                <copy tofile="${module.lib}/solaris-sparc/libsigar.so"    file="${agent.lib}/libsigar-sparc-solaris.so"   preservelastmodified="true"/>
                                <copy tofile="${module.lib}/solaris-sparcv9/libsigar.so"  file="${agent.lib}/libsigar-sparc64-solaris.so" preservelastmodified="true"/>
                                <copy tofile="${module.lib}/freebsd-i686/libsigar.so"     file="${agent.lib}/libsigar-x86-freebsd-6.so"   preservelastmodified="true"/>
                                <copy tofile="${module.lib}/freebsd-x86_64/libsigar.so"   file="${agent.lib}/libsigar-amd64-freebsd-6.so" preservelastmodified="true"/>
                                <copy tofile="${module.lib}/macosx-i686/libsigar.dylib"   file="${agent.lib}/libsigar-universal-macosx.dylib"   preservelastmodified="true"/>
                                <copy tofile="${module.lib}/macosx-x86_64/libsigar.dylib" file="${agent.lib}/libsigar-universal64-macosx.dylib" preservelastmodified="true"/>

                                <echo>Adjust default configuration</echo>
                                <property name="agent.config.dir" location="${project.build.directory}/rhq-agent/conf"/>
                                <property name="agent.config.xml" location="${agent.config.dir}/agent-configuration.xml"/>
                                <!-- because we are embedded, as can't have a prompt and ask user, so we will ensure the agent is always fully setup -->
                                <replaceregexp file="${agent.config.xml}" flags="s"
                                               match='&lt;!--(\s*)&lt;entry key="rhq.agent.configuration-setup-flag" value="false" /&gt;(\s*)--&gt;'
                                               replace='&lt;entry key="rhq.agent.configuration-setup-flag" value="true" /&gt;' />
                                <!-- we don't support agent auto-update while the agent is embedded -->
                                <replaceregexp file="${agent.config.xml}"
                                               match='&lt;entry key="rhq.agent.agent-update.enabled" value="true" /&gt;'
                                               replace='&lt;entry key="rhq.agent.agent-update.enabled" value="false" /&gt;' />
                                <!-- we don't want our embedded agent trying to shutdown the VM, even if memory is low, so turn off health checks -->
                                <replaceregexp file="${agent.config.xml}"
                                               match='&lt;entry key="rhq.agent.vm-health-check.interval-msecs" value="\d+" /&gt;'
                                               replace='&lt;entry key="rhq.agent.vm-health-check.interval-msecs" value="0" /&gt;' />
                                <!-- because we don't want log4j writing files in places we don't want, don't use file appenders. -->
                                <!-- our WildFly/EAP subsystem extension will turn this back on at runtime after log4j is properly configured. -->
                                <replaceregexp file="${agent.config.dir}/log4j.xml" flags="g"
                                               match='&lt;appender-ref ref="FILE".*/&gt;\n'
                                               replace='&lt;!-- &lt;appender-ref ref="FILE"/&gt; --&gt;&#10;' />
                                <replaceregexp file="${agent.config.dir}/log4j.xml" flags="g"
                                               match='&lt;appender-ref ref="COMMANDTRACE".*/&gt;\n'
                                               replace='&lt;!-- &lt;appender-ref ref="COMMANDTRACE"/&gt; --&gt;&#10;' />
                                <jar destfile="${agent.lib}/rhq-enterprise-agent-${project.version}.jar"
                                     basedir="${agent.config.dir}"
                                     includes="log4j.xml"
                                     update="true" />
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/scripts/module-assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>module-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.jboss.as</groupId>
            <artifactId>jboss-as-controller</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.as</groupId>
            <artifactId>jboss-as-server</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.rhq</groupId>
            <artifactId>rhq-enterprise-agent</artifactId>
            <version>${project.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- test deps -->
        <dependency>
            <groupId>org.jboss.as</groupId>
            <artifactId>jboss-as-subsystem-test</artifactId>
            <version>${jboss.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>dev</id>

            <properties>
                <rhq.rootDir>../../../..</rhq.rootDir>
                <rhq.containerDir>${rhq.rootDir}/${rhq.devContainerServerPath}</rhq.containerDir>
                <rhq.deploymentName>${project.build.finalName}.zip</rhq.deploymentName>
                <rhq.deploymentDir>${rhq.containerDir}/modules/</rhq.deploymentDir>
            </properties>

            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>

                            <execution>
                                <id>deploy</id>
                                <phase>package</phase>
                                <configuration>
                                    <target>
                                        <echo>*** Deploying Embedded Agent module to ${rhq.deploymentDir}</echo>
                                        <unzip src="target/${rhq.deploymentName}"
                                               dest="${rhq.deploymentDir}" />
                                    </target>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>

                            <execution>
                                <id>undeploy</id>
                                <phase>clean</phase>
                                <configuration>
                                    <target>
                                        <echo>*** Deleting Embedded Agent module ... ${rhq.deploymentDir}/${moduleDir}</echo>
                                        <delete dir="${rhq.deploymentDir}/${moduleDir}" />
                                    </target>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>
