#!/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.
#

# Invoke Drill using Java. Command line arguments are assumed to be
# of the form
#   exec|debug
# The debug option simply prints the command line option, but does not
# run Drill. Anything else runs Drill. (Command line options passed to
# drillbit.sh are passed into this script in the args[] variable, see
# below.)
#
# Environment Variables
#
#   SERVER_LOG_GC           Set to "1" to enable Java garbage collector
#                           logging.
#   DRILL_LOG_PREFIX        Path and name prefix for log files.
#                           (Set in drill-config.sh.)
#   DRILLBIT_LOG_PATH       Path to the Drillbit log file.
#                           (Set in drill-config.sh.)
#   DRILL_JAVA_OPTS         Optional JVM arguments such as system
#                           property overides used by both the
#                           drillbit (server) and client,
#                           set in drill-env.sh or the environment.
#   DRILLBIT_JAVA_OPTS      Optional JVM arguments specifically for
#                           the server (drillbit). Set in the
#                           environment or in the user defined
#                           drill-env.sh
#   SERVER_GC_OPTS          Defaults set in drill-config.sh, customized
#                           in drill-env.sh.
#   CP                      Drillbit classpath set in drill-config.sh
#   args[]                  -Dname=value arguments to pass to the JVM
#                           for per-run override of configuration options.

cmd=$1
shift

drill_rotate_log ()
{
    log=$1;
    num=5;
    if [ -n "$2" ]; then
    num=$2
    fi
    if [ -f "$log" ]; then # rotate logs
    while [ $num -gt 1 ]; do
        prev=`expr $num - 1`
        [ -f "$log.$prev" ] && mv -f "$log.$prev" "$log.$num"
        num=$prev
    done
    mv -f "$log" "$log.$num";
    fi
}

# Enable GC logging if requested.
# Note: if using YARN log dir, then no log rotation because each run under YARN
# gets a new log directory.

if [ "$SERVER_LOG_GC" == "1" ]; then
  loggc="${DRILL_LOG_PREFIX}.gc"
  SERVER_GC_OPTS="${SERVER_GC_OPTS} -Xloggc:${loggc}"
  if [ -z "$DRILL_YARN_LOG_DIR" ]; then
    drill_rotate_log $loggc
  fi
fi

logqueries="${DRILL_LOG_PREFIX}_queries.json"
LOG_OPTS="-Dlog.path=$DRILLBIT_LOG_PATH -Dlog.query.path=$logqueries"
if [ -n "$DRILL_JAVA_LIB_PATH" ]; then
  DRILL_JAVA_OPTS="$DRILL_JAVA_OPTS -Djava.library.path=$DRILL_JAVA_LIB_PATH"
fi
DRILL_ALL_JAVA_OPTS="$DRILLBIT_OPTS $DRILL_JAVA_OPTS $DRILLBIT_JAVA_OPTS $SERVER_GC_OPTS $@ $LOG_OPTS"
BITCMD="$JAVA $DRILL_ALL_JAVA_OPTS -cp $CP org.apache.drill.exec.server.Drillbit"

# The wrapper is purely for unit testing.

if [ -n "$_DRILL_WRAPPER_" ]; then
  BITCMD="$_DRILL_WRAPPER_ $BITCMD"
fi

# Run the command (exec) or just print it (debug).
# Three options: run as a child (run), run & replace this process (exec) or
# just print the command (debug).

case $cmd in
(debug)
  echo "----------------- Environment ------------------"
  env
  echo "------------------------------------------------"
  echo "Launch command:"
  echo $BITCMD
  ;;
(*)
  exec $BITCMD
  ;;
esac
