#!/bin/sh

if [ -z "$JAVA_HOME" ]
then
	echo 2>&1 "$0: Sorry, you must set JAVA_HOME to point to your Java installation."
	exit 1
fi

if [ -z "$HADOOP_HOME" ]
then
	echo 2>&1 "$0: Sorry, you must set HADOOP_HOME to point to your Hadoop installation."
	exit 1
fi

if [ -z $CLASSPATH ]; then
	CLASSPATH=${JAVA_HOME}/jdk/jre/lib
else
	CLASSPATH=${CLASSPATH}:${JAVA_HOME}/jdk/jre/lib
fi

for file in ${HADOOP_HOME}/*.jar ${HADOOP_HOME}/lib/*.jar
do
	CLASSPATH=${CLASSPATH}:$file
done

CLASSPATH=${CLASSPATH}:${HADOOP_HOME}/conf
export CLASSPATH

if [ -z $LIBJVM_PATH ]; then
	LIBJVM_PATH=`find $JAVA_HOME -follow -name libjvm.so | head -1l`
	if [ -z $LIBJVM_PATH ]; then
		echo 2>&1 "$0: Sorry but could not find libjvm.so anywhere under JAVA_HOME=$JAVA_HOME"
		exit 1
	fi
fi
export LIBJVM_PATH

if [ -z $LIBHDFS_PATH ]; then
	if [ `uname -m` = x86_64 ]; then
		LIBHDFS_PATH="$HADOOP_HOME/c++/`uname`-amd64-64/lib/libhdfs.so"
	else
		LIBHDFS_PATH="$HADOOP_HOME/c++/`uname`-i386-32/lib/libhdfs.so"
	fi
	if [ ! -e $LIBHDFS_PATH ]; then
		echo 2>&1 "$0: Sorry but could not find libhdfs.so anywhere under HADOOP_HOME=$HADOOP_HOME"
		exit 1
	fi
fi
export LIBHDFS_PATH

exec $(dirname $0)/parrot_run $@
