#!/bin/bash

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

hadoop="2.7.1"
case "$plugin_type" in
    "vanilla")
        case "$DIB_HADOOP_VERSION" in
            "2.7.1")
                hadoop="2.7.1";;
        esac
        DIB_HADOOP_SWIFT_JAR_NAME="hadoop-openstack-$hadoop.jar"
        ;;
    "cloudera" | "spark")
        case "$DIB_CDH_VERSION" in
            "5.4" | "5.5" | "5.7" | "5.9" | "5.11")
                hadoop="2.6.0";;
        esac
        case "$plugin_type" in
            "cloudera")
                DIB_CDH_MINOR_VERSION=${DIB_CDH_MINOR_VERSION:-$DIB_CDH_VERSION.0}
                DIB_HADOOP_SWIFT_JAR_NAME="hadoop-openstack-$hadoop-cdh$DIB_CDH_MINOR_VERSION.jar";;
            "spark")
                DIB_HADOOP_SWIFT_JAR_NAME="hadoop-openstack.jar";;
        esac
        ;;
    "ambari")
        case "$DIB_AMBARI_VERSION" in
            "" | "2.2.0.0")
                hadoop="2.7.1";;
        esac
        DIB_HADOOP_SWIFT_JAR_NAME="hadoop-openstack.jar"
    ;;
esac

HADOOP_SWIFT_BUILD_LOCATION="http://tarballs.openstack.org/sahara/dist/hadoop-openstack/master"
HDFS_LIB_DIR=${DIB_HDFS_LIB_DIR:-"/usr/share/hadoop/lib"}
SWIFT_LIB_URI="$HADOOP_SWIFT_BUILD_LOCATION/hadoop-openstack-${hadoop}.jar"
HADOOP_SWIFT_JAR_NAME=${DIB_HADOOP_SWIFT_JAR_NAME:-"hadoop-openstack.jar"}

if [ -z "${swift_url:-}" ]; then
    wget -O $HDFS_LIB_DIR/$HADOOP_SWIFT_JAR_NAME $SWIFT_LIB_URI
else
    wget -O $HDFS_LIB_DIR/$HADOOP_SWIFT_JAR_NAME $swift_url
fi

if [ $? -ne 0 ]; then
    echo -e "Could not download Swift Hadoop FS implementation.\nAborting"
    exit 1
fi

chmod 0644 $HDFS_LIB_DIR/$HADOOP_SWIFT_JAR_NAME
