#!/bin/sh
# A couple of examples how this script may be invoked:
#
#  $ infahadoopreshelper -mode update -hadoop mrv2 
#  
#  This script uses infa-hadoop-utils.jar and also requires 
#  INFA_HADOOP_DIST_DIR to be set to a valid hadoop distribution 
#  directory shipped by Informatica
#
#  -mode update updates the hadoopRes.properties file with the clusters hadoop/hdfs and Mapreduce classpath.
#  it uses either mapreduce2 or mapreduce1 based on the option provided.
#
#  $ infahadoopreshelper -mode archive -hadoop mrv2 -hive -hbase
# 
#  -mode archive packages all the required jars into a archive called HadoopDistributionJars.zip under the current Working directory.
#  You could choose the Services to be included in the archive. Like hadoop/hive/hbase.
#  When you choose hadoop you would need to specify the Mapreduce version that you want to bundle.
#  

if [ -z "${INFA_HOME}" ]; then
    INFA_HOME=../../../../../
fi

if [ -z "${INFA_HADOOP_DIST_DIR}" ]; then
    INFA_HADOOP_DIST_DIR=../
fi

# Use JAVA_HOME if it is set to invoke java
JAVA=java
if [ "$JAVA_HOME" != "" ]; then
  JAVA=$JAVA_HOME/bin/java
fi

RUNCLASS=com.informatica.platform.dtm.executor.hadoop.runtime.utils.standalone.utility.HadoopResUtility

JARFILE=infa-hadoop-utils.jar
JARFILE_PATH=${INFA_HOME}/services/shared/jars/platform/${JARFILE}

if [ ! -f ${JARFILE_PATH} ]; then
  JARFILE_PATH=${INFA_HOME}/services/shared/hadoop/${JARFILE}
#  echo "Trying to find ${JARFILE_PATH}!"
  if [ ! -f ${JARFILE_PATH} ]; then
    echo "${JARFILE} not found!"
    exit -100  
  fi
fi

THIRDPATYJARS=${INFA_HOME}/services/shared/jars/thirdparty

#_DEBUG="-Xdebug -Xrunjdwp:transport=dt_socket,address=8300,server=y,suspend=y"
_DEBUG=

COMMAND="${JAVA} ${_DEBUG} -classpath ${JARFILE_PATH}:${THIRDPATYJARS}/commons-cli-commons-cli-1.0.jar $RUNCLASS -distHome ${INFA_HADOOP_DIST_DIR} $@"

# run the command and save the exit code
${COMMAND} 2>&1
exitCode=$?

exit ${exitCode}
