#!/usr/bin/env 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.
# */

set -e

BINDIR=`dirname "$0"`
BK_HOME=`cd ${BINDIR}/..;pwd`

source ${BK_HOME}/bin/common.sh

# default variables
DEFAULT_CONF=${BK_HOME}/conf/bk_server.conf
DEFAULT_ZK_CONF=${BK_HOME}/conf/zookeeper.conf

if [ -z "$BOOKIE_CONF" ]; then
  BOOKIE_CONF_TO_CHECK=${DEFAULT_CONF}
else
  BOOKIE_CONF_TO_CHECK=${BOOKIE_CONF}
fi

# set up the classpath
BOOKIE_CLASSPATH=${BK_HOME}/lib/*


# get arguments
COMMAND=$1
shift

LOCALBOOKIES_CONFIG_DIR="${LOCALBOOKIES_CONFIG_DIR:-/tmp/localbookies-config}"
if [ ${COMMAND} == "shell" ]; then
  DEFAULT_LOG_CONF=${BK_HOME}/conf/log4j.shell.properties
  if [[ $1 == "-localbookie"  ]]; then
    if [[ $2 == *:* ]];
    then
      BOOKIE_CONF=${LOCALBOOKIES_CONFIG_DIR}/$2.conf
      shift 2
    else
      BOOKIE_CONF=${LOCALBOOKIES_CONFIG_DIR}/baseconf.conf
      shift
    fi
  fi
fi

if [ -z "$BOOKIE_ZK_CONF" ]; then
    BOOKIE_ZK_CONF=$DEFAULT_ZK_CONF
fi

if [ -z "$BOOKIE_CONF" ]; then
  BOOKIE_CONF=${DEFAULT_CONF}
fi

# Configure logging
if [ -z "$BOOKIE_LOG_CONF" ]; then
  BOOKIE_LOG_CONF=${DEFAULT_LOG_CONF}
fi
BOOKIE_LOG_DIR=${BOOKIE_LOG_DIR:-"$BK_HOME/logs"}
BOOKIE_LOG_FILE=${BOOKIE_LOG_FILE:-"bookkeeper-server.log"}
BOOKIE_ROOT_LOGGER=${BOOKIE_ROOT_LOGGER:-"INFO,CONSOLE"}

# Configure the classpath
BOOKIE_CLASSPATH="$BOOKIE_JAR:$BOOKIE_CLASSPATH:$BOOKIE_EXTRA_CLASSPATH"
BOOKIE_CLASSPATH="`dirname $BOOKIE_LOG_CONF`:$BOOKIE_CLASSPATH"

echo ${BOOKIE_LOG_CONF}

# Build the OPTS
BOOKIE_OPTS=$(build_bookie_opts)
GC_OPTS=$(build_bookie_jvm_opts ${BOOKIE_LOG_DIR} "gc_%p.log")
NETTY_OPTS=$(build_netty_opts)
LOGGING_OPTS=$(build_logging_opts ${BOOKIE_LOG_CONF} ${BOOKIE_LOG_DIR} ${BOOKIE_LOG_FILE} ${BOOKIE_ROOT_LOGGER})

OPTS="${OPTS} -cp ${BOOKIE_CLASSPATH} ${BOOKIE_OPTS} ${GC_OPTS} ${NETTY_OPTS} ${LOGGING_OPTS} ${BOOKIE_EXTRA_OPTS}"

# Create log dir if it doesn't exist
if [ ! -d ${BOOKIE_LOG_DIR} ]; then
    mkdir ${BOOKIE_LOG_DIR}
fi

#Change to BK_HOME to support relative paths
cd "$BK_HOME"
if [ ${COMMAND} == "bookie" ]; then
  exec ${JAVA} ${OPTS} ${JMX_ARGS} org.apache.bookkeeper.server.Main --conf ${BOOKIE_CONF} $@
elif [ ${COMMAND} == "autorecovery" ]; then
  exec ${JAVA} ${OPTS} ${JMX_ARGS} org.apache.bookkeeper.replication.AutoRecoveryMain --conf ${BOOKIE_CONF} $@
elif [ ${COMMAND} == "localbookie" ]; then
  NUMBER=$1
  shift
  exec ${JAVA} ${OPTS} ${JMX_ARGS} -Dzookeeper.4lw.commands.whitelist='*' org.apache.bookkeeper.util.LocalBookKeeper ${NUMBER} ${BOOKIE_CONF} $@
elif [ ${COMMAND} == "standalone" ]; then
  exec ${JAVA} ${OPTS} ${JMX_ARGS} -Dzookeeper.4lw.commands.whitelist='*' org.apache.bookkeeper.stream.cluster.StandaloneStarter --conf ${BK_HOME}/conf/standalone.conf $@
elif [ ${COMMAND} == "upgrade" ]; then
  exec ${JAVA} ${OPTS} org.apache.bookkeeper.bookie.FileSystemUpgrade --conf ${BOOKIE_CONF} $@
elif [ $COMMAND == "zookeeper" ]; then
    BOOKIE_LOG_FILE=${BOOKIE_LOG_FILE:-"zookeeper.log"}
    exec $JAVA $OPTS -Dbookkeeper.log.file=$BOOKIE_LOG_FILE -Dzookeeper.4lw.commands.whitelist='*' org.apache.zookeeper.server.quorum.QuorumPeerMain $BOOKIE_ZK_CONF $@
elif [ ${COMMAND} == "shell" ]; then
  ENTRY_FORMATTER_ARG="-DentryFormatterClass=${ENTRY_FORMATTER_CLASS:-org.apache.bookkeeper.util.StringEntryFormatter}"
  exec ${JAVA} ${OPTS} ${ENTRY_FORMATTER_ARG} org.apache.bookkeeper.bookie.BookieShell -conf ${BOOKIE_CONF} $@
else
  exec ${JAVA} ${OPTS} ${COMMAND} $@
fi

