#!/bin/bash

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

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

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 and place it in JAVA_HOME. The catalina.sh script expects
# to find it there.
locate_cdh_java_home

set -x

echo "using $CDH_VERSION as CDH_VERSION"

if [ "$CDH_VERSION" -ge "5" ]; then
  # OPSAPS-15997 - Disable sourcing default env
  export BIGTOP_DEFAULTS_DIR=""
fi

JAAS_OPT=""
if [ -n "$SECURE_ZNODE" ]; then
JAAS_OPT="--jaas $SOLR_AUTHENTICATION_JAAS_CONF"
fi

# OPSAPS-27158 - ZKCLI_TMPDIR variable is used to prepare java.io.tmpdir option in zkcli
# Directly set the ZKCLI_JVM_FLAGS instead of setting ZKCLI_TMPDIR since the CDH5.4.x version
# does not have relevant changes in solrctl
ZKCLI_TMPDIR=$CONF_DIR/temp
mkdir  $ZKCLI_TMPDIR
export ZKCLI_JVM_FLAGS="-Djava.io.tmpdir=$ZKCLI_TMPDIR $ZKCLI_JVM_FLAGS"

if [ "solrinit" = "$1" ]; then
  # CDH-12906 - solrctl expects SOLR_HOME to point to solr libraries 
  # instead of /var/lib/solr
  export SOLR_HOME=$CDH_SOLR_HOME
  exec $CDH_SOLR_HOME/bin/solrctl.sh --zk $SOLR_ZK_ENSEMBLE $JAAS_OPT init
fi

if [ $CDH_VERSION -ge 5 ]; then
  if [ -z $SCM_DEFINES_SCRIPTS ]; then
    TOMCAT_CONF=/etc/solr/$SOLR_SERVER_DIR
  else
    TOMCAT_CONF=$CDH_SOLR_HOME/../../etc/solr/$SOLR_SERVER_DIR
  fi
  export CATALINA_BASE=/var/lib/solr/tomcat-deployment
  SOLR_PLUGIN_DIR=${SOLR_PLUGIN_DIR:-/var/lib/solr/lib}

  if [ "stop" != "$1" ]; then
    env TOMCAT_CONF=${TOMCAT_CONF} TOMCAT_DEPLOYMENT=${CATALINA_BASE} \
	SOLR_HOME=${CDH_SOLR_HOME} SOLR_PLUGIN_DIR=${SOLR_PLUGIN_DIR} \
	bash ${CDH_SOLR_HOME}/tomcat-deployment.sh
  fi
else
  export CATALINA_BASE=$CDH_SOLR_HOME/server
fi

# Search-replace {{CMF_CONF_DIR}} in files
replace_conf_dir
CATALINA_OPTS=$(replace_pid $CATALINA_OPTS)
export CATALINA_HOME=$TOMCAT_HOME
export CATALINA_BASE=$CDH_SOLR_HOME/server
export CATALINA_TMPDIR=$PWD/temp
export CATALINA_OUT=$SOLR_LOG/solr.out
export SOLR_RUN=$PWD
export SOLR_LOG4J_CONFIG=$CONF_DIR/log4j.properties
export SOLR_HDFS_CONFIG=$CONF_DIR/hadoop-conf

if [ "$SOLR_SENTRY_ENABLED" = "true" ]; then
  export SOLR_AUTHORIZATION_SENTRY_SITE=$CONF_DIR/sentry-site.xml
fi
if [ "$SOLR_SENTRY_SERVICE_ENABLED" = "true" ]; then
  export SOLR_AUTHORIZATION_SENTRY_SITE=$CONF_DIR/sentry-conf/sentry-site.xml
fi

if [ "stop" = "$1" ]; then
  exec $CDH_SOLR_HOME/bin/solrd "$@"
fi

if [ "$SYNC_CONFIG_TO_ZK" = "true" ]; then
  SOLR_HOME_TMP=$SOLR_HOME
  #Required due to CDH-12906
  export SOLR_HOME=$CDH_SOLR_HOME

  if [ "$SOLR_SSL_ENABLED" = "true" ]; then
    eval $SOLR_HOME/bin/solrctl.sh --zk $SOLR_ZK_ENSEMBLE $JAAS_OPT cluster --set-property urlScheme https
  else
    eval $SOLR_HOME/bin/solrctl.sh --zk $SOLR_ZK_ENSEMBLE $JAAS_OPT cluster --set-property urlScheme http
  fi

  #Fail the script if unable to set the cluster property.
  if [ 0 == $? ]; then
    echo "Successfully configured urlScheme property for Solr cluster in Zookeeper"
  else
    echo "Failed to configure urlScheme property for Solr cluster in Zookeeper"
    exit 1
  fi

  #Required due to CDH-27531
  export SOLR_HOME=$SOLR_HOME_TMP
fi

# Create temp directory for Catalina
mkdir -p $CATALINA_TMPDIR
exec $CDH_SOLR_HOME/bin/solrd run

