#! /bin/bash

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

CMD=$1
shift 1
# parse options
if [ $# -gt 1 ]; then
    while true; do
    case "$1" in
    -c|--config)
    CFG_FILE="$2"
	shift 2
    ;;
-m|--mode)
    RUN_MODE="$2"
shift 2
    ;;
    *)
     break # out-of-args, stop looping
    ;;
esac
done
fi

# load configurations
BIGTOP_DEFAULTS_DIR=${BIGTOP_DEFAULTS_DIR:-/etc/default}
SOLR_CONFIG=${CFG_FILE:-${BIGTOP_DEFAULTS_DIR}/solr}
[ -f $SOLR_CONFIG ] && . $SOLR_CONFIG


# Autodetect JAVA_HOME if not defined
. /usr/lib/bigtop-utils/bigtop-detect-javahome

# resolve links - $0 may be a softlink
PRG="${BASH_SOURCE[0]}"

while [ -h "${PRG}" ]; do
  ls=`ls -ld "${PRG}"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "${PRG}"`/"$link"
  fi
done

BASEDIR=`dirname ${PRG}`
BASEDIR=`cd ${BASEDIR}/..;pwd`

SOLR_MODE=${RUN_MODE:-cloud}
SOLR_PORT=${SOLR_PORT:-8983}
SOLR_ADMIN_PORT=${SOLR_ADMIN_PORT:-8984}
SOLR_LOG4J_CONFIG=${LOG4J_PROPS:-/etc/solr/conf/log4j2-console.xml}

SOLR_HOME=${SOLR_HOME:-/var/lib/solr}
export SOLR_LOGS_DIR=${SOLR_LOGS_DIR:-/var/log/solr}
export SOLR_PID_DIR=${SOLR_PID_DIR:-/var/run/solr}

die() {
  echo "$@" >&2
  exit 1
}

# Preflight checks:
# SolrCloud mode check
if [ "$SOLR_MODE" = "cloud" ]; then
  if [ -z "$SOLR_ZK_ENSEMBLE" ] ; then
    die "Error: Cloud mode is set but ZK_HOST is not set in /etc/default/solr"
  fi
  SOLR_OPTS="${SOLR_OPTS} -DzkHost=${SOLR_ZK_ENSEMBLE} -Dsolr.solrxml.location=zookeeper"
fi

if [ -n "$SOLR_HDFS_HOME" ] ; then
  SOLR_OPTS="${SOLR_OPTS} -Dsolr.hdfs.home=${SOLR_HDFS_HOME}"
fi

if [ -n "$SOLR_HDFS_CONFIG" ] ; then
  SOLR_OPTS="${SOLR_OPTS} -Dsolr.hdfs.confdir=${SOLR_HDFS_CONFIG}"
fi

if [ "$SOLR_KERBEROS_ENABLED" == "true" ] ; then
  SOLR_OPTS="${SOLR_OPTS} -Dsolr.hdfs.security.kerberos.enabled=${SOLR_KERBEROS_ENABLED}"
fi

if [ -n "$SOLR_KERBEROS_KEYTAB" ] ; then
  SOLR_OPTS="${SOLR_OPTS} -Dsolr.hdfs.security.kerberos.keytabfile=${SOLR_KERBEROS_KEYTAB}"
fi

if [ -n "$SOLR_KERBEROS_PRINCIPAL" ] ; then
  SOLR_OPTS="${SOLR_OPTS} -Dsolr.hdfs.security.kerberos.principal=${SOLR_KERBEROS_PRINCIPAL}"
fi

if [ -n "$SOLR_AUTHENTICATION_TYPE" ] ; then
  SOLR_OPTS="${SOLR_OPTS} -Dsolr.authentication.type=${SOLR_AUTHENTICATION_TYPE}"
fi

if [ -n "$SOLR_AUTHENTICATION_KERBEROS_KEYTAB" ] ; then
  SOLR_OPTS="${SOLR_OPTS} -Dsolr.authentication.kerberos.keytab=${SOLR_AUTHENTICATION_KERBEROS_KEYTAB}"
fi

if [ -n "$SOLR_AUTHENTICATION_KERBEROS_PRINCIPAL" ] ; then
  SOLR_OPTS="${SOLR_OPTS} -Dsolr.authentication.kerberos.principal=${SOLR_AUTHENTICATION_KERBEROS_PRINCIPAL}"
fi

if [ -n "$SOLR_AUTHENTICATION_KERBEROS_NAME_RULES" ] ; then
  SOLR_OPTS="${SOLR_OPTS} -Dsolr.authentication.kerberos.name.rules=${SOLR_AUTHENTICATION_KERBEROS_NAME_RULES}"
fi

if [ -n "$SOLR_AUTHENTICATION_SIMPLE_ALLOW_ANON" ] ; then
  SOLR_OPTS="${SOLR_OPTS} -Dsolr.authentication.simple.anonymous.allowed=${SOLR_AUTHENTICATION_SIMPLE_ALLOW_ANON}"
fi

if [ -n "$SOLR_AUTHENTICATION_JAAS_CONF" ] ; then
  SOLR_OPTS="${SOLR_OPTS} -Djava.security.auth.login.config=${SOLR_AUTHENTICATION_JAAS_CONF}"
fi

# FIXME: we need to set this because of the jetty-centric default solr.xml
SOLR_OPTS="${SOLR_OPTS} -Dhost=$HOSTNAME -Djetty.port=$SOLR_PORT"

# SOLR-15133
SOLR_OPTS="${SOLR_OPTS} -XX:-UseLargePages"

export SOLR_OPTS="${SOLR_OPTS} -Dsolr.host=$HOSTNAME
                               -Dsolr.port=$SOLR_PORT
                               -Dsolr.admin.port=$SOLR_ADMIN_PORT
                               -Dlog4j.configurationFile=file://$SOLR_LOG4J_CONFIG
                               -Dsolr.log.dir=$SOLR_LOGS_DIR
                               -Dsolr.solr.home=$SOLR_HOME"

exec /usr/lib/solr/bin/solr $CMD "$@"
