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

. `dirname $0`/bigtop.bom

# We build Zeppelin with minimal interpreters here
# so as to reduce artifact size (cf. BIGTOP-2269).
# If we include all of these interpreters into
# the artifact, its size is 1.5GB as of v0.9.0,
# while it's around 750MB without them.
# Users can install them later via Zeppelin's "netinst" feature.
EXCLUDE_INTERPRETERS=!hbase,!jdbc,!cassandra,!elasticsearch,!bigquery,!alluxio,!groovy,!java,!neo4j,!submarine,!sparql,!mongodb

ZEPPELIN_LIVY_VERSION=${LIVY_VERSION}-incubating
ZEPPELIN_SCALA_BINARY_VERSION=${SCALA_VERSION%.*}
ZEPPELIN_SPARK_VERSION=${SPARK_VERSION%.*}

BUILD_OPTS="-Dhadoop3.3.version=${HADOOP_VERSION} \
            -Dflink1.17.version=${FLINK_VERSION} \
            -Dflink.scala.version=${SCALA_VERSION} \
            -Dlivy.version=${ZEPPELIN_LIVY_VERSION} \
            -Pbuild-distr \
            -Phadoop3 \
            -Pflink-117 \
            -Pspark-scala-${ZEPPELIN_SCALA_BINARY_VERSION} \
            -Pspark-${ZEPPELIN_SPARK_VERSION} \
            -Pweb-dist"

if [ $HOSTTYPE = "powerpc64le" ] ; then
  mvn install:install-file -DgroupId=com.google.protobuf -DartifactId=protoc -Dversion=3.5.0 \
      -Dclassifier=linux-ppcle_64 -Dpackaging=exe -Dfile=/usr/local/protobuf-3.17.3/bin/protoc
  mvn install:install-file -DgroupId=io.grpc -DartifactId=protoc-gen-grpc-java -Dversion=1.26.0 -Dclassifier=linux-ppcle_64 -Dpackaging=exe -Dfile=/usr/src/grpc-java-1.26.0/compiler/build/exe/java_plugin/protoc-gen-grpc-java
  mvn install:install-file -DgroupId=io.grpc -DartifactId=protoc-gen-grpc-java -Dversion=1.28.0 -Dclassifier=linux-ppcle_64 -Dpackaging=exe -Dfile=/usr/src/grpc-java-1.28.0/compiler/build/exe/java_plugin/protoc-gen-grpc-java
sed -i "s|<node.version>v12.3.1</node.version>|<node.version>v12.22.1</node.version>|" pom.xml
sed -i "s|<npm.version>6.9.0</npm.version>|<npm.version>6.14.12</npm.version>|" pom.xml

fi

if [ "$ZEPPELIN_RUN_TESTS" = "true" ]; then
  BUILD_OPTS="$BUILD_OPTS -DskipTests=false"
else
  BUILD_OPTS="$BUILD_OPTS -DskipTests"
fi

export MAVEN_OPTS="-Xmx1500m -Xms1500m"

# BIGTOP-2742:
# The default zeppelin PIDFILE includes the literal $(hostname) string, which is carried into the init script's pidfile declaration.
# But pidfile does not subshell out to generate the expected string.
# The zeppelin process does indeed start up, but systemctl reports a failure because it's looking for a non-existent pidfile.
#
# Workaround:
#   Remove unnecessary $HOSTNAME from zeppelin-daemon.sh and also make the same adjustment to zeppelin.svc.
# TODO:
#   Request the issue to zeppelin upstream, If accepted, remove this workaround.
sed -i "s/^.*ZEPPELIN_PID=.*$/ZEPPELIN_PID=\"\$\{ZEPPELIN_PID_DIR\}\/zeppelin\-\$\{ZEPPELIN_IDENT_STRING\}\.pid\"/" bin/zeppelin-daemon.sh

mvn $BUILD_OPTS clean package -pl ''$EXCLUDE_INTERPRETERS'' -am

mkdir -p build/dist
tar -C build/dist --strip-components=1 -xzf zeppelin-distribution/target/zeppelin-*.tar.gz
