#!/bin/bash

# Copyright (c) 2012 Cloudera, Inc. All rights reserved.
#
# Runs custom distcp jar.  Currently only
# works with CDH4's MR1.

# Time marker for both stderr and stdout
date; date 1>&2

# debug
set -x
echo "Running on: $(hostname -f) ($(hostname -i))"

cloudera_config=`dirname $0`
cloudera_config=`cd "$cloudera_config/../common"; pwd`
. ${cloudera_config}/cloudera-config.sh

# load the parcel environment
source_parcel_environment

# attempt to find java
locate_cdh_java_home

# simulate /etc/default/hadoop if necessary
. ${cloudera_config}/cdh-default-hadoop

echo "using $JAVA_HOME as JAVA_HOME"
echo "using $CDH_VERSION as CDH_VERSION"
echo "using $CONF_DIR as CONF_DIR"

set_hadoop_classpath

# Disable IPv6.
export HADOOP_OPTS="-Djava.net.preferIPv4Stack=true $HADOOP_OPTS"

acquire_kerberos_tgt hdfs.keytab

if [ "MAPREDUCE" = "$MAPREDUCE_SERVICE_TYPE" ]; then
  MR_CONF_DIR=${CONF_DIR}/hadoop-conf
  HADOOP_HOME=${CDH_MR1_HOME}
  echo "using $CDH_MR1_HOME as CDH_MR1_HOME"
  perl -pi -e "s#{{CDH_MR1_HOME}}#$CDH_MR1_HOME#g" $MR_CONF_DIR/*
elif [ "YARN" = "$MAPREDUCE_SERVICE_TYPE" ]; then
  MR_CONF_DIR=${CONF_DIR}/yarn-conf
  HADOOP_HOME=${CDH_HADOOP_HOME}
  echo "using $CDH_MR2_HOME as CDH_MR2_HOME"
  perl -pi -e "s#{{CDH_MR2_HOME}}#$CDH_MR2_HOME#g" $MR_CONF_DIR/*
else
  echo "Invalid value for MAPREDUCE_SERVICE_TYPE: $MAPREDUCE_SERVICE_TYPE"
  exit 1
fi

ACLLIB_CLASSPATH=`echo ${MGMT_HOME}/lib/dr/distcp_acllib-*.jar`
export HADOOP_CLASSPATH="$ACLLIB_CLASSPATH:$HADOOP_CLASSPATH"

exec ${HADOOP_HOME}/bin/hadoop --config ${MR_CONF_DIR} jar ${MGMT_HOME}/lib/dr/distcp-*.jar -libjars ${MGMT_HOME}/lib/dr/distcp_acllib-*.jar "$@"
