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

# 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 [[ -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/spark/*/source/* .
  exit 0
fi

if [ -z "${DO_MAVEN_DEPLOY}" ]; then
    MAVEN_INST_DEPLOY=install
else
    MAVEN_INST_DEPLOY=$DO_MAVEN_DEPLOY
fi

BUILD_OPTS="-Divy.home=${HOME}/.ivy2 -Dsbt.ivy.home=${HOME}/.ivy2 -Duser.home=${HOME} \
            -Drepo.maven.org=$IVY_MIRROR_PROP \
            -Dreactor.repo=file://${HOME}/.m2/repository${M2_REPO_SUFFIX} \
            -DskipTests -DrecompileMode=all"
## this might be an issue at times
#        http://maven.40175.n5.nabble.com/Not-finding-artifact-in-local-repo-td3727753.html
export MAVEN_OPTS="-Xmx2g -XX:ReservedCodeCacheSize=512m -XX:PermSize=1024m -XX:MaxPermSize=1024m"

mkdir -p target/zinc
MYMVN=$PWD/$(mktemp -p target mvn.XXXXXXXXXX)
cat >$MYMVN <<EOF
#!/bin/sh
export ZINC_OPTS="-Dzinc.dir=$PWD/target/zinc -Xmx2g -XX:MaxPermSize=512M -XX:ReservedCodeCacheSize=512m"
#export APACHE_MIRROR=http://mirror.infra.cloudera.com/apache
#export TYPESAFE_MIRROR=http://mirror.infra.cloudera.com/toolchain
exec $PWD/build/mvn -B "\$@"
EOF
chmod 700 $MYMVN

./dev/make-distribution.sh  --mvn $MYMVN --target $MAVEN_INST_DEPLOY -Dcdh.build=true $CAULDRON_MAVEN_EXTRA_ARGS $BUILD_OPTS
rm -f $MYMVN

if [ -n "${CAULDRON_PUBLISH_DOCKER_REGISTRY}" ]; then
echo "Building spark on k8s docker image"
(
cd dist
LANGUAGE_BINDING_BUILD_ARGS="-p kubernetes/dockerfiles/spark/bindings/python/Dockerfile"
bin/docker-image-tool.sh -r ${CAULDRON_PUBLISH_DOCKER_REGISTRY} -t ${GLOBAL_BUILD_NUMBER} ${LANGUAGE_BINDING_BUILD_ARGS} build
)
echo "Spark on k8s docker build succeeded"

echo "Export spark on k8s docker images"
mkdir -p ${DOCKER_IMAGES_PATH}
(
cd ${DOCKER_IMAGES_PATH}
for image in $(docker images --filter=reference=${CAULDRON_PUBLISH_DOCKER_REGISTRY}/spark*:${GLOBAL_BUILD_NUMBER} --format {{.Repository\}}:{{.Tag}}); do docker save -o $(echo $image | awk -F'[/:]' '{print $(NF-1)".tar"}') $image; done
)
echo "Export spark on k8s docker images succeeded"
else
    echo "Docker build disabled"
fi

rm -rf build/spark-${FULL_VERSION}*
mkdir -p build/spark-${FULL_VERSION}

mv dist/* build/spark-${FULL_VERSION}
(cd build ; tar --use-compress-program pigz -cf spark-${FULL_VERSION}.tar.gz spark-${FULL_VERSION}/ ; rm -rf spark-${FULL_VERSION}/)
