#!/bin/bash

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

tmp_dir=$TARGET_ROOT/tmp/oozie
mkdir -p $tmp_dir

if [[ "$DIB_HADOOP_VERSION" == "2.7.1" ]]; then
    OOZIE_DOWNLOAD_URL="${OOZIE_HADOOP_V2_7_1_DOWNLOAD_URL:-}"
    OOZIE_FILE="${OOZIE_HADOOP_V2_7_1_FILE:-}"
elif [[ "$DIB_HADOOP_VERSION" == "2.7.5" ]]; then
    OOZIE_DOWNLOAD_URL="${OOZIE_HADOOP_V2_7_5_DOWNLOAD_URL:-}"
    OOZIE_FILE="${OOZIE_HADOOP_V2_7_5_FILE:-}"
elif [[ "$DIB_HADOOP_VERSION" == "2.8.2" ]]; then
    OOZIE_DOWNLOAD_URL="${OOZIE_HADOOP_V2_8_2_DOWNLOAD_URL:-}"
    OOZIE_FILE="${OOZIE_HADOOP_V2_8_2_FILE:-}"
elif [[ "$DIB_HADOOP_VERSION" == "3.0.1" ]]; then
    OOZIE_DOWNLOAD_URL="${OOZIE_HADOOP_V3_0_1_DOWNLOAD_URL:-}"
    OOZIE_FILE="${OOZIE_HADOOP_V3_0_1_FILE:-}"
else
    echo "Unknown Hadoop version selected. Aborting."
    exit 1
fi

if [ -n "${OOZIE_DOWNLOAD_URL:-}" ]; then
    OOZIE_FILE=$(basename $OOZIE_DOWNLOAD_URL)
    cached_tar="$DIB_IMAGE_CACHE/$OOZIE_FILE"
    $TMP_HOOKS_PATH/bin/cache-url "$OOZIE_DOWNLOAD_URL" $cached_tar
    sudo install -D -g root -o root -m 0755 $cached_tar $tmp_dir
elif [ -n "$OOZIE_FILE" ]; then
    sudo install -D -g root -o root -m 0755 $(dirname $0)/$OOZIE_FILE $tmp_dir
fi
