#!/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
CWD="$(dirname "$0")"
COMPONENT="pig"

# This file can be called in two ways. In the normal path, it is
# invoked here as-is, and the build functions are over in the
# templates directory.  As part of packaging, it is copied into a
# package-specific directory along with the contents of the templates
# directory, and so the build functions are in the currrent working
# directory.
FUNCTIONS1="${CWD}/../../templates/buildfuncs"
FUNCTIONS2="${CWD}/buildfuncs"
if [ -f "${FUNCTIONS1}" ]; then
    . "${FUNCTIONS1}"
elif [ -f "${FUNCTIONS2}" ]; then
    . "${FUNCTIONS2}"
else
    echo "Unable to source ${FUNCTIONS1} or ${FUNCTIONS2}"
    exit 1
fi

IVY_MIRROR_PROP=${IVY_MIRROR_PROP:-http://repo1.maven.org/maven2/}

if [ -n "${CAULDRON_PKG_BUILD_MVN_PHASE}" ]; then
  # We are in the maven phase of the package build. Build stuff in a way that's
  # done for packaging.
  MAVEN_DEPLOY_ONLY=
elif [ -n "${CAULDRON_ROOT_DIR}" ]; then
  # We are in the package phase. Reuse the artifacts generated in the maven phase.
  cp -n -R "${CAULDRON_ROOT_DIR}/build/docker.common/build/cdh/${COMPONENT}"/*/source/* .
  exit 0
fi

if ! [ -e $FORREST_HOME/bin/forrest ]; then
    echo No FORREST_HOME set. Forrest documentation requires Forrest to be 1>&2
    echo installed. 1>&2
    exit 1
fi

if [ -n "${CAULDRON_BUILD}" ]; then
  IVYSETTINGS_OVERRIDE="-Divysettings.xml=${CAULDRON_TOP_DIR}/ivysettings.xml"
else
  IVYSETTINGS_OVERRIDE=""
fi

BUILD_OPTS="-Djavac.version=1.8 -Dforrest.home=${FORREST_HOME}  -Dhadoopversion=3   \
        -Dreactor.repo=file://${HOME}/.m2/repository${M2_REPO_SUFFIX} -Divy.home=${HOME}/.ivy2 -Dmvnrepo=$IVY_MIRROR_PROP \
        -propertyfile cloudera/build.properties ${IVYSETTINGS_OVERRIDE}"

# FIXME: BIGTOP-894. Please get rid of the line below when Bigtop integrates a version of Pig that includes the fix for PIG-3262
export LC_ALL=en_US.UTF-8

rm -f "test/org/apache/${COMPONENT}/test/TestHBaseStorage.java"
sed -i.orig -e 's#</modules>#<module organisation="org.apache.hadoop" name=".*" resolver="internal"/>\n</modules>#' ivy/ivysettings.xml

ant $BUILD_OPTS clean jar "$@"
for build_file in contrib/piggybank/java/build.xml ; do
    ant $BUILD_OPTS -buildfile $build_file clean jar "$@"
done

# Additional JARs needed for Avro integration
IVY_LIB=build/ivy/lib/Pig
mkdir -p contrib/piggybank/java/lib/

for jar_file in avro-* jackson-core-asl-* jackson-mapper-asl-* json-simple-* snappy-java-* ; do
cp $IVY_LIB/$jar_file.jar contrib/piggybank/java/lib/
done

if [ -z "${MAVEN_DEPLOY_ONLY}" ]; then
    TAR_TARGET="tar"
fi

# Build pig tar and pigunit..
ant $BUILD_OPTS ${TAR_TARGET} pigunit-jar smoketests-jar "$@"

if [ -z "${MAVEN_DEPLOY_ONLY}" ]; then
  # Remove GPL licensed library which is required for Pigmix data generation only
  pushd build
  tarfile=`ls pig-*.tar.gz`
  mkdir tempUntar
  tar --use-compress-program pigz -xvf $tarfile -C tempUntar
  pushd tempUntar/"${COMPONENT}"-*/test/perf/pigmix/lib
  rm -f sdsuLibJKD12.jar
  echo "For licensing reasons sdsuLibJKD12.jar has been removed from here. You can download this separately from e.g. https://talend-update.talend.com/nexus/content/repositories/libraries/org/talend/libraries/sdsuLibJKD12" > NOTICE
  popd
  cd tempUntar
  tar --use-compress-program pigz -cvf ../$tarfile "${COMPONENT}"-*
  popd

  # Copy generated docs
  # This is in the maven phase of the package build. See CAULDRON_PKG_BUILD_MVN_PHASE above.
  PUB_DOCS_DIR="${CAULDRON_OUTPUT}/cdh6/${CDH_VERSION}/docs/${COMPONENT}-${FULL_VERSION}/"
  mkdir -p "${PUB_DOCS_DIR}"
  cp -r build/docs/* "${PUB_DOCS_DIR}"
fi

# Clean up after piggybank.
rm -rf contrib/piggybank/java/build

if [ -n "$DO_MAVEN_DEPLOY" ]; then
# Change to cloudera/maven directory, and install
# (and if called from CDH nightly build, deploy) artifacts into Maven repository
    cd cloudera/maven-packaging
    mvn $CAULDRON_MAVEN_EXTRA_ARGS -B -Dnot.cdh.release.build=false install -Dmaven.repo.local=${HOME}/.m2/repository${M2_REPO_SUFFIX} $DO_MAVEN_DEPLOY
fi
