#!/bin/sh
# A couple of examples how this script may be invoked:
#
#  $ infahadooputil --debug --getclasspath
#  
#  From another script this can be invoked as
#  hdpclasspath=`infahadooputil --getclasspath`
#  
#  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
#
#  --getclasspath returns the classpath string specified by the
#  user in the resource descriptor file
#

while [ $# -gt 0 ]; do
  case "$1" in
    --debug)
      DEBUG=true
      shift
      ;;
    --getclasspath)
      export GETCLASSPATH=true
      shift
      ;;
    *)
      break
      ;;
  esac
done

if [ -z "${INFA_RESOURCES}" ]; then
    echo "The INFA_RESOURCES variable is not set.."
    exit 1;
fi

export INFA_HOME_BIN=${INFA_RESOURCES}

if [ -z "${INFA_HADOOP_DIST_DIR}" ]; then
  echo "Error: INFA_HADOOP_DIST_DIR variable is not set while invoking the script."
  exit 100;
fi

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

if [ "$GETCLASSPATH" ]; then

#find the classpath as specified by the user using the utility 
RUNCLASS=com.informatica.platform.dtm.executor.hadoop.runtime.utils.standalone.utility.HadoopClassPathGetter

JARFILE=infa-hadoop-utils.jar
JARFILE_PATH=${INFA_HOME_BIN}/../jars/platform/${JARFILE}

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

COMMAND="${JAVA} -classpath ${JARFILE_PATH} -DINFA_HADOOP_DIST_DIR=$INFA_HADOOP_DIST_DIR $RUNCLASS"

if [ "$DEBUG" ]; then
  echo ${COMMAND}
fi

# run the command and save the exit code
HDP_CLASSPATH=`${COMMAND}`
exitCode=$?

if [[ $exitCode != 0 ]] ; then
  echo "HDP_CLASSPATH = ${HDP_CLASSPATH}"
  exit $exitCode
fi

if [ "$DEBUG" ]; then
  echo "HDP_CLASSPATH = ${HDP_CLASSPATH}"
fi

echo "${HDP_CLASSPATH}"

exit ${exitCode}

fi #end [ "$GETCLASSPATH" ]; then
