#!/bin/bash

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

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

if [ -z ${HIVE_DOWNLOAD_URL:-} ]; then
    # hive package has renamed to "apache-hive-*" since 0.13.0
    if [[ "$HIVE_VERSION" < "0.13.0" ]]; then
        HIVE_FILE=hive-$HIVE_VERSION-bin.tar.gz
    else
        HIVE_FILE=apache-hive-$HIVE_VERSION-bin.tar.gz
    fi
    HIVE_DOWNLOAD_URL=http://archive.apache.org/dist/hive/hive-$HIVE_VERSION/$HIVE_FILE
fi
HIVE_FILE=$(basename $HIVE_DOWNLOAD_URL)
cached_tar="$DIB_IMAGE_CACHE/$HIVE_FILE"
$TMP_HOOKS_PATH/bin/cache-url $HIVE_DOWNLOAD_URL $cached_tar
sudo install -D -g root -o root -m 0644 $cached_tar $tmp_dir
