#!/bin/bash

# Ensure this file is executable via `chmod a+x drake`, then place it
# somewhere on your $PATH, like ~/bin. Drake will be
# installed upon first run into the ~/.drakerc directory.


# This needs to be defined before we call HTTP_CLIENT below
if [ "$HTTP_CLIENT" = "" ]; then
    if type -p curl >/dev/null 2>&1; then
        if [ "$https_proxy" != "" ]; then
            CURL_PROXY="-x $https_proxy"
        fi
        HTTP_CLIENT="curl $CURL_PROXY -f -L -o"
    else
        HTTP_CLIENT="wget -O"
    fi
fi

DRAKE_VERSION=1.0.3

DRAKE_SCRIPT_URL=https://raw.githubusercontent.com/Factual/drake/master/bin/drake
DRAKE_URL=https://github.com/Factual/drake/releases/download/$DRAKE_VERSION/drake.jar

DRAKE_TMP_SCRIPT=/tmp/drake_script.sh.tmp

if [ "$1" = "--upgrade" ]; then
  echo "Downloading latest drake script..."
  $HTTP_CLIENT $DRAKE_TMP_SCRIPT $DRAKE_SCRIPT_URL
  if [ -L $0 ]; then
    SCRIPT_TARGET=$(dirname `readlink $0`)/drake
  else
    SCRIPT_TARGET=$(dirname $0)/drake
  fi
  mv $DRAKE_TMP_SCRIPT $SCRIPT_TARGET
  chmod 755 $SCRIPT_TARGET
  echo "Drake script has been upgraded to latest version."
  exec $SCRIPT_TARGET version
  exit 0
fi

# Tries to include local hadoop library on classpath for HDFS support.
# If Hadoop client is not installed locally, defaults to built-in hadoop version.
# If you don't need HDFS support with your Drake workflows, none of this matters.
if [ -z ${HADOOP_CLASSPATH:+x} ]; then
  if [[ `which hadoop` ]]; then
    HADOOP_CLASSPATH=`hadoop classpath 2>/dev/null`
    HADOOP_VERSION=`hadoop version | head -1`
  fi
fi

if [ "$1" = "--hadoop-version" ]; then
  if [ ! -z ${HADOOP_VERSION:+x} ]; then
    echo "Found $HADOOP_VERSION on local"
  else
    echo "Warning: Hadoop client not found. HDFS related features will probably not work correctly. Ignore this message if you don't need HDFS support."
  fi
  exit 0
fi

function build_jar {
  echo "Using DRAKE_HOME: $DRAKE_HOME"
  DRAKE_JAR=${DRAKE_JAR:-$DRAKE_HOME/target/drake.jar}
  if [[ ! -f $DRAKE_JAR ]]; then
    echo ""
    echo "========= UBERJAR COMPILING ========="
    cd $DRAKE_HOME
    lein uberjar
    cd - > /dev/null
    echo "========= UBERJAR COMPILED =========="
    echo ""
  fi
}

if [ -z ${DRAKE_HOME:+x} ]; then
  DRAKE_SCRIPT_PATH=$0
  if [ -L $DRAKE_SCRIPT_PATH ]; then
    DRAKE_SCRIPT_PATH=`readlink $0`
  fi
  DRAKE_HOME=$(dirname $(dirname $DRAKE_SCRIPT_PATH))
fi

if [ ! -z ${DRAKE_HOME:+x} ] && [ -d $DRAKE_HOME ] && [ -f "$DRAKE_HOME/project.clj" ] ; then
  build_jar
else
  DRAKE_HOME=~/.drakerc
  DRAKE_JAR=${DRAKE_JAR:-$DRAKE_HOME/jar/drake-$DRAKE_VERSION-standalone.jar}
  mkdir -p $(dirname $DRAKE_JAR)
  if [ ! -f $DRAKE_JAR ]; then
    echo "Downloading drake jar for version $DRAKE_VERSION..."
    echo ""
    $HTTP_CLIENT $DRAKE_JAR.tmp $DRAKE_URL
    exit_code=$?
    if [ $exit_code -ne 0 ]; then
      echo "ERROR: Failed to download $DRAKE_URL with exit code $exit_code"
      exit 1
    fi
    mv $DRAKE_JAR.tmp $DRAKE_JAR
    echo ""
    echo "Drake jar downloaded for version $DRAKE_VERSION."
  fi
fi

if [ "$1" = "--version" ]; then
  echo "Drake now is on version: $DRAKE_VERSION"
  exit 0
fi


if [[ `which drip` ]]; then
  echo "Using drip to start up JVM..."
  drip -cp $HADOOP_CLASSPATH:$DRAKE_JAR drake.core "$@"
else
  java -cp $HADOOP_CLASSPATH:$DRAKE_JAR drake.core "$@"
fi
