#!/bin/bash

# Copyright DataStax, Inc.
#
# Licensed 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.

# https://stackoverflow.com/a/697552
# get the real path, follow links if needed
real_path() {
  if command -v realpath > /dev/null 2>&1; then
    echo "$(realpath $1)"
  else
    SELF_PATH=$(cd -P -- "$(dirname -- "$1")" && pwd -P) && SELF_PATH=$SELF_PATH/$(basename -- "$1")

    # resolve symlinks
    while [ -h "$SELF_PATH" ] ; do
      # 1) cd to directory of the symlink
      # 2) cd to the directory of where the symlink points
      # 3) get the pwd
      # 4) append the basename
      DIR=$(dirname -- "$SELF_PATH")
      SYM=$(readlink `[[ "$OSTYPE" != darwin* ]] && echo "-f"` "$SELF_PATH")
      SELF_PATH=$(cd "$DIR" && cd "$(dirname -- "$SYM")" && pwd)/$(basename -- "$SYM")
    done
    echo $SELF_PATH
  fi
}

# Look for a usable Java 8 binary in JAVA_HOME, the result of /usr/libexec/java_home, or path.
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for J in "$JAVA" "$JAVA_HOME"/bin/java "$(/usr/libexec/java_home 2> /dev/null)/bin/java" $(command -v java) ; do
  if [ ! -z "$J" -a -x "$J" ]; then
    VER=$($J -version 2>&1 | grep -i version | head -1 | sed -e 's/[^"]*"//' -e 's/".*//' -e 's/\.[^\.]*$//')
    MAJOR=$(echo $VER | sed -e 's/\..*$//')
    MINOR=$(echo $VER | sed -e 's/^.*\.//')
    # matches 1.8, 1.9, 1.10 etc. as well as 8, 9, 10...
    if [ $MAJOR -eq 1 -a $MINOR -ge 8 -o $MAJOR -ge 8 ] ; then
      # This Java is at least 1.8.
      JAVA_CMD=$J
      break
    fi
  fi
done
IFS=$SAVEIFS

if [ -z "$JAVA_CMD" ] ; then
  echo "Unable to find java 8 (or later) executable. Check JAVA_HOME and PATH environment variables." >&2
  exit 1
fi

INSTALL_DIR=$(dirname "`real_path "$0"`")/..

# Set CLASSPATH to include all the jars in the lib dir + the conf directory
# (which contains application.conf). This is non-trivial because the install-dir
# may contain spaces.
CP=$INSTALL_DIR/conf$(
find "$INSTALL_DIR/lib" -name "*.jar" | while read i ; do
  echo ":$i"
done
)
CP=$(echo $CP | sed -e 's/ :/:/g')
[ ! -z "$DSBULK_CLASSPATH_EXTRA" ] && CP="$DSBULK_CLASSPATH_EXTRA:$CP"

# Under Cygwin, translate the classpath to Windows style
if [[ "$(uname -s)" == CYGWIN* ]]; then
  CP=$(cygpath -pw "$CP")
fi

# Attempt to find the window width, to make help output look nicer. This
# expression should work on Linux and Mac at least. If the result isn't a number,
# it means we can't parse the stty output, so don't save off COLUMNS.
COLUMNS=$(stty -a 2>/dev/null | head -1 | awk -F "; " '{print $3}' | sed -e 's/ *columns *[^0-9]*//')
if expr  "$COLUMNS" + 0 > /dev/null 2>&1 ; then
  export COLUMNS
fi

# Run the tool.
"$JAVA_CMD" $DSBULK_JAVA_OPTS -cp "$CP" com.datastax.oss.dsbulk.runner.DataStaxBulkLoader "$@"
