# 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 - $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

# some directories
THIS_DIR=`dirname "$THIS"`
GIRAPH_HOME=`cd "$THIS_DIR/.." ; pwd`

# extra properties to send straight to Hadoop
HADOOP_PROPERTIES=
while [ $1 ] && [ ${1:0:2} == "-D" ] ; do
    HADOOP_PROPERTIES="$1 $HADOOP_PROPERTIES"
    shift
done

USER_JAR=$1
shift

if [ ! -e "$USER_JAR" ]; then
  echo "No user jar found at $USER_JAR"
else
  # add user jar to classpath
  CLASSPATH=${USER_JAR}
fi

# add Giraph conf dir to classpath
CLASSPATH=$CLASSPATH:$GIRAPH_HOME/conf

CLASS=org.apache.giraph.GiraphRunner

# so that filenames w/ spaces are handled correctly in loops below
IFS=

# add release dependencies to CLASSPATH
if [ -d "$GIRAPH_HOME/lib" ]; then
	for f in $GIRAPH_HOME/lib/*.jar; do
	  CLASSPATH=${CLASSPATH}:$f
	done

	for f in $GIRAPH_HOME/giraph-core*.jar ; do
	  if [ -e "$f" ]; then
	    JAR=$f
           CLASSPATH=${CLASSPATH}:$f
	    break
	  fi
	done
else
	echo "No lib directory, assuming dev environment"
	if [ ! -d "$GIRAPH_HOME/giraph-core/target" ]; then
		echo "No target directory. Build Giraph jar before proceeding."
		exit 1
	fi
	
	CLASSPATH2=`mvn dependency:build-classpath | grep -v "[INFO]"`
	CLASSPATH=$CLASSPATH:$CLASSPATH2	

	for f in $GIRAPH_HOME/giraph-core/target/giraph*.jar; do
	  if [ -e "$f" ]; then
	    JAR=$f
	    break
	  fi
	done
fi
# restore ordinary behaviour
unset IFS

if [ "$JAR" = "" ] ; then
  echo "Can't find Giraph jar."
  exit 1
fi

if [ "$HADOOP_CONF_DIR" = "" ] ; then
  HADOOP_CONF_DIR=$HADOOP_HOME/conf
  echo "No HADOOP_CONF_DIR set, using $HADOOP_HOME/conf "
else
  echo "HADOOP_CONF_DIR=$HADOOP_CONF_DIR"
fi

# Giraph's jars to add to distributed cache via -libjar, which are csv rather than :sv
GIRAPH_JARS=`echo ${JAR}:${CLASSPATH}|sed s/:/,/g`
export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:$CLASSPATH
