#!/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.

set -ex

# FIXME: this needs to go away once we start using an actuall source tarball
[ -f pom.xml ] || cd src

if [ ! -d build/hadoop-${FULL_VERSION}/src ]; then
    mkdir -p build/hadoop-${FULL_VERSION}/src 
    tar --exclude='./build/*' -cf - . | (cd build/hadoop-${FULL_VERSION}/src ; tar xf -)
fi

if [ -z "$BUNDLE_SNAPPY" ] ; then
  [ -f /usr/lib/libsnappy.so ] && BUNDLE_SNAPPY="-Dsnappy.prefix=x -Dbundle.snappy=true -Dsnappy.lib=/usr/lib -Drequire.snappy=true"
  [ -f /usr/lib64/libsnappy.so ] && BUNDLE_SNAPPY="-Dsnappy.prefix=x -Dbundle.snappy=true -Dsnappy.lib=/usr/lib64 -Drequire.snappy=true"
fi

# Build artifacts
MAVEN_SKIP_TESTS="-DskipTests -DskipTest -DskipITs"
JAVA_OPTS="-Xmx1536m -Xms256m -XX:MaxPermSize=256m" \
  MAVEN_OPTS="-Xmx1536m -Xms256m -XX:MaxPermSize=256m" \
  mvn $BUNDLE_SNAPPY \
    -Pdist -Pnative -Pfuse -Psrc -Dtar -Drequire.fuse=true \
    ${MAVEN_SKIP_TESTS} \
    install \
    "$@" \
    $DO_MAVEN_DEPLOY
JAVA_OPTS="-Xmx1536m -Xms256m -XX:MaxPermSize=256m" MAVEN_OPTS="-Xmx1536m -Xms256m -XX:MaxPermSize=256m" mvn site site:stage ${MAVEN_SKIP_TESTS} $@

# Copy generated docs
mkdir -p build/hadoop-${FULL_VERSION}/share/doc/
cp -r target/staging/hadoop-project/* build/hadoop-${FULL_VERSION}/share/doc/

# Rebuild and now mvn deploy the tarball - note the '-pl hadoop-dist' - if we chdir into hadoop-dist and build, it fails.
JAVA_OPTS="-Xmx1536m -Xms256m -XX:MaxPermSize=256m" MAVEN_OPTS="-Xmx1536m -Xms256m -XX:MaxPermSize=256m" mvn -pl hadoop-dist -Pdist -Psrc -Dtar ${MAVEN_SKIP_TESTS} -Dmaven.javadoc.skip=true -Dmaven.deploy.skip=false install "$@" $DO_MAVEN_DEPLOY

(cd build/hadoop-${FULL_VERSION} ; tar --strip-components=1 -xzvf  ../../hadoop-dist/target/hadoop-${FULL_VERSION}.tar.gz)
# (cd build/hadoop-${FULL_VERSION}/src ; tar --strip-components=1 -xzvf  ../../../hadoop-dist/target/hadoop-${FULL_VERSION}-src.tar.gz)
(cd build/hadoop-${FULL_VERSION} ; cp -r ../../cloudera .)

(cd build && tar -czf hadoop-${FULL_VERSION}.tar.gz hadoop-${FULL_VERSION})

# Copy fuse output to the build directory
cp hadoop-hdfs-project/hadoop-hdfs/target/native/main/native/fuse-dfs/fuse_dfs build/hadoop-${FULL_VERSION}/bin/

# Create a manifest for hadoop client package
(cd hadoop-client/target/hadoop-client-*/share/hadoop/client/lib ; ls) > build/hadoop-${FULL_VERSION}/hadoop-client.list

# Create a manifest for hadoop MR1 client package
mkdir hadoop-mr1-client
cat > hadoop-mr1-client/pom.xml <<__EOT__
<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>org.apache.hadoop</groupId>
    <artifactId>hadoop-project-dist</artifactId>
    <version>2.0.0-cdh4.1.1</version>
    <relativePath>../hadoop-project-dist</relativePath>
  </parent>
  <groupId>org.apache.hadoop</groupId>
  <artifactId>hadoop-client-mr1</artifactId>
  <version>2.0.0-cdh4.1.1</version>
  <packaging>pom</packaging>
  <dependencies>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>2.0.0-mr1-cdh4.1.1</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</project>
__EOT__
mvn -f hadoop-mr1-client/pom.xml dependency:copy-dependencies -DincludeScope=runtime
(cd hadoop-mr1-client/target/dependency ; ls | grep -v hadoop-client) > build/hadoop-${FULL_VERSION}/hadoop-mr1-client.list
rm -rf hadoop-mr1-client
