#!/bin/bash

######## CONFIGURATION OPTIONS ########
SCRIPT_NOT_CHANGED=0	# change this to 1 if you don't want your script overwritten!
JAVA_PROGRAM_DIR=""	# use full path to java bin dir, ex. "/usr/java/j2sdk1.4.2/bin/"
#PROGRAM_DIR="/home/username/apps/azureus"	# use full path to Azureus bin dir
JAVA_ARGS="-Xmx128m"

#export MOZILLA_FIVE_HOME="/path/to/gre"	# Full path to GRE/Mozilla. When commenting out this line, also comment out the next line
#if [ "$LD_LIBRARY_PATH x" = " x" ]; then export LD_LIBRARY_PATH=$MOZILLA_FIVE_HOME; else export LD_LIBRARY_PATH=$MOZILLA_FIVE_HOME:$LD_LIBRARY_PATH; fi
#######################################



######## YOU PROBABLY DO NOT WANT TO TOUCH ANYTHING BELOW! ########

SCRIPT_VERSION=3
START_CLASS="org.gudy.azureus2.ui.swt.Main"

MSG_LOADING="Loading Azureus:"
MSG_STARTING="Starting Azureus..."
MSG2="Java exec found in "
MSG3="OOPS, your java version is too old "
MSG4="You need to upgrade to JRE 1.4.x or newer from http://java.sun.com"
MSG5="Suitable java version found "
MSG6="Configuring environment..."
MSG7="OOPS, you don't seem to have a valid JRE "
MSG8="OOPS, unable to locate java exec in "
MSG9=" hierarchy"
MSG10="Java exec not found in PATH, starting auto-search..."
MSG11="Java exec found in PATH. Verifying..."
MSG_AZEXIT="Exit from Azureus complete"
MSG_TERMINATED="Azureus TERMINATED."
MSG_RECHECK="Re-checking with GCJ (Sun Java recommended).."
MSG_ISGCJ="Java is GCJ.. looking for Sun Java.."
MSG_JAVABORK="Java appeared to have crashed:"

SKIP_GCJ=1

look_for_java()
{
	if command -v java &>/dev/null; then
		if check_version ; then
			echo $MSG11
			return 0
		fi
	fi

	echo $MSG10


	JAVA_CHECK_DIRS="/usr/java/latest /usr/java /usr/lib/jvm/latest /usr/lib/jvm"
	for JAVADIR in $JAVA_CHECK_DIRS; do
		IFS=$'\n'
		potential_java_dirs=(`ls -1 "$JAVADIR" | sort | tac 2>/dev/null`)
		IFS=
		for D in "${potential_java_dirs[@]}"; do
			if [[ -d "$JAVADIR/$D" && -x "$JAVADIR/$D/bin/java" ]]; then
				JAVA_PROGRAM_DIR="$JAVADIR/$D/bin/"
				echo $MSG2 $JAVA_PROGRAM_DIR
				if check_version ; then
					return 0
				else
					return 1
				fi
			fi
		done
	done
	
	if [ $SKIP_GCJ ] ; then
		echo $MSG_RECHECK
		SKIP_GCJ=
		if look_for_java ; then
			return 0
		else
			return 1
		fi
	else
		echo $MSG8 "${JAVADIR}/" $MSG9 ; echo $MSG4
	fi
	return 1
}

check_version()
{
	if [ $SKIP_GCJ ] ; then
		JAVA_ISGCJ=`"${JAVA_PROGRAM_DIR}java" -version 2>&1 | grep "gcj"`
		if [ ! "$JAVA_ISGCJ x" = " x" ] ; then
			echo $MSG_ISGCJ
			return 1
		fi
	fi

	JAVA_HEADER=`"${JAVA_PROGRAM_DIR}java" -version 2>&1 | head -n 1`
	JAVA_IMPL=`echo ${JAVA_HEADER} | cut -f1 -d' '`
	if [ "$JAVA_IMPL" = "java" ] ; then
		VERSION=`echo ${JAVA_HEADER} | sed "s/java version \"\(.*\)\"/\1/"`
		if echo $VERSION | grep "^1.[0-3]" ; then
			echo $MSG3 "[${JAVA_PROGRAM_DIR}java = ${VERSION}]" ; echo $MSG4
			return 1
		else
			echo $MSG5 "[${JAVA_PROGRAM_DIR}java = ${VERSION}]" ; echo $MSG6
			return 0
		fi
	elif [ "$JAVA_IMPL" = "#" ] ; then
		echo $MSG_JAVABORK
		${JAVA_PROGRAM_DIR}java -version 2>&1
		exit 1
	else	
		echo $MSG7 "[${JAVA_PROGRAM_DIR}java = ${JAVA_IMPL}]" ; echo $MSG4
		return 1
	fi
}

