#! /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.
# */
# 
# The asynchbase command script. Based on the hadoop command script
#
# Environment Variables:
#
#   JAVA_HOME             The java implementation to use. Overrides JAVA_HOME.
#
#   ASYNCHBASE_CLASSPATH  Extra Java CLASSPATH entries.
#
#   ASYNCHBASE_OPTS       Extra Java runtime options.
#

function get_canonical_dir() {
  target="$1"

  canonical_name=`readlink -f ${target} 2>/dev/null`
  if [[ $? -eq 0 ]]; then
    canonical_dir=`dirname $canonical_name`
    echo ${canonical_dir}
    return
  fi

  # Mac has no readlink -f
  cd `dirname ${target}`
  target=`basename ${target}`

  # chase down the symlinks
  while [ -L ${target} ]; do
    target=`readlink ${target}`
    cd `dirname ${target}`
    target=`basename ${target}`
  done

  canonical_dir=`pwd -P`
  ret=${canonical_dir}
  echo $ret
}
bin_dir=$(get_canonical_dir "$0")
ASYNCHBASE_HOME=`cd "$bin_dir/..">/dev/null; pwd`
MAPR_HOME=${MAPR_HOME:-/opt/mapr}

cygwin=false
case "`uname`" in
CYGWIN*) cygwin=true;;
esac

# if no args specified, show usage
if [ $# = 0 ]; then
  echo "Usage: asynchbase <command> [<args>]"
  echo "Commands:"
  echo "  classpath       Dump AsyncHBase CLASSPATH"
  echo "  CLASSNAME       Run the class named CLASSNAME"
  exit 1
fi

# get arguments
COMMAND=$1
shift

JAVA_CLASSPATH=
#add the asynchbase jars
for f in $ASYNCHBASE_HOME/asynchbase*.jar; do
	if [[ $f = *sources.jar || $f = *javadoc.jar ]]; then
    : # Skip 
  elif [ -f $f ]; then
    JAVA_CLASSPATH=${JAVA_CLASSPATH}:$f;
  fi
done

# Add libs to CLASSPATH
for f in $ASYNCHBASE_HOME/lib/*.jar; do
  JAVA_CLASSPATH=${JAVA_CLASSPATH}:$f;
done

function find_jars() {
  DIR=$1
  shift
  ALL_JARS=
  for jar in "$@"; do
    JARS=`echo $(ls ${DIR}/${jar} 2> /dev/null) | sed 's/\s\+/:/g'`
    if [ "${JARS}" != "" ]; then
      ALL_JARS=${ALL_JARS}:${JARS}
    fi
  done
  echo "${ALL_JARS#:}"
}

# Add MapRFS jar and its dependencies
HADOOP_IN_PATH=$(PATH="${HADOOP_HOME:-${HADOOP_PREFIX}}/bin:$PATH" which hadoop 2>/dev/null)
if [ -f ${HADOOP_IN_PATH} ]; then
  hadoop_bin_dir=$(get_canonical_dir "$HADOOP_IN_PATH")
  HADOOP_HOME=`cd "$hadoop_bin_dir/..">/dev/null; pwd`
  JAVA_CLASSPATH=${JAVA_CLASSPATH}:$(find_jars $HADOOP_HOME/lib \
    "commons-collections*.jar" "commons-codec*.jar" "commons-lang*.jar" \
    "hadoop*.jar" "commons-logging*.jar" "protobuf*.jar")
  if [ -d ${MAPR_HOME}/lib ]; then
    JAVA_CLASSPATH=${JAVA_CLASSPATH}:$(find_jars $MAPR_HOME/lib \
      "maprfs*.jar" "json*.jar" "libprotodefs*.jar")  
  fi
fi

# Add user-specified CLASSPATH last
if [ "$ASYNCHBASE_CLASSPATH" != "" ]; then
  JAVA_CLASSPATH=${JAVA_CLASSPATH}:${ASYNCHBASE_CLASSPATH}
fi

JAVA_CLASSPATH="${JAVA_CLASSPATH#:}"

# Add ${ASYNCHBASE_HOME}/conf to the beginning of classpath
JAVA_CLASSPATH=${ASYNCHBASE_HOME}/conf:${JAVA_CLASSPATH}

if $cygwin; then
  # cygwin path translation
  JAVA_CLASSPATH=`cygpath -p -w "$JAVA_CLASSPATH"`
fi

if [ "$COMMAND" = "classpath" ] ; then
  echo $JAVA_CLASSPATH
  exit 0
fi

CLASS=$COMMAND
if [ -z $JAVA_HOME ]; then
  JAVA=`which java`
else
  JAVA=$JAVA_HOME/bin/java
fi

"$JAVA" -cp ${JAVA_CLASSPATH} -Dproc_$COMMAND $ASYNCHBASE_OPTS $CLASS "$@"
