#!/bin/bash

# Copyright (c) 2012 Cloudera, Inc. All rights reserved.

impala_shell_usage()
{
  echo "Wrong arguments for impala-shell command"
  echo "Usage: impala.sh impala-shell impalad_flags debugbuild hostport query-file is-ssl-enabled ssl-ca-certificate kerberized_impalad_service kerberos_principal_name"
  echo "Please note that ssl-ca-certificate or kerberized_impalad_service or kerberos_principal_name can be specified as single or double quoted string to indicate empty string"
  echo "Provide either both kerberized_impalad_service and kerberos_principal_name as valid strings or both as empty strings"
  exit 1
}

run_kinit()
{
  # run the kinit first
  kinit -V $1 -k -t ${CONF_DIR}/impala.keytab
}

set -e
set -x

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

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

export IMPALA_HOME=${IMPALA_HOME:-$CDH_IMPALA_HOME}

# set impala configuration directory
export IMPALA_CONF_DIR=${CONF_DIR}/impala-conf
export HADOOP_CONF_DIR=${CONF_DIR}/hadoop-conf
export HIVE_CONF_DIR=${CONF_DIR}/hive-conf
export HBASE_CONF_DIR=${CONF_DIR}/hbase-conf
export JAVA_TOOL_OPTIONS=$(replace_pid $JAVA_TOOL_OPTIONS)

if [[ -d $HBASE_CONF_DIR ]]; then
  perl -pi -e "s#{{HBASE_CONF_DIR}}#$HBASE_CONF_DIR#g" $HBASE_CONF_DIR/hbase-env.sh
fi


# Add JDBC jars to the path. Oracle and Mysql installed by end user, can't ship due to licensing.
JDBC_JARS="$CLOUDERA_MYSQL_CONNECTOR_JAR:$CLOUDERA_POSTGRESQL_JDBC_JAR:$CLOUDERA_ORACLE_CONNECTOR_JAR"
if [[ -z "$AUX_CLASSPATH" ]]; then
  export AUX_CLASSPATH="$JDBC_JARS"
else
  export AUX_CLASSPATH="$AUX_CLASSPATH:$JDBC_JARS"
fi
# Due to IMP-898 Impala isn't correctly picking up AUX_CLASSPATH in Impala 1.0, but it does pick up CLASSPATH.
# Just emit both.
if [[ -z "$CLASSPATH" ]]; then
  export CLASSPATH="$JDBC_JARS"
else
  export CLASSPATH="$CLASSPATH:$JDBC_JARS"
fi

# The DSSD_SCR parcel sets JAVA_LIBRARY_PATH, but impalad.sh doesn't
# add it to java.library.path. Setting LIBHDFS_OPTS is a workaround.
if [[ -n "$JAVA_LIBRARY_PATH" ]]; then
  export LIBHDFS_OPTS="$LIBHDFS_OPTS -Djava.library.path=$IMPALA_HOME/lib:$JAVA_LIBRARY_PATH"
fi

FLAG_FILE=$IMPALA_CONF_DIR/$2
USE_DEBUG_BUILD=$3

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

# Ensure .sh and .py files are executable
make_scripts_executable

# Update path to configuration directory in the flagfile
perl -pi -e "s#{{CMF_CONF_DIR}}#$CONF_DIR#g" ${FLAG_FILE}

# Update path to cgroups root in the flag file
perl -pi -e "s#{{CGROUP_ROOT_CPU}}#$CGROUP_ROOT_CPU$CGROUP_GROUP_CPU#g" ${FLAG_FILE}

# make .htpasswd file rw by owner only
if [ -f "${IMPALA_CONF_DIR}/.htpasswd" ]; then
  chmod 600 ${IMPALA_CONF_DIR}/.htpasswd
fi

# Set Impala bin location if debug build need to run. The /etc/default/impala
# will use this location and when we exec impala bash file below, it will
# use the impala binary from debug build location
if $USE_DEBUG_BUILD ; then
  export IMPALA_BIN=${IMPALA_HOME}/sbin-debug
else
  export IMPALA_BIN=${IMPALA_HOME}/sbin-retail
fi