runJavaOutput()
{
	# assume we can write to the user's home..

	${JAVA_PROGRAM_DIR}java "${JAVA_ARGS}" \
		-cp "${CLASSPATH}" \
		-Djava.library.path="${PROGRAM_DIR}" \
		-Dazureus.install.path="${PROGRAM_DIR}" \
		-Dazureus.script="$0" \
		$JAVA_PROPS \
		"$@" > ~/azScript
	if [ -f ~/azScript ]; then
		chmod +x ~/azScript
		. ~/azScript
		rm ~/azScript
	fi
}

moveInSWT()
{
	if [ ! -f "${PROGRAM_DIR}/swt.jar" ]; then
		if [ ! "$(echo ${PROGRAM_DIR}/swt/*.jar)" = "${PROGRAM_DIR}/swt/*.jar" ]; then
			if [ "`${JAVA_PROGRAM_DIR}java -cp \"${CLASSPATH}\" org.gudy.azureus2.platform.JavaBitMode`" = "64" ]; then 
				cp ${PROGRAM_DIR}/swt/swt64.jar ${PROGRAM_DIR}/swt.jar
			else
				cp ${PROGRAM_DIR}/swt/swt32.jar ${PROGRAM_DIR}/swt.jar
			fi
		fi
	fi
}


echo $MSG_STARTING

# locate and test the java executable
if [ "$JAVA_PROGRAM_DIR" == "" ]; then
	if ! look_for_java ; then
		exit 1
	fi
fi

# get the app dir if not already defined
if [ -z "$PROGRAM_DIR" ]; then
		PROGRAM_DIR=`dirname "$0"`
		PROGRAM_DIR=`cd "$PROGRAM_DIR"; pwd`
else
	if [ "$(echo ${PROGRAM_DIR}/*.jar)" = "${PROGRAM_DIR}/*.jar" ]; then
		echo "You seem to have set an invalid PROGRAM_DIR, unable to continue!"
		exit 1
	elif [ ! -f "${PROGRAM_DIR}/Azureus2.jar" ]; then
		echo "Unable to locate Azureus2.jar in $PROGRAM_DIR, aborting!"
		exit 1
	fi
fi

# Change path here so we can do for loop on program dirs with spaces
cd "${PROGRAM_DIR}"

# build the classpath
for FILE in ./*.jar; do
	CLASSPATH="${CLASSPATH:+${CLASSPATH}:}$FILE"
done

moveInSWT

# setup Java System Properties (no spaces in values)
JAVA_PROPS="-Dazureus.script.version=${SCRIPT_VERSION}"
if [ ! "$JAVA_ISGCJ x" = " x" ] ; then
	JAVA_PROPS="$JAVA_PROPS -Dgnu.gcj.runtime.VMClassLoader.library_control=never"
fi

# some distros symlink application level plugins into the users's plugin directory..
# remove all symlinks in user's plugin directory
find ~/.azureus/plugins -maxdepth 1 -type l -xtype d  -exec rm {} \;

runJavaOutput "org.gudy.azureus2.platform.unix.ScriptBeforeStartup" "$@";

echo $MSG_LOADING

echo "${JAVA_PROGRAM_DIR}java ${JAVA_ARGS} -cp \"${CLASSPATH}\" -Djava.library.path=\"${PROGRAM_DIR}\" -Dazureus.install.path=\"${PROGRAM_DIR}\" -Dazureus.script=\"$0\" $JAVA_PROPS $START_CLASS $@"
# Don't use "exec -a Azureus ..." here as exec quits this script after completion,
# preventing ScriptAfterShutdown from running, which installs updates and does
# restarts
${JAVA_PROGRAM_DIR}java "${JAVA_ARGS}" \
	-cp "${CLASSPATH}" \
	-Djava.library.path="${PROGRAM_DIR}" \
	-Dazureus.install.path="${PROGRAM_DIR}" \
	-Dazureus.script="$0" \
	$JAVA_PROPS \
	$START_CLASS "$@"

echo $MSG_AZEXIT

runJavaOutput "org.gudy.azureus2.platform.unix.ScriptAfterShutdown" "$@";

echo $MSG_TERMINATED
