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

# 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_BUILD}" ]; then
  IVYSETTINGS_OVERRIDE="-Divysettings.xml=${CAULDRON_TOP_DIR}/ivysettings.xml"
else
  IVYSETTINGS_OVERRIDE=""
fi

BUILD_OPTS="-Dversion=${FULL_VERSION}                                  \
        -Dslf4j.binding=slf4j-log4j12 -Dexclude.from.war=nothing   \
        -Divy.home=${HOME}/.ivy2 -Drepo.maven.org=$IVY_MIRROR_PROP \
        -Divy_bootstrap_url1=${IVY_MIRROR_PROP} \
        -Divy_install_path=${HOME}/.ant/lib -lib ${HOME}/.ant/lib  \
        -Dreactor.repo=file://${HOME}/.m2/repository${M2_REPO_SUFFIX} \
        ${IVYSETTINGS_OVERRIDE}"

# Setting CAULDRON_NO_CLEAN allows us to run this script twice without incurring
# the overhead of building from clean on the second invocation. First we build
# only the subset of the code needed to deploy maven artifacts (see MAVEN_DEPLOY_ONLY
# below), and on the next invocation - everything else.
if [ -n "${CAULDRON_NO_CLEAN}" ]; then
    echo "CAULDRON_NO_CLEAN is set; not performing ant bootstrap and clean operations"
else
    # FIXME: this needs to be fixed at the product level
    ant $BUILD_OPTS ivy-bootstrap
    mkdir -p "${COMPONENT}/test-framework/lib" || :

    ant $BUILD_OPTS clean
fi

# Build source package
(cd "${COMPONENT}" ; ant $BUILD_OPTS package-local-src-tgz "$@")

# If DO_MAVEN_DEPLOY == deploy, run the generate-maven-artifacts target
# For purposes of deploying to Maven, we skip the Sentry contrib package,
# which introduces a thorny and hard to satisfy sentry dependency.
# The bash array usage here (and de-reference using "${...[@]}") makes sure
# that bash doesn't expand the **, while keeping the two -D's as separate
# arguments to ant.
MAVEN_DEPLOY_OPTS=(-Dcontrib-crawl.exclude=contrib/depends-sentry-libs/build.xml -Dmaven-deps.exclude="**/depends-sentry-libs/**")
if [ "${DO_MAVEN_DEPLOY}" == "deploy" ]; then
    ant $BUILD_OPTS "${MAVEN_DEPLOY_OPTS[@]}" generate-maven-artifacts "$@"
    (cd "cloudera/${COMPONENT}-upgrade" ; \
     mvn $CAULDRON_MAVEN_EXTRA_ARGS deploy -Dreactor.repo=file://${HOME}/.m2/repository${M2_REPO_SUFFIX})
fi

if [ -z "${MAVEN_DEPLOY_ONLY}" ]; then
# Build Solr package (and if we didn't do generate-maven-artifacts, it'll build everything else it needs
    (cd "${COMPONENT}" ; ant $BUILD_OPTS create-package "$@")

# Putting the build artifact in place
    rm -rf build
    mkdir build
    pushd build
    tar --use-compress-program pigz -xf "../${COMPONENT}/build/${COMPONENT}"-*-src.tgz
    tar --use-compress-program pigz -C "${COMPONENT}"-* --strip-components=1 -xf "../${COMPONENT}/package/${COMPONENT}"-*.tgz
    cp -r ../cloudera* "${COMPONENT}-${FULL_VERSION}"
    cp -r ../cdh.build.properties "${COMPONENT}-${FULL_VERSION}"
    tar --use-compress-program pigz -cf "${COMPONENT}-${FULL_VERSION}.tar.gz" "${COMPONENT}-${FULL_VERSION}"
    popd

# Copy generated docs
    if [ -n "$CAULDRON_FIRST_NATIVE_BUILD" ]; then
        # We only need to copy docs once regardless of OS
        PUB_DOCS_DIR="${CAULDRON_OUTPUT}/cdh6/${CDH_VERSION}/docs/${COMPONENT}-${FULL_VERSION}/"
        mkdir -p "${PUB_DOCS_DIR}"
        cp -r build/"${COMPONENT}-${FULL_VERSION}"/docs/* "${PUB_DOCS_DIR}"
    fi
fi

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