#!/bin/bash

# Copyright (c) 2013 Cloudera, Inc. All rights reserved.
#
# Runs the HDFS snapshot set management tool.

# 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

set_hdfs_classpath

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

MAIN="com.cloudera.enterprise.bdr.snapshots.hdfs.Main"

# Create the CLASSPATH for executing snapshot-hdfs.
# - snapshots-common
# - cloudera-manager-api
# - several apache common libs
# - HDFS configuration
# - HDFS
# - Hadoop common
CLASSPATH=$(ls ${MGMT_HOME}/lib/dr/snapshot-hdfs-*.jar | head -n 1)
COMMONLIB=$(ls ${MGMT_HOME}/lib/dr/snapshots-common-*.jar | head -n 1)
CLASSPATH="$CLASSPATH:$COMMONLIB"
CLASSPATH="$CLASSPATH:${MGMT_HOME}/lib/*"
CLASSPATH="$CLASSPATH:$CONF_DIR/hadoop-conf"
CLASSPATH="$CLASSPATH:${CDH_HDFS_HOME}/*:${CDH_HDFS_HOME}/lib/*"
CLASSPATH="$CLASSPATH:${CDH_HADOOP_HOME}/*:${CDH_HADOOP_HOME}/lib/*"

# Add hadoop native libraries to java library path
# Disable IPv6.
set_hadoop_native_library_path
export JAVA_OPTS="$JAVA_OPTS -Djava.net.preferIPv4Stack=true -Djava.library.path=$JAVA_LIBRARY_PATH"

acquire_kerberos_tgt hdfs.keytab

exec $JAVA_HOME/bin/java $JAVA_OPTS -cp "$CLASSPATH" $MAIN \
    -f $CONF_DIR/logs/summary.json $EXTRA_ARGS "$@"
