#!/bin/bash

# === MRS Hadoop ===
#
# Find the "hadoop" command.
#
# Implements the logic of searching for the "hadoop" command-line
# utility. It is used mainly in R when connecting from a remote
# client and running hadoop/hdfs-related commands via ssh.
#
###

SCRIPT_DIR=$(cd "`dirname $(readlink -nf "$0")`"; pwd -P)

# scaleR-hadoop-0.1-SNAPSHOT.jar and mrs-hadoop should be in the same folder
# try to get scaleR-hadoop-0.1-SNAPSHOT.jar from mrs-hadoop's path.
HADOOP_JAR=${SCRIPT_DIR}/jar/scaleR-hadoop-0.1-SNAPSHOT.jar

if [ -f "${HADOOP_JAR}" ] ; then
  export HADOOP_CLASSPATH="${HADOOP_JAR}:${HADOOP_CLASSPATH}"
else
  echo "cannot find the scaleR hadoop jar" >&2
  exit 1
fi

# use HADOOP_PREFIX even if HADOOP_HOME is only set
if [ -z "${HADOOP_PREFIX}" ] ; then
  HADOOP_PREFIX="${HADOOP_HOME}"
fi

HADOOP_BIN=$(which hadoop 2>/dev/null)

# If cannot find hadoop command, look for hadoop location in the prefix
if [ -z "${HADOOP_BIN}" ] ; then
  if [ -x "${HADOOP_PREFIX}/bin/hadoop" ] ; then
    HADOOP_BIN="${HADOOP_PREFIX}/bin/hadoop"
  else
    echo "cannot find the 'hadoop' script/executable" >&2
    exit 1
  fi
fi 

exec "${HADOOP_BIN}" "$@"


