#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.

. `dirname $0`/bigtop.bom

export MAVEN_OPTS=""

# bigtop installs maven on its own and not through the package manager
export PATH=/usr/local/maven/bin:$PATH
command -v mvn > /dev/null 2>&1 || {
    echo >&2 "Maven is required but not found in PATH. Please install maven to continue the build."
    exit 1
}

# Build apex
mvn clean package -DskipTests -Dhadoop.version=$HADOOP_VERSION "$@"

# Find all the dependencies of apex
mvn dependency:copy-dependencies -DoutputDirectory=`pwd`/all-deps -DincludeScope=runtime -DexcludeScope=test -Dhadoop.version=$HADOOP_VERSION

# Copy apex-engine jar from compiled path
cp engine/target/apex-engine.jar all-deps/

# Find all the dependency for hadoop.
cat > pom-hadoop-deps.xml <<'EOF'
<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>

  <groupId>com.datatorrent</groupId>
  <artifactId>dt-hadoop-deps</artifactId>
  <packaging>pom</packaging>
  <version>0.1</version>
  
  <dependencies>
    <!-- it would be better to generate the list below from dt-engine dependencies -->
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-yarn-client</artifactId>
      <version>${hadoop.version}</version>
      <exclusions>
        <exclusion>
          <groupId>org.codehaus.jackson</groupId>
          <artifactId>jackson-core-asl</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.codehaus.jackson</groupId>
          <artifactId>jackson-jaxrs</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.codehaus.jackson</groupId>
          <artifactId>jackson-xc</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>${hadoop.version}</version>
      <exclusions>
      <exclusion>
        <artifactId>commons-beanutils-core</artifactId>
        <groupId>commons-beanutils</groupId>
      </exclusion>
      <exclusion>
        <artifactId>jackson-jaxrs</artifactId>
        <groupId>org.codehaus.jackson</groupId>
      </exclusion>
      <exclusion>
        <artifactId>jackson-xc</artifactId>
        <groupId>org.codehaus.jackson</groupId>
      </exclusion>
      <exclusion>
        <artifactId>jersey-server</artifactId>
        <groupId>com.sun.jersey</groupId>
      </exclusion>
      <exclusion>
        <groupId>org.apache.avro</groupId>
        <artifactId>avro</artifactId>
      </exclusion>
      <exclusion>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
      </exclusion>
      <exclusion>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
      </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
</project>
EOF

mvn dependency:copy-dependencies -DoutputDirectory=`pwd`/hadoop-deps \
    -DincludeScope=runtime -DexcludeScope=test \
    -DexcludeArtifactIds=jersey-client \
    -Dhadoop.version=$HADOOP_VERSION \
    -f pom-hadoop-deps.xml

# Find the diff between platform and hadoop. And that's what is required for running apex in standalone mode.
mkdir deps
for i in `diff -r all-deps/ hadoop-deps/ | grep all-deps | awk '{print $4}'`; do cp all-deps/$i deps/; done

rm -rf all-deps/ hadoop-deps/
rm -rf deps/hadoop-common-$HADOOP_VERSION-tests.jar
rm -rf deps/slf4j-*.jar

#Download non-ASF licenses for libraries
wget -t 3 -q -O jline-LICENSE.txt https://raw.githubusercontent.com/jline/jline2/jline-2.11/LICENSE.txt
wget -t 3 -q -O kryo-LICENSE.txt https://raw.githubusercontent.com/EsotericSoftware/kryo/kryo-2.24.0/license.txt
wget -t 3 -q -O mbassador-LICENSE.txt https://raw.githubusercontent.com/bennidi/mbassador/mbassador-1.1.9/LICENSE
wget -t 3 -q -O minlog-LICENSE.txt https://raw.githubusercontent.com/EsotericSoftware/minlog/1.2/license.txt
wget -t 3 -q -O jersey-LICENSE.txt https://raw.githubusercontent.com/jersey/jersey/master/LICENSE.txt
