#!/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="sqoop"

# 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

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

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

if [ -z "${MAVEN_DEPLOY_ONLY}" ]; then
    BUILD_TARGET="tar"
else
    BUILD_TARGET="jar-all"
fi

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

ant -f build.xml clean ${BUILD_TARGET} \
  -Dreactor.repo=file://${HOME}/.m2/repository${M2_REPO_SUFFIX} \
  -Dmvn_repo=$IVY_MIRROR_PROP -Divy_repo=$IVY_MIRROR_PROP -Divy.home=${HOME}/.ivy2 \
  ${IVYSETTINGS_OVERRIDE} \
  "$@"

if [ -z "${MAVEN_DEPLOY_ONLY}" ]; then
  # 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

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 -Dnot.cdh.release.build=false install -Dmaven.repo.local=${HOME}/.m2/repository${M2_REPO_SUFFIX} $DO_MAVEN_DEPLOY
fi
