#!/bin/bash
# Copyright (c) 2011-2012 Cloudera, Inc. All rights reserved.

set -e

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

set -x

FLUME_CONF_FILE=${CONF_DIR}/flume.conf
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

if [ -z "$FLUME_AGENT_NAME" ]; then
  echo "FLUME_AGENT_NAME not set"
  exit 1
fi
if [ -z "$CDH_FLUME_HOME" ]; then
  echo "CDH_FLUME_HOME not set"
  exit 1
fi

CDH_FLUME_BIN=$CDH_FLUME_HOME/bin/flume-ng
echo "using agent name $FLUME_AGENT_NAME"
echo "using Flume executable $CDH_FLUME_BIN"

set_flume_classpath

# Ensure FLUME_CLASSPATH and FLUME_JAVA_LIBRARY_PATH are propagated
# by creating flume-env.sh to set them. There is no other way.
FLUME_ENV_SH="$CONF_DIR/flume-env.sh"
echo "export FLUME_CLASSPATH=\"$FLUME_CLASSPATH\"" > "$FLUME_ENV_SH"
if [ ! -z "$FLUME_JAVA_LIBRARY_PATH" ]; then
  echo "export FLUME_JAVA_LIBRARY_PATH=\"$FLUME_JAVA_LIBRARY_PATH\"" >> "$FLUME_ENV_SH"
fi

echo "using FLUME_HOME_DIR $FLUME_HOME_DIR"
FLUME_AGENT_JAVA_OPTS="-Duser.home=$FLUME_HOME_DIR $FLUME_AGENT_JAVA_OPTS"

FLUME_AGENT_JAVA_OPTS=$(replace_pid $FLUME_AGENT_JAVA_OPTS)

# Search-replace {{CMF_CONF_DIR}} in files
replace_conf_dir

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

# If HDFS and HBase confs are present, add them to additional classpath
CLIENT_CONFS=""
if [ -d $CONF_DIR/hadoop-conf ]; then
  echo "Adding hadoop-conf to classpath"
  CLIENT_CONFS="$CONF_DIR/hadoop-conf:$CLIENT_CONFS"
fi
if [ -d $CONF_DIR/hbase-conf ]; then
  echo "Adding hbase-conf to classpath"
  CLIENT_CONFS="$CONF_DIR/hbase-conf:$CLIENT_CONFS"
  # Put in jaas.conf's actual location in hbase-env.sh
  HBASE_CONF_DIR=$CONF_DIR/hbase-conf
  perl -pi -e "s#{{HBASE_CONF_DIR}}#$HBASE_CONF_DIR#g" $HBASE_CONF_DIR/hbase-env.sh
fi
if [ "" != "$CLIENT_CONFS" ]; then
  CLIENT_CONFS="--classpath $CLIENT_CONFS"
fi

# The standard Flume script observes FLUME_CLASSPATH.
FLUME_AGENT_CMD="$CDH_FLUME_BIN agent \
  --conf $CONF_DIR $CLIENT_CONFS --conf-file $FLUME_CONF_FILE --name $FLUME_AGENT_NAME $FLUME_AGENT_JAVA_OPTS"

flume_help_output=`$CDH_FLUME_BIN help`
if [[ $flume_help_output =~ --plugins-path ]]; then
  # By default, flume-ng loads third-party plugins
  # from /usr/lib/flume-ng/plugins.d. However,parcels need
  # plugins to be installed outside service home.
  # Users can override the default location in either
  # - CM, and/or
  # - choose to write custom parcels to install plugins,
  # and add the plugins directories to FLUME_PLUGINS_DIRS
  # environment variable
  AGENT_PLUGINS_DIRS_PATH=""
  # FLUME_AGENT_PLUGINS_DIRS is set in CM
  if [ -n "$FLUME_AGENT_PLUGINS_DIRS" ]; then
    AGENT_PLUGINS_DIRS_PATH=${FLUME_AGENT_PLUGINS_DIRS}
    echo "Adding $FLUME_AGENT_PLUGINS_DIRS to plugins path"
  fi
  # FLUME_PLUGINS_DIRS is set by custom parcels
  if [ -n "$FLUME_PLUGINS_DIRS" ]; then
    AGENT_PLUGINS_DIRS_PATH="${AGENT_PLUGINS_DIRS_PATH}:${FLUME_PLUGINS_DIRS}"
    echo "Adding $FLUME_PLUGINS_DIRS to plugins path"
  fi
  if [ -n "$AGENT_PLUGINS_DIRS_PATH" ]; then
    FLUME_AGENT_CMD="${FLUME_AGENT_CMD} --plugins-path ${AGENT_PLUGINS_DIRS_PATH}"
  fi
fi

if [ -n "$FLUME_AGENT_DISABLE_ZOO_CFG" ]; then
  if [[ $flume_help_output =~ --disable-zoo-cfg ]]; then
    # OPSAPS-15019: Add arg to prevent importing zookeeper/conf 
    # from the HBase classpath.
    # This works around an issue where zoo.cfg overrides
    # hbase-site.xml for zookeeper quorum info on CDH4.
    FLUME_AGENT_CMD="${FLUME_AGENT_CMD} --disable-zoo-cfg"
  fi
fi

exec $FLUME_AGENT_CMD
