<!--
  Copyright © 2014 Cask Data, Inc.

  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy of
  the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  License for the specific language governing permissions and limitations under
  the License.
  -->

<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>co.cask.cdap</groupId>
    <artifactId>cdap-ingest</artifactId>
    <version>1.2.0</version>
  </parent>

  <artifactId>cdap-file-tailer</artifactId>
  <packaging>jar</packaging>

  <name>File Tailer</name>

  <properties>
    <script.app.mainclass>co.cask.cdap.filetailer.FileTailerMain</script.app.mainclass>
    <script.app.user>file-tailer</script.app.user>
    <script.app.name>file-tailer</script.app.name>
    <script.app.desc>File Tailer</script.app.desc>
  </properties>

  <dependencies>
    <dependency>
      <groupId>co.cask.cdap</groupId>
      <artifactId>cdap-stream-client</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>co.cask.cdap</groupId>
      <artifactId>cdap-authentication-client</artifactId>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>${logback.version}</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>${logback.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-all</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <profiles>
    <!-- Profile for prepare rpm and debian packages, common part. -->
    <profile>
      <id>prepare</id>
      <properties>
        <package.dirs>usr</package.dirs>
      </properties>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <executions>
              <execution>
                <id>jar</id>
                <phase>prepare-package</phase>
                <configuration>
                  <outputDirectory>${project.build.directory}/usr/lib/${script.app.name}/lib</outputDirectory>
                  <archive>
                    <manifest>
                      <addClasspath>true</addClasspath>
                      <classpathPrefix>lib/</classpathPrefix>
                      <mainClass>${script.app.mainclass}</mainClass>
                    </manifest>
                  </archive>
                </configuration>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
              <execution>
                <id>copy-dependencies</id>
                <phase>prepare-package</phase>
                <goals>
                  <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/usr/lib/${script.app.name}/lib</outputDirectory>
                  <overWriteReleases>false</overWriteReleases>
                  <overWriteSnapshots>false</overWriteSnapshots>
                  <overWriteIfNewer>true</overWriteIfNewer>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
              <execution>
                <id>copy-conf</id>
                <phase>process-resources</phase>
                <goals>
                  <goal>copy-resources</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/usr/lib/${script.app.name}/conf</outputDirectory>
                  <resources>
                    <resource>
                      <directory>src/main/resources</directory>
                    </resource>
                  </resources>
                </configuration>
              </execution>
              <execution>
                <id>copy-bin</id>
                <phase>process-resources</phase>
                <goals>
                  <goal>copy-resources</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/usr/lib/${script.app.name}/bin</outputDirectory>
                  <resources>
                    <resource>
                      <directory>${project.basedir}/distributions/bin</directory>
                    </resource>
                  </resources>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>buildnumber-maven-plugin</artifactId>
            <version>1.3</version>
            <executions>
              <execution>
                <id>create-timestamp</id>
                <phase>validate</phase>
                <goals>
                  <goal>create-timestamp</goal>
                </goals>
                <configuration>
                  <timestampPropertyName>package.build.timestamp</timestampPropertyName>
                </configuration>
              </execution>
              <execution>
                <id>create-year</id>
                <phase>validate</phase>
                <goals>
                  <goal>create-timestamp</goal>
                </goals>
                <configuration>
                  <timestampPropertyName>package.build.year</timestampPropertyName>
                  <timestampFormat>yyyy</timestampFormat>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.9</version>
            <executions>
              <execution>
                <id>regex-properties</id>
                <phase>validate</phase>
                <goals>
                  <goal>regex-properties</goal>
                </goals>
                <configuration>
                  <regexPropertySettings>
                    <regexPropertySetting>
                      <name>package.version</name>
                      <value>${project.version}</value>
                      <regex>-SNAPSHOT</regex>
                      <replacement>.${package.build.timestamp}</replacement>
                      <failIfNoMatch>false</failIfNoMatch>
                    </regexPropertySetting>
                  </regexPropertySettings>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>

    <!-- Profile for prepare rpm package. The "prepare" profile must be activated as well. -->
    <profile>
      <id>rpm-prepare</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
              <execution>
                <id>copy-rpm-staging</id>
                <phase>prepare-package</phase>
                <goals>
                  <goal>copy-resources</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/rpm</outputDirectory>
                  <resources>
                    <resource>
                      <directory>${project.build.directory}</directory>
                      <includes>
                        <include>usr/**</include>
                      </includes>
                    </resource>
                  </resources>
                </configuration>
              </execution>
              <execution>
                <id>copy-rpm-init-script</id>
                <phase>prepare-package</phase>
                <goals>
                  <goal>copy-resources</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/rpm/usr/lib/${script.app.name}/bin</outputDirectory>
                  <resources>
                    <resource>
                      <directory>${project.basedir}/src/main/rpm/init.d</directory>
                    </resource>
                  </resources>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
              <execution>
                <id>rpm-bin-permission</id>
                <phase>prepare-package</phase>
                <configuration>
                  <target>
                    <chmod dir="${project.build.directory}/rpm/usr/lib/${script.app.name}/bin" perm="755" includes="**/**"/>
                  </target>
                </configuration>
                <goals>
                  <goal>run</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>

    <!-- Profile for prepare debian package. The "prepare" profile must be activated as well. -->
    <profile>
      <id>deb-prepare</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
              <execution>
                <id>copy-deb-staging</id>
                <phase>prepare-package</phase>
                <goals>
                  <goal>copy-resources</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/deb</outputDirectory>
                  <resources>
                    <resource>
                      <directory>${project.build.directory}</directory>
                      <includes>
                        <include>usr/**</include>
                      </includes>
                    </resource>
                  </resources>
                </configuration>
              </execution>
              <execution>
                <id>copy-deb-init-script</id>
                <phase>prepare-package</phase>
                <goals>
                  <goal>copy-resources</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/deb/usr/lib/${script.app.name}/bin</outputDirectory>
                  <resources>
                    <resource>
                      <directory>${project.basedir}/src/main/deb/init.d</directory>
                    </resource>
                  </resources>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
              <execution>
                <id>deb-bin-permission</id>
                <phase>prepare-package</phase>
                <configuration>
                  <target>
                    <chmod dir="${project.build.directory}/deb/usr/lib/${script.app.name}/bin" perm="755" includes="**/**"/>
                  </target>
                </configuration>
                <goals>
                  <goal>run</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>

    <!-- Profile for creating rpm package. Both "prepare" and "rpm-prepare" must be activated. -->
    <profile>
      <id>rpm</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.3.1</version>
            <executions>
              <execution>
                <id>rpm-package</id>
                <phase>package</phase>
                <goals>
                  <goal>exec</goal>
                </goals>
                <configuration>
                  <workingDirectory>${project.build.directory}</workingDirectory>
                  <executable>fpm</executable>
                  <commandlineArgs>
                    -s dir
                    -t rpm
                    --name "${script.app.name}"
                    --vendor "Cask Data, Inc."
                    --maintainer "support@cask.co"
                    --license "Copyright ${package.build.year} Cask Data, Inc. Licensed under the Apache License, Version 2.0."
                    --description "Component of Cask DAP"
                    --category misc
                    -C "${project.build.directory}/rpm"
                    --directories "/usr/lib/${script.app.name}"
                    --version "${package.version}"
                    --architecture all
                    --url "http://cask.co"
                    --template-value "project=${script.app.name}"
                    --before-install "${project.basedir}/src/main/rpm/control/preinst"
                    --after-install "${project.basedir}/distributions/control/postinst"
                    --before-remove "${project.basedir}/distributions/control/prerm"
                    --after-remove "${project.basedir}/distributions/control/postrm"
                    --iteration 1
                    ${package.dirs}
                  </commandlineArgs>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>

    <!-- Profile for creating debian package. Both "prepare" and "deb-prepare" must be activated. -->
    <profile>
      <id>deb</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.3.1</version>
            <executions>
              <execution>
                <id>deb-package</id>
                <phase>package</phase>
                <goals>
                  <goal>exec</goal>
                </goals>
                <configuration>
                  <workingDirectory>${project.build.directory}</workingDirectory>
                  <executable>fpm</executable>
                  <commandlineArgs>
                    -s dir
                    -t deb
                    --name "${script.app.name}"
                    --vendor "Cask Data, Inc."
                    --maintainer "support@cask.co"
                    --license "Copyright ${package.build.year} Cask Data, Inc. Licensed under the Apache License, Version 2.0."
                    --description "Component of Cask DAP"
                    --category misc
                    -C "${project.build.directory}/deb"
                    --directories "/usr/lib/${script.app.name}"
                    --version "${package.version}"
                    --architecture all
                    --url "http://cask.co"
                    --template-value "project=${script.app.name}"
                    --before-install "${project.basedir}/src/main/deb/control/preinst"
                    --after-install "${project.basedir}/distributions/control/postinst"
                    --before-remove "${project.basedir}/distributions/control/prerm"
                    --after-remove "${project.basedir}/distributions/control/postrm"
                    --iteration 1
                    ${package.dirs}
                  </commandlineArgs>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>