if [[ "true" = "$ENABLE_CORE_DUMPS" && -n "$CORE_DUMP_DIR" && -d $CORE_DUMP_DIR ]]; then
  cd $CORE_DUMP_DIR
fi

if [ "impalad" = "$1" ]; then
  exec "$IMPALA_HOME/../../bin/impalad" --flagfile=${FLAG_FILE}
elif [ "statestore" = "$1" ]; then
  exec "$IMPALA_HOME/../../bin/statestored" --flagfile=${FLAG_FILE}
elif [ "catalogd" = "$1" ]; then
  exec "$IMPALA_HOME/../../bin/catalogd" --flagfile=${FLAG_FILE}
elif [ "impala-shell" = "$1" ]; then
  if [ "$#" -ne 9 ]; then
    impala_shell_usage
  fi
  # parse argument from 7 to 9 to determine if SSL ca certificate or
  # kerberos_service_name and kerberos_principal_name are valid or not
  eval ARG7=$7
  eval ARG8=$8
  eval ARG9=$9

  SSL_CA_CERT=
  KERBEROS_SERVICE_NAME=
  KERBEROS_PRINCIPAL_NAME=
  if [ ! -z "$ARG7" ]; then
    SSL_CA_CERT=$ARG7
  fi
  if [ ! -z "$ARG8" ]; then
    if [ -z "$ARG9" ]; then
      impala_shell_usage
    fi
    KERBEROS_SERVICE_NAME="$ARG8 -k"
    KERBEROS_PRINCIPAL_NAME=$ARG9
    run_kinit $KERBEROS_PRINCIPAL_NAME
  elif [ ! -z "$ARG9" ]; then
    impala_shell_usage
  fi

  SSLARGS=
  if [ "true" = "$6" ]; then
    SSLARGS="--ssl $SSL_CA_CERT"
  fi
  HOSTPORT="-i $4"
  QUERY_FILE="-f ${CONF_DIR}/$5"
  # run the impala shell command
  CMD="$IMPALA_HOME/../../bin/impala-shell $HOSTPORT $QUERY_FILE $SSLARGS $KERBEROS_SERVICE_NAME"
  echo $CMD
  exec $CMD | tr -s \\n
elif [ $1 = "breakpad-dump-data" ]; then
  set -e
  PYTHON_CMD=$CMF_PACKAGE_DIR/../agent/build/env/bin/python
  COLLECT_BREAKPAD_DUMPS="$IMPALA_HOME/../../bin/impala-collect-minidumps"
  PROCESS_DIR_NAME=$(pwd)
  BREAKPAD_DUMP_FILE=$PROCESS_DIR_NAME/$3.tar.gz

  #Mapping the rolenames sent by CM to the ones needed by Impala
  if [ "$3" == "IMPALAD" ]; then
    ROLE_NAME="impalad"
  elif [ "$3" == "STATESTORE" ]; then
    ROLE_NAME="statestored"
  elif [ "$3" == "CATALOGSERVER" ]; then
    ROLE_NAME="catalogd"
  else
    echo "Breakpad minidumps collection is not supported for role type: $3"
  fi

  COLLECT_BREAKPAD_DUMP_ARGS="--end_time $2 --role_name $ROLE_NAME --max_output_size $4 --output_file_path $BREAKPAD_DUMP_FILE --conf_dir $IMPALA_CONF_DIR"
  if [[ ! -z $5 ]]; then
    COLLECT_BREAKPAD_DUMP_ARGS="$COLLECT_BREAKPAD_DUMP_ARGS --start_time $5"
  fi
  PYTHON_VERSION_CHECK="import sys; print (sys.hexversion >= 0x02060000)"
  CORRECT_PYTHON_VERSION="$($PYTHON_CMD -c "$PYTHON_VERSION_CHECK")"
  if [ $CORRECT_PYTHON_VERSION != "True" ]; then
    echo "Python Version greater than 2.6 is needed to collect breakpad dumps. Skipping.."
  else
    $COLLECT_BREAKPAD_DUMPS $COLLECT_BREAKPAD_DUMP_ARGS
  fi
  if [[ ! -f $BREAKPAD_DUMP_FILE ]]; then
    echo "No Impala Dumps found."
  else
    ln -s $BREAKPAD_DUMP_FILE output.gz
  fi
fi
