#!/bin/bash

if [ "${DIB_DEBUG_TRACE:-0}" -gt 0 ]; then
    set -x
fi
set -eu
set -o pipefail

if [ "$DISTRO_NAME" = "ubuntu" ]; then
    export DEBIAN_FRONTEND=noninteractive
    export RUNLEVEL=1
fi

# Install the rest of CDH unless a limited HDFS install was requested
if [ -z "${DIB_CDH_HDFS_ONLY:-}" ]; then
    install-packages \
        cloudera-manager-agent \
        cloudera-manager-daemons \
        cloudera-manager-server \
        cloudera-manager-server-db-2 \
        hadoop-hdfs-secondarynamenode \
        hadoop-mapreduce \
        hadoop-mapreduce-historyserver \
        hadoop-yarn-nodemanager \
        hadoop-yarn-resourcemanager \
        hbase \
        hive-hcatalog \
        hive-metastore \
        hive-server2 \
        hive-webhcat-server \
        hue \
        oozie \
        spark-core \
        zookeeper \
        flume-ng \
        hadoop-kms \
        hbase-solr \
        impala \
        impala-server \
        impala-state-store \
        impala-catalog \
        impala-shell \
        keytrustee-keyprovider \
        sentry \
        solr-server \
        solr-doc \
        search \
        spark-history-server \
        sqoop2 \
        kafka \
        kafka-server
fi

DIB_CDH_MINOR_VERSION=${DIB_CDH_MINOR_VERSION:-$DIB_CDH_VERSION.0}

case "$DIB_CDH_VERSION" in
    5.7)
        HADOOP_OPENSTACK_5_7_x_URL="https://repository.cloudera.com/artifactory/repo/org/apache/hadoop/hadoop-openstack/2.6.0-cdh$DIB_CDH_MINOR_VERSION/hadoop-openstack-2.6.0-cdh$DIB_CDH_MINOR_VERSION.jar"
        ;;
    5.9)
        HADOOP_OPENSTACK_5_9_x_URL="https://repository.cloudera.com/artifactory/repo/org/apache/hadoop/hadoop-openstack/2.6.0-cdh$DIB_CDH_MINOR_VERSION/hadoop-openstack-2.6.0-cdh$DIB_CDH_MINOR_VERSION.jar"
        ;;
    5.11)
        HADOOP_OPENSTACK_5_11_x_URL="https://repository.cloudera.com/artifactory/repo/org/apache/hadoop/hadoop-openstack/2.6.0-cdh$DIB_CDH_MINOR_VERSION/hadoop-openstack-2.6.0-cdh$DIB_CDH_MINOR_VERSION.jar"
        ;;
esac
HADOOP_OPENSTACK_5_5_0_URL="https://repository.cloudera.com/artifactory/repo/org/apache/hadoop/hadoop-openstack/2.6.0-cdh5.5.0/hadoop-openstack-2.6.0-cdh5.5.0.jar"

dest=/usr/lib/hadoop-mapreduce/hadoop-openstack.jar
case "$DIB_CDH_VERSION" in
    5.5)
        wget -O $dest $HADOOP_OPENSTACK_5_5_0_URL
        if [ -z "${DIB_CDH_HDFS_ONLY:-}" ]; then
            # Create links to keep backward version support.
            ln -s /usr/lib/oozie/oozie-sharelib-yarn /usr/lib/oozie/oozie-sharelib-yarn.tar.gz
            ln -s /usr/lib/oozie/oozie-sharelib-mr1 /usr/lib/oozie/oozie-sharelib-mr1.tar.gz
            ln -s /usr/lib/oozie/oozie-sharelib-yarn.tar.gz /usr/lib/oozie/oozie-sharelib.tar.gz
        fi
        ;;
    5.7)
        wget -O $dest $HADOOP_OPENSTACK_5_7_x_URL
        if [ -z "${DIB_CDH_HDFS_ONLY:-}" ]; then
            # Create links to keep backward version support.
            ln -s /usr/lib/oozie/oozie-sharelib-yarn /usr/lib/oozie/oozie-sharelib-yarn.tar.gz
            ln -s /usr/lib/oozie/oozie-sharelib-mr1 /usr/lib/oozie/oozie-sharelib-mr1.tar.gz
            ln -s /usr/lib/oozie/oozie-sharelib-yarn.tar.gz /usr/lib/oozie/oozie-sharelib.tar.gz
        fi
        ;;
    5.9)
        wget -O $dest $HADOOP_OPENSTACK_5_9_x_URL
        if [ -z "${DIB_CDH_HDFS_ONLY:-}" ]; then
            # Create links to keep backward version support.
            ln -s /usr/lib/oozie/oozie-sharelib-yarn /usr/lib/oozie/oozie-sharelib-yarn.tar.gz
            ln -s /usr/lib/oozie/oozie-sharelib-mr1 /usr/lib/oozie/oozie-sharelib-mr1.tar.gz
            ln -s /usr/lib/oozie/oozie-sharelib-yarn.tar.gz /usr/lib/oozie/oozie-sharelib.tar.gz
        fi
        ;;
    5.11)
        wget -O $dest $HADOOP_OPENSTACK_5_11_x_URL
        if [ -z "${DIB_CDH_HDFS_ONLY:-}" ]; then
            # Create links to keep backward version support.
            ln -s /usr/lib/oozie/oozie-sharelib-yarn /usr/lib/oozie/oozie-sharelib-yarn.tar.gz
            ln -s /usr/lib/oozie/oozie-sharelib-mr1 /usr/lib/oozie/oozie-sharelib-mr1.tar.gz
            ln -s /usr/lib/oozie/oozie-sharelib-yarn.tar.gz /usr/lib/oozie/oozie-sharelib.tar.gz
        fi
        ;;
    *)
        echo "Unhandled version $DIB_CDH_VERSION for hadoop-openstack.jar"
        exit 1
esac

if [ "$DISTRO_NAME" = "ubuntu" ]; then
    unset RUNLEVEL
fi
