<?xml version="1.0"?>
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-root</artifactId>
    <version>2.7.2</version>
  </parent>

  <artifactId>ehcache-kit</artifactId>
  <packaging>pom</packaging>
  <name>ehcache-kit</name>

  <description>project to build distribution kit</description>
  
  <modules>
    <module>colorcache</module>
    <module>events</module>
  </modules>
  
  <properties>
    <productName>ehcache</productName>
    <kitFolder>${productName}-${project.version}</kitFolder>
    <tarballName>${kitFolder}-distribution</tarballName>
    <root.dir>${project.build.directory}/${kitFolder}</root.dir>
    <terracotta.kit.path>${project.build.directory}/tmp/terracotta</terracotta.kit.path>
  </properties>

  <dependencies>
    <dependency>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache</artifactId>
      <version>${project.version}</version>
      <scope>provided</scope>
    </dependency> 
    <dependency>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache</artifactId>
      <version>${project.version}</version>
      <classifier>javadoc</classifier>
      <scope>provided</scope>
    </dependency>    
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-jdk14</artifactId>
      <scope>provided</scope>
    </dependency>   
  </dependencies>
  
  <build>
    <plugins>
      <plugin>
        <groupId>com.github.goldin</groupId>
        <artifactId>copy-maven-plugin</artifactId>
        <version>0.2.3.8</version>
        <configuration>
          <verbose>false</verbose>
          <resources>
            <!-- clean up -->
            <resource>
              <runIf>{{ new File( project.build.directory, "${kitFolder}" ).isDirectory() }}</runIf>
              <directory>${root.dir}</directory>
              <includes>
                <include>**/**</include>
              </includes>
              <clean>true</clean>
              <cleanEmptyDirectories>true</cleanEmptyDirectories>
            </resource>
            <!-- skeleton -->                
            <resource>
              <targetPath>${root.dir}</targetPath>
              <directory>${basedir}/src/main/assembly/root</directory>
              <preservePath>true</preservePath>
            </resource>
            <!-- ehcache.(xsd|xml) -->
            <resource>
              <targetPath>${root.dir}</targetPath>
              <directory>${basedir}/../ehcache-core/src/main/config</directory>
              <preservePath>true</preservePath>
            </resource>
            <!-- lib -->
            <resource>
              <targetPath>${root.dir}/lib</targetPath>
              <dependencies>
                <dependency>
                  <groupId>net.sf.ehcache</groupId>
                  <artifactId>ehcache</artifactId>
                </dependency>              
                <dependency>
                  <groupId>org.slf4j</groupId>
                  <artifactId>slf4j-jdk14</artifactId>
                </dependency>
                <dependency>
                  <groupId>org.slf4j</groupId>
                  <artifactId>slf4j-api</artifactId>
                </dependency>
              </dependencies>
            </resource>
            <!-- src -->
            <resource>
              <targetPath>${root.dir}/src</targetPath>
              <dependencies>
                <dependency>
                  <groupId>net.sf.ehcache</groupId>
                  <artifactId>ehcache</artifactId>
                  <classifier>sources</classifier>
                </dependency>
              </dependencies>
              <unpack>true</unpack>
            </resource>
            <!-- javadoc -->
            <resource>
              <targetPath>${root.dir}/javadoc</targetPath>
              <dependencies>
                <dependency>
                  <groupId>net.sf.ehcache</groupId>
                  <artifactId>ehcache</artifactId>
                  <classifier>javadoc</classifier>
                </dependency>
              </dependencies>
              <unpack>true</unpack>
            </resource> 
            <!-- clustered example -->                
            <resource>
              
              <targetPath>${root.dir}/samples</targetPath>
              <dependencies>
                <dependency>
                  <groupId>net.sf.ehcache.examples</groupId>
                  <artifactId>events</artifactId>
                  <version>${project.version}</version>
                  <classifier>distribution</classifier>
                  <type>tar.gz</type>
                </dependency>
                <dependency>
                  <groupId>net.sf.ehcache.examples</groupId>
                  <artifactId>colorcache</artifactId>
                  <version>${project.version}</version>
                  <classifier>distribution</classifier>
                  <type>tar.gz</type>
                </dependency>                
              </dependencies>
              <unpack>true</unpack>
            </resource>            
            <!-- terracotta -->                
            <resource>
              <runIf>{{ new File(project.properties['terracotta.kit.path']).isDirectory() }}</runIf>
              <targetPath>${root.dir}/terracotta</targetPath>
              <directory>${terracotta.kit.path}</directory>
              <excludes>
                <exclude>**/ehcache/**</exclude>
                <exclude>**/quartz/**</exclude>
                <exclude>**/sessions/**</exclude>
              </excludes>
              <preservePath>true</preservePath>
            </resource>
            <resource>
              <runIf>{{ new File(project.properties['terracotta.kit.path']).isDirectory() }}</runIf>
              <targetPath>${root.dir}/samples</targetPath>
              <directory>${terracotta.kit.path}/ehcache/samples</directory>
              <preservePath>true</preservePath>
            </resource>
            <resource>
              <runIf>{{ new File(project.properties['terracotta.kit.path']).isDirectory() }}</runIf>
              <targetPath>${root.dir}/samples/colorcache/bin</targetPath>
              <directory>${basedir}/src/main/assembly</directory>
              <includes>
                <include>relative-paths.*</include>
              </includes>
            </resource>
            <resource>
              <runIf>{{ new File(project.properties['terracotta.kit.path']).isDirectory() }}</runIf>
              <targetPath>${root.dir}/samples/events/bin</targetPath>
              <directory>${basedir}/src/main/assembly</directory>
              <includes>
                <include>relative-paths.*</include>
              </includes>
            </resource>             
          </resources>
        </configuration>
        <executions>
          <execution>
            <id>package-with-no-tc</id>
            <phase>package</phase>
            <goals>
              <goal>copy</goal>
            </goals>
          </execution>
        </executions>            
      </plugin>
      <plugin>
        <groupId>org.codehaus.gmaven</groupId>
        <artifactId>gmaven-plugin</artifactId>
        <executions>
          <execution>
            <id>create-tarball</id>
            <phase>package</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>         
              <source>
                def srcdir = new File(project.basedir, "target/${kitFolder}") 
                def destFile = new File(project.basedir, "target/${tarballName}.tar.gz") 
                ant.tar(destfile: "${destFile}", longfile: "gnu", compression: "gzip") {
                  tarfileset(dir: "${srcdir}", prefix: "${kitFolder}", excludes: "**/bin/**,**/lib/**,**/*.sh,**/*.bat")
                  tarfileset(dir: "${srcdir}", prefix: "${kitFolder}", includes: "**/bin/**,**/lib/**,**/*.sh,**/*.bat", filemode: "755")
                } 
              </source>
            </configuration>
          </execution>              
        </executions>
      </plugin>        
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.5</version>
        <executions>
          <execution>
            <id>attach-distribution</id>
            <phase>package</phase>
            <goals>
              <goal>attach-artifact</goal>
            </goals>
            <configuration>
              <artifacts>
                <artifact>
                  <file>${project.build.directory}/${tarballName}.tar.gz</file>
                  <type>tar.gz</type>
                  <classifier>distribution</classifier>
                </artifact>                
              </artifacts>
            </configuration>
          </execution>
        </executions>
      </plugin>    
    </plugins>
  </build>

  <profiles>
    <profile>
      <id>with-tc</id>
      <activation>
        <property>
          <name>tc-kit-url</name>
        </property>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.gmaven</groupId>
            <artifactId>gmaven-plugin</artifactId>
            <executions>
              <execution>
                <id>unjar-tc-kit</id>
                <phase>process-resources</phase>
                <goals>
                  <goal>execute</goal>
                </goals>
                <configuration>
                  <source>
                    if (new File(project.properties['terracotta.kit.path']).exists()) {
                      return
                    }
                    tc_kit_url = project.properties['tc-kit-url']
                    if (tc_kit_url == null) {
                      println "-Dtc-kit-url option is missing. Building the kit without Terracotta"
                    } else {
                      ant.mkdir(dir:project.build.directory)
                      dest_file = project.build.directory + "/tc.tar.gz"
                      if (tc_kit_url.startsWith("http")) {
                        ant.get(src:tc_kit_url, dest:dest_file, usetimestamp:true)
                      } else {
                        ant.copy(file:tc_kit_url, tofile:dest_file)
                      }
                      tmp_dir = project.build.directory + "/tmp"
                      ant.delete(dir:tmp_dir, quiet:true)
                      ant.mkdir(dir:tmp_dir)
                      ant.untar(src:dest_file, dest:tmp_dir, compression:"gzip")
                      // assume only one folder inside tmp
                      kit_path = null
                      new File(tmp_dir).eachFile {
                        kit_path = it
                      }
                      kit_path.renameTo(new File(project.properties['terracotta.kit.path']))
                    }
                  </source>
                </configuration>                
              </execution>             
            </executions>
          </plugin>     
        </plugins>
      </build>
    </profile>
  </profiles>  

  
  <repositories>
    <repository>
      <id>terracotta-snapshots</id>
      <url>http://www.terracotta.org/download/reflector/snapshots</url>
    </repository>
    <repository>
      <id>terracotta-releases</id>
      <url>http://www.terracotta.org/download/reflector/releases</url>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>terracotta-snapshots</id>
      <url>http://www.terracotta.org/download/reflector/snapshots</url>
    </pluginRepository>
    <pluginRepository>
      <id>terracotta-releases</id>
      <url>http://www.terracotta.org/download/reflector/releases</url>
    </pluginRepository>
    <pluginRepository>
      <id>evgenyg.artifactoryonline.com</id>
      <url>http://evgenyg.artifactoryonline.com/evgenyg/repo/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </pluginRepository>   
  </pluginRepositories>

</project>
