#!/bin/bash -ex

DOWNLOAD_DIR=/tmp
APPLICATION_DIR=/home/apache
ZEPPELIN_DIR=zeppelin-0.7.3-bin-all    # all interpreters
TAR_FILE=${ZEPPELIN_DIR}.tgz
MRS_VERSION=`cat /opt/VERSION`
SPARK_DIR=/opt/Bigdata/${MRS_VERSION// /_}/spark/spark

echo "=====================Set env==============================================================================="
source /opt/client/bigdata_env

echo "=====================Download and unpack install package==================================================="
cd $DOWNLOAD_DIR
curl -o ./$TAR_FILE https://mrs-samples-bootstrap-cn-south-1.obs.cn-south-1.myhwclouds.com/zeppelin/zeppelin-0.7.3-bin-all.tgz 

if [ ! -f ./$TAR_FILE ] ; then
    echo "Failed to download $TAR_FILE"
    return 1
fi    
echo "Succeeded to download $TAR_FILE"

mkdir -p $APPLICATION_DIR/$ZEPPELIN_DIR
cd $APPLICATION_DIR
tar -xvzf $DOWNLOAD_DIR/$TAR_FILE -C $APPLICATION_DIR

echo "=====================Configure Zeppelin===================================================================="

echo "=====================Add SPARK_HOME to zeppelin-env.sh====================================================="
cd $APPLICATION_DIR/$ZEPPELIN_DIR
cp conf/zeppelin-env.sh.template conf/zeppelin-env.sh
chmod +x conf/zeppelin-env.sh
cat <<EOF >> conf/zeppelin-env.sh
export SPARK_HOME=$SPARK_DIR
EOF

echo "=====================Replace default zeppelin port 8080 with 7510 in zeppelin-site.xml====================="
cp conf/zeppelin-site.xml.template conf/zeppelin-site.xml
sed -i 's/<value>8080<\/value>/<value>7510<\/value>/' conf/zeppelin-site.xml

echo "=====================Start the Zeppelin daemon============================================================="
bin/zeppelin-daemon.sh start
