#!/bin/bash

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

echo "Oozie setup"

tmp_dir=/tmp/oozie

if [[ "$DIB_HADOOP_VERSION" == "2.7.1" ]]; then
    if [ -z "${OOZIE_HADOOP_V2_7_1_DOWNLOAD_URL:-}" ]; then
        OOZIE_FILE=$(basename $OOZIE_HADOOP_V2_7_1_FILE)
    else
        OOZIE_FILE=$(basename $OOZIE_HADOOP_V2_7_1_DOWNLOAD_URL)
    fi
else
    echo "Unknown Hadoop version selected. Aborting."
    exit 1
fi

cd $tmp_dir
tar xzf $OOZIE_FILE
rm $OOZIE_FILE

if [ $(ls | wc -l) == "1" ]; then
    mv $(ls) /opt/oozie
else
    mkdir /opt/oozie
    mv * /opt/oozie/
fi

rm -rf $tmp_dir
