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

# resolve links - "${BASH_SOURCE-$0}" may be a softlink
this="${BASH_SOURCE-$0}"
while [ -h "$this" ]; do
  ls=`ls -ld "$this"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '.*/.*' > /dev/null; then
    this="$link"
  else
    this=`dirname "$this"`/"$link"
  fi
done

# convert relative path to absolute path
bin=`dirname "$this"`

# Do setup, including finding the site directory, removing the
# --config argument, and copying remaining argument into the
# args array.

. "$bin/drill-config.sh"

SLARGS=()
for (( i=0; i < ${#args[@]}; i++ )); do
  case "${args[i]}" in
  -q|-e)
      QUERY=${args[i+1]}
      let i=$i+1
      ;;
  -f) FILE=${args[i+1]}
      let i=$i+1
      ;;
  --jvm)
      DRILL_SHELL_JAVA_OPTS="$DRILL_SHELL_JAVA_OPTS ${args[i+1]}"
      let i=$i+1
      ;;
   *) SLARGS+=("${args[i]}");;
  esac
done

# Override SQLLine's default initial transaction isolation level.  (SQLLine
# sets an initial level instead of leaving it at whatever the Driver's default
# is.) 
# Put our property specification before previous value of DRILL_SHELL_JAVA_OPTS
# so that it can still be overridden via DRILL_SHELL_JAVA_OPTS.
#
# This is not currently needed as the new SQLLine we are using doesn't isolate.
# DRILL_SHELL_JAVA_OPTS="-Dsqlline.isolation=TRANSACTION_NONE $DRILL_SHELL_JAVA_OPTS"

DRILL_SHELL_LOG_OPTS="-Dlog.path=$DRILL_LOG_DIR/sqlline.log -Dlog.query.path=$DRILL_LOG_DIR/sqlline_queries.json"

# Use either the SQLline options (for remote Drill) or full Drill options
# (embedded Drill)

if [ "$DRILL_EMBEDDED" = "1" ]; then
  SQLLINE_JAVA_OPTS="$DRILL_JAVA_OPTS $DRILLBIT_OPTS"
fi 

if ! $is_cygwin; then
  DRILL_SHELL_OPTS="$DRILL_SHELL_OPTS --color=true"
fi

SHELL_OPTS="$DRILL_SHELL_JAVA_OPTS $SQLLINE_JAVA_OPTS $DRILL_SHELL_LOG_OPTS $CLIENT_GC_OPTS -Dorg.jline.terminal.dumb=true"
CMD="$JAVA $SHELL_OPTS -cp $CP sqlline.SqlLine -ac org.apache.drill.exec.client.DrillSqlLineApplication"

# The wrapper is purely for unit testing.

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

if [ -n "$QUERY" ] ; then
  echo "$QUERY" | exec $CMD "${SLARGS[@]}"
elif [ -n "$FILE" ] ; then
  exec $CMD "${SLARGS[@]}" --run=$FILE
else
  exec $CMD $DRILL_SHELL_OPTS "${SLARGS[@]}"
fi
