#!/usr/bin/env 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 -x
CWD="$(dirname "$0")"

# 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

#
# Resolve source directory
#

# $0 may be a softlink
PRG="$0"
while [ -h "${PRG}" ]; do
    ls=`ls -ld "${PRG}"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
        PRG="$link"
    else
        PRG=`dirname "${PRG}"`/"$link"
    fi
done

RELEASE_DIR=`dirname ${PRG}`
RELEASE_DIR=`cd ${RELEASE_DIR}/..;pwd`

cd ..

RELEASE_DIR=$PWD

if [[ -z "${CAULDRON_PKG_BUILD_MVN_PHASE}" && -n "${CAULDRON_ROOT_DIR}" ]]; then
  # We are in the package phase. Reuse the artifacts generated in the maven phase
  # instead of compiling them from scratch.
  cp -n -R ${CAULDRON_ROOT_DIR}/build/docker.common/build/cdh/oozie/*/* .
  exit 0
fi

#
# Resolve build version
#
RELEASE_ENV_FILENAME=release.env
RELEASE_ENV_FILE=${RELEASE_DIR}/cloudera/${RELEASE_ENV_FILENAME}
if [ -f "${RELEASE_ENV_FILE}" ]; then
    echo "Setting release environment using ${RELEASE_ENV_FILE}"
    . ${RELEASE_ENV_FILE}
fi

# IMPORTANT: THE FULL_VERSION, if set, will be used only for the final TARBALL,
#            Everything inside of the TARBALL uses the version specified in the POM
if [ -z "${FULL_VERSION}" ]; then
    FULL_VERSION=${OOZIE_VERSION}
    echo "Warning: FULL_VERSION not specified. Default version [${FULL_VERSION}] will be used."
fi
VERSION_SUFFIX="-${FULL_VERSION}"

#
# Build Oozie
#

echo "Starting system build"

# Test if there is a + sign in the dir path
# We need to do this because in Linux Sun JVM inteprets '+' as ' ' in URLs
# testcases uses URLs to access testcases directories and OpenJPA enhancer uses them as well
WORKDIR=${RELEASE_DIR}
if [ -n "${MAVEN_DEPLOY_ONLY}" ]; then
    WORKDIR=${RELEASE_DIR}/source
fi

if [[ "${WORKDIR}" == *\+* ]]; then
# There is a + sign. Create a temp work dir
# and copy over the files to that place
    WORKDIR=`mktemp -d /tmp/oozie.XXXXXX`
    echo "Created temporary workspace directory: ${WORKDIR}"
    cp -R ${RELEASE_DIR}/* ${WORKDIR}
fi

# Delete and recreate build directory
BUILDDIR=${WORKDIR}/build
rm -rf ${BUILDDIR}
mkdir -p ${BUILDDIR}

EXTRA_GOALS=""

# Check if maven deploy should be done or not
if [ "${DO_MAVEN_DEPLOY}" = "deploy" ]; then
    EXTRA_GOALS="${EXTRA_GOALS} source:jar deploy"
fi

MKDISTRO_OPTIONS="-B -Dfindbugs.skip=true -DskipTests -Dnot.cdh.release.build=false ${EXTRA_GOALS} -Dmaven.repo.local=${HOME}/.m2/repository${M2_REPO_SUFFIX} $@"
JAVA_OPTS="-Xmx3g -Xms256m -XX:MaxPermSize=256m" MAVEN_OPTS="-Xmx3g -Xms256m -XX:MaxPermSize=256m" ${WORKDIR}/bin/mkdistro.sh $MKDISTRO_OPTIONS
MKDISTRO_RESULT=$?
if [ "${MKDISTRO_RESULT}" != "0" ]; then
    echo "ERROR: mkdistro.sh failed with error ${MKDISTRO_RESULT}"
    echo "Build files from failed build: ${WORKDIR}"
    exit ${MKDISTRO_RESULT}
fi
SHARELIB=${WORKDIR}/distro/target/oozie-*-distro/oozie-*/oozie-sharelib-*-sharelib.tar.gz
cp $SHARELIB ${RELEASE_DIR}/oozie-sharelib-${FULL_VERSION}-yarn.tar.gz

#
# Expand Oozie TAR and create CDH Ooozie TAR
#
# (TODO: This logic is kind of twisted, revisit.)
# (TODO: Consider doing the source copy before the build if we can assume a clean checkout.)
#

EXPTMPDIR=${BUILDDIR}/temp
mkdir -p ${EXPTMPDIR}
URLSAFE_VERSION_SUFFIX=`echo ${VERSION_SUFFIX}| sed -e "s/+/-/"`
ORIGINAL_NAME=oozie${VERSION_SUFFIX}
URLSAFE_NAME=oozie${URLSAFE_VERSION_SUFFIX}
EXPDIR=${EXPTMPDIR}/${URLSAFE_NAME}
(
    cd ${EXPTMPDIR}
    tar --use-compress-program pigz -xf ${WORKDIR}/distro/target/oozie-*.tar.gz
    if [ "${FULL_VERSION}" != "${OOZIE_VERSION}" ]; then
        mv ${EXPTMPDIR}/oozie-* ${EXPDIR}
    fi

    cd ${EXPDIR}
    rm -rf ${EXPDIR}/oozie-client-*.tar.gz

    SRCDIR=${EXPDIR}/src
    mkdir -p ${SRCDIR}
    for srcfile in ${WORKDIR}/*
    do
        if [ "${srcfile}" != "${BUILDDIR}" ]; then
            cp -R ${srcfile} ${SRCDIR}
        fi
    done
    rm -rf ${SRCDIR}/target \
        ${SRCDIR}/*/target \
        ${SRCDIR}/core/build \
        ${SRCDIR}/core/pig*.log \
        ${SRCDIR}/core/pig*.properties \
        ${SRCDIR}/examples/mem \
        ${SRCDIR}/examples/oozietests \
        ${SRCDIR}/mkdistro-*.out \
        ${SRCDIR}/*.iml \
        ${SRCDIR}/*/*.iml \
        ${SRCDIR}/*.ipr \
        ${SRCDIR}/*.iws

    cat > ${SRCDIR}/cloudera/${RELEASE_ENV_FILENAME} <<EOM
FULL_VERSION=${FULL_VERSION}
export FULL_VERSION
EOM
    cp ${SRCDIR}/LICENSE.txt ${EXPDIR}
    cp ${SRCDIR}/NOTICE.txt ${EXPDIR}
    cd ${EXPDIR}
    jar -xf ${EXPDIR}/docs.zip
    rm -rf ${EXPDIR}/docs.zip

    # 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/oozie-${FULL_VERSION}/"
    mkdir -p "${PUB_DOCS_DIR}"
    cp -r docs/* "${PUB_DOCS_DIR}"

    rm -rf ${EXPDIR}/WEB-INF
    rm -rf ${EXPDIR}/META-INF
    cd ${EXPTMPDIR}
    if [ "${URLSAFE_NAME}" != "${ORIGINAL_NAME}" ]; then
        mv ${URLSAFE_NAME} ${ORIGINAL_NAME}
    fi

    rm -f ${ORIGINAL_NAME}/oozie-sharelib-*-sharelib.tar.gz
    mv ${ORIGINAL_NAME}/src/oozie-sharelib-*.tar.gz ${ORIGINAL_NAME}/

    tar --use-compress-program pigz -cf oozie${VERSION_SUFFIX}.tar.gz ${ORIGINAL_NAME}
    mv oozie${VERSION_SUFFIX}.tar.gz ${BUILDDIR}
    cd ${BUILDDIR}
    rm -rf ${EXPTMPDIR}
)

# If a temp work dir was created, move the files back to
# basedir and clean it up.

if [ "${WORKDIR}" != "${RELEASE_DIR}" ]; then
    echo "Moving built files from temporary location to work dir"
    mv ${WORKDIR}/build ${RELEASE_DIR}/build
    rm -rf ${WORKDIR}
fi

echo "Build available at: ${RELEASE_DIR}/build"

(cd ${RELEASE_DIR} ; tar --use-compress-program pigz --strip-components 1 -xvf build/oozie${VERSION_SUFFIX}.tar.gz; rm -f bin/mkdistro.sh)

echo "SUCCESS: Build complete"
exit 0
